def disconnect_client(client): for room in Room.objects(): if client in room.clients: pool.add_task(decrease_clients_with_delay, { 'room': room.name, 'client': client })
def get_current_cards(room_id): room = Room.objects(Q(name=room_id)).first() current_cards = [] cur_nbs = room.current_numbers decks = room.decks for i in range(len(room.decks)): current_cards.append(decks[i][cur_nbs[i]]) renew_room_last_update(room_id) return current_cards
def decrease_clients(room_id): room = Room.objects(Q(name=room_id['room'])).first() if room_id['client'] in room.clients: room.clients.remove(room_id['client']) log.info('Leave game room: ' + str(room_id) + ". Current clients: " + str(room.clients)) if len(room.clients) == 0: close_room(room_id['room']) else: room.save()
def mark_up_unused(locstore): res = {} for s in locstore: if s.find('previousSocket') != -1 or s.find('settings') != -1: room_id = re.findall('(\d+)', s)[0] if Room.objects(Q(name=room_id)).first() is not None: res[s] = True else: res[s] = False return res
def get_new_cards(room_id): room = Room.objects(Q(name=room_id)).first() cards = [] cur_nbs = room.current_numbers decks = room.decks for i in range(len(room.decks)): if cur_nbs[i] + 1 < len(decks[i]): cards.append(decks[i][cur_nbs[i] + 1]) cur_nbs[i] += 1 else: sorted_cards = room.decks[i] random_sort(sorted_cards) room.decks[i] = sorted_cards cards.append(sorted_cards[0]) cur_nbs[i] = 0 room.current_numbers = cur_nbs room.save() renew_room_last_update(room_id) return cards
def get_settings(room_id): room = Room.objects(Q(name=room_id)).first() return room.settings
def set_settings(room_id, settings): room = Room.objects(Q(name=room_id)).first() room.settings = settings room.save()
def close_room(room_id): room = Room.objects(Q(name=room_id)).first() room.delete() log.info('Close game room: ' + str(room_id))
def increase_clients(room_id): room = Room.objects(Q(name=room_id['room'])).first() room.clients.append(room_id['client']) log.info('Join game room: ' + str(room_id['room']) + ". Current clients: " + str(room.clients)) room.save()
def generate_room_id(): id = random.randint(0, 100000000) while Room.objects(Q(name=id)).first() is not None: id = random.randint(0, 100000000) return id
def delete_old_rooms(): now = datetime.datetime.utcnow() for room in Room.objects(): if ((now - room.last_update).total_seconds()) > float(TIME_TO_LIVE): room.delete()
def renew_room_last_update(room_id): room = Room.objects(Q(name=room_id)).first() room.last_update = datetime.datetime.utcnow room.save()
def room_exists(room_id): if Room.objects(Q(name=room_id)).first() is not None: return True else: return False