Example #1
0
def delete_collection(user, collection_id):
    collection = Collection.query.filter_by(id=collection_id).first()

    if collection is None:
        res = {'message': 'Collection not found'}

        return jsonify(res), 404

    else:
        if collection.created_by != user.id:
            res = {'message': 'You do not own this collection'}

            return jsonify(res), 401

        else:
            db.session.delete(collection)
            db.session.commit()

            res = {
                'message':
                'Collection deleted',
                'collection':
                collection_share_schema.dump(
                    Collection.query.filter_by(id=collection_id).first())
            }

            return jsonify(res), 200
Example #2
0
def join_room(user, room_code):
    room = Room.query.filter_by(code=room_code).first()

    if room is None or room.status == 'inactive':
        return jsonify({'message': 'Room not found'}), 404

    if room.status == 'active':
        return jsonify({'message': 'Room is currently hosting a game'}), 422

    else:
        if len(room.users) == 8:
            return jsonify({'message': 'Room is full'}), 422

        existing_association = RoomAssociation.query.filter_by(
            user_id=user.id).first()

        # If user belongs to a room, remove him from there
        if existing_association is not None:
            Room.query.filter_by(
                id=existing_association.room_id).first().remove_user(user.id)

        room.add_user(user.id)

        room_data = room_share_schema.dump(room)
        room_data['collection'] = collection_share_schema.dump(room.collection)
        room_data['users'] = users_share_schema.dump(room.users)
        room_data['game'] = room.load_game()

        res = {'data': room_data}

        return jsonify(res), 200
Example #3
0
    def init_game_data(self, collection):
        game_data = {
            'state': 'Zero',
            'round_number': 1,
            'collection': collection_share_schema.dump(collection),
            'all_cards': [],
            'white_cards': [],
            'black_cards': [],
            'discarded_cards': [],
            'table_card': None,
            'players': [],
            'selected_cards': [],
            'czar_id': None,
            'round_winner': None,
            'all_players_ready': False,
            'game_winner': None,
            'max_points': self.max_points
        }

        users_in_room = self.room.users

        for user in users_in_room:
            user_dict = user_share_schema.dump(user)
            game_data['players'].append({
                'data': user_dict,
                'hand': [],
                'score': 0,
                'is_ready': False
            })

        self.room.status = 'active'
        self.set_game_data(game_data)
Example #4
0
def test_restart_game(test_client, init_game_db):
    room = Room.query.first()

    room.create_new_game(10)
    room.start_game()

    game = room.load_game()
    game_data = game.load_game_data()

    assert game_data['state'] == 'Selecting'
    assert game_data['collection'] == collection_share_schema.dump(
        room.collection)
    assert len(game_data['all_cards']) > 0
    assert len(game_data['white_cards']) > 0
    assert len(game_data['black_cards']) > 0
    assert game_data['table_card'] is not None
    assert len(game_data['discarded_cards']) == 22
    assert len(game_data['players'][0]['hand']) == 7
    assert len(game_data['players'][1]['hand']) == 7
    assert len(game_data['players'][2]['hand']) == 7
    assert game_data['players'][0]['score'] == 0
    assert game_data['players'][1]['score'] == 0
    assert game_data['players'][2]['score'] == 0
    assert game_data['selected_cards'] == []
    assert game_data['czar_id'] is not None
    assert game_data['round_winner'] is None
    assert game_data['all_players_ready'] == False
    assert game_data['game_winner'] is None

    assert room.status == 'active'
Example #5
0
def get_user_collections(user):
    collections = user.collections

    data = []

    for e in collections:
        e = collection_share_schema.dump(e)

        data.append(e)

    res = {'collections': data}

    return jsonify(res)
Example #6
0
def create_collection(user):
    body = request.get_json()

    new_collection = Collection(name=body['name'], created_by=user.id)
    db.session.add(new_collection)
    db.session.commit()

    new_collection.set_owner(user.id)

    res = {
        'collection': {
            'data': collection_share_schema.dump(new_collection),
            'cards': []
        }
    }

    return jsonify(res), 201
Example #7
0
def add_card_to_collection(user, card_id, collection_id):
    card = Card.query.filter_by(id=card_id).first()
    collection = Collection.query.filter_by(id=collection_id).first()
    existing_association = CardAssociation.query.filter_by(
        card_id=card_id, collection_id=collection_id).first()

    if card is None:
        res = {'message': 'Card not found'}

        return jsonify(res), 404

    elif collection is None:
        res = {'message': 'Collection not found'}

        return jsonify(res), 404

    elif existing_association is not None:
        res = {'message': 'Card already belongs to this collection'}

        return jsonify(res), 409

    else:
        if collection.created_by != user.id:
            res = {'message': 'You do not own this collection'}

            return jsonify(res), 403

        else:
            existing_association
            new_association = CardAssociation(card_id=card.id,
                                              collection_id=collection.id)

            db.session.add(new_association)
            db.session.commit()

            collection_response = collection_share_schema.dump(collection)
            collection_response['cards'] = cards_share_schema.dump(
                collection.cards)

            res = {
                'message': 'Card added to collection',
                'collection': collection_response
            }

            return jsonify(res)
