oldschool-toontown/toontown/racing/DistributedLeaderBoardAI.py

53 lines
2.1 KiB
Python
Raw Normal View History

import pickle
2019-12-05 21:17:10 -06:00
2019-11-08 22:55:55 -06:00
from direct.directnotify import DirectNotifyGlobal
from direct.distributed.DistributedObjectAI import DistributedObjectAI
2019-12-05 21:17:10 -06:00
from toontown.toonbase import TTLocalizer
2019-12-05 21:03:12 -06:00
2019-11-08 22:55:55 -06:00
class DistributedLeaderBoardAI(DistributedObjectAI):
notify = DirectNotifyGlobal.directNotify.newCategory('DistributedLeaderBoardAI')
2019-12-05 21:03:12 -06:00
def __init__(self, air, name, x, y, z, h, p, r):
DistributedObjectAI.__init__(self, air)
self.name = name
self.posHpr = (x, y, z, h, p, r)
2019-12-05 21:17:10 -06:00
self.records = {}
self.subscriptions = []
self.currentIndex = -1
def announceGenerate(self):
DistributedObjectAI.announceGenerate(self)
self.accept('UpdateRaceRecord', self.handleUpdateRaceRecord)
self.accept('GS_LeaderBoardSwap' + str(self.zoneId), self.updateDisplay)
2019-12-05 21:03:12 -06:00
def getName(self):
return self.name
def getPosHpr(self):
return self.posHpr
def subscribeTo(self, subscription):
2019-12-05 21:17:10 -06:00
self.records.setdefault(subscription[0], {})[subscription[1]] = [(x[0], x[3]) for x in
self.air.raceMgr.getRecords(subscription[0],
subscription[1])]
self.subscriptions.append(subscription)
def handleUpdateRaceRecord(self, record):
2020-01-05 21:09:53 -06:00
trackId, period = record
if trackId not in self.records:
return
self.records[trackId][period] = [(x[0], x[3]) for x in self.air.raceMgr.getRecords(trackId, period)]
2019-12-05 21:17:10 -06:00
def updateDisplay(self):
self.currentIndex += 1
if self.currentIndex >= len(self.subscriptions):
self.currentIndex = 0
trackName = TTLocalizer.KartRace_TrackNames[self.subscriptions[self.currentIndex][0]]
periodName = TTLocalizer.RecordPeriodStrings[self.subscriptions[self.currentIndex][1]]
leaderList = self.records[self.subscriptions[self.currentIndex][0]][self.subscriptions[self.currentIndex][1]]
self.sendUpdate('setDisplay', [pickle.dumps((trackName, periodName, leaderList))])