def getProfile(self, JID): """ Retrieves the profile for the specified JID """ stats = {} player = db.query(Player).filter(Player.jid.ilike(str(JID))) if not player.first(): return queried_player = player.first() playerID = queried_player.id if queried_player.rating != -1: stats['rating'] = str(queried_player.rating) rank = db.query(Player).filter( Player.rating >= queried_player.rating).count() stats['rank'] = str(rank) if queried_player.highest_rating != -1: stats['highestRating'] = str(queried_player.highest_rating) gamesPlayed = db.query(PlayerInfo).filter_by( player_id=playerID).count() wins = db.query(Game).filter_by(winner_id=playerID).count() stats['totalGamesPlayed'] = str(gamesPlayed) stats['wins'] = str(wins) stats['losses'] = str(gamesPlayed - wins) return stats
def getProfile(self, JID): """ Retrieves the profile for the specified JID """ stats = {} player = db.query(Player).filter(Player.jid.ilike(str(JID))) if not player.first(): return queried_player = player.first() playerID = queried_player.id if queried_player.rating != -1: stats['rating'] = str(queried_player.rating) rank = db.query(Player).filter(Player.rating >= queried_player.rating).count() stats['rank'] = str(rank) if queried_player.highest_rating != -1: stats['highestRating'] = str(queried_player.highest_rating) gamesPlayed = db.query(PlayerInfo).filter_by(player_id=playerID).count() wins = db.query(Game).filter_by(winner_id=playerID).count() stats['totalGamesPlayed'] = str(gamesPlayed) stats['wins'] = str(wins) stats['losses'] = str(gamesPlayed - wins) return stats
def getProfile(self, JID): """ Retrieves the profile for the specified JID """ stats = {} player = db.query(Player).filter(Player.jid.ilike(str(JID))) if not player.first(): return if player.first().rating != -1: stats['rating'] = str(player.first().rating) if player.first().highest_rating != -1: stats['highestRating'] = str(player.first().highest_rating) playerID = player.first().id players = db.query(Player).order_by(Player.rating.desc()).all() for rank, user in enumerate(players): if (user.jid.lower() == JID.lower()): stats['rank'] = str(rank+1) break stats['totalGamesPlayed'] = str(db.query(PlayerInfo).filter_by(player_id=playerID).count()) stats['wins'] = str(db.query(Game).filter_by(winner_id=playerID).count()) stats['losses'] = str(db.query(PlayerInfo).filter_by(player_id=playerID).count() - db.query(Game).filter_by(winner_id=playerID).count()) return stats
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 getBoard(self): """ Returns a dictionary of player rankings to JIDs for sending. """ board = {} players = db.query(Player).filter(Player.rating != -1).order_by(Player.rating.desc()).limit(100).all() for rank, player in enumerate(players): board[player.jid] = {'name': '@'.join(player.jid.split('@')[:-1]), 'rating': str(player.rating)} return board
def removePlayer(self, JID): """ Remove a player(JID) from database. Returns the player that was removed, or None if that player didn't exist. """ players = db.query(Player).filter_by(jid=JID) player = players.first() if not player: return None players.delete() return player
def getBoard(self): """ Returns a dictionary of player rankings to JIDs for sending. """ board = {} players = db.query(Player).order_by(Player.rating.desc()).limit(100).all() for rank, player in enumerate(players): # Don't send uninitialized ratings. if player.rating == -1: continue board[player.jid] = {'name': '@'.join(player.jid.split('@')[:-1]), 'rating': str(player.rating)} return board
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 getBoard(self): """ Returns a dictionary of player rankings to JIDs for sending. """ board = {} players = db.query(Player).filter(Player.rating != -1).order_by( Player.rating.desc()).limit(100).all() for rank, player in enumerate(players): board[player.jid] = { 'name': '@'.join(player.jid.split('@')[:-1]), 'rating': str(player.rating) } return board
def getRatingList(self, nicks): """ Returns a rating list of players currently in the lobby by nick because the client can't link JID to nick conveniently. """ ratinglist = {} players = db.query(Player.jid, Player.rating).filter(func.upper(Player.jid).in_([ str(JID).upper() for JID in list(nicks) ])) for player in players: rating = str(player.rating) if player.rating != -1 else '' for JID in list(nicks): if JID.upper() == player.jid.upper(): ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': rating} break return ratinglist
def getRatingList(self, nicks): """ Returns a rating list of players currently in the lobby by nick because the client can't link JID to nick conveniently. """ ratinglist = {} for JID in list(nicks): players = db.query(Player.jid, Player.rating).filter(Player.jid.ilike(str(JID))) if players.first(): if players.first().rating == -1: ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': ''} else: ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': str(players.first().rating)} else: ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': ''} return ratinglist
def getRatingList(self, nicks): """ Returns a rating list of players currently in the lobby by nick because the client can't link JID to nick conveniently. """ ratinglist = {} for JID in nicks.keys(): players = db.query(Player).filter(Player.jid.ilike(str(JID))) if players.first(): if players.first().rating == -1: ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': ''} else: ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': str(players.first().rating)} else: ratinglist[nicks[JID]] = {'name': nicks[JID], 'rating': ''} return ratinglist
def getRatingList(self, nicks): """ Returns a rating list of players currently in the lobby by nick because the client can't link JID to nick conveniently. """ ratinglist = {} players = db.query(Player.jid, Player.rating).filter( func.upper(Player.jid).in_( [str(JID).upper() for JID in list(nicks)])) for player in players: rating = str(player.rating) if player.rating != -1 else '' for JID in list(nicks): if JID.upper() == player.jid.upper(): ratinglist[nicks[JID]] = { 'name': nicks[JID], 'rating': rating } break return ratinglist
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