awacke1 commited on
Commit
818dd29
Β·
1 Parent(s): 1d43974

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -54
app.py CHANGED
@@ -3,23 +3,13 @@ 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 = {
9
- 4: "πŸŽ‚",
10
- 6: "πŸ€",
11
- 8: "πŸ„",
12
- 10: "🍁",
13
- 12: "πŸ‚",
14
- 20: "πŸƒ",
15
- 50: "πŸ’",
16
- 100: "🌟"
17
- }
18
 
19
- # Define the dice types
20
  DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100]
21
-
22
- # Define the default number of rolls
23
  DEFAULT_ROLLS = 10
24
 
25
  # Define a function to roll dice
@@ -38,18 +28,15 @@ def plot_tokens(health_tokens, coin_tokens):
38
  # Define the app
39
  st.title("Dice Rolling Game")
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,44 +44,21 @@ for dice_type in DICE_TYPES:
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
-
80
- # Concatenate all roll histories into a single dictionary
81
- all_rolls = {}
82
- for dice_type, rolls in history["roll_history"].items():
83
- all_rolls[f"{dice_type}-sided dice"] = rolls
84
- all_rolls = pd.DataFrame(all_rolls)
85
-
86
- # Create a Series for each token type
87
- health_tokens = pd.Series(history["health_tokens"], name="Health Tokens")
88
- coin_tokens = pd.Series(history["coin_tokens"], name="Coin Tokens")
89
-
90
- # Concatenate all data into a single DataFrame
91
- df = pd.concat([all_rolls, health_tokens, coin_tokens], axis=1)
92
-
93
- # Save history to CSV file
94
  df.to_csv(filename, index=False)
95
 
96
  # Show download link for CSV file
97
- st.write("Download the dice rolling history:")
98
- with open(filename, "rb") as f:
99
- bytes_data = f.read()
100
-
 
3
  import pandas as pd
4
  import plotly.graph_objects as go
5
  from datetime import datetime
6
+ from base64 import b64encode
7
 
8
  # Define emoji list
9
+ EMOJI_LIST = {4: "πŸŽ‚", 6: "πŸ€", 8: "πŸ„", 10: "🍁", 12: "πŸ‚", 20: "πŸƒ", 50: "πŸ’", 100: "🌟"}
 
 
 
 
 
 
 
 
 
10
 
11
+ # Define the dice types and default number of rolls
12
  DICE_TYPES = [4, 6, 8, 10, 12, 20, 50, 100]
 
 
13
  DEFAULT_ROLLS = 10
14
 
15
  # Define a function to roll dice
 
28
  # Define the app
29
  st.title("Dice Rolling Game")
30
 
31
+ # Get username and number of rolls
32
+ username = st.text_input("Enter your username:")
33
+ num_rolls = st.slider("Choose the number of rolls:", 1, 1000000, DEFAULT_ROLLS)
 
 
 
 
34
 
35
+ # Roll dice for each type and accumulate high rolls and tokens
36
+ history = {"health_tokens": [0], "coin_tokens": [0]}
37
  for dice_type in DICE_TYPES:
38
  rolls = roll_dice(num_rolls, dice_type)
39
+ highest_rolls = sum(roll == dice_type for roll in rolls)
40
  st.write(f"Results for {dice_type}-sided dice:")
41
  for roll in rolls:
42
  st.write(f"{EMOJI_LIST[dice_type]} {roll}")
 
44
  st.write("Congratulations! You rolled the highest value!")
45
  if dice_type == 100:
46
  st.write("Adding 10 coins for rolling over 90 on 100-sided dice.")
 
 
47
  history["coin_tokens"].append(history["coin_tokens"][-1] + 10)
48
+ history[f"{dice_type}-sided dice high rolls"] = highest_rolls
49
+ history["roll_history"] = {**history.get("roll_history", {}), dice_type: rolls}
50
+ history["health_tokens"].append(history.get("20-sided dice high rolls", 0))
51
+ history["coin_tokens"].append(history.get("100-sided dice high rolls", 0))
52
 
53
+ # Plot token accumulation
 
 
 
 
54
  st.write("Token Accumulation:")
55
  plot_tokens(history["health_tokens"], history["coin_tokens"])
56
 
57
+ # Create DataFrame and save to CSV file
58
+ df = pd.concat([pd.DataFrame(history["roll_history"]), pd.DataFrame(history["health_tokens"], columns=["Health Tokens"]), pd.DataFrame(history["coin_tokens"], columns=["Coin Tokens"])], axis=1)
59
  timestamp = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
60
+ filename = f"{username}_{timestamp}.csv"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  df.to_csv(filename, index=False)
62
 
63
  # Show download link for CSV file
64
+ st.markdown(f'<a href="data:file/csv;base64,{b64encode(open(filename, "rb").read()).decode()}" download="{filename}">Download CSV File</a>', unsafe_allow_html=True)