def getPlayerName(self, player): ''' Get the avatar name ''' try : if player.getLogin() in self.avatarNames: return self.avatarNames[player.getLogin()] self.log.debug("getting avatar name for %s (uid %i)" % (player.getLogin(), player.getId()) ) query = QSqlQuery(self.parent.db) uid = int(player.getId()) query.prepare("SELECT name, id FROM galacticwar.`avatars` WHERE `uid` = ? AND `alive` = 1") query.addBindValue(uid) query.exec_() if query.size() == 1 : query.first() name = str(query.value(0)) uid = int(query.value(1)) self.avatarNames[player.getLogin()] = name self.avatarIds[player.getLogin()] = uid self.log.debug("avatar name is %s (uid %i)" % (name, uid) ) return name else : self.avatarNames[player.getLogin()] = player.getLogin() self.avatarIds[player.getLogin()] = player.getId() return player.getLogin() except : self.log.exception("Something awful happened when getting a gw name !")
def setUndefended(self, planetuid): for uid in self.attacks : if self.attacks[uid].getPlanet() == planetuid : self.attacks[uid].defended = False query = QSqlQuery(self.parent.db) query.prepare("UPDATE `attacks` SET `defended` = 0 WHERE id = ?") query.addBindValue(uid) query.exec_() return
def getMapName(self, mapId): query = QSqlQuery(self.db) query.prepare("SELECT filename FROM table_map WHERE id = ?") query.addBindValue(mapId) query.exec_() if query.size() > 0: query.first() return str(query.value(0)).split("/")[1].replace(".zip", "") else: return None
def setUndefended(self, planetuid): for uid in self.attacks: if self.attacks[uid].getPlanet() == planetuid: self.attacks[uid].defended = False query = QSqlQuery(self.parent.db) query.prepare( "UPDATE `attacks` SET `defended` = 0 WHERE id = ?") query.addBindValue(uid) query.exec_() return
def getSelectedLadderMaps(self, playerId): query = QSqlQuery(self.db) query.prepare("SELECT idMap FROM ladder_map_selection WHERE idUser = ?") query.addBindValue(playerId) query.exec_() maps = [] if query.size() > 0: while query.next(): maps.append(int(query.value(0))) return maps
def getPlayerRealFaction(self, uid): query = QSqlQuery(self.parent.db) query.prepare("SELECT faction FROM galacticwar.`accounts` WHERE `uid` = ?") query.addBindValue(uid) query.exec_() if query.size() == 1 : query.first() return int(query.value(0)) return None
def getPlayerRealFaction(self, uid): query = QSqlQuery(self.parent.db) query.prepare( "SELECT faction FROM galacticwar.`accounts` WHERE `uid` = ?") query.addBindValue(uid) query.exec_() if query.size() == 1: query.first() return int(query.value(0)) return None
def getSelectedLadderMaps(self, playerId): query = QSqlQuery(self.db) query.prepare( "SELECT idMap FROM ladder_map_selection WHERE idUser = ?") query.addBindValue(playerId) query.exec_() maps = [] if query.size() > 0: while query.next(): maps.append(int(query.value(0))) return maps
def deleteGroup(self, group, player): query = QSqlQuery(self.parent.db) query.prepare("SELECT id FROM login WHERE `login` = ?") query.addBindValue(player) query.exec_() if query.size() == 1 : query.first() uid = int(query.value(0)) if not uid in self.deletedGroups: self.deletedGroups[uid] = [] if not group in self.deletedGroups[uid]: self.parent.lobby.sendJSON(dict(command="delete_group", group=group, playeruid=uid)) self.deletedGroups[uid].append(group)
def deleteGroup(self, group, player): query = QSqlQuery(self.parent.db) query.prepare("SELECT id FROM login WHERE `login` = ?") query.addBindValue(player) query.exec_() if query.size() == 1: query.first() uid = int(query.value(0)) if not uid in self.deletedGroups: self.deletedGroups[uid] = [] if not group in self.deletedGroups[uid]: self.parent.lobby.sendJSON( dict(command="delete_group", group=group, playeruid=uid)) self.deletedGroups[uid].append(group)
def getLeague(self, season, player): query = QSqlQuery(self.db) query.prepare("SELECT league FROM %s WHERE idUser = ?" % season) query.addBindValue(player.getId()) query.exec_() if query.size() > 0 : query.first() return int(query.value(0)) # place the player in his league ! else : query.prepare("INSERT INTO %s (`idUser` ,`league` ,`score`) VALUES (?, 1, 0)" % season) query.addBindValue(player.getId()) query.exec_() return 1
def getLeague(self, season, player): query = QSqlQuery(self.db) query.prepare("SELECT league FROM %s WHERE idUser = ?" % season) query.addBindValue(player.getId()) query.exec_() if query.size() > 0: query.first() return int(query.value(0)) # place the player in his league ! else: query.prepare( "INSERT INTO %s (`idUser` ,`league` ,`score`) VALUES (?, 1, 0)" % season) query.addBindValue(player.getId()) query.exec_() return 1
def updateTrueskillFor1v1(self): ''' Update all scores from the DB before updating the results''' try : for team in self.finalTeams1v1 : for member in team.getAllPlayers() : query = QSqlQuery(self.parent.db) query.prepare("SELECT mean, deviation FROM ladder1v1_rating WHERE id = (SELECT id FROM login WHERE login = ?)") query.addBindValue(member.getId()) query.exec_() self.log.debug("updating a player") if query.size() > 0: query.first() team.getRating(member).setMean(query.value(0)) team.getRating(member).setStandardDeviation(query.value(1)) else : self.log.debug("error updating a player") self.log.debug(member.getId()) except : self.log.exception("Something awful happened while updating trueskill!")
def add(self, winner, loser): query = QSqlQuery(self.parent.db) query.prepare("INSERT INTO `domination`(`dominant`, `slave`) VALUES (?,?)") query.addBindValue(winner) query.addBindValue(loser) query.exec_() query.prepare("UPDATE `domination` SET `dominant`=? WHERE dominant = ?") query.addBindValue(winner) query.addBindValue(loser) query.exec_() self.dominations[loser] = winner
def add(self, winner, loser): query = QSqlQuery(self.parent.db) query.prepare( "INSERT INTO `domination`(`dominant`, `slave`) VALUES (?,?)") query.addBindValue(winner) query.addBindValue(loser) query.exec_() query.prepare( "UPDATE `domination` SET `dominant`=? WHERE dominant = ?") query.addBindValue(winner) query.addBindValue(loser) query.exec_() self.dominations[loser] = winner
def updateTrueskillFor1v1(self): ''' Update all scores from the DB before updating the results''' try: for team in self.finalTeams1v1: for member in team.getAllPlayers(): query = QSqlQuery(self.parent.db) query.prepare( "SELECT mean, deviation FROM ladder1v1_rating WHERE id = (SELECT id FROM login WHERE login = ?)" ) query.addBindValue(member.getId()) query.exec_() self.log.debug("updating a player") if query.size() > 0: query.first() team.getRating(member).setMean(query.value(0)) team.getRating(member).setStandardDeviation( query.value(1)) else: self.log.debug("error updating a player") self.log.debug(member.getId()) except: self.log.exception( "Something awful happened while updating trueskill!")
def getPlayerName(self, player): ''' Get the avatar name ''' try: if player.getLogin() in self.avatarNames: return self.avatarNames[player.getLogin()] self.log.debug("getting avatar name for %s (uid %i)" % (player.getLogin(), player.getId())) query = QSqlQuery(self.parent.db) uid = int(player.getId()) query.prepare( "SELECT name, id FROM galacticwar.`avatars` WHERE `uid` = ? AND `alive` = 1" ) query.addBindValue(uid) query.exec_() if query.size() == 1: query.first() name = str(query.value(0)) uid = int(query.value(1)) self.avatarNames[player.getLogin()] = name self.avatarIds[player.getLogin()] = uid self.log.debug("avatar name is %s (uid %i)" % (name, uid)) return name else: self.avatarNames[player.getLogin()] = player.getLogin() self.avatarIds[player.getLogin()] = player.getId() return player.getLogin() except: self.log.exception( "Something awful happened when getting a gw name !")
def getGWReplaysInfos(self): self.parent.db.open() self.replayInfo["game_end"] = time.time() query = QSqlQuery(self.parent.db) self.replayInfo["featured_mod"] = "gw" self.replayInfo["game_type"] = 0 query.prepare("SELECT filename, planets.name, avatars.name \ FROM galacticwar.game_stats \ LEFT JOIN galacticwar.game_player_stats ON galacticwar.game_player_stats.`gameId` = galacticwar.game_stats.id \ LEFT JOIN galacticwar.planets ON galacticwar.planets.id = galacticwar.game_stats.planetuid \ LEFT JOIN galacticwar.planet_maps ON galacticwar.planet_maps.planetuid = galacticwar.game_stats.planetuid \ LEFT JOIN faf_lobby.table_map ON galacticwar.planet_maps.`mapuid` = faf_lobby.table_map.id \ LEFT JOIN galacticwar.avatars ON galacticwar.avatars.id = galacticwar.game_player_stats.`avatarId` \ WHERE galacticwar.game_stats.id = ? ") query.addBindValue(self.uid) query.exec_() if query.size() != 0: self.replayInfo["num_players"] = query.size() query.first() mapname = str(query.value(0)) self.replayInfo["title"] = str("battle on " +query.value(1)) self.replayInfo["featured_mod_versions"] = {} tableMod = "updates_gw" tableModFiles = tableMod + "_files" query2 = QSqlQuery(self.parent.db) query2.prepare("SELECT fileId, MAX(version) FROM `%s` LEFT JOIN %s ON `fileId` = %s.id GROUP BY fileId" % (tableModFiles, tableMod, tableMod)) query2.exec_() if query2.size() != 0 : while query2.next() : self.replayInfo["featured_mod_versions"][int(query2.value(0))] = int(query2.value(1)) self.replayInfo["mapname"] = os.path.splitext(os.path.basename(mapname))[0] self.replayInfo["complete"] = True self.parent.db.close()
def done(self): #writing file self.__logger.debug("writing the replay") self.__logger.debug(self.uid) if self.gw : self.getGWReplaysInfos() else: self.getReplaysInfos() # Construct the path where the replay is stored path = config['global']['content_path'] + "vault/replay_vault" if self.gw : path = config['global']['content_path'] + "gwreplays" dirsize = 100 depth = 5 i = depth dirname = path while i > 1: dirname = dirname + "/" + str((self.uid/(dirsize**(i-1)))%dirsize) i = i - 1 filename = dirname + "/" + str(self.uid) + ".fafreplay" self.__logger.debug("filename: " + filename) if not os.path.exists(dirname): os.makedirs(dirname) writeFile = QtCore.QFile(filename) if(writeFile.open(QtCore.QIODevice.WriteOnly)) : writeFile.write(json.dumps(self.replayInfo)) writeFile.write('\n') replayData = QtCore.QByteArray() replayDataQByte = QtCore.QByteArray() replayDataStream = QtCore.QDataStream(replayDataQByte, QtCore.QIODevice.WriteOnly) replayStream = QtCore.QDataStream(self.getReplayData(), QtCore.QIODevice.ReadOnly) replayStream.device().seek(0) while replayStream.atEnd() == False : timePacket = replayStream.readDouble() lenData = replayStream.readUInt32() datas = replayStream.readRawData(lenData) replayData.append(datas) replayDataStream.writeUInt32(replayData.size()) replayDataStream.writeRawData(zlib.compress(replayData.data(),9)) writeFile.write(replayDataQByte.toBase64()) writeFile.close() # We mention the existence of the replay inside the Database. self.parent.db.open() query = QSqlQuery(self.parent.db) if self.gw : query.prepare("INSERT INTO `galacticwar`.`game_replays`(`UID`) VALUES (?)") else : query.prepare("INSERT INTO `game_replays`(`UID`) VALUES (?)") query.addBindValue(self.uid) if not query.exec_(): self.__logger.debug("error adding replay to database") self.parent.db.close() self.__logger.debug("fafreplay written")
def specialEnding(self, logger, db, players): if len(self.invalidPlayers) == 2: self.setInvalid( "Scores not validated. Possible reason : Disconnection between players." ) if self.isValid(): if self.isDraw(): query = QSqlQuery(db) queryStr = ("SELECT id FROM table_map WHERE filename LIKE '%" + self.getGameMap() + "%'") query.exec_(queryStr) if query.size() == 1: query.first() mapId = query.value(0) queryStr = ( "UPDATE table_map_features set num_draws = (num_draws +1) WHERE map_id LIKE " + str(mapId)) query.exec_(queryStr) # tsresults = self.computeResults() # tsplayers = self.getTrueSkillPlayers() # self.trueSkillUpdate(tsresults, tsplayers, logger, db, players) tsresults = self.computeResults1v1() tsplayers = self.getTrueSkill1v1Players() self.trueSkillUpdate(tsresults, tsplayers, logger, db, players, playerFnc="setladder1v1Rating", table="ladder1v1_rating", winner=True, sendScore=False) # and for the ladder ! evenLeague = True maxleague = max(self.leagues.iteritems(), key=operator.itemgetter(1))[1] if len(set(self.leagues.values())) != 1: evenLeague = False if not self.isDraw(): query = QSqlQuery(db) for player in self.gameResult: if self.isWinner(player): # if not even league: scoreToAdd = 1 if not evenLeague: if self.leagues[player] == maxleague: scoreToAdd = 0.5 else: scoreToAdd = 1.5 query.prepare( "UPDATE %s SET score = (score + ?) WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(scoreToAdd) query.addBindValue(player) query.exec_() logger.debug(query.executedQuery()) else: # if not even league: scoreToRemove = 0.5 if not evenLeague: if self.leagues[player] == maxleague: scoreToRemove = 1 else: scoreToRemove = 0 query.prepare( "UPDATE %s SET score = GREATEST(0,(score - ?)) WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(scoreToRemove) query.addBindValue(player) query.exec_() logger.debug(query.executedQuery()) #check if the user must be promoted query.prepare( "SELECT league, score FROM %s WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() if query.size() != 0: update = False query.first() pleague = int(query.value(0)) pscore = float(query.value(1)) if pleague == 1 and pscore > 50: query.prepare( "UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True elif pleague == 2 and pscore > 75: query.prepare( "UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True elif pleague == 3 and pscore > 100: query.prepare( "UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True elif pleague == 4 and pscore > 150: query.prepare( "UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True for p in players.getAllPlayers(): if str(p.getLogin()) == str(player): query.prepare( "SELECT score, league FROM %s WHERE idUser = ?" % self.parent.season) query.addBindValue(p.getId()) query.exec_() if query.size() > 0: query.first() score = float(query.value(0)) league = int(query.value(1)) query.prepare( "SELECT name, `limit` FROM `ladder_division` WHERE `league` = ? AND `limit` >= ? ORDER BY `limit` ASC LIMIT 1" ) query.addBindValue(league) query.addBindValue(score) query.exec_() if query.size() > 0: query.first() p.setLeague(league) p.division = str(query.value(0)) else: tsplayers = self.getTrueSkillPlayers() for playerTS in tsplayers: name = playerTS.getPlayer() self.sendMessageToPlayers(players, name, self.getInvalidReason())
def addResultPlayer(self, player, faresult, score): if faresult == "recall": self.log.debug("%s recalled" % player) if not player in self.recalled: self.recalled.append(player) self.gameResult[player] = -1 self.gameFaResult[player] = faresult if faresult == "autorecall": self.gameResult[player] = -1 self.gameFaResult[player] = faresult self.log.debug("%s autorecalled" % player) if not player in self.autorecalled: self.autorecalled.append(player) query = QSqlQuery(self.parent.db) query.prepare("SELECT id FROM login WHERE `login` = ?") query.addBindValue(player) query.exec_() if query.size() == 1: query.first() playerUid = int(query.value(0)) self.log.debug(playerUid) self.parent.lobby.sendJSON( dict(command="autorecall", playeruid=playerUid)) if player in self.gameFaResult: self.log.debug("%s previous result : %s " % (player, self.gameFaResult[player])) self.log.debug("%s new result : %s " % (player, faresult)) if self.gameFaResult[player] == "score": # the play got not decicive result yet, so we can apply it. self.gameFaResult[player] = faresult #self.gameResult[player] = score else: if faresult == "defeat": if player in self.recalled or player in self.autorecalled: self.log.debug("recalled to defeat -> invalid") self.wrongReport = True elif self.gameFaResult[player] == "victory": if not player in self.invalidPlayers: self.log.debug("victory to defeat !?") #self.invalidPlayers.append(player) elif time.time() - self.startedAt < ((60 * 4) + 10): self.log.debug("too soon to die...") self.setInvalid( "Game is invalid : Play time was not long enough.") #if we try to set a defeat, but the player was victory.. We've got a disparity problem ! # else: if faresult != "score": self.gameFaResult[player] = faresult if faresult == "defeat": self.gameResult[player] = -1 elif faresult == "recall" or faresult == "autorecall": self.gameResult[player] = -1 elif faresult == "victory": self.gameResult[player] = 1 elif faresult == "draw": self.gameResult[player] = -1 else: self.log.debug("%s result : %s " % (player, faresult)) if faresult != "score": self.gameFaResult[player] = faresult if faresult == "defeat": self.gameResult[player] = -1 elif faresult == "recall" or faresult == "autorecall": self.gameResult[player] = -1 elif faresult == "victory": self.gameResult[player] = 1 elif faresult == "draw": self.gameResult[player] = -1
def specialEnding(self, logger, db, players): try: self.log.debug("special ending.") if len(self.invalidPlayers ) == self.numPlayers or self.wrongReport == True: if self.wrongReport: self.log.debug( "Recalled player declared defeated.. Player recalled:") for p in self.recalled: self.log.debug(p) self.log.debug("Scores") self.log.debug(self.gameResult) self.log.debug("Recalled player declared defeated..") else: self.log.debug( "Invalid : self.invalidPlayers == numPlayer") self.parent.lobby.sendJSON( dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results={})) return False #computing winning team teams = self.finalTeams teamsResults = {} teamsResults[1] = {} teamsResults[2] = {} teamsResults[1]["players"] = {} teamsResults[2]["players"] = {} for teams in self.finalTeams: curScore = 0 for player in teams.getAllPlayers(): uid = player.getId() self.log.debug("searching") self.log.debug(uid) query = QSqlQuery(self.parent.db) query.prepare("SELECT id FROM login WHERE `login` = ?") query.addBindValue(uid) query.exec_() playerUid = 1 if query.size() == 1: query.first() self.log.debug("found uid") playerUid = int(query.value(0)) self.log.debug(playerUid) if playerUid in self.attackers: i = 1 else: i = 2 if uid in str(self.gameResult): resultPlayer = self.gameResult[uid] if resultPlayer <= -1: if uid in self.recalled: teamsResults[i]["players"][playerUid] = 0 elif uid in self.autorecalled: teamsResults[i]["players"][playerUid] = -1 else: teamsResults[i]["players"][playerUid] = -2 else: teamsResults[i]["players"][playerUid] = 1 curScore = curScore + resultPlayer else: self.parent.lobby.sendJSON( dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results={})) self.log.debug("score not found for %s" % str(uid)) return 0 teamsResults[i]["score"] = curScore if self.desync < 2: results = json.dumps(teamsResults) self.log.debug(teamsResults) self.parent.lobby.sendJSON( dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results=results)) else: self.log.debug(self.getInvalidReason()) self.parent.lobby.sendJSON( dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results={})) except: self.log.exception( "Something awful happened when finishing a gw game !")
def addResultPlayer(self, player, faresult, score): if faresult == "recall" : self.log.debug("%s recalled" % player ) if not player in self.recalled: self.recalled.append(player) self.gameResult[player] = -1 self.gameFaResult[player] = faresult if faresult == "autorecall" : self.gameResult[player] = -1 self.gameFaResult[player] = faresult self.log.debug("%s autorecalled" % player ) if not player in self.autorecalled: self.autorecalled.append(player) query = QSqlQuery(self.parent.db) query.prepare("SELECT id FROM login WHERE `login` = ?") query.addBindValue(player) query.exec_() if query.size() == 1 : query.first() playerUid = int(query.value(0)) self.log.debug(playerUid) self.parent.lobby.sendJSON(dict(command="autorecall", playeruid=playerUid)) if player in self.gameFaResult : self.log.debug("%s previous result : %s " % (player, self.gameFaResult[player])) self.log.debug("%s new result : %s " % (player, faresult)) if self.gameFaResult[player] == "score" : # the play got not decicive result yet, so we can apply it. self.gameFaResult[player] = faresult #self.gameResult[player] = score else : if faresult == "defeat": if player in self.recalled or player in self.autorecalled: self.log.debug("recalled to defeat -> invalid") self.wrongReport = True elif self.gameFaResult[player] == "victory" : if not player in self.invalidPlayers : self.log.debug("victory to defeat !?") #self.invalidPlayers.append(player) elif time.time() - self.startedAt < ((60*4) + 10) : self.log.debug("too soon to die...") self.setInvalid("Game is invalid : Play time was not long enough.") #if we try to set a defeat, but the player was victory.. We've got a disparity problem ! # else : if faresult != "score" : self.gameFaResult[player] = faresult if faresult == "defeat" : self.gameResult[player] = -1 elif faresult == "recall" or faresult == "autorecall": self.gameResult[player] = -1 elif faresult == "victory" : self.gameResult[player] = 1 elif faresult == "draw" : self.gameResult[player] = -1 else : self.log.debug("%s result : %s " % (player, faresult)) if faresult != "score" : self.gameFaResult[player] = faresult if faresult == "defeat" : self.gameResult[player] = -1 elif faresult == "recall" or faresult == "autorecall": self.gameResult[player] = -1 elif faresult == "victory" : self.gameResult[player] = 1 elif faresult == "draw" : self.gameResult[player] = -1
def specialEnding(self, logger, db, players): try : self.log.debug("special ending.") if len(self.invalidPlayers) == self.numPlayers or self.wrongReport == True: if self.wrongReport: self.log.debug("Recalled player declared defeated.. Player recalled:") for p in self.recalled: self.log.debug(p) self.log.debug("Scores") self.log.debug(self.gameResult) self.log.debug("Recalled player declared defeated..") else: self.log.debug("Invalid : self.invalidPlayers == numPlayer") self.parent.lobby.sendJSON(dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results={})) return False #computing winning team teams = self.finalTeams teamsResults = {} teamsResults[1] = {} teamsResults[2] = {} teamsResults[1]["players"] = {} teamsResults[2]["players"] = {} for teams in self.finalTeams : curScore = 0 for player in teams.getAllPlayers() : uid = player.getId() self.log.debug("searching") self.log.debug(uid) query = QSqlQuery(self.parent.db) query.prepare("SELECT id FROM login WHERE `login` = ?") query.addBindValue(uid) query.exec_() playerUid = 1 if query.size() == 1 : query.first() self.log.debug("found uid") playerUid = int(query.value(0)) self.log.debug(playerUid) if playerUid in self.attackers : i = 1 else : i = 2 if uid in str(self.gameResult) : resultPlayer = self.gameResult[uid] if resultPlayer <= -1 : if uid in self.recalled : teamsResults[i]["players"][playerUid] = 0 elif uid in self.autorecalled: teamsResults[i]["players"][playerUid] = -1 else : teamsResults[i]["players"][playerUid] = -2 else : teamsResults[i]["players"][playerUid] = 1 curScore = curScore + resultPlayer else : self.parent.lobby.sendJSON(dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results={})) self.log.debug("score not found for %s" % str(uid)) return 0 teamsResults[i]["score"] = curScore if self.desync < 2: results = json.dumps(teamsResults) self.log.debug(teamsResults) self.parent.lobby.sendJSON(dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results=results)) else: self.log.debug(self.getInvalidReason()) self.parent.lobby.sendJSON(dict(command="results", gameuid=self.uuid, planetuid=self.planetuid, results={})) except : self.log.exception("Something awful happened when finishing a gw game !")
def specialEnding(self, logger, db, players): if len(self.invalidPlayers) == 2 : self.setInvalid("Scores not validated. Possible reason : Disconnection between players.") if self.isValid() : if self.isDraw() : query = QSqlQuery(db) queryStr = ("SELECT id FROM table_map WHERE filename LIKE '%"+self.getGameMap()+"%'") query.exec_(queryStr) if query.size() == 1: query.first() mapId = query.value(0) queryStr = ("UPDATE table_map_features set num_draws = (num_draws +1) WHERE map_id LIKE " + str(mapId)) query.exec_(queryStr) # tsresults = self.computeResults() # tsplayers = self.getTrueSkillPlayers() # self.trueSkillUpdate(tsresults, tsplayers, logger, db, players) tsresults = self.computeResults1v1() tsplayers = self.getTrueSkill1v1Players() self.trueSkillUpdate(tsresults, tsplayers, logger, db, players, playerFnc="setladder1v1Rating" ,table="ladder1v1_rating", winner=True, sendScore=False) # and for the ladder ! evenLeague = True maxleague = max(self.leagues.iteritems(), key=operator.itemgetter(1))[1] if len(set(self.leagues.values())) != 1 : evenLeague = False if not self.isDraw() : query = QSqlQuery(db) for player in self.gameResult : if self.isWinner(player) : # if not even league: scoreToAdd = 1 if not evenLeague: if self.leagues[player] == maxleague: scoreToAdd = 0.5 else : scoreToAdd = 1.5 query.prepare("UPDATE %s SET score = (score + ?) WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(scoreToAdd) query.addBindValue(player) query.exec_() logger.debug(query.executedQuery()) else: # if not even league: scoreToRemove = 0.5 if not evenLeague : if self.leagues[player] == maxleague: scoreToRemove = 1 else : scoreToRemove = 0 query.prepare("UPDATE %s SET score = GREATEST(0,(score - ?)) WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(scoreToRemove) query.addBindValue(player) query.exec_() logger.debug(query.executedQuery()) #check if the user must be promoted query.prepare("SELECT league, score FROM %s WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() if query.size() != 0: update = False query.first() pleague = int(query.value(0)) pscore = float(query.value(1)) if pleague == 1 and pscore > 50: query.prepare("UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True elif pleague == 2 and pscore > 75: query.prepare("UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True elif pleague == 3 and pscore > 100: query.prepare("UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True elif pleague == 4 and pscore > 150: query.prepare("UPDATE %s SET league = league+1, score = 0 WHERE `idUser` = (SELECT id FROM login WHERE login.login = ?)" % self.parent.season) query.addBindValue(player) query.exec_() update = True for p in players.getAllPlayers() : if str(p.getLogin()) == str(player) : query.prepare("SELECT score, league FROM %s WHERE idUser = ?" % self.parent.season) query.addBindValue(p.getId()) query.exec_() if query.size() > 0: query.first() score = float(query.value(0)) league = int(query.value(1)) query.prepare("SELECT name, `limit` FROM `ladder_division` WHERE `league` = ? AND `limit` >= ? ORDER BY `limit` ASC LIMIT 1") query.addBindValue(league) query.addBindValue(score) query.exec_() if query.size() > 0: query.first() p.setLeague(league) p.division = str(query.value(0)) else : tsplayers = self.getTrueSkillPlayers() for playerTS in tsplayers : name = playerTS.getPlayer() self.sendMessageToPlayers(players, name, self.getInvalidReason())
def cancelAttack(self, planetuid): query = QSqlQuery(self.parent.db) query.prepare("DELETE FROM `attacks` WHERE `uid_planet` = ?") query.addBindValue(planetuid) query.exec_() self.update()