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