def onCancel(client_name):
    try:
        socket_id = request.sid
        session_closed, room_id = matchmake.checkDisconnect(socket_id)
        if session_closed:
            session.close(room_id)
            chat.roomServerMessage(
                'Client ' + misc.generateNameTag(
                    socket_id, matchmake.sid_cid_pairs[socket_id]) +
                ' has left the room', room_id)
            matchmake.clearRoom(room_id, socket_id)
        else:
            room_found, room, index = matchmake.checkObserverDisconnect(
                socket_id)
            if room_found:
                del matchmake._rooms[room]['Viewers'][index]
                chat.roomServerMessage(
                    'Client ' + misc.generateNameTag(
                        socket_id, matchmake.sid_cid_pairs[socket_id]) +
                    ' has left the room', room)
                chat.roomChatInfo(socket_id,
                                  matchmake.sid_cid_pairs[socket_id])
        print((u'Client canceled: ' + client_name).encode('utf-8'))
    except Exception:
        err_log = open('err_log', 'a', encoding='utf-8')
        err_log.write(traceback.format_exc())
        print(traceback.format_exc())
def onDisconnect():
    try:
        socket_id = request.sid
        chat.removeGlobalClient(socket_id)
        session_closed, room_id = matchmake.checkDisconnect(socket_id)
        if session_closed:
            chat.roomServerMessage(
                'Client ' + misc.generateNameTag(
                    socket_id, matchmake.sid_cid_pairs[socket_id]) +
                ' has left the room', room_id)
            matchmake.clearRoom(room_id, socket_id)
            try:
                del matchmake.sid_cid_pairs[socket_id]
            except KeyError:
                print((u'sid ' + sid + u' already cleared').encode('utf-8'))
        else:
            room_found, room, index = matchmake.checkObserverDisconnect(
                socket_id)
            if room_found:
                del matchmake._rooms[room]['Viewers'][index]
                chat.roomServerMessage(
                    'Client ' + misc.generateNameTag(
                        socket_id, matchmake.sid_cid_pairs[socket_id]) +
                    ' has left the room', room)
                chat.roomChatInfo(socket_id,
                                  matchmake.sid_cid_pairs[socket_id])
        print(u"Client disconnect: " + socket_id)
    except Exception:
        err_log = open('err_log', 'a', encoding='utf-8')
        err_log.write(traceback.format_exc())
        print(traceback.format_exc())
def onSpectate(data):
    try:
        socket_id = request.sid
        client_id = data['client_name']
        room_id = data['room']
        session.emitBoard(room_id, socket_id)
        chat.roomChatInfo(socket_id, client_id)
        chat.roomServerMessage(
            'Client ' + misc.generateNameTag(
                socket_id, matchmake.sid_cid_pairs[socket_id]) +
            ' has joined the room (Spectating)', room_id)
        print(u"Client spectating: " + str(data))
    except Exception:
        err_log = open('err_log', 'a', encoding='utf-8')
        err_log.write(traceback.format_exc())
        print(traceback.format_exc())
def onReady(client_name):
    try:
        socket_id = request.sid
        client_id = client_name
        emit('ready', socket_id)
        session_formed, room_id = matchmake.checkJoin(socket_id, client_id)
        if session_formed:
            session.start(room_id)
        chat.roomChatInfo(socket_id, client_id)
        chat.roomServerMessage(
            'Client ' + misc.generateNameTag(
                socket_id, matchmake.sid_cid_pairs[socket_id]) +
            ' has joined the room', room_id)
        print((u'Client ready: ' + client_name).encode('utf-8'))
    except Exception:
        err_log = open('err_log', 'a', encoding='utf-8')
        err_log.write(traceback.format_exc())
        print(traceback.format_exc())