.BANCHO. Use tokens while spectating rather than references to clients objects

This commit is contained in:
Nyo 2016-10-07 12:42:02 +02:00
parent ca34583e7b
commit 10e182ce55
5 changed files with 93 additions and 64 deletions

View File

@ -50,7 +50,9 @@ if userToken.matchID != -1 and userToken.actionID != actions.MULTIPLAYING and us
# Enqueue our new user panel and stats to us and our spectators # Enqueue our new user panel and stats to us and our spectators
recipients = [userToken] recipients = [userToken]
if len(userToken.spectators) > 0: if len(userToken.spectators) > 0:
recipients += userToken.spectators for i in userToken.spectators:
if i in glob.tokens.tokens:
recipients.append(glob.tokens.tokens[i])
for i in recipients: for i in recipients:
if i is not None: if i is not None:

View File

@ -16,7 +16,7 @@ def handle(userToken, _=None):
# the old logout packet will still be in the queue and will be sent to # the old logout packet will still be in the queue and will be sent to
# the server, so we accept logout packets sent at least 5 seconds after login # the server, so we accept logout packets sent at least 5 seconds after login
# if the user logs out before 5 seconds, he will be disconnected later with timeout check # if the user logs out before 5 seconds, he will be disconnected later with timeout check
if int(time.time()-userToken.loginTime) >= 5 or userToken.irc == True: if (int(time.time()-userToken.loginTime) >= 5 or userToken.irc) or userToken.tournament:
# Stop spectating # Stop spectating
userToken.stopSpectating() userToken.stopSpectating()

View File

@ -40,7 +40,11 @@ class token:
# Default variables # Default variables
self.spectators = [] self.spectators = []
# TODO: Move those two vars to a class
self.spectating = None self.spectating = None
self.spectatingUserID = 0 # we need this in case we the host gets DCed
self.location = [0,0] self.location = [0,0]
self.joinedChannels = [] self.joinedChannels = []
self.ip = ip self.ip = ip
@ -152,13 +156,14 @@ class token:
self.stopSpectating() self.stopSpectating()
# Set new spectator host # Set new spectator host
self.spectating = host self.spectating = host.token
self.spectatingUserID = host.userID
# Add us to host's spectator list # Add us to host's spectator list
host.spectators.append(self) host.spectators.append(self.token)
# Create and join spectator stream # Create and join spectator stream
streamName = "spect/{}".format(self.spectating.userID) streamName = "spect/{}".format(host.userID)
glob.streams.add(streamName) glob.streams.add(streamName)
self.joinStream(streamName) self.joinStream(streamName)
host.joinStream(streamName) host.joinStream(streamName)
@ -178,44 +183,50 @@ class token:
# Get current spectators list # Get current spectators list
for i in host.spectators: for i in host.spectators:
if i != self: if i != self.token and i in glob.tokens.tokens:
self.enqueue(serverPackets.fellowSpectatorJoined(i.userID)) self.enqueue(serverPackets.fellowSpectatorJoined(glob.tokens.tokens[i].userID))
# Log # Log
log.info("{} are spectating {}".format(self.username, userUtils.getUsername(host.username))) log.info("{} is spectating {}".format(self.username, userUtils.getUsername(host.username)))
def stopSpectating(self): def stopSpectating(self):
# Remove our userID from host's spectators # Remove our userID from host's spectators
if self.spectating == None: if self.spectating is None:
return return
streamName = "spect/{}".format(self.spectating.userID) if self.spectating in glob.tokens.tokens:
hostToken = glob.tokens.tokens[self.spectating]
else:
hostToken = None
streamName = "spect/{}".format(self.spectatingUserID)
# Remove us from host's spectators list # Remove us from host's spectators list,
# and leave spectator stream # leave spectator stream
# and end the spectator left packet to host
self.leaveStream(streamName) self.leaveStream(streamName)
self.spectating.spectators.remove(self) if hostToken is not None:
hostToken.spectators.remove(self.token)
# Send the spectator left packet to host hostToken.enqueue(serverPackets.removeSpectator(self.userID))
self.spectating.enqueue(serverPackets.removeSpectator(self.userID))
# and to all other spectators # and to all other spectators
for i in self.spectating.spectators: for i in hostToken.spectators:
i.enqueue(serverPackets.fellowSpectatorLeft(self.userID)) if i in glob.tokens.tokens:
glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID))
# If nobody is spectating the host anymore, close #spectator channel # If nobody is spectating the host anymore, close #spectator channel
# and remove host from spect stream too # and remove host from spect stream too
if len(self.spectating.spectators) == 0: if len(hostToken.spectators) == 0:
chat.partChannel(token=self.spectating, channel="#spect_{}".format(self.spectating.userID), kick=True) chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True)
self.spectating.leaveStream(streamName) hostToken.leaveStream(streamName)
# Part #spectator channel # Part #spectator channel
chat.partChannel(token=self, channel="#spect_{}".format(self.spectating.userID), kick=True) chat.partChannel(token=self, channel="#spect_{}".format(self.spectatingUserID), kick=True)
# Console output # Console output
log.info("{} are no longer spectating {}".format(self.username, self.spectating.userID)) log.info("{} is no longer spectating {}".format(self.username, self.spectatingUserID))
# Set our spectating user to 0 # Set our spectating user to 0
self.spectating = None self.spectating = None
self.spectatingUserID = 0
def setCountry(self, countryID): def setCountry(self, countryID):
""" """
@ -401,12 +412,12 @@ class token:
chat.sendMessage("FokaBot",self.username, "Your account is currently in restricted mode. Please visit ripple's website for more information.") chat.sendMessage("FokaBot",self.username, "Your account is currently in restricted mode. Please visit ripple's website for more information.")
def joinStream(self, name): def joinStream(self, name):
glob.streams.join(name, self) glob.streams.join(name, token=self.token)
if name not in self.streams: if name not in self.streams:
self.streams.append(name) self.streams.append(name)
def leaveStream(self, name): def leaveStream(self, name):
glob.streams.leave(name, self) glob.streams.leave(name, token=self.token)
if name in self.streams: if name in self.streams:
self.streams.remove(name) self.streams.remove(name)

