awacke1 commited on
Commit
c11db43
Β·
1 Parent(s): 49a4692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -61
app.py CHANGED
@@ -1,66 +1,68 @@
1
- import streamlit as st
2
- import numpy as np
3
  import pandas as pd
4
- import plotly.express as px
5
-
6
- st.title('Random Dice Game')
7
-
8
- dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '🎲'},
9
- {'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '🎯'},
10
- {'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '🎯'},
11
- {'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '🎲'}]
12
-
13
- if 'name' not in st.session_state:
14
- st.session_state.name = ''
15
- if 'dice_roll_history' not in st.session_state:
16
- st.session_state.dice_roll_history = pd.DataFrame()
17
-
18
- dice_type = st.selectbox('Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}")
19
- num_rolls = st.slider('How many times do you want to roll the dice?', 1, 1000000, 1000)
20
-
21
- rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64)
22
- roll_counts = pd.Series(rolls).value_counts().sort_index()
23
-
24
- fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index],
25
- parents=['Dice Rolls'] * dice_type['sides'],
26
- values=roll_counts.values,
27
- color=[f'Roll {i}' for i in roll_counts.index],
28
- color_discrete_sequence=px.colors.qualitative.Dark24,
29
- maxdepth=2)
30
- fig.update_layout(title='Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600)
31
-
32
- show_labels = st.checkbox('Show Labels', value=True)
33
- if not show_labels:
34
- fig.update_traces(textinfo='none')
35
- fig.show()
36
-
37
- bonus_match = False
38
- for dice in dice_types:
39
- if rolls[0] == dice['sides']:
40
- bonus_match = True
41
- bonus_dice_type = dice['name']
42
- bonus_dice_emoji = dice['emoji']
43
- break
44
-
45
- dice_roll_history = st.session_state.dice_roll_history
46
- new_roll_data = pd.DataFrame({'Roll': rolls,
47
- 'Count': np.ones(num_rolls, dtype=np.uint64),
48
- 'DiceNumberOfSides': [dice_type['sides']] * num_rolls,
49
- 'DiceRollerName': [st.session_state.name] * num_rolls})
50
- if bonus_match:
51
- new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * num_rolls
52
- new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * num_rolls
53
- dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True)
54
- st.session_state.dice_roll_history = dice_roll_history
55
-
56
- if st.button('Download Results'):
57
- filename = f'dice_roll_history_{st.session_state.name}.csv'
58
- st.download_button(label='Download CSV', data=dice_roll_history.to_csv(index=False), file_name=filename, mime='text/csv')
59
-
60
- if st.session_state.dice_roll_history.shape[0] > 0:
61
- st.markdown('### Dice Roll History')
62
- st.dataframe(st.session_state.dice_roll_history)
63
 
 
 
 
64
 
65
  st.write("""
66
 
 
1
+ import os
 
2
  import pandas as pd
3
+ import streamlit as st
4
+
5
+ # Set page title and icon
6
+ st.set_page_config(page_title="Dice Roller", page_icon="🎲")
7
+
8
+ # Set page title
9
+ st.title("🎲 Dice Roller")
10
+
11
+ # Check if username is already set, if not, create a text input to set it
12
+ if 'username' not in st.session_state:
13
+ st.session_state.username = st.text_input("πŸ‘€ Username", max_chars=50)
14
+
15
+ # Define the dice types
16
+ dice_types = {
17
+ "🎲 Six-sided dice": {"sides": 6, "name": "d6", "emoji": "🎲"},
18
+ "🎲 Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "🎲"},
19
+ "🎲 Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "🎲"},
20
+ "🎲 One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "πŸ’―"},
21
+ }
22
+
23
+ # Create a selectbox to choose the dice type
24
+ dice_type = st.selectbox("🎲 Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2])
25
+
26
+ # Create a slider to choose the number of rolls
27
+ num_rolls = st.slider("🎲 How many times do you want to roll the dice?", 1, 1000000, 100)
28
+
29
+ # Create a string with the rolls for the selected dice type
30
+ rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))]
31
+ rolls = rolls[0].strip('[]').split('\n')
32
+
33
+ # Create a button to roll the dice
34
+ if st.button("🎲 Roll Dice"):
35
+ # Display the rolls
36
+ display_roll = [int(i) for i in rolls]
37
+ st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.")
38
+
39
+ # Check if the count is a multiple of the dice sides to see if there's a bonus match
40
+ bonus_match = None
41
+ count = sum(display_roll)
42
+ if count % dice_types[dice_type]['sides'] == 0:
43
+ bonus_match = dice_types[dice_type]['name']
44
+ bonus_emoji = dice_types[dice_type]['emoji']
45
+ st.write(f"πŸŽ‰ Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.")
46
+
47
+ # Create a DataFrame with the rolls, count, and dice number of sides
48
+ df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']})
49
+
50
+ # Check if the file exists
51
+ file_exists = os.path.exists("SharedState.csv")
52
+
53
+ # Append the new rolls to the file
54
+ if file_exists:
55
+ df.to_csv("SharedState.csv", mode='a', header=False, index=False)
56
+ # If the file doesn't exist, create it and write the headers
57
+ else:
58
+ df.to_csv("SharedState.csv", mode='w', header=True, index=False)
59
+
60
+ # Create a checkbox to download the roll history
61
+ download_roll_history = st.checkbox("πŸ“₯ Download Roll History", value=True)
62
 
63
+ # Check if the file exists and display it as a download link
64
+ if os.path.exists("SharedState.csv"):
65
+ data
66
 
67
  st.write("""
68