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 globalMessage(sid, message): if message['contents'].strip() == '' or message['sender'].strip() == '': return message['contents'] = removeHazzards(message['contents']).strip() log( 'chat/global.log', '<' + removeHazzards( misc.generateNameTag(sid, message['sender'].replace(' ', '%20'))) + '> ' + message['contents']) emit('global-client-message', { 'contents': message['contents'], 'sender': removeHazzards(misc.generateNameTag(sid, message['sender'])) }, broadcast=True)
def removeGlobalClient(sid): try: cid = matchmake.sid_cid_pairs[sid] del chatters[chatters.index( removeHazzards(misc.generateNameTag(sid, cid)))] except KeyError: print(sid + ' key err') emit('global-chat-setup', chatters, broadcast=True)
def formRoom(sid1, cid1, sid2, cid2): print((cid1 + u' vs. ' + cid2).encode('utf-8')) room_id = (cid1 + u'|' + sid1 + u'-' + cid2 + u'|' + sid2) join_room(room_id, sid=sid1) join_room(room_id, sid=sid2) emit(u'join', cid1 + u' and ' + cid2 + u' have entered the room.', room=room_id) clearSID(sid1) _rooms[room_id] = { u'Code': id_generator(), u'Viewers': [misc.generateNameTag(sid1, cid1), misc.generateNameTag(sid2, cid2)] } return room_id
def checkObserverDisconnect(sid): for room in _rooms: print((str(_rooms[room]['Viewers'])).encode('utf-8')) for index, viewer in enumerate(_rooms[room]['Viewers']): try: if viewer == misc.generateNameTag(sid, sid_cid_pairs[sid]): return True, room, index except: err_log = open('err_log', 'a', encoding='utf-8') err_log.write(traceback.format_exc()) print(traceback.format_exc()) return False, None, -1
def roomMessage(sid, message): if message['contents'].strip() == '' or message['sender'].strip() == '': return client_room = '' for room in rooms(): if room.find('|') > -1: client_room = room break emit('room-client-message', { 'contents': removeHazzards(message['contents']), 'sender': removeHazzards(misc.generateNameTag(sid, message['sender'])) }, room=client_room)
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())
def roomChatInfo(sid, cid): room_name = '' for room in rooms(): if room.find('|') > -1: room_name = room break if room_name == '': room_name = rooms()[-1] try: all_viewers = matchmake._rooms[room_name]['Viewers'] for index, viewer in enumerate(all_viewers): all_viewers[index] = removeHazzards(viewer) room_code = matchmake._rooms[room_name]['Code'] emit('room-chat-setup', { 'Room': room_code, 'Viewers': all_viewers }, room=room_name) except KeyError: emit('room-chat-setup', { 'Room': 'Forming...', 'Viewers': [misc.generateNameTag(sid, cid)] }, room=room_name)
def globalChatInfo(sid, cid): chatters.append(removeHazzards(misc.generateNameTag(sid, cid))) emit('global-chat-setup', chatters, broadcast=True)
def becomeObserver(sid, cid, rid): _rooms[rid]['Viewers'].append(misc.generateNameTag(sid, cid)) join_room(rid, sid=sid) createSIDCIDPair(sid, cid)