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 move(sid, position): room_id = determineRoom(sid) #legal if validateTurn(sid, position['x'], position['y'], room_id): print('val') chat.roomServerMessage( str( misc.generateNameTag(sid, sessions[room_id][sid]['Client-Name'])) + ' moved to ' + str(position['x'] + 1) + ', ' + str(position['y'] + 1), room_id) placePieceBoard(position['x'], position['y'], room_id) sessions[room_id]['Previous-Turn'] = str(position['x']) + "|" + str( position['y']) sessions[room_id]['Move'] = sessions[room_id]['Move'] + 1 three_x_three_main = reduce3X3(position['x'], position['y'], sessions[room_id]['Board']) if checkGridWon(three_x_three_main): three_x_three_reduced = array3X3( sessions[room_id]['Reduced-Board']) if three_x_three_reduced[floor(position['y'] / 3)][floor( position['x'] / 3)] == '-': sessions[room_id]['Reduced-Board'] = placePieceOn3X3( floor(position['x'] / 3), floor(position['y'] / 3), three_x_three_reduced, sessions[room_id]['Turn']) if checkGridWon(three_x_three_reduced): sessions[room_id]['Message'] = 'Player ' + sessions[ room_id][sid]['Client-Name'] + '(' + sessions[room_id][ 'Turn'] + ') wins!' chat.roomServerMessage( str( misc.generateNameTag( sid, sessions[room_id][sid]['Client-Name'])) + ' Wins.', room_id) storeBoard(room_id) swapTurn(room_id) else: swapTurn(room_id) sessions[room_id][ 'Message'] = 'Section Won - Turn ' + sessions[room_id][ 'Turn'] else: swapTurn(room_id) sessions[room_id][ 'Message'] = 'Section Already Won - Turn ' + sessions[ room_id]['Turn'] else: sessions[room_id]['Message'] = '' swapTurn(room_id) sessions[room_id]['Success'] = '1' emitBoard(room_id, target=room_id) #illegal else: sessions[room_id]['Success'] = '0' emitBoard(room_id, target=sid) print(room_id) print(sid) print(sessions[room_id])
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())