Пример #1
0
def test_npc_interaction():
    """
      start
        |
        w1 <- dialog becomes available
        |
        end
    """
    game = Game('test')
    start = Waypoint(game.graph, 'start')
    w1 = Waypoint(game.graph, 'w1')
    end = Waypoint(game.graph, 'end')
    start.add_destination(w1)
    w1.add_destination(end)
    game.set_start(start)
    npc_dialog = Dialog()
    ds = Mail(npc_dialog.graph, 'test subject', 'test body')
    with pytest.raises(PlayerStateException):
        NonPlayableCharacter('test', 'npc', npc_dialog)
    npc_dialog.set_start(ds)
    npc_dialog.start.waypoints.append(w1)
    game.add_non_playable_character(NonPlayableCharacter('test', 'npc', npc_dialog))
    instance = game.create_new_game()
    instance.add_player(Player('test', 'player'), 'testy', 'mctestpants')
    assert len(instance.npc_states) == 1
    assert ds not in [k for k, _ in instance.player_states[0].inventory.values()]
    interactions = instance.player_states[0].move_to(w1)
    assert len(interactions.keys()) == 1 and len(interactions.values()) == 1
Пример #2
0
def test_create_and_delete_game(wait_for_api, create_clean_db, client):
    # create our game
    game = Game('test')
    start = Waypoint(game.graph, 'start')
    w1 = Waypoint(game.graph, 'w1')
    w2 = Waypoint(game.graph, 'w2', items=['some item'])
    end = Waypoint(game.graph, 'end')
    start.add_destination(w1)
    task = Task(w2, 'test description', 'test text', 'answer')
    w1.add_task(task)
    w2.add_destination(end)
    game.set_start(start)
    # create user
    lg = client.post(
        f"{ROOT_URL}/register", json=default_account
    )
    assert lg.status_code == 201
    token = json.loads(lg.data)
    # convert it to an API object
    game_data = generate_api_game(game)
    lg = client.post(
        f"{ROOT_URL}/games", json=game_data, headers={'X-TOKEN': token}
    )
    assert lg.status_code == 201
    lg = client.get(
        f"{ROOT_URL}/games"
    )
    assert lg.status_code == 200
    assert game.title in [g['title'] for g in lg.data]
Пример #3
0
def test_simple_graph_generation():
    game = Game('test')
    start = Waypoint(game.graph, 'start')
    end = Waypoint(game.graph, 'end')
    start.add_destination(end)
    assert len(game.graph.nodes) == 2
    assert len(game.graph.edges) == 1
    assert not game.start_is_set()
Пример #4
0
def test_identify_player_path():
    """
    start
      |
      w1
      |  <- task answer required before transition becomes available
      w2
      |
      end
    """
    game = Game('test')
    start = Waypoint(game.graph, 'start')
    w1 = Waypoint(game.graph, 'w1')
    w2 = Waypoint(game.graph, 'w2', items=['some item'])
    end = Waypoint(game.graph, 'end')
    start.add_destination(w1)
    task = Task(w2, 'test description', 'test text', 'answer')
    w1.add_task(task)
    w2.add_destination(end)
    game.set_start(start)
    instance = game.create_new_game()
    player = Player('test', 'player')
    instance.add_player(player, 'testy', 'mctestpants')
    assert len(instance.player_states[0].path) == 1
    assert instance.player_states[0].path[0][1] == start
    assert w1 in instance.player_states[0].available_moves()
    assert w2 in instance.player_states[0].available_path()
    # not allowed to skip to end
    with pytest.raises(PlayerIllegalMoveException):
        instance.player_states[0].move_to(end)
    instance.player_states[0].move_to(w1)
    # no backsies
    with pytest.raises(PlayerIllegalMoveException):
        instance.player_states[0].move_to(start)
    # w2 is blocked from w1
    with pytest.raises(PlayerIllegalMoveException):
        instance.player_states[0].move_to(w2)
    answer = 'answer'
    assert len(instance.player_states[0].inventory) == 0
    instance.player_states[0].move_to(w2, answer)
    assert not instance.player_states[0].is_finished()
    assert len(instance.player_states[0].inventory) == 1
    instance.player_states[0].move_to(end)
    assert instance.player_states[0].current_position() == end
    assert instance.player_states[0].is_finished()
