Poodletooth-iLand/toontown/ai/NewsManagerAI.py

110 lines
4.1 KiB
Python
Raw Normal View History

2015-03-03 22:10:12 +00:00
from direct.distributed.DistributedObjectAI import DistributedObjectAI
2015-06-25 18:20:15 +00:00
from direct.distributed.ClockDelta import globalClockDelta
2015-06-22 07:23:46 +00:00
from direct.task import Task
2015-06-25 18:20:15 +00:00
from toontown.effects.DistributedFireworkShowAI import DistributedFireworkShowAI
from toontown.effects import FireworkShows
2015-06-22 07:23:46 +00:00
from toontown.toonbase import ToontownGlobals
2015-06-25 18:20:15 +00:00
from toontown.parties import PartyGlobals
2015-06-22 07:23:46 +00:00
import HolidayGlobals
2015-06-25 18:20:15 +00:00
import datetime, random
2015-03-03 22:10:12 +00:00
class NewsManagerAI(DistributedObjectAI):
2015-06-22 07:23:46 +00:00
def __init__(self, air):
DistributedObjectAI.__init__(self, air)
self.activeHolidays = []
2015-06-25 18:20:15 +00:00
self.fireworkTask = None
2015-06-22 07:23:46 +00:00
2015-03-03 22:10:12 +00:00
def announceGenerate(self):
DistributedObjectAI.announceGenerate(self)
2015-06-22 07:23:46 +00:00
self.__checkHolidays()
self.checkTask = taskMgr.doMethodLater(15, self.__checkHolidays, 'holidayCheckTask')
2015-03-03 22:10:12 +00:00
self.accept('avatarEntered', self.__handleAvatarEntered)
2015-06-22 07:23:46 +00:00
def delete(self):
DistributedObjectAI.delete(self)
taskMgr.remove(self.checkTask)
2015-06-25 18:20:15 +00:00
self.deleteFireworkTask()
2015-06-25 18:20:15 +00:00
def deleteFireworkTask(self):
if self.fireworkTask:
taskMgr.remove(self.fireworkTask)
self.fireworkTask = None
2015-06-22 07:23:46 +00:00
def __handleAvatarEntered(self, av):
2015-06-30 18:50:15 +00:00
avId = av.getDoId()
if self.air.suitInvasionManager.getInvading():
self.air.suitInvasionManager.notifyInvasionBulletin(avId)
self.sendUpdateToAvatarId(avId, 'setActiveHolidays', [self.activeHolidays])
2015-06-25 18:20:15 +00:00
def getActiveHolidays(self):
return self.activeHolidays
2015-06-25 18:20:15 +00:00
2015-06-22 07:23:46 +00:00
def __checkHolidays(self, task=None):
2015-06-25 18:20:15 +00:00
date = datetime.datetime.utcnow().replace(tzinfo=HolidayGlobals.TIME_ZONE)
2015-06-22 07:23:46 +00:00
for id in HolidayGlobals.Holidays:
holiday = HolidayGlobals.Holidays[id]
running = self.isHolidayRunning(id)
2015-06-25 18:20:15 +00:00
if self.isHolidayInRange(holiday, date):
2015-06-22 07:23:46 +00:00
if not running:
self.startHoliday(id)
elif running:
self.endHoliday(id)
return Task.again
2015-06-25 18:20:15 +00:00
def isHolidayInRange(self, holiday, date):
if 'weekDay' in holiday:
return holiday['weekDay'] == date.weekday()
else:
return HolidayGlobals.getStartDate(holiday) <= date <= HolidayGlobals.getEndDate(holiday)
2015-06-22 07:23:46 +00:00
def isHolidayRunning(self, id):
return id in self.activeHolidays
def startHoliday(self, id):
if id in self.activeHolidays or id not in HolidayGlobals.Holidays:
return
self.activeHolidays.append(id)
self.startSpecialHoliday(id)
self.sendUpdate('startHoliday', [id])
def endHoliday(self, id):
if id not in self.activeHolidays or id not in HolidayGlobals.Holidays:
return
self.activeHolidays.remove(id)
self.endSpecialHoliday(id)
self.sendUpdate('endHoliday', [id])
def startSpecialHoliday(self, id):
if id == ToontownGlobals.FISH_BINGO or id == ToontownGlobals.SILLY_SATURDAY:
messenger.send('checkBingoState')
2015-06-25 18:20:15 +00:00
elif id in [ToontownGlobals.SUMMER_FIREWORKS, ToontownGlobals.NEW_YEAR_FIREWORKS]:
if not self.fireworkTask:
self.fireworkTask = taskMgr.doMethodLater(3600, self.startFireworks, 'newsFireworkTask', extraArgs=[id, Task.again])
taskMgr.doMethodLater(10, self.startFireworks, 'newsFireworkTask-initial', extraArgs=[id, Task.done])
2015-06-22 07:23:46 +00:00
def endSpecialHoliday(self, id):
if id == ToontownGlobals.FISH_BINGO or id == ToontownGlobals.SILLY_SATURDAY:
2015-06-25 18:20:15 +00:00
messenger.send('checkBingoState')
elif id in [ToontownGlobals.SUMMER_FIREWORKS, ToontownGlobals.NEW_YEAR_FIREWORKS]:
self.deleteFireworkTask()
def startFireworks(self, type, again, task=None):
maxShow = len(FireworkShows.shows.get(type, [])) - 1
for hood in self.air.hoods:
if hood.zoneId == ToontownGlobals.GolfZone:
continue
fireworkShow = DistributedFireworkShowAI(self.air)
fireworkShow.generateWithRequired(hood.zoneId)
fireworkShow.b_startShow(type, random.randint(0, maxShow), globalClockDelta.getRealNetworkTime())
return again