From 4cda455edc2ee9c8c9af62319809d9aa9ce34267 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 6 Jun 2015 20:43:58 +0300 Subject: [PATCH] DANIEL: Leaderboards are done 8======D --- dependencies/astron/dclass/stride.dc | 2 +- toontown/ai/ToontownAIRepository.py | 4 + toontown/hood/GSHoodAI.py | 3 + toontown/racing/DistributedLeaderBoard.py | 8 +- toontown/racing/DistributedLeaderBoardAI.py | 88 +++++++++++++++------ toontown/racing/DistributedRaceAI.py | 1 + toontown/racing/LeaderboardMgrAI.py | 61 ++++++++++++++ toontown/racing/RaceGlobals.py | 5 ++ toontown/toonbase/TTLocalizerEnglish.py | 1 + 9 files changed, 142 insertions(+), 31 deletions(-) create mode 100644 toontown/racing/LeaderboardMgrAI.py diff --git a/dependencies/astron/dclass/stride.dc b/dependencies/astron/dclass/stride.dc index 9319e513..0e85b84c 100644 --- a/dependencies/astron/dclass/stride.dc +++ b/dependencies/astron/dclass/stride.dc @@ -2625,7 +2625,7 @@ dclass DistributedViewingBlock : DistributedStartingBlock { struct LeaderboardResult { string name; - uint32 time; + uint16 time; }; dclass DistributedLeaderBoard : DistributedObject { diff --git a/toontown/ai/ToontownAIRepository.py b/toontown/ai/ToontownAIRepository.py index 7996862e..e7564e5e 100755 --- a/toontown/ai/ToontownAIRepository.py +++ b/toontown/ai/ToontownAIRepository.py @@ -44,6 +44,7 @@ from toontown.hood import OZHoodAI from toontown.hood import SellbotHQAI from toontown.hood import TTHoodAI from toontown.hood import ZoneUtil +from toontown.racing.LeaderboardMgrAI import LeaderboardMgrAI from toontown.pets.PetManagerAI import PetManagerAI from toontown.safezone.SafeZoneManagerAI import SafeZoneManagerAI from toontown.suit.SuitInvasionManagerAI import SuitInvasionManagerAI @@ -81,6 +82,7 @@ class ToontownAIRepository(ToontownInternalRepository): self.wantFishing = self.config.GetBool('want-fishing', True) self.wantHousing = self.config.GetBool('want-housing', True) self.wantPets = self.config.GetBool('want-pets', True) + self.wantKarts = self.config.GetBool('want-karts', True) self.wantParties = self.config.GetBool('want-parties', True) self.wantEmblems = self.config.GetBool('want-emblems', True) self.wantCogbuildings = self.config.GetBool('want-cogbuildings', True) @@ -126,6 +128,8 @@ class ToontownAIRepository(ToontownInternalRepository): self.buildingQueryMgr = DistributedBuildingQueryMgrAI(self) self.buildingQueryMgr.generateWithRequired(2) self.groupManager.generateWithRequired(2) + if self.wantKarts: + self.leaderboardMgr = LeaderboardMgrAI(self) if self.wantFishing: self.fishManager = FishManagerAI(self) if self.wantHousing: diff --git a/toontown/hood/GSHoodAI.py b/toontown/hood/GSHoodAI.py index 672c75fd..91ffd211 100755 --- a/toontown/hood/GSHoodAI.py +++ b/toontown/hood/GSHoodAI.py @@ -122,6 +122,9 @@ class GSHoodAI(HoodAI.HoodAI): viewPad.addStartingBlock(viewingBlock) def findLeaderBoards(self, dnaGroup, zoneId): + if not self.air.wantKarts: + return + leaderBoards = [] if isinstance(dnaGroup, DNAGroup) and ('leader_board' in dnaGroup.getName()): diff --git a/toontown/racing/DistributedLeaderBoard.py b/toontown/racing/DistributedLeaderBoard.py index a03483af..3eead365 100755 --- a/toontown/racing/DistributedLeaderBoard.py +++ b/toontown/racing/DistributedLeaderBoard.py @@ -84,12 +84,8 @@ class DistributedLeaderBoard(DistributedObject.DistributedObject): self.nameTextNodes[i].setText('-') self.timeTextNodes[i].setText('-') else: - name = pLeaderList[i][1] - time = pLeaderList[i][0] - secs, hundredths = divmod(time, 1) - min, sec = divmod(secs, 60) - self.nameTextNodes[i].setText(name[:22]) - self.timeTextNodes[i].setText('%02d:%02d:%02d' % (min, sec, hundredths * 100)) + self.nameTextNodes[i].setText(pLeaderList[i][0][:22]) + self.timeTextNodes[i].setText(TTLocalizer.convertSecondsToDate(pLeaderList[i][1])) def buildTitleRow(self): row = hidden.attachNewNode('TitleRow') diff --git a/toontown/racing/DistributedLeaderBoardAI.py b/toontown/racing/DistributedLeaderBoardAI.py index 669a825b..973e4730 100755 --- a/toontown/racing/DistributedLeaderBoardAI.py +++ b/toontown/racing/DistributedLeaderBoardAI.py @@ -1,7 +1,8 @@ from direct.directnotify import DirectNotifyGlobal from direct.task import Task from direct.distributed.DistributedObjectAI import DistributedObjectAI -import random +from toontown.racing import RaceGlobals +import random, time class DistributedLeaderBoardAI(DistributedObjectAI): notify = DirectNotifyGlobal.directNotify.newCategory("DistributedLeaderBoardAI") @@ -9,39 +10,78 @@ class DistributedLeaderBoardAI(DistributedObjectAI): def __init__(self, air, displays): DistributedObjectAI.__init__(self, air) self.displays = displays - self.display = (0, 0, []) + self.display = [0, 0, []] + self.currentId = 0 self.posHpr = (0, 0, 0, 0, 0, 0) - + def generateWithRequired(self, zoneId): DistributedObjectAI.generateWithRequired(self, zoneId) - self.setup() - + self.accept('goofyLeaderboardChange', self.__setDisplay) + self.accept('goofyLeaderboardDisplay', self.__setDisplayRace) + self.__updateDisplay() + self.switchTask = taskMgr.doMethodLater(15, self.nextDisplay, 'leaderboardSwitchTask-%s' % random.random()) + def delete(self): DistributedObjectAI.delete(self) + self.ignoreAll() taskMgr.remove(self.switchTask) - - def setup(self): - self.currentId = 0 - self.switchDisplay() - self.switchTask = taskMgr.doMethodLater(15, self.switchDisplay, 'leaderboardSwitchTask-%s' % random.random()) - - def switchDisplay(self, task=None): - race = self.displays[self.currentId] - - self.display = (race[0], race[1], []) - self.currentId += 1 - - if self.currentId >= len(self.displays): - self.currentId = 0 - self.sendUpdate('setDisplay', [self.display[0], self.display[1], self.display[2]]) - return Task.again - def setPosHpr(self, x, y, z, h, p, r): self.posHpr = (x, y, z, h, p, r) def getPosHpr(self): return self.posHpr - + def getDisplay(self): - return self.display \ No newline at end of file + return self.display + + def getDisplayName(self): + return '%s, %s' % (self.display[0], self.display[1]) + + def getMaxTimeDifference(self): + return RaceGlobals.MaxTimeDifference[self.display[1]] + + def hasMaxTimeDifference(self): + return self.display[1] in RaceGlobals.MaxTimeDifference + + def nextDisplay(self, task=None): + self.__updateDisplay() + self.currentId += 1 + + if self.currentId >= len(self.displays): + self.currentId = 0 + + return Task.again + + def __setDisplayRace(self, race): + self.currentId = race + self.__updateDisplay() + + def __updateDisplay(self): + race = self.displays[self.currentId] + + self.display = [race[0], race[1], []] + self.__setDisplay() + + def __setDisplay(self): + database = self.air.leaderboardMgr.getDatabase() + displayName = self.getDisplayName() + + if not displayName in database: + self.sendDisplayUpdate([]) + return + + displayEntry = database[displayName] + + if self.hasMaxTimeDifference(): + difference = time.time() - displayEntry[0] + + if difference >= self.getMaxTimeDifference(): + self.air.leaderboardMgr.clearRace(displayName) + return + + self.sendDisplayUpdate(self.air.leaderboardMgr.trimList(displayEntry[1])) + + def sendDisplayUpdate(self, players): + self.display[2] = players + self.sendUpdate('setDisplay', self.display) \ No newline at end of file diff --git a/toontown/racing/DistributedRaceAI.py b/toontown/racing/DistributedRaceAI.py index 4b7da873..5a9f942f 100755 --- a/toontown/racing/DistributedRaceAI.py +++ b/toontown/racing/DistributedRaceAI.py @@ -303,6 +303,7 @@ class DistributedRaceAI(DistributedObjectAI, FSM): qualify = False if totalTime < RaceGlobals.getQualifyingTime(self.trackId): qualify = True + self.air.leaderboardMgr.submitRace(self.trackId, av.getName(), totalTime) if self.raceType == RaceGlobals.Practice: winnings = RaceGlobals.PracticeWinnings trophies = [] diff --git a/toontown/racing/LeaderboardMgrAI.py b/toontown/racing/LeaderboardMgrAI.py new file mode 100644 index 00000000..0b11217c --- /dev/null +++ b/toontown/racing/LeaderboardMgrAI.py @@ -0,0 +1,61 @@ +from otp.ai.MagicWordGlobal import * +import RaceGlobals, operator, time + +class LeaderboardMgrAI: + + def __init__(self, air): + self.air = air + self.database = simbase.backups.load('leaderboard', (self.air.districtId,), default=({})) + + 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): + for i in xrange(3): + race = '%s, %s' % (raceId, i) + + if race in self.database: + originalRace = self.database[race][1] + newRace = list(originalRace) + + newRace.append((name, timestamp)) + sortedRace = self.trimList(sorted(newRace, key=operator.itemgetter(1))) + + if originalRace != sortedRace: + self.database[race][1] = sortedRace + else: + self.database[race] = (time.time(), [(name, timestamp)]) + + self.saveDatabase() + +@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: + return 'Unknown command! Commands:\n- clear\n- submit\n- refresh\n- change' \ No newline at end of file diff --git a/toontown/racing/RaceGlobals.py b/toontown/racing/RaceGlobals.py index a0a5d184..d2ecea67 100755 --- a/toontown/racing/RaceGlobals.py +++ b/toontown/racing/RaceGlobals.py @@ -553,3 +553,8 @@ Exit_UserReq = 0 Exit_Barrier = 1 Exit_Slow = 2 Exit_BarrierNoRefund = 3 + +MaxTimeDifference = { + 0: 86400, + 1: 604800 +} \ No newline at end of file diff --git a/toontown/toonbase/TTLocalizerEnglish.py b/toontown/toonbase/TTLocalizerEnglish.py index db3c6aea..9a11c5e9 100755 --- a/toontown/toonbase/TTLocalizerEnglish.py +++ b/toontown/toonbase/TTLocalizerEnglish.py @@ -8473,3 +8473,4 @@ def convertSecondsToDate(seconds): h, m = divmod(m, 60) print '%d:%02d:%02d' % (h, m, s) + return '%d:%02d:%02d' % (h, m, s)