pep.py/events/startSpectatingEvent.py

59 lines
2.0 KiB
Python
Raw Normal View History

2016-10-02 20:48:14 +00:00
from common.log import logUtils as log
from common.ripple import userUtils
2016-05-18 17:12:46 +00:00
from constants import clientPackets
from constants import exceptions
2016-10-02 20:48:14 +00:00
from constants import serverPackets
from helpers import chatHelper as chat
2016-10-02 20:48:14 +00:00
from objects import glob
2016-04-19 17:40:59 +00:00
def handle(userToken, packetData):
try:
# Get usertoken data
userID = userToken.userID
username = userToken.username
# Start spectating packet
packetData = clientPackets.startSpectating(packetData)
# Stop spectating old user if needed
if userToken.spectating != 0:
oldTargetToken = glob.tokens.getTokenFromUserID(userToken.spectating)
oldTargetToken.enqueue(serverPackets.removeSpectator(userID))
userToken.stopSpectating()
# Start spectating new user
userToken.startSpectating(packetData["userID"])
# Get host token
targetToken = glob.tokens.getTokenFromUserID(packetData["userID"])
2016-09-02 15:45:10 +00:00
if targetToken is None:
2016-04-19 17:40:59 +00:00
raise exceptions.tokenNotFoundException
# Add us to host's spectators
targetToken.addSpectator(userID)
# Send spectator join packet to host
targetToken.enqueue(serverPackets.addSpectator(userID))
# Create and join #spectator (#spect_userid) channel
glob.channels.addTempChannel("#spect_{}".format(targetToken.userID))
chat.joinChannel(token=userToken, channel="#spect_{}".format(targetToken.userID))
2016-04-19 17:40:59 +00:00
if len(targetToken.spectators) == 1:
# First spectator, send #spectator join to host too
chat.joinChannel(token=targetToken, channel="#spect_{}".format(targetToken.userID))
2016-04-19 17:40:59 +00:00
2016-06-08 15:26:31 +00:00
# send fellowSpectatorJoined to all spectators
2016-06-16 13:54:35 +00:00
for spec in targetToken.spectators:
if spec is not userID:
c = glob.tokens.getTokenFromUserID(spec)
userToken.enqueue(serverPackets.fellowSpectatorJoined(c.userID))
c.enqueue(serverPackets.fellowSpectatorJoined(userID))
2016-06-08 15:26:31 +00:00
2016-04-19 17:40:59 +00:00
# Console output
2016-10-02 20:48:14 +00:00
log.info("{} are spectating {}".format(username, userUtils.getUsername(packetData["userID"])))
2016-04-19 17:40:59 +00:00
except exceptions.tokenNotFoundException:
# Stop spectating if token not found
log.warning("Spectator start: token not found")
2016-04-19 17:40:59 +00:00
userToken.stopSpectating()