awacke1 commited on
Commit
c28abe2
Β·
1 Parent(s): 1cc2da1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -63
app.py CHANGED
@@ -5,6 +5,7 @@ import pandas as pd
5
  import plotly.express as px
6
 
7
  st.set_page_config(layout='wide')
 
8
  st.title('🎲 Random Dice Game')
9
 
10
  dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '🎲'},
@@ -12,12 +13,9 @@ dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '🎲'},
12
  {'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '🎯'},
13
  {'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': 'πŸ’―'}]
14
 
15
- if 'username' not in st.session_state:
16
- st.session_state.username = ''
17
- if 'dice_roll_history' not in st.session_state:
18
- st.session_state.dice_roll_history = pd.DataFrame()
19
-
20
  def username_input():
 
 
21
  st.text_input('πŸ‘€ Username', key='username')
22
 
23
  def display_dice_roll_distribution():
@@ -47,66 +45,29 @@ def display_bonus_match(bonus_match, bonus_dice_type, bonus_dice_emoji):
47
  st.success(f'πŸŽ‰ You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!')
48
 
49
  def update_dice_roll_history(dice_type, rolls):
50
- history = st.session_state.dice_roll_history
51
- count = st.session_state.count
52
- dice_name = dice_type['name']
53
- dice_emoji = dice_type['emoji']
54
- bonus_match = None
55
-
56
- # Check if the count matches the number of sides of the dice
57
- if count > 0 and count % dice_type['sides'] == 0:
58
- bonus_match = dice_type['name']
59
- bonus_emoji = dice_type['emoji']
60
-
61
- # Update the dice roll history
62
- if history is None:
63
- history = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_type['sides']})
64
- else:
65
- new_history = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_type['sides']})
66
- history = history.append(new_history, ignore_index=True)
67
-
68
- if bonus_match is not None:
69
- history['BonusMatchToDiceName'] = bonus_match
70
- history['BonusMatchToDiceEmoji'] = bonus_emoji
71
-
72
- # Add username to history
73
- history['Username'] = st.session_state.username
74
-
75
- st.session_state.dice_roll_history = history
76
- return history
77
-
78
-
79
- def display_dice_roll_results():
80
- download_history = st.checkbox('πŸ“₯ Download Roll History', value=True)
81
-
82
- if download_history:
83
- csv_filename = f'dice_roll_history_{st.session_state.username}.csv'
84
- st.markdown(f'πŸ“₯ [Download roll history]({csv_filename})')
85
- dice_roll_history = st.session_state.dice_roll_history[['Roll', 'Count', 'DiceNumberOfSides']]
86
- if st.checkbox('πŸŽ‰ Show Bonus Matches', value=True):
87
- dice_roll_history = dice_roll_history.join(
88
- st.session_state.dice_roll_history[['BonusMatchToDiceName', 'BonusMatchToDiceEmoji']])
89
- if st.checkbox('πŸ‘€ Show Username', value=True):
90
- dice_roll_history['Username'] = st.session_state.dice_roll_history['Username']
91
- dice_roll_history.to_csv(csv_filename, index=False)
92
-
93
- dice_roll_history_files = [f for f in os.listdir() if f.startswith('dice_roll_history_') and f.endswith('.csv')]
94
- if len(dice_roll_history_files) > 0:
95
- st.write('πŸ“š Roll History Files:')
96
- for history_file in dice_roll_history_files:
97
- st.markdown(f'[Download {history_file}]({history_file})')
98
-
99
- st.sidebar.title('πŸ”§ Settings')
100
- username_input()
101
-
102
- if st.button('🎲 Roll Dice'):
103
- dice_type, rolls = display_dice_roll_distribution()
104
- display_bonus_match(update_dice_roll_history(dice_type, rolls), dice_type['name'], dice_type['emoji'])
105
-
106
- display_dice_roll_results()
107
-
108
 
 
109
 
 
 
110
 
111
 
112
  st.write("""
 
5
  import plotly.express as px
6
 
7
  st.set_page_config(layout='wide')
8
+
9
  st.title('🎲 Random Dice Game')
10
 
11
  dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '🎲'},
 
13
  {'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '🎯'},
14
  {'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': 'πŸ’―'}]
15
 
 
 
 
 
 
16
  def username_input():
17
+ if 'username' not in st.session_state:
18
+ st.session_state.username = ''
19
  st.text_input('πŸ‘€ Username', key='username')
20
 
21
  def display_dice_roll_distribution():
 
45
  st.success(f'πŸŽ‰ You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!')
46
 
47
  def update_dice_roll_history(dice_type, rolls):
48
+ bonus_match = False
49
+ for dice in dice_types:
50
+ if rolls[0] == dice['sides']:
51
+ bonus_match = True
52
+ bonus_dice_type = dice['name']
53
+ bonus_dice_emoji = dice['emoji']
54
+ break
55
+
56
+ dice_roll_history = st.session_state.dice_roll_history
57
+ new_roll_data = pd.DataFrame({'Roll': rolls,
58
+ 'Count': np.ones(len(rolls), dtype=np.uint64),
59
+ 'DiceNumberOfSides': [dice_type['sides']] * len(rolls),
60
+ 'Username': [st.session_state.username] * len(rolls)})
61
+ if bonus_match:
62
+ new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * len(rolls)
63
+ new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * len(rolls)
64
+ dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
65
+ st.session_state.dice_roll_history = dice_roll_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ return bonus_match, bonus_dice_type, bonus_dice_emoji
68
 
69
+ def download_dice_roll_history(include_name_column):
70
+ dice_roll
71
 
72
 
73
  st.write("""