def getOrCreatePlayer(self, JID): """ Stores a player(JID) in the database if they don't yet exist. Returns either the newly created instance of the Player model, or the one that already exists in the database. """ players = db.query(Player).filter_by(jid=str(JID)) if not players.first(): player = Player(jid=str(JID), rating=-1) db.add(player) db.commit() return player return players.first()
def addGame(self, gamereport): """ Adds a game to the database and updates the data on a player(JID) from game results. Returns the created Game object, or None if the creation failed for any reason. Side effects: Inserts a new Game instance into the database. """ # Discard any games still in progress. if any(map(lambda state: state == 'active', dict.values(gamereport['playerStates']))): return None players = map(lambda jid: db.query(Player).filter_by(jid=jid).first(), dict.keys(gamereport['playerStates'])) winning_jid = list(dict.keys({jid: state for jid, state in gamereport['playerStates'].items() if state == 'won'}))[0] def get(stat, jid): return gamereport[stat][jid] stats = {'civ': 'civs', 'foodGathered': 'foodGathered', 'foodUsed': 'foodUsed', 'woodGathered': 'woodGathered', 'woodUsed': 'woodUsed', 'stoneGathered': 'stoneGathered', 'stoneUsed': 'stoneUsed', 'metalGathered': 'metalGathered', 'metalUsed': 'metalUsed'} playerInfos = [] for player in players: jid = player.jid playerinfo = PlayerInfo(player=player) for dbname, reportname in stats.items(): setattr(playerinfo, dbname, get(reportname, jid)) playerInfos.append(playerinfo) game = Game(map=gamereport['mapName'], duration=int(gamereport['timeElapsed'])) game.players.extend(players) game.player_info.extend(playerInfos) game.winner = db.query(Player).filter_by(jid=winning_jid).first() db.add(game) db.commit() return game
def addGame(self, gamereport): """ Adds a game to the database and updates the data on a player(JID) from game results. Returns the created Game object, or None if the creation failed for any reason. Side effects: Inserts a new Game instance into the database. """ # Discard any games still in progress. if any(map(lambda state: state == 'active', dict.values(gamereport['playerStates']))): return None players = map(lambda jid: db.query(Player).filter(Player.jid.ilike(str(jid))).first(), dict.keys(gamereport['playerStates'])) winning_jid = list(dict.keys({jid: state for jid, state in gamereport['playerStates'].items() if state == 'won'}))[0] def get(stat, jid): return gamereport[stat][jid] singleStats = {'timeElapsed', 'mapName', 'teamsLocked', 'matchID'} totalScoreStats = {'economyScore', 'militaryScore', 'totalScore'} resourceStats = {'foodGathered', 'foodUsed', 'woodGathered', 'woodUsed', 'stoneGathered', 'stoneUsed', 'metalGathered', 'metalUsed', 'vegetarianFoodGathered', 'treasuresCollected', 'lootCollected', 'tributesSent', 'tributesReceived'} unitsStats = {'totalUnitsTrained', 'totalUnitsLost', 'enemytotalUnitsKilled', 'infantryUnitsTrained', 'infantryUnitsLost', 'enemyInfantryUnitsKilled', 'workerUnitsTrained', 'workerUnitsLost', 'enemyWorkerUnitsKilled', 'femaleUnitsTrained', 'femaleUnitsLost', 'enemyFemaleUnitsKilled', 'cavalryUnitsTrained', 'cavalryUnitsLost', 'enemyCavalryUnitsKilled', 'championUnitsTrained', 'championUnitsLost', 'enemyChampionUnitsKilled', 'heroUnitsTrained', 'heroUnitsLost', 'enemyHeroUnitsKilled', 'shipUnitsTrained', 'shipUnitsLost', 'enemyShipUnitsKilled', 'traderUnitsTrained', 'traderUnitsLost', 'enemyTraderUnitsKilled'} buildingsStats = {'totalBuildingsConstructed', 'totalBuildingsLost', 'enemytotalBuildingsDestroyed', 'civCentreBuildingsConstructed', 'civCentreBuildingsLost', 'enemyCivCentreBuildingsDestroyed', 'houseBuildingsConstructed', 'houseBuildingsLost', 'enemyHouseBuildingsDestroyed', 'economicBuildingsConstructed', 'economicBuildingsLost', 'enemyEconomicBuildingsDestroyed', 'outpostBuildingsConstructed', 'outpostBuildingsLost', 'enemyOutpostBuildingsDestroyed', 'militaryBuildingsConstructed', 'militaryBuildingsLost', 'enemyMilitaryBuildingsDestroyed', 'fortressBuildingsConstructed', 'fortressBuildingsLost', 'enemyFortressBuildingsDestroyed', 'wonderBuildingsConstructed', 'wonderBuildingsLost', 'enemyWonderBuildingsDestroyed'} marketStats = {'woodBought', 'foodBought', 'stoneBought', 'metalBought', 'tradeIncome'} miscStats = {'civs', 'teams', 'percentMapExplored'} stats = totalScoreStats | resourceStats | unitsStats | buildingsStats | marketStats | miscStats playerInfos = [] for player in players: jid = player.jid playerinfo = PlayerInfo(player=player) for reportname in stats: setattr(playerinfo, reportname, get(reportname, jid.lower())) playerInfos.append(playerinfo) game = Game(map=gamereport['mapName'], duration=int(gamereport['timeElapsed']), teamsLocked=bool(gamereport['teamsLocked']), matchID=gamereport['matchID']) game.players.extend(players) game.player_info.extend(playerInfos) game.winner = db.query(Player).filter(Player.jid.ilike(str(winning_jid))).first() db.add(game) db.commit() return game