awacke1 commited on
Commit
60b006e
ยท
verified ยท
1 Parent(s): 0fbec67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -163
app.py CHANGED
@@ -8,102 +8,11 @@ from streamlit_flow import streamlit_flow
8
  from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
9
  from streamlit_flow.layouts import TreeLayout
10
 
11
- # Set Streamlit to wide mode
12
- st.set_page_config(layout="wide")
 
13
 
14
- # ๐ŸŒ Game World Data
15
- SITUATIONS = [
16
- {
17
- "id": "feline_escape",
18
- "name": "The Great Feline Escape",
19
- "description": "Your cat rider is trapped in an old mansion, which is about to be demolished. Using agility, wit, and bravery, orchestrate the perfect escape.",
20
- "emoji": "๐Ÿšช",
21
- "type": "escape",
22
- "variations": [
23
- "Sneak through creaky floorboards ๐Ÿฆถ",
24
- "Navigate a maze of mirrors ๐Ÿชž",
25
- "Outsmart the demolition crew ๐Ÿ‘ท",
26
- "Find a secret passage ๐Ÿ•ต๏ธ",
27
- "Tame a guard dog ๐Ÿ•",
28
- "Climb down from the attic ๐Ÿชœ",
29
- "Distract the caretaker ๐Ÿ‘ด",
30
- "Ride a dumbwaiter to safety ๐Ÿ“ฆ",
31
- "Use curtains as a makeshift rope ๐Ÿชข",
32
- "Slide down the chimney ๐Ÿ "
33
- ]
34
- },
35
- {
36
- "id": "lost_temple",
37
- "name": "The Treasure of the Lost Temple",
38
- "description": "On a quest to retrieve an ancient artifact, your cat rider must navigate through a labyrinth filled with traps and guardian spirits.",
39
- "emoji": "๐Ÿ›๏ธ",
40
- "type": "exploration",
41
- "variations": [
42
- "Decipher ancient hieroglyphs ๐Ÿ“œ",
43
- "Cross a rickety bridge ๐ŸŒ‰",
44
- "Dodge poison darts ๐ŸŽฏ",
45
- "Solve a stone puzzle ๐Ÿงฉ",
46
- "Appease an angry spirit ๐Ÿ‘ป",
47
- "Navigate a pitch-black cavern ๐Ÿ•ฏ๏ธ",
48
- "Retrieve a gem from a statue's eye ๐Ÿ’Ž",
49
- "Swim through an underground river ๐ŸŠ",
50
- "Climb a crumbling tower ๐Ÿ—ผ",
51
- "Outwit a cunning sphinx ๐Ÿฆ"
52
- ]
53
- },
54
- {
55
- "id": "royal_tournament",
56
- "name": "The Royal Tournament",
57
- "description": "Compete in a grand tournament where the finest cat riders showcase their skills and bravery to earn the title of the Royal Rider.",
58
- "emoji": "๐Ÿ‘‘",
59
- "type": "competition",
60
- "variations": [
61
- "Race through an obstacle course ๐Ÿ",
62
- "Perform acrobatic stunts ๐Ÿคธ",
63
- "Win a jousting match ๐ŸŽญ",
64
- "Showcase elegant dressage ๐ŸŽ",
65
- "Complete a scavenger hunt ๐Ÿ”",
66
- "Demonstrate cat-whispering skills ๐Ÿ˜บ",
67
- "Win a battle of wits ๐Ÿง ",
68
- "Prove your valor in mock combat โš”๏ธ",
69
- "Navigate a magical maze ๐ŸŒ€",
70
- "Impress the royal judges ๐Ÿ‘จโ€โš–๏ธ"
71
- ]
72
- }
73
- ]
74
-
75
- ACTIONS = [
76
- {
77
- "id": "stealth",
78
- "name": "Use Stealth",
79
- "description": "Sneak past obstacles or enemies without being detected.",
80
- "emoji": "๐Ÿคซ",
81
- "type": "skill"
82
- },
83
- {
84
- "id": "agility",
85
- "name": "Showcase Agility",
86
- "description": "Perform impressive acrobatic maneuvers to overcome challenges.",
87
- "emoji": "๐Ÿƒ",
88
- "type": "physical"
89
- },
90
- {
91
- "id": "charm",
92
- "name": "Charm Others",
93
- "description": "Use your cat's natural charisma to win over allies or distract foes.",
94
- "emoji": "๐Ÿ˜ป",
95
- "type": "social"
96
- },
97
- {
98
- "id": "resourcefulness",
99
- "name": "Be Resourceful",
100
- "description": "Utilize the environment or items in creative ways to solve problems.",
101
- "emoji": "๐Ÿง ",
102
- "type": "mental"
103
- }
104
- ]
105
-
106
- # ๐ŸŽฒ Game Mechanics
107
  def generate_situation():
