def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0] if state == "setup": if primary in ("size", "sz",): self.set_size(player, command_bits[1:]) handled = True if primary in ("directional", "goals", "dir", "goal",): self.set_directional(player, command_bits[1]) handled = True if primary in ("done", "ready", "d", "r",): self.channel.broadcast_cc(self.prefix + "The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ("config", "setup", "conf",): self.state.set("setup") self.channel.broadcast_cc(self.prefix + "^R%s^~ has switched the game to setup mode.\n" % player) handled = True elif state == "playing": made_move = False if primary in ("move", "play", "mv", "pl",): invalid = False move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.move(player, move_bits[0]) else: invalid = True if invalid: player.tell_cc(self.prefix + "Invalid move command.\n") handled = True elif primary in ("swap",): if self.turn_number == 2 and self.seats[1].player == player: self.swap(player) made_move = True else: player.tell_cc(self.prefix + "Unsuccessful swap.\n") handled = True elif primary in ("resign",): if self.resign(player): made_move = True handled = True if made_move: if self.turn == BLACK: self.turn = WHITE else: self.turn = BLACK # Did someone win? winner = self.find_winner() if winner: self.resolve(winner) self.finish() else: # Nope. show everyone the board, and keep on. self.send_board() if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.lower().split() primary = command_bits[0] if state == "setup": if primary in ("size", "sz"): if len(command_bits) == 2: self.set_size(player, command_bits[1]) else: self.tell_pre(player, "Invalid size command.\n") handled = True elif primary in ("done", "ready", "d", "r"): self.bc_pre("The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ("config", "setup", "conf"): self.bc_pre("^R%s^~ has switched the game to setup mode.\n" % player) self.state.set("setup") handled = True elif state == "playing": made_move = False if primary in ("move", "play", "mv", "pl"): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 2: made_move = self.move(player, move_bits[0], move_bits[1]) else: self.tell_pre(player, "Invalid move command.\n") handled = True elif primary in ("remove", "re"): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.remove(player, move_bits[0]) else: self.tell_pre(player, "Invalid remove command.\n") handled = True elif primary in ("resign",): made_move = self.resign(player) handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: # Yup! self.resolve(winner) self.finish() else: # No. Change turns and send the board to listeners. self.turn = self.next_seat(self.turn) self.send_board() if not handled: self.tell_pre(player, "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands first. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0].lower() if primary in ("score", "scores"): self.show_scores(player) handled = True elif state == "need_players": if primary in ("column", "columns"): if len(command_bits) == 2: self.set_max_columns(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid columns command.\n") handled = True elif primary in ("delay",): if len(command_bits) == 2: self.set_delay(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid delay command.\n") handled = True elif primary in ("cards", "count"): if len(command_bits) == 2: self.set_max_count(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid cards command.\n") handled = True elif primary in ("borders", "border"): if len(command_bits) == 2: self.set_border(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid border command.\n") handled = True elif primary in ("start",): if not len(self.seats): player.tell_cc(self.prefix + "Need at least one player!\n") else: self.state.set("playing") self.channel.broadcast_cc(self.prefix + "Game on!\n") self.build_deck() self.build_layout() self.update_printable_layout() self.send_layout() self.last_play_time = time.time() handled = True elif state == "playing": # Everything at this point should be a move, which consists # of a list of 3 card choices. As always, do the polite thing # for players who do the play/pl/move/mv/thing. if primary in ('play', 'move', 'pl', 'mv'): play_bits = demangle_move(command_bits[1:]) else: play_bits = demangle_move(command_bits) if play_bits and len(play_bits) == 3: self.declare(player, play_bits) handled = True if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.lower().split() primary = command_bits[0] if state == "setup": if primary in ("size", "sz"): self.set_size(player, command_bits[1:]) handled = True elif primary in ( "done", "ready", "d", "r", ): self.bc_pre("The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ( "config", "setup", "conf", ): self.bc_pre( "^R%s^~ has switched the game to setup mode.\n" % player) self.state.set("setup") handled = True elif state == "playing": made_move = False if primary in ( "move", "play", "mv", "pl", ): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.move(player, move_bits[0]) else: self.tell_pre(player, "Invalid move command.\n") handled = True if primary in ( "redstone", "red", "r", ): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.red(player, move_bits[0]) else: self.tell_pre(player, "Invalid red command.\n") handled = True elif primary in ("resign", ): made_move = self.resign(player) handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: # Yup! self.resolve(winner) self.finish() else: # No. Switch turns. self.turn = self.next_seat(self.turn) self.send_board() if not handled: self.tell_pre(player, "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0].lower() if state == "setup": if primary in ("size", "sz",): if len(command_bits) == 2: self.set_size(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid size command.\n") handled = True elif primary in ("players", "player", "pl",): if len(command_bits) == 2: self.set_player_mode(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid player mode command.\n") handled = True elif primary in ("pit", "hole",): loc_list = demangle_move(command_bits[1:]) if loc_list: self.toggle_pits(player, loc_list) else: player.tell_cc(self.prefix + "Invalid pit command.\n") handled = True elif primary in ("ready", "done", "r", "d",): self.channel.broadcast_cc(self.prefix + "The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ("config", "setup", "conf",): self.state.set("setup") self.channel.broadcast_cc(self.prefix + "^R%s^~ has switched the game to setup mode.\n" % player) handled = True elif state == "playing": made_move = False if primary in ("move", "play", "mv", "pl",): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 2: made_move = self.move(player, move_bits[0], move_bits[1]) else: player.tell_cc(self.prefix + "Invalid move command.\n") handled = True elif primary in ("resign",): self.resign(player) made_move = True handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: self.resolve(winner) self.finish() else: # Okay, well, let's see whose turn it is. If it comes # back around to us, the game is over anyway. curr_turn = self.turn done = False while not done: if self.turn == RED: self.turn = BLUE elif self.turn == BLUE: # The only tough one; switch depending on mode. if self.player_mode == 2: self.turn = RED else: self.turn = GREEN elif self.turn == GREEN: self.turn = YELLOW elif self.turn == YELLOW: self.turn = RED # Now see if this player even has a move. if self.color_has_move(self.turn): done = True elif self.turn == curr_turn: # If we've wrapped back around to the current # turn, no one had a move. Bail as well. done = True # Check to see if we're back at the mover. if curr_turn == self.turn: # No one had a valid move. Game's over. self.no_move_resolve() self.finish() else: # Otherwise it's some other player's turn; game on. self.send_board() if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0].lower() if state == "setup": if primary in ( "size", "sz", ): if len(command_bits) == 2: self.set_size(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid size command.\n") handled = True elif primary in ( "players", "player", "pl", ): if len(command_bits) == 2: self.set_player_mode(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid player mode command.\n") handled = True elif primary in ( "pit", "hole", ): loc_list = demangle_move(command_bits[1:]) if loc_list: self.toggle_pits(player, loc_list) else: player.tell_cc(self.prefix + "Invalid pit command.\n") handled = True elif primary in ( "ready", "done", "r", "d", ): self.channel.broadcast_cc( self.prefix + "The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ( "config", "setup", "conf", ): self.state.set("setup") self.channel.broadcast_cc( self.prefix + "^R%s^~ has switched the game to setup mode.\n" % player) handled = True elif state == "playing": made_move = False if primary in ( "move", "play", "mv", "pl", ): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 2: made_move = self.move(player, move_bits[0], move_bits[1]) else: player.tell_cc(self.prefix + "Invalid move command.\n") handled = True elif primary in ("resign", ): self.resign(player) made_move = True handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: self.resolve(winner) self.finish() else: # Okay, well, let's see whose turn it is. If it comes # back around to us, the game is over anyway. curr_turn = self.turn done = False while not done: if self.turn == RED: self.turn = BLUE elif self.turn == BLUE: # The only tough one; switch depending on mode. if self.player_mode == 2: self.turn = RED else: self.turn = GREEN elif self.turn == GREEN: self.turn = YELLOW elif self.turn == YELLOW: self.turn = RED # Now see if this player even has a move. if self.color_has_move(self.turn): done = True elif self.turn == curr_turn: # If we've wrapped back around to the current # turn, no one had a move. Bail as well. done = True # Check to see if we're back at the mover. if curr_turn == self.turn: # No one had a valid move. Game's over. self.no_move_resolve() self.finish() else: # Otherwise it's some other player's turn; game on. self.send_board() if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands first. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0].lower() if primary in ("score", "scores"): self.show_scores(player) handled = True elif state == "need_players": if primary in ("column", "columns"): if len(command_bits) == 2: self.set_max_columns(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid columns command.\n") handled = True elif primary in ("delay", ): if len(command_bits) == 2: self.set_delay(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid delay command.\n") handled = True elif primary in ("cards", "count"): if len(command_bits) == 2: self.set_max_count(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid cards command.\n") handled = True elif primary in ("borders", "border"): if len(command_bits) == 2: self.set_border(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid border command.\n") handled = True elif primary in ("start", ): if not len(self.seats): player.tell_cc(self.prefix + "Need at least one player!\n") else: self.state.set("playing") self.channel.broadcast_cc(self.prefix + "Game on!\n") self.build_deck() self.build_layout() self.update_printable_layout() self.send_layout() self.last_play_time = time.time() handled = True elif state == "playing": # Everything at this point should be a move, which consists # of a list of 3 card choices. As always, do the polite thing # for players who do the play/pl/move/mv/thing. if primary in ('play', 'move', 'pl', 'mv'): play_bits = demangle_move(command_bits[1:]) else: play_bits = demangle_move(command_bits) if play_bits and len(play_bits) == 3: self.declare(player, play_bits) handled = True if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.lower().split() primary = command_bits[0] if state == "setup": if primary in ("size", "sz"): self.set_size(player, command_bits[1:]) handled = True elif primary in ( "done", "ready", "d", "r", ): self.bc_pre("The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ( "config", "setup", "conf", ): self.bc_pre( "^R%s^~ has switched the game to setup mode.\n" % player) self.state.set("setup") handled = True elif state == "playing": made_move = False if primary in ( "move", "play", "mv", "pl", ): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.move(player, move_bits[0]) else: self.tell_pre(player, "Invalid move command.\n") handled = True elif primary in ("resign", ): made_move = self.resign(player) handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: # Yup! self.resolve(winner) self.finish() else: # No. If the move was not a capturing move, see if the # next player has a move; if so, change turns. If not, # print a message and stay here. other = self.next_seat(self.turn) if not self.move_was_capture: if not self.has_move(other): self.bc_pre( "%s has no valid move; ^Rskipping their turn^~.\n" % self.get_sp_str(other)) else: self.turn = other elif not self.has_move(self.turn): self.bc_pre("%s has no further valid moves.\n" % self.get_sp_str(self.turn)) self.turn = other else: self.bc_pre("%s continues their turn.\n" % self.get_sp_str(self.turn)) # No matter what, send the board again. self.send_board() if not handled: self.tell_pre(player, "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0] if state == "setup": if primary in ( "size", "sz", ): if len(command_bits) == 2: self.set_size(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid size command.\n") handled = True elif primary in ("ko", ): if len(command_bits) == 2: self.set_ko_fight(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid ko command.\n") handled = True if primary in ( "done", "ready", "d", "r", ): self.channel.broadcast_cc( self.prefix + "The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ( "config", "setup", "conf", ): self.state.set("setup") self.channel.broadcast_cc( self.prefix + "^R%s^~ has switched the game to setup mode.\n" % player) handled = True elif state == "playing": made_move = False if primary in ( "move", "play", "mv", "pl", ): invalid = False move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.move(player, move_bits[0]) else: invalid = True if invalid: player.tell_cc(self.prefix + "Invalid move command.\n") handled = True elif primary in ("swap", ): if self.seats[1].player == player and self.turn_number == 2: self.swap(player) made_move = True else: player.tell_cc(self.prefix + "Invalid swap command.\n") handled = True elif primary in ("resign", ): if self.resign(player): made_move = True handled = True if made_move: # Okay, something happened on the board. Update. self.update_printable_board() # Did someone win? winner = self.find_winner() if winner: self.resolve(winner) self.finish() else: # Nope. Switch turns... if self.turn == BLACK: self.turn = WHITE else: self.turn = BLACK # ...show everyone the board, and keep on. self.send_board() if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) state = self.state.get() command_bits = command_str.split() primary = command_bits[0].lower() if state == "setup": if primary in ('size', 'sz'): if len(command_bits) == 2: self.set_size(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid size command.\n") handled = True elif primary in ('master', 'm'): if len(command_bits) == 2: self.set_master(player, command_bits[1]) else: player.tell_cc(self.prefix + "Invalid master command.\n") handled = True elif primary in ('done', 'ready', 'd', 'r'): self.channel.broadcast_cc(self.prefix + "The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ('config', 'setup', 'conf'): self.state.set("setup") self.channel.broadcast_cc(self.prefix + "^R%s^~ has switched the game to setup mode.\n" % (player)) handled = True elif state == "playing": made_move = False # For all move types, don't bother if it's not this player's turn. if primary in ('move', 'mv', 'play', 'pl', 'swap', 'resign'): seat = self.get_seat_of_player(player) if not seat: player.tell_cc(self.prefix + "You can't move; you're not playing!\n") return elif seat.data.color != self.turn: player.tell_cc(self.prefix + "You must wait for your turn to move.\n") return if primary in ('move', 'mv', 'play', 'pl'): move_bits = demangle_move(command_bits[1:]) if move_bits: success = self.move(seat, move_bits) if success: move = success made_move = True else: player.tell_cc(self.prefix + "Unsuccessful move.\n") else: player.tell_cc(self.prefix + "Unsuccessful move.\n") handled = True elif primary in ('swap',): if self.turn_number == 2 and seat.player == player: self.swap() move = "swap" made_move = True else: player.tell_cc(self.prefix + "Unsuccessful swap.\n") handled = True elif primary in ('resign',): if self.resign(seat): move = "resign" made_move = True handled = True if made_move: self.update_printable_board() self.send_board() self.move_list.append(move) self.turn_number += 1 winner = self.find_winner() if winner: self.resolve(winner) self.finish() else: if self.turn == WHITE: self.turn = BLACK else: self.turn = WHITE self.channel.broadcast_cc(self.prefix + self.get_turn_str()) if not handled: player.tell_cc(self.prefix + "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.lower().split() primary = command_bits[0] if state == "setup": if primary in ("size", "sz"): self.set_size(player, command_bits[1:]) handled = True elif primary in ("done", "ready", "d", "r",): self.bc_pre("The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ("config", "setup", "conf",): self.bc_pre("^R%s^~ has switched the game to setup mode.\n" % player) self.state.set("setup") handled = True elif state == "playing": made_move = False if primary in ("move", "play", "mv", "pl",): move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 1: made_move = self.move(player, move_bits[0]) else: self.tell_pre(player, "Invalid move command.\n") handled = True elif primary in ("resign",): made_move = self.resign(player) handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: # Yup! self.resolve(winner) self.finish() else: # No. If the move was not a capturing move, see if the # next player has a move; if so, change turns. If not, # print a message and stay here. other = self.next_seat(self.turn) if not self.move_was_capture: if not self.has_move(other): self.bc_pre("%s has no valid move; ^Rskipping their turn^~.\n" % self.get_sp_str(other)) else: self.turn = other elif not self.has_move(self.turn): self.bc_pre("%s has no further valid moves.\n" % self.get_sp_str(self.turn)) self.turn = other else: self.bc_pre("%s continues their turn.\n" % self.get_sp_str(self.turn)) # No matter what, send the board again. self.send_board() if not handled: self.tell_pre(player, "Invalid command.\n")
def handle(self, player, command_str): # Handle common commands. handled = self.handle_common_commands(player, command_str) if not handled: state = self.state.get() command_bits = command_str.split() primary = command_bits[0] if state == "setup": if primary in ("rows", "ro",): self.set_rows(player, command_bits[1:]) handled = True if primary in ("size", "sz",): self.set_size(player, command_bits[1:]) handled = True if primary in ("done", "ready", "d", "r",): self.bc_pre("The game is now looking for players.\n") self.state.set("need_players") handled = True elif state == "need_players": if primary in ("config", "setup", "conf",): self.state.set("setup") self.bc_pre("^R%s^~ has switched the game to setup mode.\n" % player) handled = True elif state == "playing": made_move = False if primary in ("move", "play", "mv", "pl",): invalid = False move_bits = demangle_move(command_bits[1:]) if move_bits and len(move_bits) == 2: made_move = self.move(player, move_bits[0], move_bits[1]) else: invalid = True if invalid: self.tell_pre(player, "Invalid move command.\n") handled = True elif primary in ("resign",): if self.resign(player): made_move = True handled = True if made_move: # Did someone win? winner = self.find_winner() if winner: self.resolve(winner) self.finish() else: # Nope. Switch turns... self.turn = self.next_seat(self.turn) # ...show everyone the board, and keep on. self.send_board() if not handled: self.tell_pre(player, "Invalid command.\n")