.BANCHO. Use tokens in multiplayer rather than references to clients objects

This commit is contained in:
Nyo 2016-10-07 13:15:50 +02:00
parent 10e182ce55
commit ea83d3c86d
2 changed files with 27 additions and 27 deletions

View File

@ -100,8 +100,8 @@ class match:
# Slot user ID. Write only if slot is occupied # Slot user ID. Write only if slot is occupied
for i in range(0,16): for i in range(0,16):
if self.slots[i].user is not None: if self.slots[i].user is not None and self.slots[i].user in glob.tokens.tokens:
struct.append([self.slots[i].user.userID, dataTypes.UINT32]) struct.append([glob.tokens.tokens[self.slots[i].user].userID, dataTypes.UINT32])
# Other match data # Other match data
struct.extend([ struct.extend([
@ -129,15 +129,15 @@ class match:
newHost -- new host userID newHost -- new host userID
""" """
slotID = self.getUserSlotID(newHost) slotID = self.getUserSlotID(newHost)
if slotID is None: if slotID is None or self.slots[slotID].user not in glob.tokens.tokens:
return return
token = self.slots[slotID].user token = glob.tokens.tokens[self.slots[slotID].user]
self.hostUserID = newHost self.hostUserID = newHost
token.enqueue(serverPackets.matchTransferHost()) token.enqueue(serverPackets.matchTransferHost())
self.sendUpdates() self.sendUpdates()
log.info("MPROOM{}: {} is now the host".format(self.matchID, token.username)) log.info("MPROOM{}: {} is now the host".format(self.matchID, token.username))
def setSlot(self, slotID, status = None, team = None, user = -1, mods = None, loaded = None, skip = None, complete = None): def setSlot(self, slotID, status = None, team = None, user = "", mods = None, loaded = None, skip = None, complete = None):
#self.setSlot(i, slotStatuses.notReady, 0, user, 0) #self.setSlot(i, slotStatuses.notReady, 0, user, 0)
if status is not None: if status is not None:
self.slots[slotID].status = status self.slots[slotID].status = status
@ -145,10 +145,10 @@ class match:
if team is not None: if team is not None:
self.slots[slotID].team = team self.slots[slotID].team = team
if user is not -1: if user is not "":
self.slots[slotID].user = user self.slots[slotID].user = user
if mods is not -1: if mods is not None:
self.slots[slotID].mods = mods self.slots[slotID].mods = mods
if loaded is not None: if loaded is not None:
@ -203,11 +203,11 @@ class match:
newStatus = slotStatuses.locked newStatus = slotStatuses.locked
# Send updated settings to kicked user, so he returns to lobby # Send updated settings to kicked user, so he returns to lobby
if self.slots[slotID].user is not None: if self.slots[slotID].user is not None and self.slots[slotID].user in glob.tokens.tokens:
self.slots[slotID].user.enqueue(serverPackets.updateMatch(self.matchID)) glob.tokens.tokens[self.slots[slotID].user].enqueue(serverPackets.updateMatch(self.matchID))
# Set new slot status # Set new slot status
self.setSlot(slotID, newStatus, 0, -1, 0) self.setSlot(slotID, status=newStatus, team=0, user=None, mods=0)
# Send updates to everyone else # Send updates to everyone else
self.sendUpdates() self.sendUpdates()
@ -259,7 +259,8 @@ class match:
log.info("MPROOM{}: User {} skipped".format(self.matchID, userID)) log.info("MPROOM{}: User {} skipped".format(self.matchID, userID))
# Send skip packet to every playing user # Send skip packet to every playing user
glob.streams.broadcast(self.playingStreamName, serverPackets.playerSkipped(self.slots[slotID].user.userID)) #glob.streams.broadcast(self.playingStreamName, serverPackets.playerSkipped(glob.tokens.tokens[self.slots[slotID].user].userID))
glob.streams.broadcast(self.playingStreamName, serverPackets.playerSkipped(slotID))
# Check all skipped # Check all skipped
total = 0 total = 0
@ -337,7 +338,7 @@ class match:
return -- slot id if found, None if user is not in room return -- slot id if found, None if user is not in room
""" """
for i in range(0,16): for i in range(0,16):
if self.slots[i].user is not None and self.slots[i].user.userID == userID: if self.slots[i].user is not None and self.slots[i].user in glob.tokens.tokens and glob.tokens.tokens[self.slots[i].user].userID == userID:
return i return i
return None return None
@ -351,7 +352,7 @@ class match:
# Make sure we're not in this match # Make sure we're not in this match
for i in range(0,16): for i in range(0,16):
if self.slots[i].user == user: if self.slots[i].user == user.token:
# Set bugged slot to free # Set bugged slot to free
self.setSlot(i, slotStatuses.free, 0, None, 0) self.setSlot(i, slotStatuses.free, 0, None, 0)
@ -359,7 +360,7 @@ class match:
for i in range(0,16): for i in range(0,16):
if self.slots[i].status == slotStatuses.free: if self.slots[i].status == slotStatuses.free:
# Occupy slot # Occupy slot
self.setSlot(i, slotStatuses.notReady, 0, user, 0) self.setSlot(i, slotStatuses.notReady, 0, user.token, 0)
# Send updated match data # Send updated match data
self.sendUpdates() self.sendUpdates()
@ -395,8 +396,8 @@ class match:
if user.userID == self.hostUserID: if user.userID == self.hostUserID:
# Give host to someone else # Give host to someone else
for i in range(0,16): for i in range(0,16):
if self.slots[i].user is not None: if self.slots[i].user is not None and self.slots[i].user in glob.tokens.tokens:
self.setHost(self.slots[i].user.userID) self.setHost(glob.tokens.tokens[self.slots[i].user].userID)
break break
# Send updated match data # Send updated match data
@ -422,11 +423,8 @@ class match:
return return
# Get old slot data # Get old slot data
oldData = dill.copy(self.slots[oldSlotID]) #oldData = dill.copy(self.slots[oldSlotID])
oldData = copy.deepcopy(self.slots[oldSlotID])
# Oh no we have a huge meme here.
# Get pointer to right token and DON'T copy it
oldData.user = glob.tokens.tokens[oldData.user.token]
# Free old slot # Free old slot
self.setSlot(oldSlotID, slotStatuses.free, 0, None, 0, False, False, False) self.setSlot(oldSlotID, slotStatuses.free, 0, None, 0, False, False, False)
@ -496,11 +494,11 @@ class match:
slotID -- ID of slot slotID -- ID of slot
""" """
# Make sure there is someone in that slot # Make sure there is someone in that slot
if self.slots[slotID].user is None: if self.slots[slotID].user is None or self.slots[slotID].user not in glob.tokens.token:
return return
# Transfer host # Transfer host
self.setHost(self.slots[slotID].user.userID) self.setHost(glob.tokens.tokens[self.slots[slotID].user].userID)
# Send updates # Send updates
self.sendUpdates() self.sendUpdates()
@ -589,7 +587,7 @@ class match:
# We have teams, check if they are valid # We have teams, check if they are valid
firstTeam = -1 firstTeam = -1
for i in range(0,16): for i in range(0,16):
if self.slots[i].user is not None and (self.slots[i].status&slotStatuses.noMap) == 0: if self.slots[i].user is not None and (self.slots[i].status & slotStatuses.noMap) == 0:
if firstTeam == -1: if firstTeam == -1:
firstTeam = self.slots[i].team firstTeam = self.slots[i].team
elif firstTeam != self.slots[i].team: elif firstTeam != self.slots[i].team:
@ -613,12 +611,12 @@ class match:
# Set playing to ready players and set load, skip and complete to False # Set playing to ready players and set load, skip and complete to False
# Make clients join playing stream # Make clients join playing stream
for i in range(0, 16): for i in range(0, 16):
if (self.slots[i].status & slotStatuses.ready) > 0: if (self.slots[i].status & slotStatuses.ready) > 0 and self.slots[i].user in glob.tokens.tokens:
self.slots[i].status = slotStatuses.playing self.slots[i].status = slotStatuses.playing
self.slots[i].loaded = False self.slots[i].loaded = False
self.slots[i].skip = False self.slots[i].skip = False
self.slots[i].complete = False self.slots[i].complete = False
self.slots[i].user.joinStream(self.playingStreamName) glob.tokens.tokens[self.slots[i].user].joinStream(self.playingStreamName)
# Send match start packet # Send match start packet
glob.streams.broadcast(self.playingStreamName, serverPackets.matchStart(self.matchID)) glob.streams.broadcast(self.playingStreamName, serverPackets.matchStart(self.matchID))

View File

@ -1,4 +1,5 @@
from objects import stream from objects import stream
from objects import glob
class streamList: class streamList:
def __init__(self): def __init__(self):
@ -23,7 +24,8 @@ class streamList:
""" """
if name in self.streams: if name in self.streams:
for i in self.streams[name].clients: for i in self.streams[name].clients:
i.leaveStream(name) if i in glob.tokens.tokens:
glob.tokens.tokens[i].leaveStream(name)
self.streams.pop(name) self.streams.pop(name)