108
  return random.choice(SITUATIONS)
109
 
@@ -113,98 +22,55 @@ def generate_actions():
113
  def evaluate_action(action, gear_strength, rider_skill, history):
114
  base_success_chance = (gear_strength + rider_skill) / 2
115
  if action['id'] in history:
116
- success_chance = base_success_chance + (history[action['id']] * 5) # 5% increase per try
117
  else:
118
  success_chance = base_success_chance
119
- success_chance = min(success_chance, 95) # Cap at 95% chance
120
  outcome = random.randint(1, 100) <= success_chance
121
  return outcome, success_chance
122
 
123
  def generate_encounter_conclusion(situation, action, outcome):
 
124
  if outcome:
125
  conclusions = [
126
- f"Your {action['name']} was successful! You've overcome the challenges of {situation['name']}.",
127
- f"Through clever use of {action['name']}, you've triumphed in the {situation['name']}!",
128
- f"Your mastery of {action['name']} has led to a victorious outcome in the {situation['name']}!"
129
  ]
130
  else:
131
  conclusions = [
132
- f"Despite your efforts with {action['name']}, you've failed to overcome the {situation['name']}.",
133
- f"Your attempt at {action['name']} wasn't enough to succeed in the {situation['name']}.",
134
- f"The challenges of {situation['name']} proved too great for your {action['name']} this time."
135
  ]
136
  return random.choice(conclusions)
137
 
138
  def update_character_stats(game_state, outcome):
 
139
  if outcome:
140
  game_state['gear_strength'] = min(10, game_state['gear_strength'] + random.uniform(0.1, 0.5))
141
  game_state['rider_skill'] = min(10, game_state['rider_skill'] + random.uniform(0.1, 0.5))
142
  return game_state
143
 
144
- # ๐ŸŒณ Journey Visualization with Heterogeneous Graph Structure
145
  def create_heterogeneous_graph(history_df):
146
- nodes = []
147
- edges = []
148
-
149
- # Define node shapes based on situation and action types
150
- situation_shapes = {
151
- "escape": "diamond",
152
- "exploration": "triangle",
153
- "competition": "star"
154
- }
155
- action_shapes = {
156
- "skill": "square",
157
- "physical": "circle",
158
- "social": "hexagon",
159
- "mental": "octagon"
160
- }
161
-
162
- for index, row in history_df.iterrows():
163
- situation_id = f"situation-{index}"
164
- action_id = f"action-{index}"
165
- conclusion_id = f"conclusion-{index}"
166
-
167
- # Create situation node
168
- situation_content = f"{row['situation_emoji']} {row['situation_name']}\n{row['variation']}\n๐Ÿ•’ {row['timestamp']}"
169
- situation_node = StreamlitFlowNode(situation_id, (0, 0), {'content': situation_content}, 'output', 'bottom', 'top', shape=situation_shapes[row['situation_type']])
170
- nodes.append(situation_node)
171
-
172
- # Create action node
173
- action_content = f"{row['action_emoji']} {row['action_name']}\nOutcome: {'โœ… Success' if row['outcome'] else 'โŒ Failure'}"
174
- action_node = StreamlitFlowNode(action_id, (0, 0), {'content': action_content}, 'output', 'bottom', 'top', shape=action_shapes[row['action_type']])
175
- nodes.append(action_node)
176
-
177
- # Create conclusion node
178
- conclusion_content = f"๐Ÿ“œ {row['conclusion']}\n๐Ÿ’ช Gear: {row['gear_strength']:.2f} | ๐Ÿ‹๏ธ Skill: {row['rider_skill']:.2f}"
179
- conclusion_node = StreamlitFlowNode(conclusion_id, (0, 0), {'content': conclusion_content}, 'output', 'bottom', 'top', shape='parallelogram')
180
- nodes.append(conclusion_node)
181
-
182
- # Create edges
183
- edges.append(StreamlitFlowEdge(f"{situation_id}-{action_id}", situation_id, action_id, animated=True, dashed=False))
184
- edges.append(StreamlitFlowEdge(f"{action_id}-{conclusion_id}", action_id, conclusion_id, animated=True, dashed=False))
185
-
186
- # Create edge to previous conclusion if not the first node
187
- if index > 0:
188
- prev_conclusion_id = f"conclusion-{index-1}"
189
- edges.append(StreamlitFlowEdge(f"{prev_conclusion_id}-{situation_id}", prev_conclusion_id, situation_id, animated=True, dashed=True))
190
-
191
- return nodes, edges
192
 