View File

@ -1,4 +1,5 @@
from common.log import logUtils as log from common.log import logUtils as log
from objects import glob
class stream: class stream:
def __init__(self, name): def __init__(self, name):
@ -10,27 +11,37 @@ class stream:
self.name = name self.name = name
self.clients = [] self.clients = []
def addClient(self, client): def addClient(self, client=None, token=None):
""" """
Add a client to this stream if not already in Add a client to this stream if not already in
:param client: client (osuToken) object :param client: client (osuToken) object
:param token: client uuid string
:return: :return:
""" """
if client not in self.clients: if client is None and token is None:
log.info("{} has joined stream {}".format(client.username, self.name)) return
self.clients.append(client) if client is not None:
token = client.token
if token not in self.clients:
log.info("{} has joined stream {}".format(token, self.name))
self.clients.append(token)
def removeClient(self, client): def removeClient(self, client=None, token=None):
""" """
Remove a client from this stream if in Remove a client from this stream if in
:param client: client (osuToken) object :param client: client (osuToken) object
:param token: client uuid string
:return: :return:
""" """
if client in self.clients: if client is None and token is None:
log.info("{} has left stream {}".format(client.username, self.name)) return
self.clients.remove(client) if client is not None:
token = client.token
if token in self.clients:
log.info("{} has left stream {}".format(token, self.name))
self.clients.remove(token)
def broadcast(self, data): def broadcast(self, data):
""" """
@ -40,5 +51,7 @@ class stream:
:return: :return:
""" """
for i in self.clients: for i in self.clients:
if i is not None: if i in glob.tokens.tokens:
i.enqueue(data) glob.tokens.tokens[i].enqueue(data)
else:
self.removeClient(token=i)

View File

@ -26,6 +26,33 @@ class streamList:
i.leaveStream(name) i.leaveStream(name)
self.streams.pop(name) self.streams.pop(name)
def join(self, streamName, client=None, token=None):
"""
Add a client to a stream
:param streamName: stream name
:param client: client (osuToken) object
:param token: client uuid string
:return:
"""
if streamName not in self.streams:
return
self.streams[streamName].addClient(client=client, token=token)
def leave(self, streamName, client=None, token=None):
"""
Remove a client from a stream
:param streamName: stream name
:param client: client (osuToken) object
:param token: client uuid string
:return:
"""
if streamName not in self.streams:
return
self.streams[streamName].removeClient(client=client, token=token)
def broadcast(self, streamName, data): def broadcast(self, streamName, data):
""" """
Send some data to all clients in a stream Send some data to all clients in a stream
@ -38,7 +65,7 @@ class streamList:
return return
self.streams[streamName].broadcast(data) self.streams[streamName].broadcast(data)
def getClients(self, streamName): '''def getClients(self, streamName):
""" """
Get all clients in a stream Get all clients in a stream
@ -47,28 +74,4 @@ class streamList:
""" """
if streamName not in self.streams: if streamName not in self.streams:
return return
return self.streams[streamName].clients return self.streams[streamName].clients'''
def join(self, streamName, client):
"""
Add a client to a stream
:param streamName: stream name
:param client: client (osuToken) object
:return:
"""
if streamName not in self.streams:
return
self.streams[streamName].addClient(client)
def leave(self, streamName, client):
"""
Remove a client from a stream
:param streamName: stream name
:param client: client (osuToken) object
:return:
"""
if streamName not in self.streams:
return
self.streams[streamName].removeClient(client)