2016-05-18 17:12:46 +00:00
|
|
|
from constants import clientPackets
|
|
|
|
from constants import serverPackets
|
|
|
|
from constants import exceptions
|
|
|
|
from objects import glob
|
|
|
|
from helpers import userHelper
|
2016-06-04 10:44:54 +00:00
|
|
|
from helpers import logHelper as log
|
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"])
|
|
|
|
if targetToken == None:
|
|
|
|
raise exceptions.tokenNotFoundException
|
|
|
|
|
|
|
|
# Add us to host's spectators
|
|
|
|
targetToken.addSpectator(userID)
|
|
|
|
|
|
|
|
# Send spectator join packet to host
|
|
|
|
targetToken.enqueue(serverPackets.addSpectator(userID))
|
|
|
|
|
|
|
|
# Join #spectator channel
|
|
|
|
userToken.enqueue(serverPackets.channelJoinSuccess(userID, "#spectator"))
|
|
|
|
|
|
|
|
if len(targetToken.spectators) == 1:
|
|
|
|
# First spectator, send #spectator join to host too
|
|
|
|
targetToken.enqueue(serverPackets.channelJoinSuccess(userID, "#spectator"))
|
|
|
|
|
2016-06-08 15:26:31 +00:00
|
|
|
# send fellowSpectatorJoined to all spectators
|
|
|
|
for c in targetToken.spectators:
|
|
|
|
if c is not userID:
|
2016-06-08 18:27:59 +00:00
|
|
|
#targetToken.enqueue(serverPackets.fellowSpectatorJoined(c))
|
2016-06-08 15:26:31 +00:00
|
|
|
specToken = glob.tokens.getTokenFromUserID(c)
|
|
|
|
specToken.enqueue(serverPackets.fellowSpectatorJoined(userID))
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
# Console output
|
2016-06-04 10:44:54 +00:00
|
|
|
log.info("{} are spectating {}".format(username, userHelper.getUsername(packetData["userID"])))
|
2016-04-19 17:40:59 +00:00
|
|
|
except exceptions.tokenNotFoundException:
|
|
|
|
# Stop spectating if token not found
|
2016-06-04 10:44:54 +00:00
|
|
|
log.warning("Spectator start: token not found")
|
2016-04-19 17:40:59 +00:00
|
|
|
userToken.stopSpectating()
|