.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
recipients = [userToken]
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:
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 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 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
userToken.stopSpectating()

View File

@ -40,7 +40,11 @@ class token:
# Default variables
self.spectators = []
# TODO: Move those two vars to a class
self.spectating = None
self.spectatingUserID = 0 # we need this in case we the host gets DCed
self.location = [0,0]
self.joinedChannels = []
self.ip = ip
@ -152,13 +156,14 @@ class token:
self.stopSpectating()
# Set new spectator host
self.spectating = host
self.spectating = host.token
self.spectatingUserID = host.userID
# Add us to host's spectator list
host.spectators.append(self)
host.spectators.append(self.token)
# Create and join spectator stream
streamName = "spect/{}".format(self.spectating.userID)
streamName = "spect/{}".format(host.userID)
glob.streams.add(streamName)
self.joinStream(streamName)
host.joinStream(streamName)
@ -178,44 +183,50 @@ class token:
# Get current spectators list
for i in host.spectators:
if i != self:
self.enqueue(serverPackets.fellowSpectatorJoined(i.userID))
if i != self.token and i in glob.tokens.tokens:
self.enqueue(serverPackets.fellowSpectatorJoined(glob.tokens.tokens[i].userID))
# 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):
# Remove our userID from host's spectators
if self.spectating == None:
if self.spectating is None:
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
# and leave spectator stream
# Remove us from host's spectators list,
# leave spectator stream
# and end the spectator left packet to host
self.leaveStream(streamName)
self.spectating.spectators.remove(self)
if hostToken is not None:
hostToken.spectators.remove(self.token)
hostToken.enqueue(serverPackets.removeSpectator(self.userID))
# Send the spectator left packet to host
self.spectating.enqueue(serverPackets.removeSpectator(self.userID))
# and to all other spectators
for i in hostToken.spectators:
if i in glob.tokens.tokens:
glob.tokens.tokens[i].enqueue(serverPackets.fellowSpectatorLeft(self.userID))
# and to all other spectators
for i in self.spectating.spectators:
i.enqueue(serverPackets.fellowSpectatorLeft(self.userID))
# If nobody is spectating the host anymore, close #spectator channel
# and remove host from spect stream too
if len(self.spectating.spectators) == 0:
chat.partChannel(token=self.spectating, channel="#spect_{}".format(self.spectating.userID), kick=True)
self.spectating.leaveStream(streamName)
# If nobody is spectating the host anymore, close #spectator channel
# and remove host from spect stream too
if len(hostToken.spectators) == 0:
chat.partChannel(token=hostToken, channel="#spect_{}".format(hostToken.userID), kick=True)
hostToken.leaveStream(streamName)
# 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
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
self.spectating = None
self.spectatingUserID = 0
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.")
def joinStream(self, name):
glob.streams.join(name, self)
glob.streams.join(name, token=self.token)
if name not in self.streams:
self.streams.append(name)
def leaveStream(self, name):
glob.streams.leave(name, self)
glob.streams.leave(name, token=self.token)
if name in self.streams:
self.streams.remove(name)

View File

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

View File

@ -26,6 +26,33 @@ class streamList:
i.leaveStream(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):
"""
Send some data to all clients in a stream
@ -38,7 +65,7 @@ class streamList:
return
self.streams[streamName].broadcast(data)
def getClients(self, streamName):
'''def getClients(self, streamName):
"""
Get all clients in a stream
@ -47,28 +74,4 @@ class streamList:
"""
if streamName not in self.streams:
return
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)
return self.streams[streamName].clients'''