Poodletooth-iLand/toontown/racing/LeaderboardMgrAI.py

72 lines
2.6 KiB
Python
Raw Normal View History

2015-06-06 12:43:58 -05:00
from otp.ai.MagicWordGlobal import *
2015-06-06 12:52:37 -05:00
from toontown.toonbase import TTLocalizer
2015-06-06 12:43:58 -05:00
import RaceGlobals, operator, time
class LeaderboardMgrAI:
def __init__(self, air):
self.air = air
2015-08-05 02:55:11 -05:00
self.database = {}
if not self.air.dbConn:
self.database = simbase.backups.load('leaderboard', (self.air.districtId,), default=({}))
else:
self.air.mongodb.toontown.leaderboard.ensure_index([('ai', 1)])
district = {'ai': self.air.districtId}
2015-08-05 02:55:11 -05:00
try:
doc = self.air.mongodb.toontown.leaderboard.find_one(district)
2015-08-05 02:55:11 -05:00
except AutoReconnect:
return blocks
2015-06-06 12:43:58 -05:00
def getDatabase(self):
return self.database
def saveDatabase(self):
simbase.backups.save('leaderboard', (self.air.districtId,), self.database)
messenger.send('goofyLeaderboardChange')
def trimList(self, list):
return list[:RaceGlobals.NumRecordsPerPeriod]
def clearRace(self, race):
if race in self.database:
del self.database[race]
self.saveDatabase()
def submitRace(self, raceId, name, timestamp):
2015-06-06 12:52:37 -05:00
for i in xrange(len(TTLocalizer.RecordPeriodStrings)):
2015-06-06 12:43:58 -05:00
race = '%s, %s' % (raceId, i)
if race in self.database:
originalRace = self.database[race][1]
newRace = list(originalRace)
newRace.append([name, timestamp])
2015-06-06 12:43:58 -05:00
sortedRace = self.trimList(sorted(newRace, key=operator.itemgetter(1)))
if originalRace != sortedRace:
self.database[race][1] = sortedRace
2015-06-06 12:59:31 -05:00
self.saveDatabase()
2015-06-06 12:43:58 -05:00
else:
self.database[race] = [time.time(), [(name, timestamp)]]
2015-06-06 12:59:31 -05:00
self.saveDatabase()
2015-06-06 12:43:58 -05:00
@magicWord(category=CATEGORY_PROGRAMMER, types=[str, int, int, str, int])
def leaderboard(command, raceId=0, type=0, name='', time=0):
command = command.lower()
race = '%s, %s' % (raceId, type)
if command == 'clear':
simbase.air.leaderboardMgr.clearRace(race)
return 'Cleared race %s!' % race
elif command == 'submit':
simbase.air.leaderboardMgr.submitRace(raceId, name, time)
return 'Submitted race %s for %s with %s seconds!' % (raceId, name, time)
elif command == 'refresh':
messenger.send('goofyLeaderboardChange')
return 'Refreshed leaderboards!'
elif command == 'change':
messenger.send('goofyLeaderboardDisplay', [raceId])
return 'Made all leaderboards show %s!' % raceId
else:
2015-07-05 18:59:25 -05:00
return 'Unknown command! Commands:\n- clear\n- submit\n- refresh\n- change'