awacke1 commited on
Commit
6820e31
Β·
verified Β·
1 Parent(s): 5f6d1ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -42
app.py CHANGED
@@ -3,61 +3,113 @@ import pandas as pd
3
  import plotly.express as px
4
  import random
5
  import json
 
6
  from streamlit_flow import streamlit_flow
7
  from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
8
  from streamlit_flow.layouts import TreeLayout
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def generate_situation():
11
- situations = [
12
- "The Village in Peril",
13
- "The Cursed Artifact",
14
- "The Sacred Pact",
15
- "The Shapeshifter's Challenge",
16
- "The Fox Fire Trial"
17
- ]
18
- return random.choice(situations)
19
 
20
  def generate_actions():
21
- actions = [
22
- ("Use Fox Fire", "πŸ”₯"),
23
- ("Shapeshift", "🦊"),
24
- ("Possess an Object", "πŸ‘»"),
25
- ("Call Upon Ancient Spirits", "🌟"),
26
- ("Use Mystical Artifact", "πŸ—‘οΈ")
27
- ]
28
- return random.sample(actions, 3)
29
 
30
  def evaluate_action(action, power_level, mystical_energy, history):
31
  base_success_chance = (power_level + mystical_energy) / 2
32
-
33
- # Adjust success chance based on history
34
- if action in history:
35
- success_chance = base_success_chance + (history[action] * 2)
36
  else:
37
  success_chance = base_success_chance
38
-
39
  outcome = random.randint(1, 100) <= success_chance
40
  return outcome, success_chance
41
 
42
- def update_graph(nodes, edges, situation, action, outcome):
43
- new_node_id = f"{len(nodes)}-{situation}-{action}"
44
- new_node = StreamlitFlowNode(new_node_id, (0, 0), {'content': f"{situation}\n{action}\nOutcome: {'Success' if outcome else 'Failure'}"}, 'output', 'bottom', 'top')
 
45
  nodes.append(new_node)
46
 
47
  if len(nodes) > 1:
48
- new_edge = StreamlitFlowEdge(f"{nodes[-2].id}-{new_node.id}", nodes[-2].id, new_node.id, animated=True)
49
  edges.append(new_edge)
50
 
51
  return nodes, edges
52
 
53
  def save_game_state(state):
54
- return json.dumps(state)
55
 
56
  def load_game_state(state_json):
57
  return json.loads(state_json)
58
 
59
  def app():
60
- st.markdown("# Kitsune - The Mystical Shape-Shifter Game 🦊")
61
 
62
  if 'game_state' not in st.session_state:
63
  st.session_state.game_state = {
@@ -70,8 +122,8 @@ def app():
70
  }
71
 
72
  # Game Stats
73
- st.sidebar.markdown("## Game Stats")
74
- st.sidebar.markdown(f"Score: {st.session_state.game_state['score']}")
75
 
76
  # Character Stats
77
  power_level = st.sidebar.slider('Power Level ⚑', 0, 100, st.session_state.game_state['power_level'])
@@ -81,37 +133,42 @@ def app():
81
  situation = generate_situation()
82
  actions = generate_actions()
83
 
84
- st.markdown(f"## Current Situation: {situation}")
85
- st.markdown("Choose your action:")
 
86
 
87
  cols = st.columns(3)
88
- for i, (action, emoji) in enumerate(actions):
89
- if cols[i].button(f"{emoji} {action}"):
90
  outcome, success_chance = evaluate_action(action, power_level, mystical_energy, st.session_state.game_state['history'])
 
91
 
92
- st.markdown(f"You decided to: {action}")
93
- st.markdown(f"Outcome: {'Success!' if outcome else 'Failure.'}")
94
- st.markdown(f"Success Chance: {success_chance:.2f}%")
 
95
 
96
  if outcome:
97
  st.session_state.game_state['score'] += 1
98
 
99
  # Update history
100
- if action in st.session_state.game_state['history']:
101
- st.session_state.game_state['history'][action] += 1 if outcome else -1
102
  else:
103
- st.session_state.game_state['history'][action] = 1 if outcome else -1
104
 
105
  st.session_state.game_state['nodes'], st.session_state.game_state['edges'] = update_graph(
106
  st.session_state.game_state['nodes'],
107
  st.session_state.game_state['edges'],
108
  situation,
109
  action,
110
- outcome
 
111
  )
112
 
113
  # Display Graph
