File size: 2,675 Bytes
1532c35
 
a88f931
7fe9bd6
a88f931
 
6d4a32a
 
 
 
 
 
 
7fe9bd6
a88f931
09c334f
a88f931
 
6d4a32a
 
1532c35
 
 
 
 
 
 
 
 
 
 
 
 
7fe9bd6
 
 
a88f931
 
 
 
 
 
 
 
 
7fe9bd6
 
 
1532c35
a88f931
 
7fe9bd6
1532c35
a88f931
 
 
1532c35
7fe9bd6
 
 
a88f931
 
 
7fe9bd6
 
 
 
 
 
 
 
 
a88f931
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import logging

from src.constants import END_WORD_NUMBER
from src.constants import MAX_ATTEMPTS
from src.constants import STARTING_INDEX
from src.handlers import handle_both_wrong
from src.handlers import handle_lm_win
from src.handlers import handle_next_word
from src.handlers import handle_no_input
from src.handlers import handle_out_of_attempts
from src.handlers import handle_player_win
from src.handlers import handle_tie
from src.params import ReducerParams
from src.shared import INITIAL_STATE
from src.shared import token_predictions
from src.shared import tokenizer
from src.utils import get_current_lm_guess_str
from src.utils import get_current_prompt_text
from src.utils import guess_is_correct
from src.utils import lm_is_correct

logger = logging.getLogger(__name__)


def handle_guess(*args) -> ReducerParams:
    """
    Given current state of interface, updates program to next state
    """
    params = ReducerParams(*args)
    return _handle_guess(params)


def _handle_guess(params: ReducerParams) -> ReducerParams:
    if params.button_label == "Restart":
        return INITIAL_STATE

    if params.word_number >= END_WORD_NUMBER:
        # FIXME: BAD PRACTICE
        if params.player_points > params.lm_points:
            s = "YOU WIN"
        elif params.player_points < params.lm_points:
            s = "YOU LOSE"
        else:
            s = "CAN YOU BELIEVE IT!? A TIE"

        params.button_label = "Restart"
        params.bottom_html = s
        return params

    logger.debug(params)
    logger.debug(token_predictions[STARTING_INDEX + params.word_number])

    if params.button_label == "Next word":
        params = handle_next_word(params)
    elif not params.guess_field:
        params = handle_no_input(params)
    else:
        params.current_guesses += (
            params.guess_field if params.remaining_attempts == MAX_ATTEMPTS else "\n" + params.guess_field
        )
        player_correct = guess_is_correct(params, tokenizer)
        lm_correct = lm_is_correct(params)

        params.remaining_attempts -= 1
        if player_correct and lm_correct:
            params = handle_tie(params)
        elif player_correct and not lm_correct:
            params = handle_player_win(params)
        elif lm_correct and not player_correct:
            params = handle_lm_win(params)
        elif params.remaining_attempts > 0:
            params = handle_both_wrong(params)
        else:
            params = handle_out_of_attempts(params)

    params.lm_guesses = get_current_lm_guess_str(params.word_number, params.remaining_attempts)
    params.prompt_text = get_current_prompt_text(params.word_number)

    return params