def send_action(self, action): url = server + "/games/" + self.game_id + "/do_action" print("sending json:", document_to_string(action)) request = requests.post(url, data=document_to_string(action), headers={"Content-Type": "application/json"}) print("received: " + request.text) json_response = request.json() if json_response["Status"] == "OK": if "Action outcome" in json_response: return Outcome.from_document(json_response["Action outcome"]) else: raise Exception("Unexpected response: " + document_to_string(json_response))
def select_action(self, gamestate): game = self.get_game() if not game: print("No new data on the server. By the way, our last update was from", self.last_modified) # No new data on the server return None, None, None expected_action = str(gamestate.action_count + 1) if game["action_count"] > gamestate.action_count + 1: # Several new things happened on the server # Handle this one now, but clear last_modified to make # sure we hear about the other stuff self.last_modified = datetime(1970, 1, 1) if game["action_count"] > gamestate.action_count: print("received action", document_to_string(game[expected_action])) action = Action.from_document(gamestate.all_units(), game[expected_action]) outcome = None if action.is_attack: outcome = Outcome.from_document(game[expected_action + "_outcome"]) upgrade = None if expected_action + "_options" in game: options = game[expected_action + "_options"] if "move_with_attack" in options: action.move_with_attack = bool(options["move_with_attack"]) if "upgrade" in options: upgrade = get_enum_attributes(options["upgrade"]) return action, outcome, upgrade return None, None, None
def save(self, view, action, outcome): action_count = self.gamestate.action_count action.outcome = outcome self.actions[action_count] = action if outcome: outcome_document = outcome.to_document() self.outcomes[action_count] = outcome_document filename = self.savegame_folder + "/" + str(action_count) view.save_screenshot(filename + ".jpeg") savegame_document = { "gamestate": self.gamestate.to_document(), "initial_gamestate": self.initial_gamestate.to_document(), "action_count": action_count } for action_number, action in self.actions.items(): savegame_document[action_number] = action.to_document() for action_number, document in self.outcomes.items(): savegame_document[str(action_number) + "_outcome"] = document for action_number, options in self.options.items(): savegame_document[str(action_number) + "_options"] = options with open(filename + ".json", 'w') as gamestate_file: gamestate_file.write(document_to_string(savegame_document))
def validate_upgrade(action_document, gamestate): position, unit = gamestate.get_upgradeable_unit() upgrade_options = unit.get_upgrade_choices() upgrade = get_enum_attributes(action_document["upgrade"]) if upgrade in upgrade_options: message = "Upgraded " + unit.pretty_name + " on " + str(position) new_unit = unit.get_upgraded_unit_from_upgrade(upgrade) new_unit_string = document_to_string(new_unit.to_document()) return True, message, new_unit_string else: option1 = str(get_string_attributes(upgrade_options[0])) option2 = str(get_string_attributes(upgrade_options[1])) message = "The upgrade must be one of " + option1 + " or " + option2 return False, message, ""
def validate_input(log_document, gamestate, action_document): expected_number, expected_type = get_expected_action( log_document, gamestate) if action_document["type"] == "action": action_type = "action" elif "move_with_attack" in action_document: action_type = "move_with_attack" elif "upgrade" in action_document: action_type = "upgrade" else: raise Exception("Unknown action type " + document_to_string(action_document)) if action_type != expected_type or expected_number != int( action_document["number"]): message = "The next action must have type " + expected_type + " and have number " + str( expected_number) return {"Status": "Error", "Message": message}
def __str__(self): return document_to_string(self.to_document())