// Function for making appriopriate payments and emitting apprioriate message attributes
// based on the result of a hand
Function for making appriopriate payments and emitting apprioriate message attributes
based on the result of a hand
Function for making appriopriate payments and emitting apprioriate message attributes based on the result of a hand
fn handle_hand_result(
    updated_game_state: &mut GameState,
    player1_game_move: GameMove,
    player2_game_move: GameMove,
) -> Result<Response, ContractError> {
    let result = get_result(player1_game_move.clone(), player2_game_move.clone());
    match result {
        GameResult::Player1Wins => {
            // increment player 1 win counter
            updated_game_state.player1_hands_won += 1;
            // check if game is over
            if updated_game_state.player1_hands_won == updated_game_state.num_hands_to_win {
                updated_game_state.result = Some(result);
                // pay the winner
                Ok(send_double_tokens(
                    updated_game_state.clone().player1,
                    updated_game_state.clone().bet_amount,
                )
                .add_attribute("action", "reveal_move")
                .add_attribute(
                    "players",
                    format!(
                        "{}{}",
                        updated_game_state.clone().player1,
                        updated_game_state.clone().player2
                    ),
                )
                .add_attribute("game_won", updated_game_state.clone().player1)
                .add_attribute("player1_game_move", player1_game_move.to_string())
                .add_attribute("player2_game_move", player2_game_move.to_string())
                .add_attribute(
                    "game_state",
                    serde_json::to_string(&updated_game_state).unwrap(),
                ))
            } else {
                Ok(Response::new()
                    .add_attribute("action", "reveal_move")
                    .add_attribute(
                        "players",
                        format!(
                            "{}{}",
                            updated_game_state.clone().player1,
                            updated_game_state.clone().player2
                        ),
                    )
                    .add_attribute("hand_won", updated_game_state.clone().player1)
                    .add_attribute("player1_game_move", player1_game_move.to_string())
                    .add_attribute("player2_game_move", player2_game_move.to_string())
                    .add_attribute(
                        "game_state",
                        serde_json::to_string(&updated_game_state).unwrap(),
                    ))
            }
        }
        GameResult::Player2Wins => {
            // increment player 2 win counter
            updated_game_state.player2_hands_won += 1;
            // check if game is over
            if updated_game_state.player2_hands_won == updated_game_state.num_hands_to_win {
                updated_game_state.result = Some(result);
                // pay the winner
                Ok(send_double_tokens(
                    updated_game_state.clone().player2,
                    updated_game_state.clone().bet_amount,
                )
                .add_attribute("action", "reveal_move")
                .add_attribute(
                    "players",
                    format!(
                        "{}{}",
                        updated_game_state.clone().player1,
                        updated_game_state.clone().player2
                    ),
                )
                .add_attribute("game_won", updated_game_state.clone().player2)
                .add_attribute("player1_game_move", player1_game_move.to_string())
                .add_attribute("player2_game_move", player2_game_move.to_string())
                .add_attribute(
                    "game_state",
                    serde_json::to_string(&updated_game_state).unwrap(),
                ))
            } else {
                Ok(Response::new()
                    .add_attribute("action", "reveal_move")
                    .add_attribute(
                        "players",
                        format!(
                            "{}{}",
                            updated_game_state.clone().player1,
                            updated_game_state.clone().player2
                        ),
                    )
                    .add_attribute("hand_won", updated_game_state.clone().player2)
                    .add_attribute("player1_game_move", player1_game_move.to_string())
                    .add_attribute("player2_game_move", player2_game_move.to_string())
                    .add_attribute(
                        "game_state",
                        serde_json::to_string(&updated_game_state).unwrap(),
                    ))
            }
        }
        GameResult::Tie => {
            // increment tie counter
            updated_game_state.hands_tied += 1;
            Ok(Response::new()
                .add_attribute("action", "reveal_move")
                .add_attribute(
                    "players",
                    format!(
                        "{}{}",
                        updated_game_state.clone().player1,
                        updated_game_state.clone().player2
                    ),
                )
                .add_attribute("hand_won", "tie")
                .add_attribute("player1_game_move", player1_game_move.to_string())
                .add_attribute("player2_game_move", player2_game_move.to_string())
                .add_attribute(
                    "game_state",
                    serde_json::to_string(&updated_game_state).unwrap(),
                ))
        }
    }
}
fn handle_hand_result ( updated_game_state : & mut GameState , player1_game_move : GameMove , player2_game_move : GameMove , ) -> Result < Response , ContractError > { let result = get_result ( player1_game_move . clone ( ) , player2_game_move . clone ( ) ) ; match result { GameResult :: Player1Wins => { // increment player 1 win counter updated_game_state . player1_hands_won += 1 ; // check if game is over if updated_game_state . player1_hands_won == updated_game_state . num_hands_to_win { updated_game_state . result = Some ( result ) ; // pay the winner Ok ( send_double_tokens ( updated_game_state . clone ( ) . player1 , updated_game_state . clone ( ) . bet_amount , ) . add_attribute ( " " , " " ) . add_attribute ( " " , format ! ( " " updated_game_state clone ( ) player1 updated_game_state clone ( ) player2 ) , ) . add_attribute ( " " , updated_game_state . clone ( ) . player1 ) . add_attribute ( " " , player1_game_move . to_string ( ) ) . add_attribute ( " " , player2_game_move . to_string ( ) ) . add_attribute ( " " , serde_json :: to_string ( & updated_game_state ) . unwrap ( ) , ) ) } else { Ok ( Response :: new ( ) . add_attribute ( " " , " " ) . add_attribute ( " " , format ! ( " " updated_game_state clone ( ) player1 updated_game_state clone ( ) player2 ) , ) . add_attribute ( " " , updated_game_state . clone ( ) . player1 ) . add_attribute ( " " , player1_game_move . to_string ( ) ) . add_attribute ( " " , player2_game_move . to_string ( ) ) . add_attribute ( " " , serde_json :: to_string ( & updated_game_state ) . unwrap ( ) , ) ) } } GameResult :: Player2Wins => { // increment player 2 win counter updated_game_state . player2_hands_won += 1 ; // check if game is over if updated_game_state . player2_hands_won == updated_game_state . num_hands_to_win { updated_game_state . result = Some ( result ) ; // pay the winner Ok ( send_double_tokens ( updated_game_state . clone ( ) . player2 , updated_game_state . clone ( ) . bet_amount , ) . add_attribute ( " " , " " ) . add_attribute ( " " , format ! ( " " updated_game_state clone ( ) player1 updated_game_state clone ( ) player2 ) , ) . add_attribute ( " " , updated_game_state . clone ( ) . player2 ) . add_attribute ( " " , player1_game_move . to_string ( ) ) . add_attribute ( " " , player2_game_move . to_string ( ) ) . add_attribute ( " " , serde_json :: to_string ( & updated_game_state ) . unwrap ( ) , ) ) } else { Ok ( Response :: new ( ) . add_attribute ( " " , " " ) . add_attribute ( " " , format ! ( " " updated_game_state clone ( ) player1 updated_game_state clone ( ) player2 ) , ) . add_attribute ( " " , updated_game_state . clone ( ) . player2 ) . add_attribute ( " " , player1_game_move . to_string ( ) ) . add_attribute ( " " , player2_game_move . to_string ( ) ) . add_attribute ( " " , serde_json :: to_string ( & updated_game_state ) . unwrap ( ) , ) ) } } GameResult :: Tie => { // increment tie counter updated_game_state . hands_tied += 1 ; Ok ( Response :: new ( ) . add_attribute ( " " , " " ) . add_attribute ( " " , format ! ( " " updated_game_state clone ( ) player1 updated_game_state clone ( ) player2 ) , ) . add_attribute ( " " , " " ) . add_attribute ( " " , player1_game_move . to_string ( ) ) . add_attribute ( " " , player2_game_move . to_string ( ) ) . add_attribute ( " " , serde_json :: to_string ( & updated_game_state ) . unwrap ( ) , ) ) } } }
Function for making appriopriate payments and emitting apprioriate message attributes
based on the result of a hand
Function for making appriopriate payments and emitting apprioriate message attributes based on the result of a hand
updated_game_state
&mut GameState
player1_game_move
GameMove
player2_game_move
GameMove