示例#1
0
def test_miss_turn_handler(app, test_client, db, make_players,
                           make_socketio_test_client, get_id, room_manager,
                           make_custom_player):
    test_client.get('/')
    test_client.get('/room?id=' + get_id)
    test_client_sockets = make_socketio_test_client(app, test_client)
    test_client_sockets.emit('join', (get_id))

    with pytest.raises(MissTokenError) as info:
        test_client_sockets.emit('miss_turn')

    assert 'Not your turn!' in str(info.value)

    with test_client.session_transaction() as sess:
        player_manager = game_objects.PlayerManager(sess['pers_id'], get_id)

    players = make_players(3)
    for player in players:
        player.playing = True
        db.session.add(player)

    player_manager.player.token_presence = True
    player_manager.player.playing = True

    test_client_sockets.emit('miss_turn')

    with test_client.session_transaction() as sess:
        player_manager = game_objects.PlayerManager(sess['pers_id'], get_id)

    assert player_manager.player.token_presence is False
    assert players[0].token_presence is True
示例#2
0
def set_figure_handler(x, y):
    pm = game_objects.PlayerManager(session['pers_id'], session['room_id'])
    message_list = []
    winner = False

    try:
        winner = pm.set_figure(x, y)
    except ValueError as e:
        message_list = [{-1: str(e)}]
        emit('inf_board_message', message_list)
        raise ValueError(str(e))
    except MissTokenError as e:
        message_list = [{-1: str(e)}]
        emit('inf_board_message', message_list)
        raise MissTokenError(str(e))
    except TwiceRolledDiceError as e:
        message_list = [{-1: str(e)}]
        emit('inf_board_message', message_list)
        raise TwiceRolledDiceError(str(e))

    if winner is not False:
        player_win_event(pm, winner)
    else:
        color = pm.player.order_of_turn
        message_list.append({color: session['username'] + ' set figure'})
        message_list = miss_turn(pm, message_list)

    field = pm.room_manager.game_field.get_json_field()

    emit('refresh_game_field', field, room=session['room_id'])

    send_board_message_and_refresh_score_table(pm.room_manager, message_list)
示例#3
0
def surrender_handler():
    pm = game_objects.PlayerManager(session['pers_id'], session['room_id'])

    message_list = []
    room_status = pm.room_manager.is_playing()

    if room_status:
        try:
            miss_turn(pm)
        except MissTokenError:
            pass

    try:
        winner = pm.surrender()
    except GameDoesNotStartError as e:
        message_list.append({-1: str(e)})
        emit("inf_board_message", message_list)
        raise GameDoesNotStartError('Game doesn`t start yet')

    if winner is not False:
        player_win_event(pm, winner)

    color = pm.player.order_of_turn
    message_list.append({color: pm.player.username + ' is surrendered'})

    send_board_message_and_refresh_score_table(pm.room_manager, message_list)
示例#4
0
    def test_create_player(self, get_id, room_manager, db):
        p_id = str(uuid.uuid4())

        player = Player.query.filter_by(id=p_id, room_id=get_id).first()
        assert player is None
        game_objects.PlayerManager.create_player(p_id, get_id, 'test')
        player = game_objects.PlayerManager(p_id, get_id).player
        assert player is not None
示例#5
0
def miss_turn_handler():
    room_id = session['room_id']
    message_list = []
    pm = game_objects.PlayerManager(session['pers_id'], room_id)

    color = pm.player.order_of_turn
    message = session['username'] + ' miss turn'
    message_list.append({color: message})

    message_list = miss_turn(pm, message_list)

    send_board_message_and_refresh_score_table(pm.room_manager, message_list)
示例#6
0
def join_handler(room_id):
    message_list = []
    session['room_id'] = room_id

    join_room(room_id)

    pm = game_objects.PlayerManager(session['pers_id'], room_id)

    color = pm.player.order_of_turn
    message_list.append({color: session['username'] + ' join room'})

    field = pm.room_manager.game_field.get_json_field()

    emit('set_local_init_data', (color, field, ))
    emit('set_room_status', pm.room_manager.is_playing(), room=room_id)

    send_board_message_and_refresh_score_table(pm.room_manager, message_list)
