Added a game over control to lock game and reset

This commit is contained in:
Carlos Bazaga
2025-09-05 22:51:21 +02:00
parent 97967e33e9
commit 3ee1b67042

View File

@@ -3,6 +3,7 @@
from typing import NamedTuple
import gradio as gr
from logging import getLogger
# Define interface's configuration class.
@@ -39,14 +40,55 @@ def get_interface(submit_function, config: Interface_Config):
# Player's command.
user_input = gr.Textbox(
label=config.input_label, placeholder=config.input_command)
user_input.submit(
fn=submit_function,
inputs=[user_input, history_state],
outputs=[scene_image, description_box, history_state, user_input])
# Submit button.
submit_btn = gr.Button(config.input_button)
# Define Game Over control.
def _reset_game():
"""Return Initial values for game restart."""
return (config.start_img, config.start_scene, [], '',
gr.update(interactive=True),
gr.update(value=config.input_button))
def _game_over(scene, response):
"""Return Game Over values, blocking input field."""
return (scene, response, [], config.game_over_field,
gr.update(interactive=False),
gr.update(value=config.game_over_label))
def game_over_wrap(message, history, button_label):
"""Check Game over status Before and After Storyteller call."""
# Check game over before.
print(button_label)
print(config.game_over_label)
if button_label == config.game_over_label:
_logger.warning('GAME OVER STATUS. RESTARTING...')
return _reset_game()
# Call Storyteller.
scene, response, history, input = submit_function(message, history)
_logger.warning(response)
# Check game over after.
if response.game_over:
_logger.info('GAME OVER AFTER MOVE. LOCKING.')
return _game_over(scene, response)
# Return Storyteller response.
return scene, response, history, input, gr.update(), gr.update()
# Assign function to button click event.
submit_btn.click(
fn=submit_function,
inputs=[user_input, history_state],
outputs=[scene_image, description_box, history_state, user_input])
fn=game_over_wrap,
inputs=[user_input, history_state, submit_btn],
outputs=[scene_image, description_box, history_state, user_input,
user_input, submit_btn])
# Assign function to input submit event. (Press enter)
user_input.submit(
fn=game_over_wrap,
inputs=[user_input, history_state, submit_btn],
outputs=[scene_image, description_box, history_state, user_input,
user_input, submit_btn])
return ui
_logger = getLogger(__name__)