def joinRoom(roomInfo): if len(roomInfo) == 0: return if request.sid in rooms: #Checks if the user is already in a room if rooms[request.sid] == roomInfo: return leave_room(rooms[request.sid]) send("<b> " + names[request.sid] + " has left the room<b>", room=rooms[request.sid]) winner = Game.removeUser(games[rooms[request.sid]], request.sid) if len(games[rooms[request.sid]] ['users']) == 0: #Deletes game room if no-one is in it games.pop(rooms[request.sid]) elif winner != None: send("<b>" + names[games[rooms[request.sid]][winner]] + " has won the game!</b>", room=rooms[request.sid]) join_room(roomInfo) #Places user in a room if roomInfo not in games: #Create new game games[roomInfo] = Game.newGame() Game.addUser(games[roomInfo], request.sid) rooms[request. sid] = roomInfo #Sets room of user in a dictionary for later use emit('joinRoom', roomInfo) emit('gameUpdate', games[roomInfo]['board']) send("<b> " + names[request.sid] + " joined " + roomInfo + "<b>", room=roomInfo)
def disconn(): #Executed when a client disconnects from the server try: send("<b> " + names[request.sid] + " has left the room<b>", room=rooms[request.sid]) winner = Game.removeUser(games[rooms[request.sid]], request.sid) if len(games[rooms[request.sid]] ['users']) == 0: #Deletes game room if no-one is in it games.pop(rooms[request.sid]) elif winner != None: send("<b>" + names[games[rooms[request.sid]][winner]] + " has won the game!</b>", room=rooms[request.sid]) rooms.pop(request.sid) except: #Fallback in case user leaves before joining a room pass names.pop(request.sid)
def disconn(): #Executed when a client disconnects from the server if request.sid in rooms: currGame = games[rooms[request.sid]] #dbu.rgame(session['username']) currDrawerRemoved = Game.removeUser(games[rooms[request.sid]], request.sid) if len(currGame['players'] ) == 0: #Deletes game room if no-one is in it games.pop(rooms[request.sid]) elif currDrawerRemoved and len( currGame['order']) > currGame['currDrawer']: socketio.emit('yourturn', currGame['offeredWords'], room=currGame['order'][currGame['currDrawer']]) emit('highlightDrawer', names[currGame['order'][currGame['currDrawer']]], broadcast=True, room=rooms[request.sid]) socketio.send('<b>' + names[request.sid] + ' has left the room</b>') emit('playerLeave', names[request.sid], broadcast=True, room=rooms[request.sid]) rooms.pop(request.sid) elif request.sid in lobbyrooms: currLobby = lobbies[lobbyrooms[request.sid]] currLobby.remove(request.sid) if len(currLobby) == 0: lobbies.pop(lobbyrooms[request.sid]) else: emit('playerLeave', names[request.sid], broadcast=True, room=lobbyrooms[request.sid], namespace='/lobby') if request.sid == lobbyLeaders[lobbyrooms[request.sid]]: lobbyLeaders[lobbyrooms[request.sid]] = random.sample( currLobby, 1)[0] emit('newLeader', names[lobbyLeaders[lobbyrooms[request.sid]]], broadcast=True, room=lobbyrooms[request.sid], namespace='/lobby')