示例#7
0
def roll_dice_handler():
    # dice_rolled
    room_id = session['room_id']
    pm = game_objects.PlayerManager(session['pers_id'], room_id)
    rm = game_objects.RoomManager(room_id)
    number_of_moves = rm.get_number_of_moves()
    width, height = pm.roll_dice()
    color = pm.player.order_of_turn

    message = (session['username'] + ' has figure with sides: ' +
               str(width) + ' and ' + str(height))

    async_board_message.apply_async((room_id, message, color,
                                     number_of_moves,),
                                    countdown=2)

    emit('dice_rolled', (width, height,))
示例#8
0
def disconnect_handler():
    message_list = []
    room_id = session['room_id']
    winner = False

    pm = game_objects.PlayerManager(session['pers_id'], room_id)
    room_status = pm.room_manager.is_playing()

    color = pm.player.order_of_turn
    message_list.append({color: session['username'] + ' leave room'})
    if room_status:
        try:
            miss_turn(pm)
        except MissTokenError:
            pass

    winner = pm.exit()
    if winner is not False:
        player_win_event(pm, winner)

    send_board_message_and_refresh_score_table(pm.room_manager, message_list)
示例#9
0
def test_disconnect_handler_third(app, db, test_client,
                                  make_socketio_test_client, room_manager,
                                  get_id, make_players):
    # 3) test when game is started and in room 2 players
    test_client.get('/')
    test_client.get('/room?id=' + get_id)
    test_client_sockets = make_socketio_test_client(app, test_client)
    test_client_sockets.emit('join', (get_id))

    room_manager.room.status = 'playing'

    with test_client.session_transaction() as sess:
        player_manager = game_objects.PlayerManager(sess['pers_id'], get_id)
    player_manager.player.ready = True
    player_manager.player.playing = True

    players = make_players(1)
    p_id = players[0].id
    players[0].ready = True
    players[0].playing = True
    db.session.add(players[0])
    db.session.commit()

    test_client_sockets.emit('disconnect')

    received = test_client_sockets.get_received()
    event_names = []
    for el in received:
        event_names.append(el['name'])
    assert 'show_win_window' in event_names
    player = Player.query.filter_by(id=p_id).first()
    assert player.ready is False
    assert player.playing is False
    room = Room.query.filter_by(id=get_id).options(raiseload('*')).first()
    assert room.status == 'waiting'
    with test_client.session_transaction() as sess:
        client_player = (Player.query.filter_by(id=sess['pers_id'],
                                                room_id=get_id).options(
                                                    raiseload('*')).first())
    assert client_player is None
示例#10
0
        def test_delet_room(get_id, room_manager):
            assert room_manager is not None

            player = make_custom_player(score=20,
                                        order_of_turn=0,
                                        ready=True,
                                        playing=True,
                                        surrender=True,
                                        token_presence=False,
                                        dice_has_rolled=False,
                                        last_dice_values=[0, 0],
                                        username='******')
            room_manager.delete_room()

            assert room_manager is not None

            player_manager = game_objects.PlayerManager(player.id, get_id)
            player_manager.exit()

            room_manager.delete_room()

            assert room_manager is None
示例#11
0
def change_ready_status_handler():
    message_list = []
    room_id = session['room_id']

    pm = game_objects.PlayerManager(session['pers_id'], room_id)
    room_manager = game_objects.RoomManager(room_id)

    if room_manager.is_playing():
        message_list.append({-1: 'You cant do this'})
        emit('inf_board_message', message_list)
        raise InappropriateActionError('You can`t do this')

    all_ready = pm.change_ready_status()

    color = pm.player.order_of_turn
    if pm.player.ready is True:
        message_list.append({color: session['username'] + ' is ready'})
    else:
        message_list.append({color: session['username'] + ' is not ready'})

    if all_ready:
        message_list = start_game_event(room_manager, message_list)

    send_board_message_and_refresh_score_table(room_manager, message_list)
示例#12
0
def player_manager(get_id, room_manager, db):
    p_id = str(uuid.uuid4())
    game_objects.PlayerManager.create_player(p_id, get_id, 'test')
    return game_objects.PlayerManager(p_id, get_id)