Exemple #1
0
def run_loop(
    stdscr,
    text,
    force_perfect=True,
    replay_tt=None,
    instant_death=False,
    target_wpm=None,
):
    """Run curses loop - actual implementation."""
    tt = TypedText(text)
    writer = TypedTextWriter(tt,
                             stdscr,
                             replay_tt=replay_tt,
                             target_wpm=target_wpm)
    stdscr.nodelay(1)  # makes getch non-blocking

    while not tt.check_finished(force_perfect=force_perfect):
        writer.render()

        writer.process_character()

        if instant_death and tt.n_wrong_characters > 0:
            tt.end_ts = tt.start_ts
            break

    return tt
Exemple #2
0
def test_time_per_character():
    text = "hello"
    tt = TypedText(text)

    tt.type_character(0, "h")  # first caracter does not count
    tt.type_character(1, "e")
    tt.type_character(2, "l")
    tt.type_character(3, "l")
    tt.type_character(4, "m")  # mistake
    tt.type_character(4)  # backspace

    stats = times_per_character(tt)

    assert len(stats) == 2
    assert {"e", "l"} == set(stats.keys())
    assert len(stats["e"]) == 1
    assert len(stats["l"]) == 2
def test_strip(monkeypatch):
    fake_curses = Mock()
    tt = TypedText("Hello")
    fake_curses.wrapper.return_value = tt
    monkeypatch.setattr("mltype.interactive.curses", fake_curses)

    fake_text = MagicMock(spec=str)

    main_basic(fake_text, None, None, None, None)
    fake_text.rstrip.assert_called_once()
Exemple #4
0
def main_replay(replay_file, force_perfect, overwrite, instant_death,
                target_wpm):
    """Run main curses loop with a replay.

    Parameters
    ----------
    force_perfect : bool
        If True, then one cannot finish typing before all characters
        are typed without any mistakes.


    overwrite : bool
        If True, the replay file will be overwritten in case
        we are faster than it.

    replay_file : str or pathlib.Path
        Typed text in this file from some previous game.

    instant_death : bool
        If active, the first mistake will end the game.

    target_wpm : None or int
        The desired speed to be shown as guide.
    """
    replay_file = pathlib.Path(replay_file)
    replay_tt = TypedText.load(replay_file)

    tt = curses.wrapper(
        run_loop,
        replay_tt.text,
        force_perfect=force_perfect,
        replay_tt=replay_tt,
        instant_death=instant_death,
        target_wpm=target_wpm,
    )

    wpm_replay = replay_tt.compute_wpm()
    wpm_new = tt.compute_wpm()

    with print_section(" Statistics ", fill_char="=", add_ts=False):
        print(f"Old WPM: {wpm_replay:.1f}\nNew WPM: {wpm_new:.1f}")

        if wpm_new > wpm_replay:
            print("Congratulations!")
            if overwrite:
                print("Updating the checkpoint file")
                tt.save(replay_file)
        else:
            print("You lost!")
Exemple #5
0
    def test_basic(self):
        tt = TypedText("hello")

        assert tt == tt
        assert tt != "wrong type"

        # nothing typed
        assert tt.elapsed_seconds == 0

        assert tt.n_characters == 5
        assert tt.n_correct_characters == 0
        assert tt.n_untouched_characters == 5
        assert tt.n_backspace_characters == 0
        assert tt.n_wrong_characters == 0

        assert tt.n_actions == 0
        assert tt.n_backspace_actions == 0

        assert tt.compute_accuracy() == 0

        assert not tt.check_finished()
        assert not tt.unroll_actions()

        # type start
        tt.type_character(0, "h")
        tt.type_character(1, "w")
        tt.type_character(1, None)
        tt.type_character(1, "e")

        assert tt.elapsed_seconds > 0
        assert tt.elapsed_seconds != tt.elapsed_seconds

        assert tt.n_characters == 5
        assert tt.n_correct_characters == 2
        assert tt.n_untouched_characters == 3
        assert tt.n_backspace_characters == 0
        assert tt.n_wrong_characters == 0

        assert tt.n_actions == 4
        assert tt.n_backspace_actions == 1

        assert tt.compute_accuracy() == 2 / 3

        assert not tt.check_finished()
        assert len(tt.unroll_actions()) == 4

        # finish typing
        tt.type_character(2, "l")
        tt.type_character(3, "l")
        tt.type_character(4, "a")
        tt.type_character(4)
        tt.type_character(4, "o")

        assert tt.elapsed_seconds == tt.elapsed_seconds > 0

        assert tt.n_characters == 5
        assert tt.n_correct_characters == 5
        assert tt.n_untouched_characters == 0
        assert tt.n_backspace_characters == 0
        assert tt.n_wrong_characters == 0

        assert tt.n_actions == 4 + 5
        assert tt.n_backspace_actions == 2

        assert tt.compute_accuracy() == 5 / 7

        assert tt.check_finished()
        assert tt.check_finished(force_perfect=False)

        assert tt == tt

        ua = tt.unroll_actions()
        assert len(ua) == 9
        assert ua[0][0] == 0
        assert ua[-1][0] == 4

        with pytest.raises(IndexError):
            tt.type_character(5)
Exemple #6
0
    def test_save_and_load(self, tmpdir):
        path_dir = pathlib.Path(str(tmpdir))
        path_file = path_dir / "cool.rlt"

        tt = TypedText("Hello")

        # finish typing
        tt.type_character(0, "H")
        tt.type_character(1, "w")
        tt.type_character(1)
        tt.type_character(1, "e")
        tt.type_character(2, "l")
        tt.type_character(3, "l")

        tt.save(path_file)
        assert tt == TypedText.load(path_file)