114
  if st.session_state.game_state['nodes']:
 
115
  streamlit_flow('kitsune_game_flow',
116
  st.session_state.game_state['nodes'],
117
  st.session_state.game_state['edges'],
@@ -120,14 +177,14 @@ def app():
120
  height=600)
121
 
122
  # Character Stats Visualization
123
- data = {"Stat": ["Power Level", "Mystical Energy"],
124
  "Value": [power_level, mystical_energy]}
125
  df = pd.DataFrame(data)
126
  fig = px.bar(df, x='Stat', y='Value', title="Kitsune Stats πŸ“Š")
127
  st.plotly_chart(fig)
128
 
129
  # Save and Load Game State
130
- st.markdown("## Save/Load Game")
131
  if st.button("Save Game"):
132
  game_state_json = save_game_state(st.session_state.game_state)
133
  st.download_button(
 
3
  import plotly.express as px
4
  import random
5
  import json
6
+ from datetime import datetime
7
  from streamlit_flow import streamlit_flow
8
  from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
9
  from streamlit_flow.layouts import TreeLayout
10
 
11
+ # Rich data structures for game content
12
+ SITUATIONS = [
13
+ {
14
+ "id": "village_peril",
15
+ "name": "The Village in Peril",
16
+ "description": "A once-peaceful village is shrouded in dark miasma. Villagers cower in fear as shadowy figures lurk in the mist.",
17
+ "emoji": "🏘️"
18
+ },
19
+ {
20
+ "id": "cursed_artifact",
21
+ "name": "The Cursed Artifact",
22
+ "description": "Deep in an ancient tomb, a glowing artifact pulses with malevolent energy, its whispers echoing in your mind.",
23
+ "emoji": "🏺"
24
+ },
25
+ {
26
+ "id": "sacred_pact",
27
+ "name": "The Sacred Pact",
28
+ "description": "At a moonlit shrine, spirits of the land gather, awaiting a mediator to forge a new covenant between realms.",
29
+ "emoji": "πŸŒ™"
30
+ },
31
+ {
32
+ "id": "shapeshifter_challenge",
33
+ "name": "The Shapeshifter's Challenge",
34
+ "description": "A rival kitsune issues a challenge, their forms flickering between human and fox. The air crackles with competitive energy.",
35
+ "emoji": "🦊"
36
+ },
37
+ {
38
+ "id": "fox_fire_trial",
39
+ "name": "The Fox Fire Trial",
40
+ "description": "Sacred blue flames dance before you, a test of your mastery over kitsune magic. The heat is both inviting and intimidating.",
41
+ "emoji": "πŸ”₯"
42
+ }
43
+ ]
44
+
45
+ ACTIONS = [
46
+ {
47
+ "id": "fox_fire",
48
+ "name": "Use Fox Fire",
49
+ "description": "Summon mystical flames to illuminate the darkness or ward off evil spirits.",
50
+ "emoji": "πŸ”₯"
51
+ },
52
+ {
53
+ "id": "shapeshift",
54
+ "name": "Shapeshift",
55
+ "description": "Transform your appearance to blend in or deceive others.",
56
+ "emoji": "🦊"
57
+ },
58
+ {
59
+ "id": "possess_object",
60
+ "name": "Possess an Object",
61
+ "description": "Infuse your spirit into an inanimate object to manipulate or gather information.",
62
+ "emoji": "πŸ‘»"
63
+ },
64
+ {
65
+ "id": "call_spirits",
66
+ "name": "Call Upon Ancient Spirits",
67
+ "description": "Invoke the wisdom and power of your ancestors to guide you.",
68
+ "emoji": "🌟"
69
+ },
70
+ {
71
+ "id": "use_artifact",
72
+ "name": "Use Mystical Artifact",
73
+ "description": "Activate a powerful magical item to unleash its effects.",
74
+ "emoji": "πŸ—‘οΈ"
75
+ }
76
+ ]
77
+
78
  def generate_situation():
79
+ return random.choice(SITUATIONS)
 
 
 
 
 
 
 
80
 
81
  def generate_actions():
82
+ return random.sample(ACTIONS, 3)
 
 
 
 
 
 
 
83
 
84
  def evaluate_action(action, power_level, mystical_energy, history):
85
  base_success_chance = (power_level + mystical_energy) / 2
86
+ if action['id'] in history:
87
+ success_chance = base_success_chance + (history[action['id']] * 2)
 
 
88
  else:
89
  success_chance = base_success_chance
 
90
  outcome = random.randint(1, 100) <= success_chance
91
  return outcome, success_chance
92
 
93
+ def update_graph(nodes, edges, situation, action, outcome, timestamp):
94
+ new_node_id = f"{len(nodes)}-{situation['id']}-{action['id']}"
95
+ content = f"{situation['emoji']} {situation['name']}\n{action['emoji']} {action['name']}\nOutcome: {'βœ… Success' if outcome else '❌ Failure'}\nπŸ•’ {timestamp}"
96
+ new_node = StreamlitFlowNode(new_node_id, (0, 0), {'content': content}, 'output', 'bottom', 'top')
97
  nodes.append(new_node)
98
 
99
  if len(nodes) > 1:
100
+ new_edge = StreamlitFlowEdge(f"{nodes[-2].id}-{new_node.id}", nodes[-2].id, new_node.id, animated=True, dashed=True)
101
  edges.append(new_edge)
102
 
103
  return nodes, edges
104
 
105
  def save_game_state(state):
106
+ return json.dumps(state, default=str) # Use default=str to handle datetime objects
107
 
108
  def load_game_state(state_json):
109
  return json.loads(state_json)
110
 
111
  def app():
112
+ st.markdown("# 🦊 Kitsune - The Mystical Shape-Shifter Game 🦊")
113
 
114
  if 'game_state' not in st.session_state:
115
  st.session_state.game_state = {
 
122
  }
123
 
124
  # Game Stats
125
+ st.sidebar.markdown("## πŸ“Š Game Stats")
126
+ st.sidebar.markdown(f"**Score:** {st.session_state.game_state['score']}")
127
 
128
  # Character Stats
129
  power_level = st.sidebar.slider('Power Level ⚑', 0, 100, st.session_state.game_state['power_level'])
 
133
  situation = generate_situation()
134
  actions = generate_actions()
135
 
136
+ st.markdown(f"## {situation['emoji']} Current Situation: {situation['name']}")
137
+ st.markdown(situation['description'])
138
+ st.markdown("### 🎭 Choose your action:")
139
 
140
  cols = st.columns(3)
141
+ for i, action in enumerate(actions):
142
+ if cols[i].button(f"{action['emoji']} {action['name']}"):
143
  outcome, success_chance = evaluate_action(action, power_level, mystical_energy, st.session_state.game_state['history'])
144
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
145
 
146
+ st.markdown(f"You decided to: **{action['name']}**")
147
+ st.markdown(action['description'])
148
+ st.markdown(f"**Outcome:** {'βœ… Success!' if outcome else '❌ Failure.'}")
149
+ st.markdown(f"**Success Chance:** {success_chance:.2f}%")
150
 
151
  if outcome:
152
  st.session_state.game_state['score'] += 1
153
 
154
  # Update history
155
+ if action['id'] in st.session_state.game_state['history']:
156
+ st.session_state.game_state['history'][action['id']] += 1 if outcome else -1
157
  else:
158
+ st.session_state.game_state['history'][action['id']] = 1 if outcome else -1
159
 
160
  st.session_state.game_state['nodes'], st.session_state.game_state['edges'] = update_graph(
161
  st.session_state.game_state['nodes'],
162
  st.session_state.game_state['edges'],
163
  situation,
164
  action,
165
+ outcome,
166
+ timestamp
167
  )
168
 
169
  # Display Graph
170
  if st.session_state.game_state['nodes']:
171
+ st.markdown("## 🌳 Your Journey")
172
  streamlit_flow('kitsune_game_flow',
173
  st.session_state.game_state['nodes'],
174
  st.session_state.game_state['edges'],
 
177
  height=600)
178
 
179
  # Character Stats Visualization
180
+ data = {"Stat": ["Power Level ⚑", "Mystical Energy ✨"],
181
  "Value": [power_level, mystical_energy]}
182
  df = pd.DataFrame(data)
183
  fig = px.bar(df, x='Stat', y='Value', title="Kitsune Stats πŸ“Š")
184
  st.plotly_chart(fig)
185
 
186
  # Save and Load Game State
187
+ st.markdown("## πŸ’Ύ Save/Load Game")
188
  if st.button("Save Game"):
189
  game_state_json = save_game_state(st.session_state.game_state)
190
  st.download_button(