awacke1 commited on
Commit
e9122f4
ยท
verified ยท
1 Parent(s): cdb2e44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -38
app.py CHANGED
@@ -1,16 +1,134 @@
1
  import streamlit as st
2
- import gradio as gr
3
  import pandas as pd
 
 
 
 
 
 
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def main():
6
- # Title and Introduction
7
  st.title("๐Ÿฑ Cat Rider ๐Ÿ‡")
8
  st.markdown("""
9
  ## Welcome to Cat Rider!
10
  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.
11
  """)
12
 
13
- # Rules Table
14
  st.markdown("""
15
  ### ๐Ÿ“œ Game Rules
16
  | ๐Ÿ›ค๏ธ Step | ๐Ÿ“ Description |
@@ -22,46 +140,82 @@ def main():
22
  | 5๏ธโƒฃ | Complete the Quest |
23
  """)
24
 
25
- # Character Plot Elements: Dramatic Situations
26
- st.markdown("### ๐ŸŽญ Dramatic Situations")
27
-
28
- st.markdown('''#### Situation 1: The Great Feline Escape ๐Ÿšช๐Ÿˆ
29
- Your cat rider is trapped in an old mansion, which is about to be demolished. Using agility, wit, and bravery, orchestrate the perfect escape.
30
- ''')
31
-
32
- st.markdown('''#### Situation 2: The Treasure of the Lost Temple ๐Ÿ›๏ธ๐Ÿฑ
33
- On a quest to retrieve an ancient artifact, your cat rider must navigate through a labyrinth filled with traps and guardian spirits.
34
- ''')
35
-
36
- st.markdown('''#### Situation 3: The Royal Tournament ๐Ÿ‘‘๐Ÿ†
37
- Compete in a grand tournament where the finest cat riders showcase their skills and bravery to earn the title of the Royal Rider.
38
- ''')
39
-
40
- # UI Elements
41
- st.button('๐Ÿพ Choose your Cat Rider')
42
- st.slider('Select your gear strength', 1, 10)
43
- st.selectbox('Choose your path', ('Forest ๐Ÿž๏ธ', 'Desert ๐ŸŒต', 'Mountains ๐Ÿ”๏ธ'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Example of Data Table
46
- data = {
 
47
  'Gear': ['Helmet', 'Armor', 'Boots', 'Gloves'],
48
  'Protection Level': [8, 7, 5, 4]
49
  }
50
-
51
- df = pd.DataFrame(data)
52
- st.dataframe(df)
53
-
54
- # Gradio Interactive Elements
55
- def cat_rider_gear(gear_strength):
56
- return f'{gear_strength} - The perfect balance of agility and protection!'
57
-
58
- interface = gr.Interface(
59
- fn=cat_rider_gear,
60
- inputs=gr.inputs.Slider(minimum=1, maximum=10, default=5, label="Gear Strength"),
61
- outputs="text"
62
- )
63
-
64
- interface.launch(share=True)
65
 
66
  if __name__ == "__main__":
67
  main()
 
1
  import streamlit as st
 
2
  import pandas as pd
3
+ import plotly.express as px
4
+ import random
5
+ import json
6
+ import uuid
7
+ from datetime import datetime
8
+ from streamlit_flow import streamlit_flow
9
+ from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
10
+ from streamlit_flow.layouts import TreeLayout
11
 
12
+ # ๐ŸŒ Game World Data
13
+ SITUATIONS = [
14
+ {
15
+ "id": "feline_escape",
16
+ "name": "The Great Feline Escape",
17
+ "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.",
18
+ "emoji": "๐Ÿšช"
19
+ },
20
+ {
21
+ "id": "lost_temple",
22
+ "name": "The Treasure of the Lost Temple",
23
+ "description": "On a quest to retrieve an ancient artifact, your cat rider must navigate through a labyrinth filled with traps and guardian spirits.",
24
+ "emoji": "๐Ÿ›๏ธ"
25
+ },
26
+ {
27
+ "id": "royal_tournament",
28
+ "name": "The Royal Tournament",
29
+ "description": "Compete in a grand tournament where the finest cat riders showcase their skills and bravery to earn the title of the Royal Rider.",
30
+ "emoji": "๐Ÿ‘‘"
31
+ }
32
+ ]
33
+
34
+ ACTIONS = [
35
+ {
36
+ "id": "stealth",
37
+ "name": "Use Stealth",
38
+ "description": "Sneak past obstacles or enemies without being detected.",
39
+ "emoji": "๐Ÿคซ"
40
+ },
41
+ {
42
+ "id": "agility",
43
+ "name": "Showcase Agility",
44
+ "description": "Perform impressive acrobatic maneuvers to overcome challenges.",
45
+ "emoji": "๐Ÿƒ"
46
+ },
47
+ {
48
+ "id": "charm",
49
+ "name": "Charm Others",
50
+ "description": "Use your cat's natural charisma to win over allies or distract foes.",
51
+ "emoji": "๐Ÿ˜ป"
52
+ },
53
+ {
54
+ "id": "resourcefulness",
55
+ "name": "Be Resourceful",
56
+ "description": "Utilize the environment or items in creative ways to solve problems.",
57
+ "emoji": "๐Ÿง "
58
+ }
59
+ ]
60
+
61
+ # ๐ŸŽฒ Game Mechanics
62
+ def generate_situation():
63
+ """๐Ÿ”„ Randomly select a new situation for the player"""
64
+ return random.choice(SITUATIONS)
65
+
66
+ def generate_actions():
67
+ """๐ŸŽญ Generate a set of three random actions for the player to choose from"""
68
+ return random.sample(ACTIONS, 3)
69
+
70
+ def evaluate_action(action, gear_strength, rider_skill, history):
71
+ """โš–๏ธ Determine the outcome of a player's action"""
72
+ base_success_chance = (gear_strength + rider_skill) / 2
73
+ if action['id'] in history:
74
+ success_chance = base_success_chance + (history[action['id']] * 2)
75
+ else:
76
+ success_chance = base_success_chance
77
+ outcome = random.randint(1, 100) <= success_chance
78
+ return outcome, success_chance
79
+
80
+ # ๐ŸŒณ Journey Visualization
81
+ def create_graph_from_history(history_df):
82
+ """๐Ÿ“Š Create a visual representation of the player's journey"""
83
+ nodes = []
84
+ edges = []
85
+ for index, row in history_df.iterrows():
86
+ node_id = f"{index}-{row['situation_id']}-{row['action_id']}"
87
+ content = f"{row['situation_emoji']} {row['situation_name']}\n{row['action_emoji']} {row['action_name']}\nOutcome: {'โœ… Success' if row['outcome'] else 'โŒ Failure'}\n๐Ÿ•’ {row['timestamp']}"
88
+ new_node = StreamlitFlowNode(node_id, (0, 0), {'content': content}, 'output', 'bottom', 'top')
89
+ nodes.append(new_node)
90
+
91
+ if index > 0:
92
+ prev_node_id = f"{index-1}-{history_df.iloc[index-1]['situation_id']}-{history_df.iloc[index-1]['action_id']}"
93
+ new_edge = StreamlitFlowEdge(f"{prev_node_id}-{node_id}", prev_node_id, node_id, animated=True, dashed=True)
94
+ edges.append(new_edge)
95
+
96
+ return nodes, edges
97
+
98
+ # ๐Ÿ”„ Game State Management
99
+ def update_game_state(game_state, situation, action, outcome, timestamp):
100
+ """๐Ÿ”„ Update the game state with the latest action and its outcome"""
101
+ new_record = pd.DataFrame({
102
+ 'user_id': [game_state['user_id']],
103
+ 'timestamp': [timestamp],
104
+ 'situation_id': [situation['id']],
105
+ 'situation_name': [situation['name']],
106
+ 'situation_emoji': [situation['emoji']],
107
+ 'action_id': [action['id']],
108
+ 'action_name': [action['name']],
109
+ 'action_emoji': [action['emoji']],
110
+ 'outcome': [outcome],
111
+ 'score': [game_state['score']]
112
+ })
113
+ game_state['history_df'] = pd.concat([game_state['history_df'], new_record], ignore_index=True)
114
+
115
+ if action['id'] in game_state['history']:
116
+ game_state['history'][action['id']] += 1 if outcome else -1
117
+ else:
118
+ game_state['history'][action['id']] = 1 if outcome else -1
119
+
120
+ return game_state
121
+
122
+ # ๐ŸŽฎ Main Game Application
123
  def main():
124
+ """๐ŸŽฎ Main game loop and Streamlit interface"""
125
  st.title("๐Ÿฑ Cat Rider ๐Ÿ‡")
126
  st.markdown("""
127
  ## Welcome to Cat Rider!
128
  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.
129
  """)
130
 
131
+ # ๐Ÿ“œ Game Rules
132
  st.markdown("""
133
  ### ๐Ÿ“œ Game Rules
134
  | ๐Ÿ›ค๏ธ Step | ๐Ÿ“ Description |
 
140
  | 5๏ธโƒฃ | Complete the Quest |
141
  """)
142
 
143
+ # ๐Ÿ Initialize game state
144
+ if 'game_state' not in st.session_state:
145
+ st.session_state.game_state = {
146
+ 'user_id': str(uuid.uuid4()),
147
+ 'score': 0,
148
+ 'history': {},
149
+ 'gear_strength': 5,
150
+ 'rider_skill': 5,
151
+ 'history_df': pd.DataFrame(columns=['user_id', 'timestamp', 'situation_id', 'situation_name', 'situation_emoji', 'action_id', 'action_name', 'action_emoji', 'outcome', 'score'])
152
+ }
153
+
154
+ # ๐Ÿ“Š Game Stats
155
+ st.sidebar.markdown("## ๐Ÿ“Š Game Stats")
156
+ st.sidebar.markdown(f"**Score:** {st.session_state.game_state['score']}")
157
+
158
+ # ๐Ÿฆธ Character Stats
159
+ gear_strength = st.sidebar.slider('Gear Strength ๐Ÿ›ก๏ธ', 1, 10, st.session_state.game_state['gear_strength'])
160
+ rider_skill = st.sidebar.slider('Rider Skill ๐Ÿ‡', 1, 10, st.session_state.game_state['rider_skill'])
161
+
162
+ # ๐ŸŽญ Game Loop
163
+ situation = generate_situation()
164
+ actions = generate_actions()
165
+
166
+ st.markdown(f"## {situation['emoji']} Current Situation: {situation['name']}")
167
+ st.markdown(situation['description'])
168
+ st.markdown("### ๐ŸŽญ Choose your action:")
169
+
170
+ cols = st.columns(3)
171
+ for i, action in enumerate(actions):
172
+ if cols[i].button(f"{action['emoji']} {action['name']}"):
173
+ outcome, success_chance = evaluate_action(action, gear_strength, rider_skill, st.session_state.game_state['history'])
174
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
175
+
176
+ st.markdown(f"You decided to: **{action['name']}**")
177
+ st.markdown(action['description'])
178
+ st.markdown(f"**Outcome:** {'โœ… Success!' if outcome else 'โŒ Failure.'}")
179
+ st.markdown(f"**Success Chance:** {success_chance:.2f}%")
180
+
181
+ if outcome:
182
+ st.session_state.game_state['score'] += 1
183
+
184
+ # ๐Ÿ”„ Update game state
185
+ st.session_state.game_state = update_game_state(
186
+ st.session_state.game_state,
187
+ situation,
188
+ action,
189
+ outcome,
190
+ timestamp
191
+ )
192
+
193
+ # ๐ŸŒณ Display Journey Graph
194
+ if not st.session_state.game_state['history_df'].empty:
195
+ st.markdown("## ๐ŸŒณ Your Journey")
196
+ nodes, edges = create_graph_from_history(st.session_state.game_state['history_df'])
197
+ streamlit_flow('cat_rider_flow',
198
+ nodes,
199
+ edges,
200
+ layout=TreeLayout(direction='down'),
201
+ fit_view=True,
202
+ height=600)
203
+
204
+ # ๐Ÿ“Š Character Stats Visualization
205
+ data = {"Stat": ["Gear Strength ๐Ÿ›ก๏ธ", "Rider Skill ๐Ÿ‡"],
206
+ "Value": [gear_strength, rider_skill]}
207
+ df = pd.DataFrame(data)
208
+ fig = px.bar(df, x='Stat', y='Value', title="Cat Rider Stats ๐Ÿ“Š")
209
+ st.plotly_chart(fig)
210
 
211
  # Example of Data Table
212
+ st.markdown("### ๐Ÿ› ๏ธ Available Gear")
213
+ gear_data = {
214
  'Gear': ['Helmet', 'Armor', 'Boots', 'Gloves'],
215
  'Protection Level': [8, 7, 5, 4]
216
  }
217
+ gear_df = pd.DataFrame(gear_data)
218
+ st.dataframe(gear_df)
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
  if __name__ == "__main__":
221
  main()