def challenge(args): room = args.get('room') user = args.get('user') target_user = args.get('target_user') word = args.get('word') game_state = get_game_by_name(room) if target_user != user: game_state.create_challenge(target_user, word) update_game_state(room, game_state) print_log_line('room %s: %s is challenging %s\'s word - %s' % (room, user, target_user, word)) status_msg = '%s is challenging %s\'s word: %s' % (user, target_user, word) socketio.emit('challenge', {'status': status_msg}, room=room) else: game_state.rollback(user, word) new_state = game_state.generate_game_state() update_game_state(room, game_state) status_msg = '%s Has self challenged: %s' % (user, word) socketio.emit('game_state_update', { 'status': status_msg, 'event': 'self_challenge', 'game_state': new_state }, room=room)
def flip_tile(args): user = args.get('user') game = args.get('room') cur = cursor() game_state = get_game_by_name(game) if game_state is None: print_log_line('user %s attempted to access missing room %s' % (user, game)) return flipped_tile = game_state.flip_tile() new_state = game_state.generate_game_state() update_game_state(game, game_state) state_update = 'No more letters' if flipped_tile is not None: article = 'an' if flipped_tile in 'ERIOASFHLXNM' else 'a' state_update = '%s flipped %s "%s"' % (user, article, flipped_tile) print_log_line('room %s: %s flipped %s "%s"' % (game, user, article, flipped_tile)) socketio.emit('game_state_update', { 'status': state_update, 'event': 'flip', 'game_state': new_state }, room=game)
def steal_word(self, user, word, typing_time): steal_time = time.time() - typing_time if self.challenge is not None: return None #Steal from person stealing_dict_keys = sorted(self.active_users, key=self.calculate_score) stealing_dict = collections.OrderedDict() for dict_user in stealing_dict_keys: stealing_dict[dict_user] = sorted(self.active_users[dict_user], key=neg_len) our_tmp = stealing_dict[user] del stealing_dict[user] stealing_dict[user] = our_tmp multi_result = self.multi_word_recurse(user, word, stealing_dict, 0) if multi_result is not None: print_log_line(str(multi_result)) still_needed = multi_result[0] words_stolen = multi_result[1:] self.active_users[user].append(word) for username, word_stolen in words_stolen: self.active_users[username].remove(word_stolen) self.prev_source.append( (user, word, still_needed, words_stolen, steal_time)) return True for username, words in stealing_dict.items(): for stealable_word in words: still_needed = list_subtraction(list(word), list(stealable_word)) if still_needed is None or still_needed == []: continue new_middle = list_subtraction(self.middle, still_needed) if new_middle is None: continue self.middle = new_middle if username == user: overwrite_index = self.active_users[username].index( stealable_word) self.active_users[username][overwrite_index] = word else: self.active_users[user].append(word) self.active_users[username].remove(stealable_word) ##Sort words self.prev_source.append((user, word, list(still_needed), [ (username, stealable_word) ], steal_time)) return True #Steal from middle new_middle = list_subtraction(self.middle, list(word)) if len(word) < 3 or new_middle is None: return False else: self.middle = new_middle self.active_users[user].append(word) self.prev_source.append((user, word, list(word), [], steal_time)) return True
def send_message(args): user = args.get('user') message = args.get('message') room = args.get('room') print_log_line('Message sent in %s' % (room, )) socketio.emit('message_sent', { 'user': user, 'message': message }, room=room)
def user_disc(): sid = request.sid cur = cursor() cur.execute('SELECT NAME, GAME FROM USERS WHERE SID = %s', (sid, )) user_data = cur.fetchone() if user_data is None: print_log_line('Unknown user with SID = %s disconnected' % (sid, )) return username, game = user_data print_log_line('%s (%s) disconnected' % (username, sid)) cur.execute('UPDATE USERS SET SID = NULL WHERE SID = %s', (sid, )) cur.execute('SELECT NAME FROM USERS WHERE GAME = %s AND SID IS NOT NULL', (game, )) if cur.fetchone() is None: cur.execute('DELETE FROM GAMES WHERE NAME = %s', (game, ))
def vote(args): room = args.get('room') user = args.get('user') vote = args.get('vote') print_log_line('room %s: %s votes to %s the word' % (room, user, vote)) game_state = get_game_by_name(room) game_state.set_vote(user, vote) if game_state.all_votes_in(): finish_challenge(game_state, room) else: update_game_state(room, game_state) status_msg = '%s has voted: %s' % (user, vote) socketio.emit('vote_cast', { 'status': status_msg, 'votes': game_state.get_votes() }, room=room)
def steal_word(args): user = args.get('user') word = args.get('word') room = args.get('room') typing_time = args.get('typing_time') typing_time = typing_time / 1000 if typing_time > MAX_TYPING_TIME: typing_time = MAX_TYPING_TIME word = game_data.char_strip(word) game_state = get_game_by_name(room) steal_result = game_state.steal_word(user, word, typing_time) while steal_result == False: prev_time = game_state.prev_time() if prev_time > time.time() - typing_time: game_state.rollback() steal_result = game_state.steal_word(user, word, typing_time) else: break old_game_state = get_game_by_name(room) truncate = game_state.hist_len() tail = old_game_state.hist_tail(truncate) for steal_record in tail: game_state.steal_word(steal_record[0], steal_record[1], time.time() - stail_record[4]) new_state = game_state.generate_game_state() status_msg = '%s stole the word "%s"' % (user, word) if steal_result == False: status_msg = '%s tried to steal the word "%s"' % (user, word) game_state = old_game_state else: print_log_line('room %s: %s stole the word "%s"' % (room, user, word)) update_game_state(room, game_state) socketio.emit('game_state_update', { 'status': status_msg, 'event': 'steal', 'game_state': new_state }, room=room)
def user_rem(): sid = request.sid cur = cursor() cur.execute('SELECT NAME, GAME FROM USERS WHERE SID = %s', (sid, )) user_data = cur.fetchone() if user_data is None: print_log_line('Phantom sid tried to disconnect: %s' % (sid, )) return username = user_data[0] game = user_data[1] print_log_line('User %s (%s) leaving %s' % (username, sid, game)) game_state = get_game_by_name(game) if game_state is None: print_log_line('Missing game data: %s' % (game, )) return game_state.remove_user(username) if game_state.num_users() == 0: cur.execute('DELETE FROM GAMES WHERE NAME = %s', (game, )) else: update_game_state(game, game_state) cur.execute('DELETE FROM USERS WHERE SID = %s', (sid, )) new_state = game_state.generate_game_state() update_message = 'User %s has left' % (username, ) socketio.emit('game_state_update', { 'game_state': new_state, 'event': 'leave', 'status': update_message }, room=game)
def join_game(data): sid = request.sid username = data['username'] game_name = data['game_name'] cur = cursor() game_state = get_game_by_name(game_name) cur.execute('DELETE FROM USERS WHERE NAME = %s', (username, )) cur.execute('INSERT INTO USERS (NAME, SID, GAME) VALUES (%s, %s, %s)', (username, sid, game_name)) flask_socketio.join_room(game_name) if game_state is None: ##create the game: cur.execute('INSERT INTO GAMES (NAME) VALUES (%s)', (game_name, )) game_state = game_data.game_room(username) else: if game_state.has_user(username): print_log_line('%s updating sid to %s' % (username, sid)) send_challenge_updates(sid, game_state) return game_state.add_user(username) print_log_line('user %s (%s) joining game %s' % (username, sid, game_name)) update_game_state(game_name, game_state) cur.execute('UPDATE GAMES SET STATE = %s WHERE NAME = %s', (json.dumps(game_state.generate_game_state()), game_name)) new_state = game_state.generate_game_state() update_message = 'User %s has joined' % (username, ) socketio.emit('game_state_update', { 'game_state': new_state, 'event': 'join', 'status': update_message }, room=game_name) send_challenge_updates(sid, game_state)