Added a game over control to lock game and reset
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
|
from logging import getLogger
|
||||||
|
|
||||||
|
|
||||||
# Define interface's configuration class.
|
# Define interface's configuration class.
|
||||||
@@ -39,14 +40,55 @@ def get_interface(submit_function, config: Interface_Config):
|
|||||||
# Player's command.
|
# Player's command.
|
||||||
user_input = gr.Textbox(
|
user_input = gr.Textbox(
|
||||||
label=config.input_label, placeholder=config.input_command)
|
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 button.
|
||||||
submit_btn = gr.Button(config.input_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(
|
submit_btn.click(
|
||||||
fn=submit_function,
|
fn=game_over_wrap,
|
||||||
inputs=[user_input, history_state],
|
inputs=[user_input, history_state, submit_btn],
|
||||||
outputs=[scene_image, description_box, history_state, user_input])
|
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
|
return ui
|
||||||
|
|
||||||
|
|
||||||
|
_logger = getLogger(__name__)
|
||||||
|
|||||||
Reference in New Issue
Block a user