Poodletooth-iLand/toontown/friends/TTSFriendsManagerUD.py

359 lines
15 KiB
Python
Raw Normal View History

from direct.directnotify.DirectNotifyGlobal import directNotify
2015-03-03 16:10:12 -06:00
from direct.distributed.DistributedObjectGlobalUD import DistributedObjectGlobalUD
from direct.distributed.PyDatagram import *
from direct.fsm.FSM import FSM
import time
2015-03-03 16:10:12 -06:00
# -- FSMS --
class OperationFSM(FSM):
def __init__(self, mgr, air, senderAvId, targetAvId=None, callback=None):
FSM.__init__(self, 'OperationFSM-%s' % senderAvId)
self.mgr = mgr
self.air = air
self.sender = senderAvId
self.result = None
self.target = targetAvId
self.callback = callback
def enterOff(self):
if self.callback:
if self.result is not None:
self.callback(self.sender, self.result)
else:
self.callback()
if self in self.mgr.operations:
self.mgr.operations.remove(self)
def enterError(self, message=None):
self.mgr.notify.warning("An error has occurred in a '%s'. Message: %s" %
(type(self).__name__, message) )
if self.sender in self.mgr.operations:
del self.mgr.operations[self.sender]
# -- Friends list --
class FriendsListOperation(OperationFSM):
def enterStart(self):
self.air.dbInterface.queryObject(self.air.dbId, self.sender,
self.handleRetrieveSender)
def handleRetrieveSender(self, dclass, fields):
if dclass != self.air.dclassesByName['DistributedToonUD']:
self.demand('Error', 'Distributed Class was not a Toon.')
return
self.demand('Retrieved', fields['setFriendsList'][0])
def enterRetrieved(self, friendsList):
self.friendsList = friendsList
if len(self.friendsList) <= 0:
self.result = []
self.demand('Off')
return
self.friendIndex = 0
self.realFriendsList = []
2015-05-31 06:12:48 -05:00
self.air.dbInterface.queryObject(self.air.dbId, self.friendsList[0],
2015-03-03 16:10:12 -06:00
self.addFriend)
def addFriend(self, dclass, fields):
if dclass != self.air.dclassesByName['DistributedToonUD']:
self.demand('Error', 'Friend was not a Toon')
return
2015-05-31 06:12:48 -05:00
friendId = self.friendsList[self.friendIndex]
2015-03-03 16:10:12 -06:00
self.realFriendsList.append([friendId, fields['setName'][0],
2015-06-02 12:07:50 -05:00
fields['setDNAString'][0], fields['setAdminAccess'][0], fields['setPetId'][0]])
2015-03-03 16:10:12 -06:00
if len(self.realFriendsList) >= len(self.friendsList):
self.result = self.realFriendsList
self.demand('Off')
return
self.friendIndex += 1
self.air.dbInterface.queryObject(self.air.dbId,
2015-05-31 06:12:48 -05:00
self.friendsList[self.friendIndex], self.addFriend)
2015-03-03 16:10:12 -06:00
# -- Remove Friends --
class RemoveFriendOperation(OperationFSM):
def __init__(self, mgr, air, senderAvId, targetAvId=None, callback=None, alert=False):
OperationFSM.__init__(self, mgr, air, senderAvId, targetAvId, callback)
self.alert = alert
def enterStart(self):
self.air.dbInterface.queryObject(self.air.dbId, self.sender,
self.handleRetrieve)
def handleRetrieve(self, dclass, fields):
if dclass != self.air.dclassesByName['DistributedToonUD']:
self.demand('Error', 'Distributed Class was not a Toon.')
return
self.demand('Retrieved', fields['setFriendsList'][0])
def enterRetrieved(self, friendsList):
2015-05-31 06:12:48 -05:00
friendsList.remove(self.target)
2015-03-03 16:10:12 -06:00
if self.sender in self.mgr.onlineToons:
dg = self.air.dclassesByName['DistributedToonUD'].aiFormatUpdate(
'setFriendsList', self.sender, self.sender,
2015-05-31 06:12:48 -05:00
self.air.ourChannel, [friendsList])
2015-03-03 16:10:12 -06:00
self.air.send(dg)
if self.alert:
dg = self.air.dclassesByName['DistributedToonUD'].aiFormatUpdate(
'friendsNotify', self.sender, self.sender,
self.air.ourChannel, [self.target, 1])
self.air.send(dg)
self.demand('Off')
return
self.air.dbInterface.updateObject(self.air.dbId, self.sender,
self.air.dclassesByName['DistributedToonUD'],
2015-05-31 06:12:48 -05:00
{'setFriendsList' : [friendsList]})
2015-03-03 16:10:12 -06:00
self.demand('Off')
# -- Clear List --
class ClearListOperation(OperationFSM):
def enterStart(self):
self.air.dbInterface.queryObject(self.air.dbId, self.sender,
self.handleRetrieved)
def handleRetrieved(self, dclass, fields):
if dclass != self.air.dclassesByName['DistributedToonUD']:
self.demand('Error', 'Distributed Class was not a Toon.')
return
self.demand('Retrieved', fields['setFriendsList'][0])
def enterRetrieved(self, friendsList):
for friend in friendsList:
2015-05-31 06:12:48 -05:00
newOperation = RemoveFriendOperation(self.mgr, self.air, friend,
2015-03-03 16:10:12 -06:00
targetAvId=self.sender, alert=True)
self.mgr.operations.append(newOperation)
newOperation.demand('Start')
self.demand('Off')
# -- FriendsManager --
2015-05-16 20:04:41 -05:00
class TTSFriendsManagerUD(DistributedObjectGlobalUD):
notify = directNotify.newCategory('TTSFriendsManagerUD')
2015-03-03 16:10:12 -06:00
def announceGenerate(self):
DistributedObjectGlobalUD.announceGenerate(self)
self.onlineToons = []
self.tpRequests = {}
self.whisperRequests = {}
self.operations = []
self.delayTime = 1.0
# -- Friends list --
def requestFriendsList(self):
avId = self.air.getAvatarIdFromSender()
newOperation = FriendsListOperation(self, self.air, avId,
callback = self.sendFriendsList)
self.operations.append(newOperation)
newOperation.demand('Start')
def sendFriendsList(self, sender, friendsList):
self.sendUpdateToAvatarId(sender, 'friendList', [friendsList])
if sender not in self.onlineToons:
self.toonOnline(sender, friendsList)
# -- Remove Friend --
def removeFriend(self, friendId):
avId = self.air.getAvatarIdFromSender()
# Sender remove Friend
newOperation = RemoveFriendOperation(self, self.air, avId, friendId)
self.operations.append(newOperation)
newOperation.demand('Start')
# Friend remove Sender
newOperation = RemoveFriendOperation(self, self.air, friendId, avId,
alert=True)
self.operations.append(newOperation)
newOperation.demand('Start')
# -- Avatar Info --
def getAvatarDetails(self, avId):
senderId = self.air.getAvatarIdFromSender()
def handleToon(dclass, fields):
if dclass != self.air.dclassesByName['DistributedToonUD']:
return
inventory = fields['setInventory'][0]
trackAccess = fields['setTrackAccess'][0]
trophies = 0 # fields['setTrophyScore'][0] is not db
hp = fields['setHp'][0]
maxHp = fields['setMaxHp'][0]
defaultShard = fields['setDefaultShard'][0]
lastHood = fields['setLastHood'][0]
dnaString = fields['setDNAString'][0]
experience = fields['setExperience'][0]
trackBonusLevel = fields['setTrackBonusLevel'][0]
2015-05-30 11:09:41 -05:00
2015-03-03 16:10:12 -06:00
self.sendUpdateToAvatarId(senderId, 'friendDetails', [avId, inventory, trackAccess, trophies, hp, maxHp, defaultShard, lastHood, dnaString, experience, trackBonusLevel])
self.air.dbInterface.queryObject(self.air.dbId, avId, handleToon)
2015-07-05 18:59:25 -05:00
2015-03-29 10:41:02 -05:00
def getPetDetails(self, avId):
senderId = self.air.getAvatarIdFromSender()
def handlePet(dclass, fields):
2015-03-29 17:16:26 -05:00
if dclass != self.air.dclassesByName['DistributedPetAI']:
2015-03-29 10:41:02 -05:00
return
dna = [fields.get(x, [0])[0] for x in ("setHead", "setEars", "setNose", "setTail", "setBodyTexture", "setColor",
"setColorScale", "setEyeColor", "setGender")]
moods = [fields.get(x, [0])[0] for x in ("setBoredom", "setRestlessness", "setPlayfulness", "setLoneliness",
"setSadness", "setAffection", "setHunger", "setConfusion", "setExcitement",
"setFatigue", "setAnger", "setSurprise")]
traits = [fields.get(x, [0])[0] for x in ("setForgetfulness", "setBoredomThreshold", "setRestlessnessThreshold",
"setPlayfulnessThreshold", "setLonelinessThreshold", "setSadnessThreshold",
"setFatigueThreshold", "setHungerThreshold", "setConfusionThreshold",
"setExcitementThreshold", "setAngerThreshold", "setSurpriseThreshold",
"setAffectionThreshold")]
self.sendUpdateToAvatarId(senderId, 'petDetails', [avId, fields.get("setOwnerId", [0])[0], fields.get("setPetName", ["???"])[0],
fields.get("setTraitSeed", [0])[0], fields.get("setSafeZone", [0])[0],
traits, moods, dna, fields.get("setLastSeenTimestamp", [0])[0]])
self.air.dbInterface.queryObject(self.air.dbId, avId, handlePet)
2015-03-03 16:10:12 -06:00
# -- Toon Online/Offline --
def toonOnline(self, doId, friendsList):
2015-06-06 14:38:52 -05:00
if doId not in self.onlineToons:
2015-05-31 06:12:48 -05:00
self.onlineToons.append(doId)
2015-03-03 16:10:12 -06:00
channel = self.GetPuppetConnectionChannel(doId)
dgcleanup = self.dclass.aiFormatUpdate('goingOffline', self.doId, self.doId, self.air.ourChannel, [doId])
dg = PyDatagram()
dg.addServerHeader(channel, self.air.ourChannel, CLIENTAGENT_ADD_POST_REMOVE)
dg.addString(dgcleanup.getMessage())
self.air.send(dg)
for friend in friendsList:
if friend[0] in self.onlineToons:
2015-05-31 06:12:48 -05:00
self.sendUpdateToAvatarId(doId, 'friendOnline', [friend[0]])
self.sendUpdateToAvatarId(friend[0], 'friendOnline', [doId])
2015-03-03 16:10:12 -06:00
def goingOffline(self, avId):
self.toonOffline(avId)
def toonOffline(self, doId):
if doId not in self.onlineToons:
return
def handleToon(dclass, fields):
if dclass != self.air.dclassesByName['DistributedToonUD']:
return
friendsList = fields['setFriendsList'][0]
2015-05-31 06:12:48 -05:00
for friendId in friendsList:
2015-03-03 16:10:12 -06:00
if friendId in self.onlineToons:
self.sendUpdateToAvatarId(friendId, 'friendOffline', [doId])
if doId in self.onlineToons:
self.onlineToons.remove(doId)
self.air.dbInterface.queryObject(self.air.dbId, doId, handleToon)
# -- Clear List --
def clearList(self, doId):
newOperation = ClearListOperation(self, self.air, doId)
self.operations.append(newOperation)
newOperation.demand('Start')
# -- Teleport and Whispers --
def routeTeleportQuery(self, toId):
fromId = self.air.getAvatarIdFromSender()
if fromId in self.tpRequests.values():
return
self.tpRequests[fromId] = toId
self.sendUpdateToAvatarId(toId, 'teleportQuery', [fromId])
taskMgr.doMethodLater(5, self.giveUpTeleportQuery, 'tp-query-timeout-%d' % fromId, extraArgs=[fromId, toId])
def giveUpTeleportQuery(self, fromId, toId):
# The client didn't respond to the query within the set time,
# So we will tell the query sender that the toon is unavailable.
if fromId in self.tpRequests:
del self.tpRequests[fromId]
self.sendUpdateToAvatarId(fromId, 'setTeleportResponse', [toId, 0, 0, 0, 0])
self.notify.warning('Teleport request that was sent by %d to %d timed out.' % (fromId, toId))
def teleportResponse(self, toId, available, shardId, hoodId, zoneId):
# Here is where the toId and fromId swap (because we are now sending it back)
fromId = self.air.getAvatarIdFromSender()
# We got the query response, so no need to give up!
if taskMgr.hasTaskNamed('tp-query-timeout-%d' % toId):
taskMgr.remove('tp-query-timeout-%d' % toId)
if toId not in self.tpRequests:
return
if self.tpRequests.get(toId) != fromId:
self.air.writeServerEvent('suspicious', fromId, 'toon tried to send teleportResponse for a query that isn\'t theirs!')
return
self.sendUpdateToAvatarId(toId, 'setTeleportResponse', [fromId, available, shardId, hoodId, zoneId])
del self.tpRequests[toId]
def whisperSCTo(self, toId, msgIndex):
fromId = self.air.getAvatarIdFromSender()
currStamp = time.time()
if fromId in self.whisperRequests:
elapsed = currStamp - self.whisperRequests[fromId]
if elapsed < self.delayTime:
self.whisperRequests[fromId] = currStamp
return
self.whisperRequests[fromId] = currStamp
self.sendUpdateToAvatarId(toId, 'setWhisperSCFrom', [fromId, msgIndex])
def whisperSCCustomTo(self, toId, msgIndex):
fromId = self.air.getAvatarIdFromSender()
currStamp = time.time()
if fromId in self.whisperRequests:
elapsed = currStamp - self.whisperRequests[fromId]
if elapsed < self.delayTime:
self.whisperRequests[fromId] = currStamp
return
self.whisperRequests[fromId] = currStamp
self.sendUpdateToAvatarId(toId, 'setWhisperSCCustomFrom', [fromId, msgIndex])
def whisperSCEmoteTo(self, toId, msgIndex):
fromId = self.air.getAvatarIdFromSender()
currStamp = time.time()
if fromId in self.whisperRequests:
elapsed = currStamp - self.whisperRequests[fromId]
if elapsed < self.delayTime:
self.whisperRequests[fromId] = currStamp
return
self.whisperRequests[fromId] = currStamp
self.sendUpdateToAvatarId(toId, 'setWhisperSCEmoteFrom', [fromId, msgIndex])
def sendTalkWhisper(self, toId, message):
fromId = self.air.getAvatarIdFromSender()
currStamp = time.time()
if fromId in self.whisperRequests:
elapsed = currStamp - self.whisperRequests[fromId]
if elapsed < self.delayTime:
self.whisperRequests[fromId] = currStamp
return
self.whisperRequests[fromId] = currStamp
self.sendUpdateToAvatarId(toId, 'receiveTalkWhisper', [fromId, message])
self.air.writeServerEvent('whisper-said', fromId, toId, message)
# -- Routes --
def battleSOS(self, toId):
requester = self.air.getAvatarIdFromSender()
self.sendUpdateToAvatarId(toId, 'setBattleSOS', [requester])
def teleportGiveup(self, toId):
requester = self.air.getAvatarIdFromSender()
self.sendUpdateToAvatarId(toId, 'setTeleportGiveup', [requester])
def whisperSCToontaskTo(self, toId, taskId, toNpcId, toonProgress, msgIndex):
requester = self.air.getAvatarIdFromSender()
self.sendUpdateToAvatarId(toId, 'setWhisperSCToontaskFrom', [requester,
taskId, toNpcId, toonProgress, msgIndex]
)
def sleepAutoReply(self, toId):
requester = self.air.getAvatarIdFromSender()
self.sendUpdateToAvatarId(toId, 'setSleepAutoReply', [requester])