Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
|
|
3 |
import plotly.graph_objects as go
|
|
|
4 |
|
5 |
# Define emoji list
|
6 |
EMOJI_LIST = {
|
@@ -20,14 +22,6 @@ DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100]
|
|
20 |
# Define the default number of rolls
|
21 |
DEFAULT_ROLLS = 100
|
22 |
|
23 |
-
# Define session state
|
24 |
-
if "username" not in st.session_state:
|
25 |
-
st.session_state.username = ""
|
26 |
-
if "rolls" not in st.session_state:
|
27 |
-
st.session_state.rolls = DEFAULT_ROLLS
|
28 |
-
if "history" not in st.session_state:
|
29 |
-
st.session_state.history = {}
|
30 |
-
|
31 |
# Define a function to roll dice
|
32 |
def roll_dice(num_rolls, dice_type):
|
33 |
rolls = np.random.randint(1, dice_type + 1, size=num_rolls)
|
@@ -46,15 +40,16 @@ st.title("Dice Rolling Game")
|
|
46 |
|
47 |
# Get username
|
48 |
st.write("Enter your username:")
|
49 |
-
|
50 |
|
51 |
# Get number of rolls
|
52 |
st.write("Choose the number of rolls:")
|
53 |
-
|
54 |
|
55 |
# Get dice types and roll dice
|
|
|
56 |
for dice_type in DICE_TYPES:
|
57 |
-
rolls = roll_dice(
|
58 |
st.write(f"Results for {dice_type}-sided dice:")
|
59 |
for roll in rolls:
|
60 |
st.write(f"{EMOJI_LIST[dice_type]} {roll}")
|
@@ -62,20 +57,30 @@ for dice_type in DICE_TYPES:
|
|
62 |
st.write("Congratulations! You rolled the highest value!")
|
63 |
if dice_type == 100:
|
64 |
st.write("Adding 10 coins for rolling over 90 on 100-sided dice.")
|
65 |
-
if "coin_tokens" not in
|
66 |
-
|
67 |
-
|
68 |
-
if "roll_history" not in
|
69 |
-
|
70 |
-
|
71 |
|
72 |
# Plot tokens
|
73 |
-
if "health_tokens" not in
|
74 |
-
|
75 |
-
if "coin_tokens" not in
|
76 |
-
|
77 |
st.write("Token Accumulation:")
|
78 |
-
plot_tokens(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
-
#
|
81 |
-
st.
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
import plotly.graph_objects as go
|
5 |
+
from datetime import datetime
|
6 |
|
7 |
# Define emoji list
|
8 |
EMOJI_LIST = {
|
|
|
22 |
# Define the default number of rolls
|
23 |
DEFAULT_ROLLS = 100
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Define a function to roll dice
|
26 |
def roll_dice(num_rolls, dice_type):
|
27 |
rolls = np.random.randint(1, dice_type + 1, size=num_rolls)
|
|
|
40 |
|
41 |
# Get username
|
42 |
st.write("Enter your username:")
|
43 |
+
username = st.text_input("Username")
|
44 |
|
45 |
# Get number of rolls
|
46 |
st.write("Choose the number of rolls:")
|
47 |
+
num_rolls = st.slider("Number of Rolls", 1, 1000000, DEFAULT_ROLLS)
|
48 |
|
49 |
# Get dice types and roll dice
|
50 |
+
history = {}
|
51 |
for dice_type in DICE_TYPES:
|
52 |
+
rolls = roll_dice(num_rolls, dice_type)
|
53 |
st.write(f"Results for {dice_type}-sided dice:")
|
54 |
for roll in rolls:
|
55 |
st.write(f"{EMOJI_LIST[dice_type]} {roll}")
|
|
|
57 |
st.write("Congratulations! You rolled the highest value!")
|
58 |
if dice_type == 100:
|
59 |
st.write("Adding 10 coins for rolling over 90 on 100-sided dice.")
|
60 |
+
if "coin_tokens" not in history:
|
61 |
+
history["coin_tokens"] = [0]
|
62 |
+
history["coin_tokens"].append(history["coin_tokens"][-1] + 10)
|
63 |
+
if "roll_history" not in history:
|
64 |
+
history["roll_history"] = {}
|
65 |
+
history["roll_history"][dice_type] = rolls
|
66 |
|
67 |
# Plot tokens
|
68 |
+
if "health_tokens" not in history:
|
69 |
+
history["health_tokens"] = [0]
|
70 |
+
if "coin_tokens" not in history:
|
71 |
+
history["coin_tokens"] = [0]
|
72 |
st.write("Token Accumulation:")
|
73 |
+
plot_tokens(history["health_tokens"], history["coin_tokens"])
|
74 |
+
|
75 |
+
# Save history to CSV file
|
76 |
+
timestamp = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
|
77 |
+
filename = f"{timestamp}.csv"
|
78 |
+
data = {"Username": [username], "Date": [timestamp], **history}
|
79 |
+
df = pd.DataFrame(data)
|
80 |
+
df.to_csv(filename, index=False)
|
81 |
|
82 |
+
# Show download link for CSV file
|
83 |
+
st.write("Download the dice rolling history:")
|
84 |
+
with open(filename, "rb") as f:
|
85 |
+
bytes_data = f.read()
|
86 |
+
|