def begin_game(clients, room_id, is_first=False): users = UserTool.room_users(room_id) current_user = None index = 0 flag = False next_flag = False for client, topic in zip(clients, TopicTool.get_topics()): if client: if is_first: user = UserTool.update_user(client, topic=topic, status=Status.playing) else: user = UserTool.current_user(client) if is_first and index == 0: user = UserTool.update_user(client, status=Status.action) current_user = user elif next_flag: user = UserTool.update_user(client, status=Status.action) current_user = user flag = True next_flag = False elif not is_first and not flag and user[ 'status'] == Status.action: user = UserTool.update_user(client, status=Status.playing) next_flag = True p_id = int(user['p_id']) users[p_id] = user index += 1 other_clients = [] for client in clients: if client: user = UserTool.current_user(client) if user['status'] != Status.action: other_clients.append(client) over = False if not is_first and not flag: # game over over = True for client in clients: if client: user = UserTool.update_user(client, topic=None, status=Status.waiting) p_id = int(user['p_id']) users[p_id] = user RoomTool.update_room(room_id, status=RoomStatus.ready) return { 'room': RoomTool.update_room(room_id, users=users), 'user': current_user, 'over': over, 'other_clients': other_clients }
def current_topic(r_id): room = RoomTool.room_by(r_id) if room['status'] != RoomStatus.playing: return None users = UserTool.room_users(r_id) for user in users: if user: if user.get('status', None) == Status.action: return user['topic'] return None
def begin_game(clients, room_id, is_first=False): users = UserTool.room_users(room_id) current_user = None index = 0 flag = False next_flag = False # 1 2 => 2 1 # 1 2 3 4 => 3 1 4 2 0 2 4 # 1 2 3 4 5 6 => 4 1 5 2 6 3 4 1 2 3 5 6 4 1 5 2 3 6 seq_clients = [] fil_clients = filter(lambda x: x, clients) mid = len(fil_clients) / 2 is_black = True dict_clients = dict() for i in range(0, len(fil_clients)): dict_clients[i] = fil_clients[i] print dict_clients while len(filter(lambda x: x, dict_clients.values())) > 0: # print filter(lambda x: x, dict_clients.values()) for key in dict_clients.keys(): print type(key) if is_black and key >= mid and dict_clients[key] is not None: seq_clients.append(dict_clients[key]) dict_clients[key] = None print dict_clients is_black = False break if not is_black and key < mid and dict_clients[key] is not None: seq_clients.append(dict_clients[key]) dict_clients[key] = None is_black = True break # print 1111 print seq_clients for client in seq_clients: if client: if is_first: user = UserTool.update_user(client, status=Status.playing) else: user = UserTool.current_user(client) if is_first and index == 0: user = UserTool.update_user(client, status=Status.action) current_user = user elif next_flag: user = UserTool.update_user(client, status=Status.action) current_user = user flag = True next_flag = False elif not is_first and not flag and user[ 'status'] == Status.action: user = UserTool.update_user(client, status=Status.playing) next_flag = True print index p_id = int(user['p_id']) users[p_id] = user index += 1 # over = False if not is_first and not flag: # game over # over = True for client in seq_clients: if client: user = UserTool.update_user(client, status=Status.action) current_user = user break try: p_id = int(user.get('p_id', None)) users[p_id] = user except TypeError as e: pass begin_client = None other_clients = [] index = 0 for client in clients: if client: user = UserTool.current_user(client) if user['status'] != Status.action: other_clients.append(client) else: begin_client = client action_army = u'白色方' if index < mid else u'黑色方' index += 1 return { 'room': RoomTool.update_room(room_id, users=users), 'user': current_user, 'army': action_army, 'begin_client': begin_client }