Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -121,47 +121,80 @@ def update_character_stats(game_state, outcome):
|
|
121 |
def create_heterogeneous_graph(history_df):
|
122 |
nodes = []
|
123 |
edges = []
|
|
|
124 |
for index, row in history_df.iterrows():
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
#
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
# Action node
|
134 |
-
action_node = StreamlitFlowNode(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
nodes.append(action_node)
|
136 |
|
137 |
-
#
|
|
|
138 |
stars = 'β' * int(row['score'])
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
# Edges
|
143 |
-
edges.append(StreamlitFlowEdge(
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
return nodes, edges
|
152 |
|
153 |
-
# π Markdown Preview with
|
154 |
def create_markdown_preview(history_df):
|
155 |
markdown = "## π³ Journey Preview\n\n"
|
156 |
-
|
157 |
|
158 |
-
for situation_name, group in
|
159 |
-
markdown += f"
|
160 |
for _, row in group.iterrows():
|
|
|
161 |
stars = 'β' * int(row['score'])
|
162 |
-
markdown += f"
|
163 |
-
markdown += "
|
164 |
-
markdown += f" π {row['conclusion']} {stars} πͺ Gear: {row['gear_strength']:.2f} | ποΈ Skill: {row['rider_skill']:.2f}\n"
|
165 |
markdown += "\n"
|
166 |
return markdown
|
167 |
|
@@ -260,7 +293,6 @@ def main():
|
|
260 |
st.markdown("## Choose Your Cat Rider:")
|
261 |
cols = st.columns(len(CAT_RIDERS))
|
262 |
for i, rider in enumerate(CAT_RIDERS):
|
263 |
-
# Adding unique key for each button
|
264 |
if cols[i].button(f"{rider['emoji']} {rider['name']} ({rider['type']})", key=f"rider_{i}"):
|
265 |
st.session_state.game_state['cat_rider'] = rider
|
266 |
st.session_state.game_state['rider_skill'] = rider['skill']
|
@@ -270,7 +302,6 @@ def main():
|
|
270 |
st.markdown("## Select Your Riding Gear:")
|
271 |
cols = st.columns(len(RIDING_GEAR))
|
272 |
for i, gear in enumerate(RIDING_GEAR):
|
273 |
-
# Adding unique key for each button
|
274 |
if cols[i].button(f"{gear['name']} ({gear['type']})", key=f"gear_{i}"):
|
275 |
st.session_state.game_state['riding_gear'] = gear
|
276 |
st.session_state.game_state['gear_strength'] = gear['strength']
|
@@ -286,7 +317,6 @@ def main():
|
|
286 |
|
287 |
cols = st.columns(3)
|
288 |
for i, action in enumerate(actions):
|
289 |
-
# Adding unique key for each button
|
290 |
if cols[i].button(f"{action['emoji']} {action['name']} ({action['type']})", key=f"action_{i}"):
|
291 |
outcome, success_chance = evaluate_action(situation, action, st.session_state.game_state['gear_strength'], st.session_state.game_state['rider_skill'], st.session_state.game_state['history'])
|
292 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
@@ -320,31 +350,31 @@ def main():
|
|
320 |
# π
Display Scoreboard
|
321 |
display_scoreboard(st.session_state.game_state)
|
322 |
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
|
349 |
if __name__ == "__main__":
|
350 |
main()
|
|
|
121 |
def create_heterogeneous_graph(history_df):
|
122 |
nodes = []
|
123 |
edges = []
|
124 |
+
node_ids = {}
|
125 |
for index, row in history_df.iterrows():
|
126 |
+
situation_node_id = f"situation_{row['situation_id']}_{index}"
|
127 |
+
action_node_id = f"action_{row['action_id']}_{index}"
|
128 |
+
outcome_node_id = f"outcome_{index}"
|
129 |
+
|
130 |
+
# Situation node
|
131 |
+
if situation_node_id not in node_ids:
|
132 |
+
situation_node = StreamlitFlowNode(
|
133 |
+
id=situation_node_id,
|
134 |
+
position=(0, 0),
|
135 |
+
data={'content': f"{row['situation_emoji']} {row['situation_name']}"},
|
136 |
+
type='default',
|
137 |
+
sourcePosition='bottom',
|
138 |
+
targetPosition='top',
|
139 |
+
shape='ellipse'
|
140 |
+
)
|
141 |
+
nodes.append(situation_node)
|
142 |
+
node_ids[situation_node_id] = situation_node_id
|
143 |
|
144 |
# Action node
|
145 |
+
action_node = StreamlitFlowNode(
|
146 |
+
id=action_node_id,
|
147 |
+
position=(0, 0),
|
148 |
+
data={'content': f"{row['action_emoji']} {row['action_name']}"},
|
149 |
+
type='default',
|
150 |
+
sourcePosition='bottom',
|
151 |
+
targetPosition='top',
|
152 |
+
shape='ellipse'
|
153 |
+
)
|
154 |
nodes.append(action_node)
|
155 |
|
156 |
+
# Outcome node with success celebration
|
157 |
+
outcome_content = 'β
Success' if row['outcome'] else 'β Failure'
|
158 |
stars = 'β' * int(row['score'])
|
159 |
+
outcome_node = StreamlitFlowNode(
|
160 |
+
id=outcome_node_id,
|
161 |
+
position=(0, 0),
|
162 |
+
data={'content': f"{row['conclusion']}\n{outcome_content}\n{stars}"},
|
163 |
+
type='default',
|
164 |
+
sourcePosition='bottom',
|
165 |
+
targetPosition='top',
|
166 |
+
shape='ellipse'
|
167 |
+
)
|
168 |
+
nodes.append(outcome_node)
|
169 |
|
170 |
# Edges
|
171 |
+
edges.append(StreamlitFlowEdge(
|
172 |
+
id=f"edge_{situation_node_id}_{action_node_id}_{index}",
|
173 |
+
source=situation_node_id,
|
174 |
+
target=action_node_id,
|
175 |
+
animated=True
|
176 |
+
))
|
177 |
+
edges.append(StreamlitFlowEdge(
|
178 |
+
id=f"edge_{action_node_id}_{outcome_node_id}_{index}",
|
179 |
+
source=action_node_id,
|
180 |
+
target=outcome_node_id,
|
181 |
+
animated=True
|
182 |
+
))
|
183 |
|
184 |
return nodes, edges
|
185 |
|
186 |
+
# π Markdown Preview with Subpoints for Each Action
|
187 |
def create_markdown_preview(history_df):
|
188 |
markdown = "## π³ Journey Preview\n\n"
|
189 |
+
grouped = history_df.groupby('situation_name', sort=False)
|
190 |
|
191 |
+
for situation_name, group in grouped:
|
192 |
+
markdown += f"### {group.iloc[0]['situation_emoji']} **{situation_name}**\n"
|
193 |
for _, row in group.iterrows():
|
194 |
+
outcome_str = 'β
Success' if row['outcome'] else 'β Failure'
|
195 |
stars = 'β' * int(row['score'])
|
196 |
+
markdown += f"- {row['action_emoji']} **{row['action_name']}**: {outcome_str} {stars}\n"
|
197 |
+
markdown += f" - {row['conclusion']}\n"
|
|
|
198 |
markdown += "\n"
|
199 |
return markdown
|
200 |
|
|
|
293 |
st.markdown("## Choose Your Cat Rider:")
|
294 |
cols = st.columns(len(CAT_RIDERS))
|
295 |
for i, rider in enumerate(CAT_RIDERS):
|
|
|
296 |
if cols[i].button(f"{rider['emoji']} {rider['name']} ({rider['type']})", key=f"rider_{i}"):
|
297 |
st.session_state.game_state['cat_rider'] = rider
|
298 |
st.session_state.game_state['rider_skill'] = rider['skill']
|
|
|
302 |
st.markdown("## Select Your Riding Gear:")
|
303 |
cols = st.columns(len(RIDING_GEAR))
|
304 |
for i, gear in enumerate(RIDING_GEAR):
|
|
|
305 |
if cols[i].button(f"{gear['name']} ({gear['type']})", key=f"gear_{i}"):
|
306 |
st.session_state.game_state['riding_gear'] = gear
|
307 |
st.session_state.game_state['gear_strength'] = gear['strength']
|
|
|
317 |
|
318 |
cols = st.columns(3)
|
319 |
for i, action in enumerate(actions):
|
|
|
320 |
if cols[i].button(f"{action['emoji']} {action['name']} ({action['type']})", key=f"action_{i}"):
|
321 |
outcome, success_chance = evaluate_action(situation, action, st.session_state.game_state['gear_strength'], st.session_state.game_state['rider_skill'], st.session_state.game_state['history'])
|
322 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
350 |
# π
Display Scoreboard
|
351 |
display_scoreboard(st.session_state.game_state)
|
352 |
|
353 |
+
# Integration point for both functions
|
354 |
+
if not st.session_state.game_state['history_df'].empty:
|
355 |
+
# π Display Markdown Preview
|
356 |
+
st.markdown(create_markdown_preview(st.session_state.game_state['history_df']))
|
357 |
+
|
358 |
+
# π³ Display Heterogeneous Journey Graph
|
359 |
+
st.markdown("## π³ Your Journey (Heterogeneous Graph)")
|
360 |
+
nodes, edges = create_heterogeneous_graph(st.session_state.game_state['history_df'])
|
361 |
+
try:
|
362 |
+
streamlit_flow('cat_rider_flow',
|
363 |
+
nodes,
|
364 |
+
edges,
|
365 |
+
layout=TreeLayout(direction='down'),
|
366 |
+
fit_view=True,
|
367 |
+
height=600)
|
368 |
+
except Exception as e:
|
369 |
+
st.error(f"An error occurred while rendering the journey graph: {str(e)}")
|
370 |
+
st.markdown("Please try refreshing the page if the graph doesn't appear.")
|
371 |
+
|
372 |
+
# π Character Stats Visualization
|
373 |
+
data = {"Stat": ["Gear Strength π‘οΈ", "Rider Skill π"],
|
374 |
+
"Value": [st.session_state.game_state['gear_strength'], st.session_state.game_state['rider_skill']]}
|
375 |
+
df = pd.DataFrame(data)
|
376 |
+
fig = px.bar(df, x='Stat', y='Value', title="Cat Rider Stats π")
|
377 |
+
st.plotly_chart(fig)
|
378 |
|
379 |
if __name__ == "__main__":
|
380 |
main()
|