193
- # ๐Ÿ“ Markdown Preview
194
  def create_markdown_preview(history_df):
195
  markdown = "## ๐ŸŒณ Journey Preview\n\n"
196
  for index, row in history_df.iterrows():
197
  indent = " " * (index * 3)
198
  markdown += f"{indent}๐ŸŒŸ **{row['situation_name']}** ({row['situation_type']})\n"
199
- markdown += f"{indent} ๐Ÿ“Œ {row['variation']}\n"
200
  markdown += f"{indent} โ†ช {row['action_emoji']} {row['action_name']} ({row['action_type']}): "
201
  markdown += "โœ… Success\n" if row['outcome'] else "โŒ Failure\n"
202
  markdown += f"{indent} ๐Ÿ“œ {row['conclusion']}\n"
203
  markdown += f"{indent} ๐Ÿ’ช Gear: {row['gear_strength']:.2f} | ๐Ÿ‹๏ธ Skill: {row['rider_skill']:.2f}\n\n"
204
  return markdown
205
 
206
- # ๐Ÿ”„ Game State Management
207
  def update_game_state(game_state, situation, action, outcome, timestamp):
 
208
  conclusion = generate_encounter_conclusion(situation, action, outcome)
209
  game_state = update_character_stats(game_state, outcome)
210
 
@@ -215,7 +81,6 @@ def update_game_state(game_state, situation, action, outcome, timestamp):
215
  'situation_name': [situation['name']],
216
  'situation_emoji': [situation['emoji']],
217
  'situation_type': [situation['type']],
218
- 'variation': [random.choice(situation['variations'])],
219
  'action_id': [action['id']],
220
  'action_name': [action['name']],
221
  'action_emoji': [action['emoji']],
@@ -229,13 +94,13 @@ def update_game_state(game_state, situation, action, outcome, timestamp):
229
  game_state['history_df'] = pd.concat([game_state['history_df'], new_record], ignore_index=True)
230
 
231
  if action['id'] in game_state['history']:
232
- game_state['history'][action['id']] += 1
233
  else:
234
- game_state['history'][action['id']] = 1
235
 
236
  return game_state
237
 
238
- # ๐ŸŽฎ Main Game Application
239
  def main():
240
  st.title("๐Ÿฑ Cat Rider ๐Ÿ‡")
241
  st.markdown("""
@@ -243,7 +108,7 @@ def main():
243
  In this immersive adventure, you will explore the thrilling world of feline riders. This game sets the stage for dramatic situations and guided storytelling with engaging interactive elements.
244
  """)
245
 
246
- # ๐Ÿ“œ Game Rules
247
  st.markdown("""
248
  ### ๐Ÿ“œ Game Rules
249
  | ๐Ÿ›ค๏ธ Step | ๐Ÿ“ Description |
@@ -255,7 +120,7 @@ def main():
255
  | 5๏ธโƒฃ | Complete the Quest and Grow Stronger |
256
  """)
257
 
258
- # ๐Ÿ Initialize game state
259
  if 'game_state' not in st.session_state:
260
  st.session_state.game_state = {
261
  'user_id': str(uuid.uuid4()),
@@ -263,10 +128,10 @@ def main():
263
  'history': {},
264
  'gear_strength': 5,
265
  'rider_skill': 5,
266
- 'history_df': pd.DataFrame(columns=['user_id', 'timestamp', 'situation_id', 'situation_name', 'situation_emoji', 'situation_type', 'variation', 'action_id', 'action_name', 'action_emoji', 'action_type', 'outcome', 'conclusion', 'gear_strength', 'rider_skill', 'score'])
267
  }
268
 
269
- # ๐Ÿ“Š Game Stats
270
  st.sidebar.markdown("## ๐Ÿ“Š Game Stats")
271
  st.sidebar.markdown(f"**Score:** {st.session_state.game_state['score']}")
272
  st.sidebar.markdown(f"**Gear Strength:** {st.session_state.game_state['gear_strength']:.2f}")
@@ -278,7 +143,6 @@ def main():
278
 
279
  st.markdown(f"## {situation['emoji']} Current Situation: {situation['name']} ({situation['type']})")