Example #8
0
def create_owned_collection(user, collection_id):
    if user_owns_collection(user.id, collection_id) is True:
        return jsonify({'message': 'You already own this collection'}), 422

    collection = Collection.query.filter_by(id=collection_id).first()

    if collection is None:
        return jsonify({'message': 'Collection not found'}), 404

    owned_collection = OwnedCollection(user_id=user.id,
                                       collection_id=collection_id)

    db.session.add(owned_collection)
    db.session.commit()

    res = {'data': collection_share_schema.dump(collection)}

    return jsonify(res), 201
Example #9
0
def get_collection_info(user, collection_id):
    collection = Collection.query.filter_by(id=collection_id).first()

    if collection is None:
        res = {'message': 'Collection not found'}

        return jsonify(res), 404

    if user_owns_collection(user.id, collection.id) is False:
        res = {'message': 'You do not own this collection'}

        return jsonify(res), 403

    collection_obj = collection_share_schema.dump(collection)
    collection_obj['cards'] = cards_share_schema.dump(collection.cards)

    res = {'collection': collection_obj}

    return jsonify(res)
Example #10
0
def create_room(user):
    body = request.get_json()

    if body['code'] and body['collection_id']:
        collection = Collection.query.filter_by(
            id=body['collection_id']).first()

        if collection is None:
            return jsonify({'message': 'Collection not found'}), 404

        existing_active_room = Room.query.filter_by(code=body['code'],
                                                    status='active').first()

        existing_waiting_room = Room.query.filter_by(code=body['code'],
                                                     status='waiting').first()

        if existing_active_room is not None or existing_waiting_room is not None:
            res = {'message': f"Room '{body['code']}' is already in use"}

            return jsonify(res), 403

        existing_association = RoomAssociation.query.filter_by(
            user_id=user.id).first()

        # If user belongs to a room, remove him from there
        if existing_association is not None:
            Room.query.filter_by(
                id=existing_association.room_id).first().remove_user(user.id)

        new_room = Room.query.filter_by(code=body['code'],
                                        status='inactive').first()

        if new_room is None:
            new_room = Room(code=body['code'],
                            created_by=user.id,
                            status='waiting',
                            collection_id=body['collection_id'])

            db.session.add(new_room)
            db.session.commit()

        else:
            new_room.created_by = user.id
            new_room.status = 'waiting'

        new_join = RoomAssociation(user_id=user.id, room_id=new_room.id)

        db.session.add(new_join)
        db.session.commit()

        room_data = room_share_schema.dump(new_room)
        room_data['collection'] = collection_share_schema.dump(
            new_room.collection)
        room_data['users'] = users_share_schema.dump(new_room.users)
        room_data['game'] = None

        res = {'data': room_data}

        return jsonify(res)

    else:
        return jsonify({'message': 'Missing data in body'}), 422
Example #11
0
def test_init_game_data(test_client, init_game_db, token):
    room = Room.query.first()
    collection = Collection.query.filter_by(id=room.collection_id).first()

    room.create_new_game(15)
    game = room.load_game()

    game.init_game_data(collection)
    game_data = game.load_game_data()

    assert game_data['state'] == 'Zero'
    assert game_data['round_number'] == 1
    assert game_data['collection'] == collection_share_schema.dump(collection)
    assert len(game_data['all_cards']) == 0
    assert len(game_data['white_cards']) == 0
    assert len(game_data['black_cards']) == 0
    assert game_data['discarded_cards'] == []
    assert game_data['table_card'] is None
    assert game_data['players'] == [{
        'data': {
            'id': 1,
            'name': 'user_1',
            'username': None,
            'profile_img': None,
            'source': 'cardin',
            'profile_color': '#000000'
        },
        'hand': [],
        'score': 0,
        'is_ready': False
    }, {
        'data': {
            'id': 2,
            'name': 'user_2',
            'username': None,
            'profile_img': None,
            'source': 'cardin',
            'profile_color': '#000000'
        },
        'hand': [],
        'score': 0,
        'is_ready': False
    }, {
        'data': {
            'id': 3,
            'name': 'user_3',
            'username': None,
            'profile_img': None,
            'source': 'cardin',
            'profile_color': '#000000'
        },
        'hand': [],
        'score': 0,
        'is_ready': False
    }]
    assert game_data['selected_cards'] == []
    assert game_data['czar_id'] is None
    assert game_data['round_winner'] is None
    assert game_data['all_players_ready'] == False
    assert game_data['game_winner'] is None

    assert room.status == 'active'