async def tournament_progress_update(self, match_ups): """Updates the observer on the progress of a board game tournament by consuming the given players who advanced to the next round and the players who got knocked out. Args: match_ups (list): a lisg of list of player names where each list is the group of players in a match. """ msg = Message.construct_msg(MsgType.T_PROGRESS, match_ups) await self.socket.send(msg)
async def tournament_end_update(self, winners): """Updates the observer on the final winners of the board game tournament, the finals winners include the top three players, with first player in the winners list as first place and the last one as thrid place. Args: winners (list(str)): a list of player names """ msg = Message.construct_msg(MsgType.T_END, winners) self.writer.write(msg.encode())
async def tournament_start_update(self, players): """Updatest the observer on the start of a board game tournament with the initial contestents. Args: players (list(str)): a list of string representing player names """ msg = Message.construct_msg(MsgType.T_START, players) await self.socket.send(msg)
async def game_kick_update(self, player): """Updates the observer on a player kick from the board game. Args: player (str): a color string representing a player """ msg = Message.construct_msg(MsgType.G_KICK, player) await self.socket.send(msg)
async def game_start_update(self, game_state): """Updates the observer on the start of a board game by consuming the given starting players and the game state. Args: game_state (IState): a game state object """ msg = Message.construct_msg(MsgType.G_START, game_state.serialize()) await self.socket.send(msg)
async def game_action_update(self, action, game_state): """Updates the observer on an action progress of a board game. Args: action (Action): an action game_state (IState): a game state object """ msg = Message.construct_msg(MsgType.G_ACTION, [action, game_state.serialize()]) await self.socket.send(msg)
async def playing_as(self, color): """Updates the player the color that it's playing as in a board game. Args: color (str): a color string """ self.color = color msg = Message.construct_msg(MsgType.PLAYING_AS, color) self.writer.write(msg.encode()) await self.writer.drain()
async def game_kick_update(self, player): """Updates the observer on a player kick from the board game. Args: player (str): a color string representing a player """ if self.color == player: self.writer.close() await self.writer.wait_closed() else: msg = Message.construct_msg(MsgType.G_KICK, player) self.writer.write(msg.encode()) await self.writer.drain()
async def get_action(self, game_state): """Finds the action to take in a board game by consuming the given game state, the player also recieves all action and player kick updates due to being an observer, thus a stateful implementation is also viable. Args: game_state (IState): a game state object Returns: Action: an action to take """ msg = Message.construct_msg(MsgType.T_ACTION, game_state.serialize()) self.writer.write(msg.encode()) await self.writer.drain() resp = await self.reader.read(1024) _, content = Message.decode(resp) return content
async def signup(self): """Signs up to a board game tournament with the server by sending it the player name, returns the stream reader and writer for communication with server on successful signup else returns False incidating the signup failed. Returns: union(tuple, False): a tuple with the first a stream reader and second or False """ try: reader, writer = await open_connection(self.host, self.port) msg = Message.construct_msg(MsgType.SIGNUP, self.name) writer.write(msg.encode()) await writer.drain() return reader, writer except Exception: writer.close() await writer.wait_closed() return False
async def communication_loop(self, reader, writer): """Runs the communication loop, sending and recieving messeages to and from the server as specified by the plug and play protocol using the given stream reader and writer, the communication can be stopped by setting the stop_communication class instance variable to true. Args: reader (Streams.StreamReader): a stream reader writer (Streams.StreamWriter): a stream writer """ self.stop_communication = False while not self.stop_communication and not reader.at_eof(): msg = await reader.read(1024) if msg: resp = await self.process_message(msg) if resp: msg = Message.construct_msg(MsgType.R_ACTION, resp) writer.write(msg.encode()) await writer.drain() writer.close() await writer.wait_closed()