280
  st.markdown(situation['description'])
281
- st.markdown(f"**Specific Challenge:** {random.choice(situation['variations'])}")
282
  st.markdown("### ๐ŸŽญ Choose your action:")
283
 
284
  cols = st.columns(3)
@@ -305,4 +169,39 @@ def main():
305
  )
306
 
307
  # Display conclusion
308
- conclusion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
9
  from streamlit_flow.layouts import TreeLayout
10
 
11
+ # ๐ŸŒ Game World Data (unchanged)
12
+ SITUATIONS = [ ... ] # Your existing SITUATIONS data
13
+ ACTIONS = [ ... ] # Your existing ACTIONS data
14
 
15
+ # ๐ŸŽฒ Game Mechanics (unchanged)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def generate_situation():
17
  return random.choice(SITUATIONS)
18
 
 
22
  def evaluate_action(action, gear_strength, rider_skill, history):
23
  base_success_chance = (gear_strength + rider_skill) / 2
24
  if action['id'] in history:
25
+ success_chance = base_success_chance + (history[action['id']] * 2)
26
  else:
27
  success_chance = base_success_chance
 
28
  outcome = random.randint(1, 100) <= success_chance
29
  return outcome, success_chance
30
 
31
  def generate_encounter_conclusion(situation, action, outcome):
32
+ # Scenario-based conclusions
33
  if outcome:
34
  conclusions = [
35
+ f"Your {action['name']} was successful! You've overcome the challenges of {situation['name']}. ๐ŸŽ‰",
36
+ f"Through clever use of {action['name']}, you've triumphed in the {situation['name']}! ๐Ÿ†",
37
+ f"Your mastery of {action['name']} led to victory in the {situation['name']}! ๐Ÿ’ช"
38
  ]
39
  else:
40
  conclusions = [
41
+ f"Despite your efforts with {action['name']}, you couldn't overcome the {situation['name']}. ๐Ÿ˜”",
42
+ f"Your attempt at {action['name']} wasn't enough in the {situation['name']}. ๐Ÿ˜ข",
43
+ f"The challenges of {situation['name']} proved too great this time. ๐Ÿ’€"
44
  ]
45
  return random.choice(conclusions)
46
 
47
  def update_character_stats(game_state, outcome):
48
+ # Improve stats only on success
49
  if outcome:
50
  game_state['gear_strength'] = min(10, game_state['gear_strength'] + random.uniform(0.1, 0.5))
51
  game_state['rider_skill'] = min(10, game_state['rider_skill'] + random.uniform(0.1, 0.5))
52
  return game_state
53
 
54
+ # ๐ŸŒณ Journey Visualization with Heterogeneous Graph Structure (unchanged)
55
  def create_heterogeneous_graph(history_df):
56
+ # Your existing node and edge creation logic
57
+ ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # ๐Ÿ“ Markdown Preview (unchanged)
60
  def create_markdown_preview(history_df):
61
  markdown = "## ๐ŸŒณ Journey Preview\n\n"
62
  for index, row in history_df.iterrows():
63
  indent = " " * (index * 3)
64
  markdown += f"{indent}๐ŸŒŸ **{row['situation_name']}** ({row['situation_type']})\n"
 
65
  markdown += f"{indent} โ†ช {row['action_emoji']} {row['action_name']} ({row['action_type']}): "
66
  markdown += "โœ… Success\n" if row['outcome'] else "โŒ Failure\n"
67
  markdown += f"{indent} ๐Ÿ“œ {row['conclusion']}\n"
68
  markdown += f"{indent} ๐Ÿ’ช Gear: {row['gear_strength']:.2f} | ๐Ÿ‹๏ธ Skill: {row['rider_skill']:.2f}\n\n"
69
  return markdown
70
 
71
+ # ๐Ÿ”„ Game State Management (unchanged)
72
  def update_game_state(game_state, situation, action, outcome, timestamp):
73
+ # Update based on encounter outcome
74
  conclusion = generate_encounter_conclusion(situation, action, outcome)
75
  game_state = update_character_stats(game_state, outcome)
76
 
 
81
  'situation_name': [situation['name']],
82
  'situation_emoji': [situation['emoji']],
83
  'situation_type': [situation['type']],
 
84
  'action_id': [action['id']],
85
  'action_name': [action['name']],
86
  'action_emoji': [action['emoji']],
 
94
  game_state['history_df'] = pd.concat([game_state['history_df'], new_record], ignore_index=True)
95
 
96
  if action['id'] in game_state['history']:
