def card_store_request_handler(self, data): username, room, game, state = reconstruct_game(data) selected_cards_ui_message = state.deck_handler.json_store() self.send_to_client(CARD_STORE_RESPONSE, username, room, selected_cards_ui_message)
def force_yield_tokyo_request_handler(self, data): username, room, game, state = reconstruct_game(data) yielding_player = data['payload'] DropFromHighAltitude().immediate_effect( state.players.get_player_by_username_from_alive(username), state.players.get_player_by_username_from_alive(yielding_player)) self.update_player_status(state, username, room, game)
def end_turn_handler(self, data): # a method to end a players turn and let the next guy go username, room, game, state = reconstruct_game(data) state.post_roll_actions(state.players.current_player) next_player: Player = state.get_next_player_turn() state.dice_handler.roll_initial( state.players.current_player.dice_allowed, DEFAULT_RE_ROLL_COUNT) values = state.dice_handler.dice_values rolled_dice_ui_message = dice_values_message_create(values) if next_player: self.send_to_client( SERVER_RESPONSE, username, room, dice_vals_log_message(next_player.username, values)) self.send_to_client(BEGIN_TURN_RESPONSE, username, room, next_player.username) self.send_to_client(DICE_ROLLS_RESPONSE, next_player.username, room, rolled_dice_ui_message) else: self.send_to_client(BEGIN_TURN_RESPONSE, username, room, "None") self.update_player_status(state, username, room, game) for player in state.players.players: if state.check_if_winner(player): self.send_to_client(WINNER_ALERT, state.players.current_player.username, room, "Winner") break
def selected_dice_handler(self, data): username, room, game, state = reconstruct_game(data) i_repository_dice = IRepositoryDice() payload = data['payload'] selected_dice = decode_selected_dice_indexes(payload) try: state.dice_handler.re_roll_dice(selected_dice) values = state.dice_handler.dice_values self.send_to_client(SERVER_RESPONSE, username, room, dice_vals_log_message(username, values)) rolled_dice_ui_message = dice_values_message_create(values) self.send_to_client(DICE_ROLLS_RESPONSE, username, room, rolled_dice_ui_message) save_game(game, state) except ValueError: self.send_to_client(SERVER_RESPONSE, username, room, "{} out of rolls.".format(username)) # serialize then store modified GameState object save_game(game, state) values = state.dice_handler.dice_values if len(values) == 6: dice_store = i_repository_dice.save_dice( username, room, values[0], values[1], values[2], values[3], values[4], values[5], None, None, state.dice_handler.re_rolls_left, selected_dice) rolled_dice_ui_message = dice_values_message_create(values) self.send_to_client(DICE_ROLLS_RESPONSE, username, room, rolled_dice_ui_message)
def resolve_dice_handler(self, data): username, room, game, state = reconstruct_game(data) self.send_to_client( SERVER_RESPONSE, username, room, "{} locked in dice".format(state.players.current_player.username)) dice_resolution( state.dice_handler.dice_values, state.players.get_current_player(), state.players.get_all_alive_players_minus_current_player()) self.update_player_status(state, username, room, game) self.trigger_yield_popup_if_necessary(state, room)
def return_dice_state_handler(self, data): username, room, game, state = reconstruct_game(data) i_repository_dice = IRepositoryDice() values = state.dice_handler.dice_values if len(values) == 6: dice_store = i_repository_dice.save_dice( username, room, values[0], values[1], values[2], values[3], values[4], values[5], None, None, state.dice_handler.re_rolls_left, None) rolled_dice_ui_message = dice_values_message_create(values) self.send_to_client(SERVER_RESPONSE, username, room, dice_vals_log_message(username, values)) self.send_to_client(DICE_ROLLS_RESPONSE, username, room, rolled_dice_ui_message)
def buy_card_request_handler(self, data): username, room, game, state = reconstruct_game(data) if username != state.players.current_player.username: return index_to_buy = data['payload'] try: bought = state.deck_handler.buy_card_from_store( index_to_buy, state.players.current_player, state.players.get_all_alive_players_minus_current_player()) if bought: i_repository_play = IRepositoryPlay() i_repository_play.save_play_card_purchased( username, room, bought.name, bought.card_type, state.players.current_player.location, state.players.current_player.victory_points, state.players.current_player.energy, state.players.current_player.current_health) if isinstance(bought, DropFromHighAltitude): if state.players.get_count_in_tokyo_ignore_current_player( ) > 1: self.trigger_force_yield_choice(username, state, room) else: move_players_out_of_tokyo( state.players. get_all_alive_players_minus_current_player()) state.players.current_player.move_to_tokyo() current_card_store = state.deck_handler.json_store() self.send_to_client(CARD_STORE_RESPONSE, username, room, current_card_store) self.update_player_status(state, username, room, game) self.send_to_client( SERVER_RESPONSE, username, room, "{} bought {}!".format(state.players.current_player.username, bought.name)) except InsufficientFundsException: self.send_to_client( SERVER_RESPONSE, username, room, "{} tried to buy {} but has insufficient energy!".format( username, state.deck_handler.store[index_to_buy].name)) save_game(game, state)
def yield_tokyo_request_handler(self, data): username, room, game, state = reconstruct_game(data) player = state.players.get_player_by_username_from_alive(username) if player.allowed_to_yield: state.yield_tokyo_to_current_player(player) self.send_to_client( SERVER_RESPONSE, username, room, "{} yields Tokyo to {}!".format( username, state.players.current_player.username)) state.players.reset_allowed_to_yield() self.update_player_status(state, username, room, game) self.send_to_client(END_TURN, state.players.current_player.username, room, "allow end turn") else: print("{} can't yield tokyo!".format(username)) save_game(game, state)
def keep_tokyo_request_handler(self, data): username, room, game, state = reconstruct_game(data) player = state.players.get_player_by_username_from_alive(username) player.allowed_to_yield = False save_game(game, state) self.send_to_client(SERVER_RESPONSE, username, room, "{} refuses to yield Tokyo!".format(username)) awaiting_yield_response = False for p in state.players.players: if p.allowed_to_yield: awaiting_yield_response = True self.send_to_client( SERVER_RESPONSE, username, room, "Waiting for {} to decide whether to yield Tokyo!".format( p.username)) if not awaiting_yield_response: self.send_to_client(END_TURN, state.players.current_player.username, room, "allow end turn")
def card_store_sweep_request_handler(self, data): username, room, game, state = reconstruct_game(data) if state.players.current_player.username == username: cards_swept = state.deck_handler.store.copy() print("Cards to be swept {}".format(cards_swept)) successfully_swept_cardstore = state.deck_handler.sweep_store( state.players.current_player) if successfully_swept_cardstore is None: i_repository_play = IRepositoryPlay() i_repository_play.save_play_card_swept( username, room, cards_swept[2].name, cards_swept[2].card_type, cards_swept[1].name, cards_swept[1].card_type, cards_swept[0].name, cards_swept[0].card_type, state.players.current_player.location, state.players.current_player.victory_points, state.players.current_player.energy, state.players.current_player.current_health) cards_swept.clear() if not successfully_swept_cardstore: message = "{} does not have enough funds to sweep the card store!".format( username) self.send_to_client(SERVER_RESPONSE, username, room, message) else: print("{} tried to sweep out of turn!".format(username)) self.update_player_status(state, username, room, game) selected_cards_ui_message = state.deck_handler.json_store() self.send_to_client(CARD_STORE_RESPONSE, username, room, selected_cards_ui_message) save_game(game, state)
def gamelog_send_handler(self, data): username, room, game, state = reconstruct_game(data) mud_gamelog_input = data['payload'] self.send_to_client(SERVER_RESPONSE, username, room, mud_gamelog_input)