def decode(self, msg): """ Decides which type of message the server has sent and verifies that the timing of the message is valid and communicates to the player if the message is valid. :param msg: the message from the server to decode. :return: the message to send to the Dealer, if any. """ if msg == "ok": return if "feeding" in possible_next_states[self.state] and len(msg) == 5: player = Convert.json_to_player_state(msg[0:3]) opponents = Convert.json_to_listof_listof_species(msg[4]) feeding = self.player.next_feeding(player, msg[3], opponents) self.state = "feeding" return Convert.feeding_to_json(feeding) (player, wh) = self.decode_start(msg) if player and "start" in possible_next_states[self.state]: print("Got start message") self.player.start(player, wh) self.state = "start" return decode_choose = self.decode_choose(msg) if decode_choose and "choose" in possible_next_states[self.state]: print("Choosing action") choice = self.player.choose(decode_choose) self.state = "choose" return Convert.action_to_json(choice) raise Exception
def decode_start(self, msg): """ Attempts to decode the given msg into a tuple (player, watering_hole) :param msg: The JSON message to decode. :return: The tuple (PlayerState, Int) representing the player and the food in the watering hole, or (False, False) if the conversion fails. """ try: assert(isinstance(msg[0], int)) return (Convert.json_to_player_state(msg[1:]), msg[0]) except Exception: return (False, False)