def to_json(self): """ const Game = backbone.Model.extend({ validate: function(attr) { let err = [] if (attr.players === undefined) err.push("must have players") if (attr.winner === undefined) err.push("must have a winner") if (attr.gameID === undefined) err.push("must have a gameID") if (!Array.isArray(attr.players)) err.push("players must be an array") if(err.length) return err // checkpoint if (attr.players.length === 0) err.push("players must not be empty") let winnerFound = false attr.players.forEach(function(player, idx) { if (player.name === undefined) err.push("players[" + idx + "] must have a name") if (player.userID === undefined) err.push("players[" + idx + "] must have a userID") if (player.deck === undefined) err.push("players[" + idx + "] must have a deck") if (player.name === attr.winner) winnerFound = true }) if (!winnerFound) err.push("winner " + attr.winner + " not found in players") if(err.length) return err // checkpoint } }) :return: """ assert isinstance(self.hero, Player) assert isinstance(self.opponent, Player) hero_chess_time_total, oppo_chess_time_total = self.calculate_chess_timer_total() hero_obj = { "name": self.hero.player_name, "userID": self.hero.player_id, "deck": self.hero.original_deck.to_min_json(), "playedCards": self.hero.played_cards_to_min_json(), "mulliganCount": self.hero.mulligan_count, "timeSpent": str(hero_chess_time_total), } opponent_obj = { "name": self.opponent.player_name, "userID": self.opponent.player_id, "deck": self.opponent.seen_cards_to_min_json(), "mulliganCount": self.opponent.mulligan_count, "timeSpent": str(oppo_chess_time_total), } gameJSON = { "players": [hero_obj, opponent_obj], "winner": self.winner.player_name, "gameID": self.match_id, "turnNumber": self.turn_number, "elapsedTime": str(datetime.datetime.now() - self.start_time), "currentPlayer": self.current_player, "currentPhase": self.current_phase, "onThePlay": self.on_the_play, "opponentStartingRank": self.opponent_rank, "eventID": self.event_id, } gameJSON["game_hash"] = hash_json_object(gameJSON) return gameJSON
def save_game_result(self, game): anon_token_url = self.api_url + "/public-api/anon-api-token" token = requests.get(anon_token_url).json()["token"] url = self.api_url + "/anon-api/game" game["client_version"] = client_version game["game_hash"] = hash_json_object(game) print(requests.post(url, json=game, headers={"token": token}).json())
def to_json(self): """ const Game = backbone.Model.extend({ validate: function(attr) { let err = [] if (attr.players === undefined) err.push("must have players") if (attr.winner === undefined) err.push("must have a winner") if (attr.gameID === undefined) err.push("must have a gameID") if (!Array.isArray(attr.players)) err.push("players must be an array") if(err.length) return err // checkpoint if (attr.players.length === 0) err.push("players must not be empty") let winnerFound = false attr.players.forEach(function(player, idx) { if (player.name === undefined) err.push("players[" + idx + "] must have a name") if (player.userID === undefined) err.push("players[" + idx + "] must have a userID") if (player.deck === undefined) err.push("players[" + idx + "] must have a deck") if (player.name === attr.winner) winnerFound = true }) if (!winnerFound) err.push("winner " + attr.winner + " not found in players") if(err.length) return err // checkpoint } }) :return: """ assert isinstance(self.hero, Player) assert isinstance(self.opponent, Player) print(self.hero.original_deck) hero_obj = { "name": self.hero.player_name, "userID": self.hero.player_id, "deck": self.hero.original_deck.to_min_json() } opponent_obj = { "name": self.opponent.player_name, "userID": self.opponent.player_id, "deck": self.opponent.seen_cards_to_min_json() } gameJSON = { "players": [hero_obj, opponent_obj], "winner": self.winner.player_name, "gameID": self.match_id, } gameJSON["game_hash"] = hash_json_object(gameJSON) return gameJSON
def to_json(self): """ const Game = backbone.Model.extend({ validate: function(attr) { let err = [] if (attr.players === undefined) err.push("must have players") if (attr.winner === undefined) err.push("must have a winner") if (attr.gameID === undefined) err.push("must have a gameID") if (!Array.isArray(attr.players)) err.push("players must be an array") if(err.length) return err // checkpoint if (attr.players.length === 0) err.push("players must not be empty") let winnerFound = false attr.players.forEach(function(player, idx) { if (player.name === undefined) err.push("players[" + idx + "] must have a name") if (player.userID === undefined) err.push("players[" + idx + "] must have a userID") if (player.deck === undefined) err.push("players[" + idx + "] must have a deck") if (player.name === attr.winner) winnerFound = true }) if (!winnerFound) err.push("winner " + attr.winner + " not found in players") if(err.length) return err // checkpoint } }) :return: """ assert isinstance(self.hero, Player) assert isinstance(self.opponent, Player) hero_chess_time_total, oppo_chess_time_total = self.calculate_chess_timer_total() hero_obj = { "name": self.hero.player_name, "userID": self.hero.player_id, "deck": self.hero.original_deck.to_min_json(), "playedCards": self.hero.played_cards_to_min_json(), "mulliganCount": self.hero.mulligan_count, "timeSpent": str(hero_chess_time_total), } opponent_obj = { "name": self.opponent.player_name, "userID": self.opponent.player_id, "deck": self.opponent.seen_cards_to_min_json(), "mulliganCount": self.opponent.mulligan_count, "timeSpent": str(oppo_chess_time_total), } # haxx to condense game history logs (8kb record -> 4kb record!) # assign each unique blob a numeric ID, then whenever a blob is repeated, # drop in the ID instead of the full blob # # pseudocode for decoding: # for event in gameHistory: # for blob in event: # print(historyKey[str(blob)]) minimized_events = [] blob_map = {} blob_id = 0 for event_list in self.events: minimized_event_list = [] for blob in event_list: if isinstance(blob, dict) and "type" in blob.keys() and blob["type"] == "turn": blob = "turn++" if blob not in blob_map.values(): blob_map[blob_id] = blob minimized_event_list.append(blob_id) blob_id += 1 else: for key in blob_map.keys(): if blob_map[key] == blob: minimized_event_list.append(key) break # these in theory will be 1:1, but only add once to be sure minimized_events.append(minimized_event_list) gameJSON = { "players": [hero_obj, opponent_obj], "winner": self.winner.player_name, "gameID": self.match_id, "turnNumber": self.turn_number, "elapsedTime": str(datetime.datetime.now() - self.start_time), "currentPlayer": self.current_player, "currentPhase": self.current_phase, "onThePlay": self.on_the_play, "opponentStartingRank": self.opponent_rank, "eventID": self.event_id, "gameHistory": minimized_events, "historyKey": blob_map } gameJSON["game_hash"] = hash_json_object(gameJSON) return gameJSON