awacke1 commited on
Commit
1d43974
ยท
1 Parent(s): 68c84b8

Create Backup-app-pyBeforeDownload.py

Browse files
Files changed (1) hide show
  1. Backup-app-pyBeforeDownload.py +99 -0
Backup-app-pyBeforeDownload.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = {
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
26
+ def roll_dice(num_rolls, dice_type):
27
+ rolls = np.random.randint(1, dice_type + 1, size=num_rolls)
28
+ return rolls
29
+
30
+ # Define a function to plot tokens
31
+ def plot_tokens(health_tokens, coin_tokens):
32
+ fig = go.Figure()
33
+ fig.add_trace(go.Scatter(x=list(range(1, len(health_tokens) + 1)), y=health_tokens, name="Health"))
34
+ fig.add_trace(go.Scatter(x=list(range(1, len(coin_tokens) + 1)), y=coin_tokens, name="Coins"))
35
+ fig.update_layout(title="Token Accumulation", xaxis_title="Rolls", yaxis_title="Tokens")
36
+ st.plotly_chart(fig)
37
+
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}")
56
+ if roll == dice_type:
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()