Exemple #1
0
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
            })
Exemple #2
0
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
Exemple #3
0
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()
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
def get_settings(room_id):
    room = Room.objects(Q(name=room_id)).first()
    return room.settings
Exemple #7
0
def set_settings(room_id, settings):
    room = Room.objects(Q(name=room_id)).first()
    room.settings = settings
    room.save()
Exemple #8
0
def close_room(room_id):
    room = Room.objects(Q(name=room_id)).first()
    room.delete()
    log.info('Close game room: ' + str(room_id))
Exemple #9
0
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()
Exemple #10
0
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
Exemple #11
0
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()
Exemple #12
0
def renew_room_last_update(room_id):
    room = Room.objects(Q(name=room_id)).first()
    room.last_update = datetime.datetime.utcnow
    room.save()
Exemple #13
0
def room_exists(room_id):
    if Room.objects(Q(name=room_id)).first() is not None:
        return True
    else:
        return False