Poodletooth-iLand/toontown/racing/DistributedLeaderBoardAI.py

88 lines
2.7 KiB
Python
Raw Normal View History

2015-03-03 16:10:12 -06:00
from direct.directnotify import DirectNotifyGlobal
2015-06-06 09:12:47 -05:00
from direct.task import Task
2015-03-03 16:10:12 -06:00
from direct.distributed.DistributedObjectAI import DistributedObjectAI
2015-06-06 12:43:58 -05:00
from toontown.racing import RaceGlobals
import random, time
2015-03-03 16:10:12 -06:00
class DistributedLeaderBoardAI(DistributedObjectAI):
notify = DirectNotifyGlobal.directNotify.newCategory("DistributedLeaderBoardAI")
2015-06-06 09:12:47 -05:00
def __init__(self, air, displays):
2015-06-06 07:57:29 -05:00
DistributedObjectAI.__init__(self, air)
2015-06-06 09:12:47 -05:00
self.displays = displays
2015-06-06 12:43:58 -05:00
self.display = [0, 0, []]
self.currentId = 0
2015-06-06 07:57:29 -05:00
self.posHpr = (0, 0, 0, 0, 0, 0)
2015-06-06 12:43:58 -05:00
2015-06-06 09:12:47 -05:00
def generateWithRequired(self, zoneId):
DistributedObjectAI.generateWithRequired(self, zoneId)
2015-06-06 12:43:58 -05:00
self.accept('goofyLeaderboardChange', self.__setDisplay)
self.accept('goofyLeaderboardDisplay', self.__setDisplayRace)
self.__updateDisplay()
self.switchTask = taskMgr.doMethodLater(15, self.nextDisplay, 'leaderboardSwitchTask-%s' % random.random())
2015-06-06 09:12:47 -05:00
def delete(self):
DistributedObjectAI.delete(self)
2015-06-06 12:43:58 -05:00
self.ignoreAll()
2015-06-06 09:12:47 -05:00
taskMgr.remove(self.switchTask)
2015-06-06 07:57:29 -05:00
def setPosHpr(self, x, y, z, h, p, r):
self.posHpr = (x, y, z, h, p, r)
def getPosHpr(self):
return self.posHpr
2015-06-06 12:43:58 -05:00
2015-06-06 09:12:47 -05:00
def getDisplay(self):
2015-06-06 12:43:58 -05:00
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
2015-07-05 18:59:25 -05:00
self.sendUpdate('setDisplay', self.display)