97
+ game_state['history'][action['id']] += 1 if outcome else -1
98
  else:
99
+ game_state['history'][action['id']] = 1 if outcome else -1
100
 
101
  return game_state
102
 
103
+ # ๐ŸŽฎ Main Game Application (main logic with improvements)
104
  def main():
105
  st.title("๐Ÿฑ Cat Rider ๐Ÿ‡")
106
  st.markdown("""
 
108
  In this immersive adventure, you will explore the thrilling world of feline riders. This game sets the stage for dramatic situations and guided storytelling with engaging interactive elements.
109
  """)
110
 
111
+ # ๐Ÿ“œ Game Rules (unchanged)
112
  st.markdown("""
113
  ### ๐Ÿ“œ Game Rules
114
  | ๐Ÿ›ค๏ธ Step | ๐Ÿ“ Description |
 
120
  | 5๏ธโƒฃ | Complete the Quest and Grow Stronger |
121
  """)
122
 
123
+ # ๐Ÿ Initialize game state (unchanged)
124
  if 'game_state' not in st.session_state:
125
  st.session_state.game_state = {
126
  'user_id': str(uuid.uuid4()),
 
128
  'history': {},
129
  'gear_strength': 5,
130
  'rider_skill': 5,
131
+ 'history_df': pd.DataFrame(columns=['user_id', 'timestamp', 'situation_id', 'situation_name', 'situation_emoji', 'situation_type', 'action_id', 'action_name', 'action_emoji', 'action_type', 'outcome', 'conclusion', 'gear_strength', 'rider_skill', 'score'])
132
  }
133
 
134
+ # ๐Ÿ“Š Game Stats (unchanged)
135
  st.sidebar.markdown("## ๐Ÿ“Š Game Stats")
136
  st.sidebar.markdown(f"**Score:** {st.session_state.game_state['score']}")
137
  st.sidebar.markdown(f"**Gear Strength:** {st.session_state.game_state['gear_strength']:.2f}")
 
143
 
144
  st.markdown(f"## {situation['emoji']} Current Situation: {situation['name']} ({situation['type']})")
145
  st.markdown(situation['description'])
 
146
  st.markdown("### ๐ŸŽญ Choose your action:")
147
 
148
  cols = st.columns(3)
 
169
  )
170
 
171
  # Display conclusion
172
+ conclusion = st.session_state.game_state['history_df'].iloc[-1]['conclusion']
173
+ st.markdown(f"**Encounter Conclusion:** {conclusion}")
174
+
175
+ # Display updated stats
176
+ st.markdown(f"**Updated Stats:**")
177
+ st.markdown(f"๐Ÿ’ช Gear Strength: {st.session_state.game_state['gear_strength']:.2f}")
178
+ st.markdown(f"๐Ÿ‹๏ธ Rider Skill: {st.session_state.game_state['rider_skill']:.2f}")
179
+
180
+ # ๐Ÿ“ Display Markdown Preview (unchanged)
181
+ if not st.session_state.game_state['history_df'].empty:
182
+ st.markdown(create_markdown_preview(st.session_state.game_state['history_df']))
183
+
184
+ # ๐ŸŒณ Display Heterogeneous Journey Graph (unchanged)
185
+ if not st.session_state.game_state['history_df'].empty:
186
+ st.markdown("## ๐ŸŒณ Your Journey (Heterogeneous Graph)")
187
+ nodes, edges = create_heterogeneous_graph(st.session_state.game_state['history_df'])
188
+ try:
189
+ streamlit_flow('cat_rider_flow',
190
+ nodes,
191
+ edges,
192
+ layout=TreeLayout(direction='down'),
193
+ fit_view=True,
194
+ height=600)
195
+ except Exception as e:
196
+ st.error(f"An error occurred while rendering the journey graph: {str(e)}")
197
+ st.markdown("Please try refreshing the page if the graph doesn't appear.")
198
+
199
+ # ๐Ÿ“Š Character Stats Visualization (unchanged)
200
+ data = {"Stat": ["Gear Strength ๐Ÿ›ก๏ธ", "Rider Skill ๐Ÿ‡"],
201
+ "Value": [st.session_state.game_state['gear_strength'], st.session_state.game_state['rider_skill']]}
202
+ df = pd.DataFrame(data)
203
+ fig = px.bar(df, x='Stat', y='Value', title="Cat Rider Stats ๐Ÿ“Š")
204
+ st.plotly_chart(fig)
205
+
206
+ if __name__ == "__main__":
207
+ main()