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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import random
|
5 |
+
import uuid
|
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 |
+
# 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 |
+
|
110 |
+
def generate_actions():
|
111 |
+
return random.sample(ACTIONS, 3)
|
112 |
+
|
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 |
+
|
211 |
+
new_record = pd.DataFrame({
|
212 |
+
'user_id': [game_state['user_id']],
|
213 |
+
'timestamp': [timestamp],
|
214 |
+
'situation_id': [situation['id']],
|
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']],
|
222 |
+
'action_type': [action['type']],
|
223 |
+
'outcome': [outcome],
|
224 |
+
'conclusion': [conclusion],
|
225 |
+
'gear_strength': [game_state['gear_strength']],
|
226 |
+
'rider_skill': [game_state['rider_skill']],
|
227 |
+
'score': [game_state['score']]
|
228 |
+
})
|
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("""
|
242 |
+
## Welcome to Cat Rider!
|
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 |
|
250 |
+
|---------|----------------|
|
251 |
+
| 1๏ธโฃ | Choose your Cat Rider |
|
252 |
+
| 2๏ธโฃ | Select the Riding Gear |
|
253 |
+
| 3๏ธโฃ | Set off on an Adventure |
|
254 |
+
| 4๏ธโฃ | Encounter Challenges and Make Decisions |
|
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()),
|
262 |
+
'score': 0,
|
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}")
|
273 |
+
st.sidebar.markdown(f"**Rider Skill:** {st.session_state.game_state['rider_skill']:.2f}")
|
274 |
+
|
275 |
+
# ๐ญ Game Loop
|
276 |
+
situation = generate_situation()
|
277 |
+
actions = generate_actions()
|
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)
|
285 |
+
for i, action in enumerate(actions):
|
286 |
+
if cols[i].button(f"{action['emoji']} {action['name']} ({action['type']})"):
|
287 |
+
outcome, success_chance = evaluate_action(action, st.session_state.game_state['gear_strength'], st.session_state.game_state['rider_skill'], st.session_state.game_state['history'])
|
288 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
289 |
+
|
290 |
+
st.markdown(f"You decided to: **{action['name']}** ({action['type']})")
|
291 |
+
st.markdown(action['description'])
|
292 |
+
st.markdown(f"**Outcome:** {'โ
Success!' if outcome else 'โ Failure.'}")
|
293 |
+
st.markdown(f"**Success Chance:** {success_chance:.2f}%")
|
294 |
+
|
295 |
+
if outcome:
|
296 |
+
st.session_state.game_state['score'] += 1
|
297 |
+
|
298 |
+
# ๐ Update game state
|
299 |
+
st.session_state.game_state = update_game_state(
|
300 |
+
st.session_state.game_state,
|
301 |
+
situation,
|
302 |
+
action,
|
303 |
+
outcome,
|
304 |
+
timestamp
|
305 |
+
)
|
306 |
+
|
307 |
+
# Display conclusion
|
308 |
+
conclusion
|