Пример #5
0
def test_npc_required_response():
    """
      start
        |
        w1 <- dialog becomes available
        |
        end <- blocked till dialog in phase 2 (ds2)
    """
    game = Game('test')
    start = Waypoint(game.graph, 'start')
    w1 = Waypoint(game.graph, 'w1')
    end = Waypoint(game.graph, 'end')
    game.set_start(start)
    npc_dialog = Dialog()
    ds1 = Mail(npc_dialog.graph, 'test subject 1', 'test body 1')
    ds1.waypoints.append(w1)
    start.add_destination(w1)
    npc_dialog.set_start(ds1)
    ds2 = Mail(npc_dialog.graph, 'test subject 2', 'test body 2', items=['test item'], destination=end)
    ds1.add_follow_up(ds2, end)
    w1.add_interaction(ds2)
    npc = NonPlayableCharacter('test', 'npc', npc_dialog)
    game.add_non_playable_character(npc)
    instance = game.create_new_game()
    instance.add_player(Player('test', 'player'), 'testy', 'mctestpants')
    assert len(instance.player_states[0].available_moves()) == 1
    interactions = instance.player_states[0].move_to(w1)
    assert len(instance.player_states[0].available_moves()) == 0
    npc_instance = next(iter([n for n in instance.npc_states if n == npc]), None)
    if not npc_instance:
        assert fail('could not find npc instance in game instance')
    assert len(instance.player_states[0].inventory) == 0
    npc_instance.update_player_dialog(instance.player_states[0], interactions[npc], 'mail response')
    assert len(instance.player_states[0].inventory) == 1
    assert len(instance.player_states[0].available_moves()) == 1
    assert instance.player_states[0].move_to(end)
Пример #6
0
def game():
    """
                  start  (path with dialog)
                  /   \
               w1 (m1) w2 (d1)
                  \   /
                   knot1
                 /  |  \
                w3  w4 w5  <- with task t1,t2,t3
                \   |
                  end <- task from dialog td1
    :return: game with described graph
    """
    #TODO, check interaction model in waypoint
    dialog = Dialog()
    dialog_start = Mail(dialog.graph, 'start sub', 'start body')
    m1 = Mail(dialog.graph, 'second sub', 'second body')
    td1 = Task(None, 'some task 1', 'some task', 'some solution')
    d1 = Speech(dialog.graph, 'content', task=td1)

    game = Game('test')
    start = Waypoint(game.graph, 'start')
    w1 = Waypoint(game.graph, 'w1')
    w2 = Waypoint(game.graph, 'w2')
    knot = Waypoint(game.graph, 'knot')
    w3 = Waypoint(game.graph, 'w3')
    t1 = Task(w3, 'some task 1', 'some task')
    w4 = Waypoint(game.graph, 'w4')
    t2 = Task(w4, 'some task 2', 'some task')
    w5 = Waypoint(game.graph, 'w5')
    t3 = Task(w5, 'some task 3', 'some task')
    end = Waypoint(game.graph, 'end')
    start.add_destination(w1, 1.1)
    start.add_destination(w2)
    w1.add_destination(knot)
    w2.add_destination(knot)
    knot.add_task(t1)
    knot.add_task(t2)
    knot.add_task(t3)
    w3.add_destination(end)
    w4.add_destination(end)
    game.set_start(start)

    dialog_end = Speech(dialog.graph, 'next content', destination=end)
    dialog.set_start(dialog_start)
    dialog_start.add_follow_up(d1)
    dialog_start.add_follow_up(m1)
    dialog_start.waypoints.append(start)
    d1.add_follow_up(dialog_end, None)

    game.add_non_playable_character(NonPlayableCharacter(
        'bob', 'test', dialog))

    return game