async def get_num_records(self, player): """ Get the number of local records. :param player: Player instance. :type player: pyplanet.apps.core.maniaplanet.models.Player :return: count or False when not possible to fetch. :rtype: int """ if self.app.instance.game.game == 'tm' and 'local_records' in self.app.instance.apps.apps: return await LocalRecord.objects.count( LocalRecord.select(LocalRecord).where( LocalRecord.player == player)) return False
async def get_num_top_3(self, player): """ Get the number of top-3 records of one player. :param player: Player instance. :type player: pyplanet.apps.core.maniaplanet.models.Player :return: count or False when not possible to fetch. :rtype: int """ # Get the players number of top-3 records (tm only + when local records is active). if self.app.instance.game.game == 'tm' and 'local_records' in self.app.instance.apps.apps: top = 0 records = await LocalRecord.objects.execute( LocalRecord.select(LocalRecord).where( LocalRecord.player == player)) for record in records: top_3 = await LocalRecord.objects.execute( LocalRecord.select(LocalRecord).where( LocalRecord.map_id == record.map_id).limit(3)) if record in top_3: top += 1 return top return False
async def get_topsums(self): """ Get the topsums of the server. :return: List of top 100 players on the server with the statistics. """ if 'local_records' not in self.app.instance.apps.apps: return None now = datetime.now() diff = now - self.topsums_cache_time if self.topsums_cache_time else False if self.topsums_cache and diff and diff.total_seconds() <= 60: return self.topsums_cache self.topsums_cache = None self.topsums_cache_time = None maps = self.app.instance.map_manager.maps players = {} for map_instance in maps: res = await LocalRecord.objects.execute( LocalRecord.select(LocalRecord, Player) .join(Player) .where(LocalRecord.map_id == map_instance.id) .order_by(LocalRecord.score) .limit(3) ) for rank, entry in enumerate(res): if entry.player not in players: players[entry.player] = [0, 0, 0] players[entry.player][rank] += 1 topsums = list(players.items()) topsums.sort(key=lambda item: item[1][0] + item[1][1] + item[1][2], reverse=True) self.topsums_cache = topsums[:100] self.topsums_cache_time = datetime.now() return self.topsums_cache