jonpreamble commited on
Commit
9ad6c07
·
1 Parent(s): a7342c4

Fix random.choice only being run once per reboot

Browse files
Files changed (2) hide show
  1. app.py +14 -8
  2. game_content.py +6 -2
app.py CHANGED
@@ -3,6 +3,7 @@
3
  import os, random
4
  import openai
5
  import gradio as gr
 
6
  from game_content import GAME_INTRO_CHOICES, NOTES_TO_THE_NARRATOR_AT_START, AWAITING_INPUT, NOTES_TO_THE_NARRATOR_EVERY_TIME
7
  from game_content import game_over_victory_txt, game_over_fail_txt, S_GAME_OVER
8
  from game_content import N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER, N_TURNS_REQUIRED_TO_REACH_HOME
@@ -63,7 +64,17 @@ def run_1_game_turn(s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_u
63
 
64
  finally_add2_both_tscripts = ""
65
 
66
- if s_user_input == "":
 
 
 
 
 
 
 
 
 
 
67
  s_user_transcript += "You must choose an action.\n"
68
 
69
  elif S_GAME_OVER in s_narr_transcript:
@@ -131,15 +142,10 @@ openai.api_key = os.environ.get("OPENAI_KEY")
131
  demo = gr.Blocks()
132
 
133
  with demo:
134
- game_intro = random.choice(GAME_INTRO_CHOICES)
135
-
136
- s_narr_transcript = game_intro + NOTES_TO_THE_NARRATOR_AT_START + AWAITING_INPUT
137
- s_user_transcript = game_intro + AWAITING_INPUT
138
-
139
  gr_top_of_scr_padding = gr.HTML(TOP_OF_SCREEN_PADDING_DIV)
140
 
141
- gr_narr_transcript = gr.Textbox(label="", value=s_narr_transcript, interactive=False, max_lines=9999, visible=False)
142
- gr_user_transcript = gr.Textbox(label="", value=s_user_transcript, interactive=False, max_lines=9999, elem_classes="parleygame")
143
 
144
  gr_pls_be_patient = gr.HTML(PLEASE_BE_PATIENT_DIV)
145
 
 
3
  import os, random
4
  import openai
5
  import gradio as gr
6
+ from game_content import INITIAL_WELCOME_TEXT
7
  from game_content import GAME_INTRO_CHOICES, NOTES_TO_THE_NARRATOR_AT_START, AWAITING_INPUT, NOTES_TO_THE_NARRATOR_EVERY_TIME
8
  from game_content import game_over_victory_txt, game_over_fail_txt, S_GAME_OVER
9
  from game_content import N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER, N_TURNS_REQUIRED_TO_REACH_HOME
 
64
 
65
  finally_add2_both_tscripts = ""
66
 
67
+ if n_turns_elapsed == 0 and s_narr_transcript == "":
68
+ # New game.
69
+ # I learned the hard way that we have to do the random choice thing from within the game code;
70
+ # because if we just put it inside the "with demo:" block,
71
+ # then the gradio server only runs the random choice once per reboot.
72
+ game_intro = random.choice(GAME_INTRO_CHOICES)
73
+ s_narr_transcript = game_intro + NOTES_TO_THE_NARRATOR_AT_START
74
+ s_user_transcript = game_intro
75
+
76
+
77
+ elif s_user_input == "":
78
  s_user_transcript += "You must choose an action.\n"
79
 
80
  elif S_GAME_OVER in s_narr_transcript:
 
142
  demo = gr.Blocks()
143
 
144
  with demo:
 
 
 
 
 
145
  gr_top_of_scr_padding = gr.HTML(TOP_OF_SCREEN_PADDING_DIV)
146
 
147
+ gr_narr_transcript = gr.Textbox(label="", value="", interactive=False, max_lines=9999, visible=False)
148
+ gr_user_transcript = gr.Textbox(label="", value=INITIAL_WELCOME_TEXT, interactive=False, max_lines=9999, elem_classes="parleygame")
149
 
150
  gr_pls_be_patient = gr.HTML(PLEASE_BE_PATIENT_DIV)
151
 
game_content.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  GAME_HEADER = """----------------------------------- ------------------ ----------------
2
  - ----- ----- -
3
  - PARLEY - -
@@ -11,14 +15,14 @@ GAME_HEADER = """----------------------------------- ------------------ -------
11
 
12
  """
13
 
14
- GAME_INTRO_CHOICES = []
15
-
16
  # Generating a new scenario is an automatic process but takes something like 2-8 minutes.
17
  # Thus, I used the command line version of the game
18
  # https://github.com/uart-byte/parley/tree/working-cmd-line
19
  # to pre-generate 20 scenarios.
20
  # That way the web version of the game can load instantly
21
 
 
 
22
  # Intro_1.txt
23
  GAME_INTRO_CHOICES.append(
24
  GAME_HEADER
 
1
+ INITIAL_WELCOME_TEXT = """Welcome to Parley - the role playing game where you negotiate with bandits!
2
+ To begin, type "begin" into the text input box below, then click the Run Next Turn button:"""
3
+
4
+
5
  GAME_HEADER = """----------------------------------- ------------------ ----------------
6
  - ----- ----- -
7
  - PARLEY - -
 
15
 
16
  """
17
 
 
 
18
  # Generating a new scenario is an automatic process but takes something like 2-8 minutes.
19
  # Thus, I used the command line version of the game
20
  # https://github.com/uart-byte/parley/tree/working-cmd-line
21
  # to pre-generate 20 scenarios.
22
  # That way the web version of the game can load instantly
23
 
24
+ GAME_INTRO_CHOICES = []
25
+
26
  # Intro_1.txt
27
  GAME_INTRO_CHOICES.append(
28
  GAME_HEADER