Spaces:
Runtime error
Runtime error
Commit
·
a135531
1
Parent(s):
b707618
Game is working! Time for some cleanup
Browse files- app.py +42 -24
- game_content.py +24 -24
app.py
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from game_content import GAME_INTRO, NOTES_TO_THE_NARRATOR_AT_START, AWAITING_INPUT
|
| 3 |
from game_content import game_over_victory_txt, game_over_fail_txt, S_GAME_OVER
|
| 4 |
from game_content import N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER, N_TURNS_REQUIRED_TO_REACH_HOME
|
| 5 |
import decider_utils
|
| 6 |
from decider_utils import YES, NO
|
| 7 |
-
from decider_questions import *
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
|
| 12 |
N_COMPLETIONS_WHEN_ELABORATING = 1 # I previously had this set to 3, but that made the program very slow.
|
| 13 |
MINIMUM_COMPLETION_LENGTH_CHARS_WHEN_ELABORATING = 7
|
| 14 |
|
|
|
|
| 15 |
def elaborate(
|
| 16 |
str_beginning,
|
| 17 |
prevent_user_from_reaching_home=True,
|
|
@@ -54,15 +55,6 @@ def elaborate(
|
|
| 54 |
return str_beginning + longest_completion
|
| 55 |
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
def run_1_game_turn(s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_user_input):
|
| 67 |
n_turns_elapsed = int(s_n_turns_elapsed)
|
| 68 |
|
|
@@ -77,44 +69,70 @@ def run_1_game_turn(s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_u
|
|
| 77 |
elif decider_utils.yesno(QUESTION_IS_ACTION_LIKELY_LETHAL, s_user_input, default=NO):
|
| 78 |
finally_add2_both_tscripts += game_over_fail_txt("You have taken an action that is likely to result in killing someone.")
|
| 79 |
|
| 80 |
-
elif decider_utils.yesno(QUESTION_IS_ACTION_RUNNING_AWAY,
|
| 81 |
finally_add2_both_tscripts += "Invalid entry. You cannot outrun these bandits.\n"
|
| 82 |
|
| 83 |
-
elif decider_utils.yesno(QUESTION_IS_ACTION_MAGIC,
|
| 84 |
finally_add2_both_tscripts += "Invalid entry. You are not a spellcaster and have no magic items except your revolver.\n"
|
| 85 |
|
| 86 |
else:
|
| 87 |
# User input accepted.
|
| 88 |
n_turns_elapsed += 1
|
| 89 |
-
|
| 90 |
s_narr_transcript += s_user_input + "\n"
|
| 91 |
s_narr_transcript += NOTES_TO_THE_NARRATOR_EVERY_TIME
|
| 92 |
|
| 93 |
s_new_narr_transcript = elaborate(
|
| 94 |
-
|
| 95 |
prevent_user_from_reaching_home=n_turns_elapsed < N_TURNS_REQUIRED_TO_REACH_HOME,
|
| 96 |
require_user_to_be_still_engaged_with_bandits=n_turns_elapsed < N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER,
|
| 97 |
)
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
| 102 |
s_n_turns_elapsed = str(n_turns_elapsed)
|
| 103 |
s_user_input = ""
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
s_narr_transcript += finally_add2_both_tscripts
|
| 106 |
s_user_transcript += finally_add2_both_tscripts
|
| 107 |
|
| 108 |
return [s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_user_input]
|
| 109 |
|
| 110 |
|
|
|
|
|
|
|
|
|
|
| 111 |
demo = gr.Blocks()
|
| 112 |
|
| 113 |
with demo:
|
| 114 |
s_narr_transcript = GAME_INTRO + NOTES_TO_THE_NARRATOR_AT_START + AWAITING_INPUT
|
| 115 |
s_user_transcript = GAME_INTRO + AWAITING_INPUT
|
| 116 |
|
| 117 |
-
gr_narr_transcript = gr.Textbox(label="", value=s_narr_transcript, interactive=False, max_lines=9999)
|
| 118 |
gr_user_transcript = gr.Textbox(label="", value=s_user_transcript, interactive=False, max_lines=9999)
|
| 119 |
|
| 120 |
gr_markdown1 = gr.Markdown("After clicking Run Next Turn, please be patient as it may take up to a minute for the game state to update.")
|
|
@@ -122,9 +140,9 @@ with demo:
|
|
| 122 |
gr_button1 = gr.Button(value="Run Next Turn")
|
| 123 |
|
| 124 |
gr_n_turns_elapsed = gr.Textbox(label="N Turns Elapsed", value="0", interactive=False)
|
| 125 |
-
|
| 126 |
-
gr_button1.click(
|
| 127 |
-
inputs=[gr_narr_transcript, gr_n_turns_elapsed, gr_user_transcript, gr_user_input],
|
| 128 |
-
|
| 129 |
|
| 130 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
import gradio as gr
|
| 4 |
+
from game_content import GAME_INTRO, NOTES_TO_THE_NARRATOR_AT_START, AWAITING_INPUT, NOTES_TO_THE_NARRATOR_EVERY_TIME
|
| 5 |
from game_content import game_over_victory_txt, game_over_fail_txt, S_GAME_OVER
|
| 6 |
from game_content import N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER, N_TURNS_REQUIRED_TO_REACH_HOME
|
| 7 |
import decider_utils
|
| 8 |
from decider_utils import YES, NO
|
| 9 |
+
from decider_questions import * # QUESTION_IS_USER_HOME, QUESTION_IS_USER_ENGAGED_WITH_BANDITS, etc.
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
N_COMPLETIONS_WHEN_ELABORATING = 1 # I previously had this set to 3, but that made the program very slow.
|
| 13 |
MINIMUM_COMPLETION_LENGTH_CHARS_WHEN_ELABORATING = 7
|
| 14 |
|
| 15 |
+
|
| 16 |
def elaborate(
|
| 17 |
str_beginning,
|
| 18 |
prevent_user_from_reaching_home=True,
|
|
|
|
| 55 |
return str_beginning + longest_completion
|
| 56 |
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
def run_1_game_turn(s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_user_input):
|
| 59 |
n_turns_elapsed = int(s_n_turns_elapsed)
|
| 60 |
|
|
|
|
| 69 |
elif decider_utils.yesno(QUESTION_IS_ACTION_LIKELY_LETHAL, s_user_input, default=NO):
|
| 70 |
finally_add2_both_tscripts += game_over_fail_txt("You have taken an action that is likely to result in killing someone.")
|
| 71 |
|
| 72 |
+
elif decider_utils.yesno(QUESTION_IS_ACTION_RUNNING_AWAY, s_user_input, default=NO):
|
| 73 |
finally_add2_both_tscripts += "Invalid entry. You cannot outrun these bandits.\n"
|
| 74 |
|
| 75 |
+
elif decider_utils.yesno(QUESTION_IS_ACTION_MAGIC, s_user_input, default=NO):
|
| 76 |
finally_add2_both_tscripts += "Invalid entry. You are not a spellcaster and have no magic items except your revolver.\n"
|
| 77 |
|
| 78 |
else:
|
| 79 |
# User input accepted.
|
| 80 |
n_turns_elapsed += 1
|
| 81 |
+
s_user_transcript += s_user_input + "\n"
|
| 82 |
s_narr_transcript += s_user_input + "\n"
|
| 83 |
s_narr_transcript += NOTES_TO_THE_NARRATOR_EVERY_TIME
|
| 84 |
|
| 85 |
s_new_narr_transcript = elaborate(
|
| 86 |
+
s_narr_transcript,
|
| 87 |
prevent_user_from_reaching_home=n_turns_elapsed < N_TURNS_REQUIRED_TO_REACH_HOME,
|
| 88 |
require_user_to_be_still_engaged_with_bandits=n_turns_elapsed < N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER,
|
| 89 |
)
|
| 90 |
|
| 91 |
+
s_new_part = s_new_narr_transcript.replace(s_narr_transcript, "")
|
| 92 |
+
|
| 93 |
+
s_narr_transcript += s_new_part + "\n"
|
| 94 |
+
s_user_transcript += s_new_part + "\n"
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
did_user_kill = decider_utils.yesno(QUESTION_DID_PROTAGONIST_KILL, s_new_part, default=NO)
|
| 98 |
+
did_user_kill = did_user_kill or decider_utils.yesno(QUESTION_DID_PROTAGONIST_KILL, s_narr_transcript, default=NO)
|
| 99 |
+
if did_user_kill:
|
| 100 |
+
finally_add2_both_tscripts += game_over_fail_txt("You have taken a life.")
|
| 101 |
+
|
| 102 |
+
else:
|
| 103 |
+
is_user_home = decider_utils.yesno(QUESTION_IS_USER_HOME, s_narr_transcript, default=NO)
|
| 104 |
+
if is_user_home:
|
| 105 |
+
has_at_least_30_gold = decider_utils.yesno(QUESTION_DOES_USER_STILL_HAVE_AT_LEAST_30_GOLD, s_narr_transcript, default=NO)
|
| 106 |
+
if has_at_least_30_gold:
|
| 107 |
+
finally_add2_both_tscripts += game_over_victory_txt("You made it home with 30+ gold! Your family is grateful and you all hug in celebration.")
|
| 108 |
+
else:
|
| 109 |
+
finally_add2_both_tscripts += game_over_fail_txt("You reached home with less than 30 gold - too little for your family to live on.")
|
| 110 |
|
| 111 |
|
| 112 |
+
# End of code block User input accepted.
|
| 113 |
+
|
| 114 |
s_n_turns_elapsed = str(n_turns_elapsed)
|
| 115 |
s_user_input = ""
|
| 116 |
+
|
| 117 |
+
if S_GAME_OVER not in finally_add2_both_tscripts and S_GAME_OVER not in s_narr_transcript:
|
| 118 |
+
finally_add2_both_tscripts += AWAITING_INPUT
|
| 119 |
+
|
| 120 |
s_narr_transcript += finally_add2_both_tscripts
|
| 121 |
s_user_transcript += finally_add2_both_tscripts
|
| 122 |
|
| 123 |
return [s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_user_input]
|
| 124 |
|
| 125 |
|
| 126 |
+
openai.organization = os.environ.get("OPENAI_ORGANIZATION")
|
| 127 |
+
openai.api_key = os.environ.get("OPENAI_KEY")
|
| 128 |
+
|
| 129 |
demo = gr.Blocks()
|
| 130 |
|
| 131 |
with demo:
|
| 132 |
s_narr_transcript = GAME_INTRO + NOTES_TO_THE_NARRATOR_AT_START + AWAITING_INPUT
|
| 133 |
s_user_transcript = GAME_INTRO + AWAITING_INPUT
|
| 134 |
|
| 135 |
+
gr_narr_transcript = gr.Textbox(label="", value=s_narr_transcript, interactive=False, max_lines=9999) # , visible=False)
|
| 136 |
gr_user_transcript = gr.Textbox(label="", value=s_user_transcript, interactive=False, max_lines=9999)
|
| 137 |
|
| 138 |
gr_markdown1 = gr.Markdown("After clicking Run Next Turn, please be patient as it may take up to a minute for the game state to update.")
|
|
|
|
| 140 |
gr_button1 = gr.Button(value="Run Next Turn")
|
| 141 |
|
| 142 |
gr_n_turns_elapsed = gr.Textbox(label="N Turns Elapsed", value="0", interactive=False)
|
| 143 |
+
|
| 144 |
+
gr_button1.click(
|
| 145 |
+
fn=run_1_game_turn, inputs=[gr_narr_transcript, gr_n_turns_elapsed, gr_user_transcript, gr_user_input], outputs=[gr_narr_transcript, gr_n_turns_elapsed, gr_user_transcript, gr_user_input]
|
| 146 |
+
)
|
| 147 |
|
| 148 |
demo.launch()
|
game_content.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
-
|
| 4 |
-
-
|
| 5 |
-
-
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
|
| 11 |
-
-------------- ------------------------------- ------- -
|
| 12 |
|
| 13 |
The year is 603, in an alternate reality fantasy world with a little bit of magic, although you yourself cannot cast spells and the only magical item you own is your revolver. Magically-powered firearms are common, especially six-shooter revolvers, which you and almost everyone carries. You are a seasonal laborer making a once a year summer trek from Tibet across the Himalayas to Nepal to return home to your family with your year's worth of wages which is 100 gold coins. It is very common to be waylaid by bandits who will try to steal some of your gold, or to take some of it for 'protection'. If you make it home with less than 30 coins or do not make it home at all, your family will not be able to afford food to eat.
|
| 14 |
|
|
@@ -19,48 +18,49 @@ You have been traveling for weeks now and you are nearing the end of your journe
|
|
| 19 |
You turn around to find two bandits blocking your path, each armed with a magical revolver. They demand that you hand over half of your gold coins as 'protection money'. You know that if you give them what they want, there's no guarantee that they won't just take all of it anyway. On the other hand, if you refuse to pay up then they could easily shoot you down and take all of your coins anyway.
|
| 20 |
|
| 21 |
What do you do?
|
| 22 |
-
|
| 23 |
|
| 24 |
-
NOTES_TO_THE_NARRATOR_AT_START =
|
| 25 |
IMPORTANT NOTES TO THE NARRATOR:
|
| 26 |
BANDITS ARE TOUGH, POWERFUL CHARACTERS THAT CANNOT BE EASILY DEFEATED OR FOOLED.
|
| 27 |
ATTEMPTS TO REACH HOME MUST ALWAYS BE FOILED BY GETTING LOST, ENCOUNTERING DANGEROUS WILD ANIMALS, OR ENCOUNTERING MORE BANDITS.
|
| 28 |
|
| 29 |
-
|
| 30 |
|
| 31 |
-
NOTES_TO_THE_NARRATOR_EVERY_TIME =
|
| 32 |
|
| 33 |
What happens in JUST THE NEXT THREE SECONDS? DO NOT say that the protagonist continues home! That's too easy! Make this game hard for the player!!
|
| 34 |
-
|
| 35 |
|
| 36 |
-
AWAITING_INPUT =
|
| 37 |
-
|
| 38 |
|
| 39 |
|
| 40 |
# You can check if S_GAME_OVER is in either transcript, to see if the game is over.
|
| 41 |
S_GAME_OVER = "GAME OVER"
|
| 42 |
|
|
|
|
| 43 |
def game_over_victory_txt(s_reason):
|
| 44 |
-
|
| 45 |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 46 |
-
!! {S_GAME_OVER}.
|
| 47 |
-
!! YOU WIN!
|
| 48 |
!! {s_reason}
|
| 49 |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 50 |
|
| 51 |
To play again, please refresh the page.
|
| 52 |
-
|
| 53 |
|
| 54 |
|
| 55 |
def game_over_fail_txt(s_reason):
|
| 56 |
-
|
| 57 |
## {S_GAME_OVER}. &&
|
| 58 |
## {sreason}
|
| 59 |
## YOU LOSE. &&
|
| 60 |
//################################################################################################//
|
| 61 |
|
| 62 |
To play again, please refresh the page.
|
| 63 |
-
|
| 64 |
|
| 65 |
|
| 66 |
N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER = 3
|
|
|
|
| 1 |
+
GAME_INTRO = """----------------------------------- ------------------ ----------------
|
| 2 |
+
- ----- ----- -
|
| 3 |
+
- PARLEY - -
|
| 4 |
+
- ----- -
|
| 5 |
+
- The one-of-a-kind role playing game -
|
| 6 |
+
- that tests your ability to achieve -
|
| 7 |
+
- your objective without killing -
|
| 8 |
+
- any sentient beings. -
|
| 9 |
+
- ----- -
|
| 10 |
+
-------------- ------------------------------- ------- ----------------
|
|
|
|
| 11 |
|
| 12 |
The year is 603, in an alternate reality fantasy world with a little bit of magic, although you yourself cannot cast spells and the only magical item you own is your revolver. Magically-powered firearms are common, especially six-shooter revolvers, which you and almost everyone carries. You are a seasonal laborer making a once a year summer trek from Tibet across the Himalayas to Nepal to return home to your family with your year's worth of wages which is 100 gold coins. It is very common to be waylaid by bandits who will try to steal some of your gold, or to take some of it for 'protection'. If you make it home with less than 30 coins or do not make it home at all, your family will not be able to afford food to eat.
|
| 13 |
|
|
|
|
| 18 |
You turn around to find two bandits blocking your path, each armed with a magical revolver. They demand that you hand over half of your gold coins as 'protection money'. You know that if you give them what they want, there's no guarantee that they won't just take all of it anyway. On the other hand, if you refuse to pay up then they could easily shoot you down and take all of your coins anyway.
|
| 19 |
|
| 20 |
What do you do?
|
| 21 |
+
"""
|
| 22 |
|
| 23 |
+
NOTES_TO_THE_NARRATOR_AT_START = """
|
| 24 |
IMPORTANT NOTES TO THE NARRATOR:
|
| 25 |
BANDITS ARE TOUGH, POWERFUL CHARACTERS THAT CANNOT BE EASILY DEFEATED OR FOOLED.
|
| 26 |
ATTEMPTS TO REACH HOME MUST ALWAYS BE FOILED BY GETTING LOST, ENCOUNTERING DANGEROUS WILD ANIMALS, OR ENCOUNTERING MORE BANDITS.
|
| 27 |
|
| 28 |
+
"""
|
| 29 |
|
| 30 |
+
NOTES_TO_THE_NARRATOR_EVERY_TIME = """
|
| 31 |
|
| 32 |
What happens in JUST THE NEXT THREE SECONDS? DO NOT say that the protagonist continues home! That's too easy! Make this game hard for the player!!
|
| 33 |
+
"""
|
| 34 |
|
| 35 |
+
AWAITING_INPUT = """Awaiting user input:
|
| 36 |
+
"""
|
| 37 |
|
| 38 |
|
| 39 |
# You can check if S_GAME_OVER is in either transcript, to see if the game is over.
|
| 40 |
S_GAME_OVER = "GAME OVER"
|
| 41 |
|
| 42 |
+
|
| 43 |
def game_over_victory_txt(s_reason):
|
| 44 |
+
return f"""
|
| 45 |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 46 |
+
!! {S_GAME_OVER}. !!
|
| 47 |
+
!! YOU WIN! !!
|
| 48 |
!! {s_reason}
|
| 49 |
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
| 50 |
|
| 51 |
To play again, please refresh the page.
|
| 52 |
+
"""
|
| 53 |
|
| 54 |
|
| 55 |
def game_over_fail_txt(s_reason):
|
| 56 |
+
return f"""//################################################################################################//
|
| 57 |
## {S_GAME_OVER}. &&
|
| 58 |
## {sreason}
|
| 59 |
## YOU LOSE. &&
|
| 60 |
//################################################################################################//
|
| 61 |
|
| 62 |
To play again, please refresh the page.
|
| 63 |
+
"""
|
| 64 |
|
| 65 |
|
| 66 |
N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER = 3
|