import os import streamlit as st import numpy as np import pandas as pd import plotly.express as px st.set_page_config(layout='wide') st.title('🎲 Random Dice Game') dice_types = [{'name': 'Six-sided Dice', 'sides': 6, 'emoji': '🎲'}, {'name': 'Twenty-sided Dice', 'sides': 20, 'emoji': '🎯'}, {'name': 'Thirty-sided Dice', 'sides': 30, 'emoji': '🎯'}, {'name': 'One Hundred-sided Dice', 'sides': 100, 'emoji': '💯'}] if 'username' not in st.session_state: st.session_state.username = '' if 'dice_roll_history' not in st.session_state: st.session_state.dice_roll_history = pd.DataFrame() def username_input(): st.text_input('👤 Username', key='username') def display_dice_roll_distribution(): dice_type = st.selectbox('🎲 Choose a type of dice', dice_types, format_func=lambda d: f"{d['name']} {d['emoji']}") num_rolls = st.slider('🔄 How many times do you want to roll the dice?', 1, 1000000, 1000) rolls = np.random.randint(1, dice_type['sides'] + 1, num_rolls, dtype=np.uint64) roll_counts = pd.Series(rolls).value_counts().sort_index() fig = px.sunburst(names=[f'Roll {i}' for i in roll_counts.index], parents=['Dice Rolls'] * dice_type['sides'], values=roll_counts.values, color=[f'Roll {i}' for i in roll_counts.index], color_discrete_sequence=px.colors.qualitative.Dark24, maxdepth=2) fig.update_layout(title='📈 Dice Roll Distribution', margin=dict(l=20, r=20, t=40, b=20), width=800, height=600) show_labels = st.checkbox('🏷️ Show Labels', value=True) if not show_labels: fig.update_traces(textinfo='none') fig.show() return dice_type, rolls def display_bonus_match(bonus_match, bonus_dice_type, bonus_dice_emoji): if bonus_match: st.success(f'🎉 You rolled a {bonus_dice_type} {bonus_dice_emoji} on the first roll!') def update_dice_roll_history(dice_type, rolls): bonus_match = False for dice in dice_types: if rolls[0] == dice['sides']: bonus_match = True bonus_dice_type = dice['name'] bonus_dice_emoji = dice['emoji'] break dice_roll_history = st.session_state.dice_roll_history new_roll_data = pd.DataFrame({'Roll': rolls, 'Count': np.ones(len(rolls), dtype=np.uint64), 'DiceNumberOfSides': [dice_type['sides']] * len(rolls), 'Username': [st.session_state.username] * len(rolls)}) if bonus_match: new_roll_data['BonusMatchToDiceName'] = [bonus_dice_type] * len(rolls) new_roll_data['BonusMatchToDiceEmoji'] = [bonus_dice_emoji] * len(rolls) dice_roll_history = dice_roll_history.append(new_roll_data, ignore_index=True) st.session_state.dice_roll_history = dice_roll_history return bonus_match def display_dice_roll_results(): download_history = st.checkbox('📥 Download Roll History', value=False) if download_history: csv_filename = f'dice_roll_history_{st.session_state.username}.csv' st.markdown(f'📥 [Download roll history]({csv_filename})') dice_roll_history = st.session_state.dice_roll_history[['Roll', 'Count', 'DiceNumberOfSides']] if st.checkbox('🎉 Show Bonus Matches', value=True): dice_roll_history = dice_roll_history.join( st.session_state.dice_roll_history[['BonusMatchToDiceName', 'BonusMatchToDiceEmoji']]) if st.checkbox('👤 Show Username', value=True): dice_roll_history['Username'] = st.session_state.dice_roll_history['Username'] dice_roll_history.to_csv(csv_filename, index=False) dice_roll_history_files = [f for f in os.listdir() if f.startswith('dice_roll_history_') and f.endswith('.csv')] if len(dice_roll_history_files) > 0: st.write('📚 Roll History Files:') for history_file in dice_roll_history_files: st.markdown(f'[Download {history_file}]({history_file})') st.sidebar.title('🔧 Settings') username_input() if st.button('🎲 Roll Dice'): dice_type, rolls = display_dice_roll_distribution() display_bonus_match(update_dice_roll_history(dice_type, rolls), dice_type['name'], dice_type['emoji']) display_dice_roll_results() st.write(""" 🍞 Bread, 🥐 Croissant, 🥖 Baguette Bread, 🫓 Flatbread, 🥨 Pretzel, 🥯 Bagel, 🥞 Pancakes, 🧇 Waffle, 🧀 Cheese Wedge, 🍖 Meat on Bone, 🍗 Poultry Leg, 🥩 Cut of Meat, 🥓 Bacon, 🍔 Hamburger, 🍟 French Fries, 🍕 Pizza, 🌭 Hot Dog, 🥪 Sandwich, 🌮 Taco, 🌯 Burrito, 🫔 Tamale, 🥙 Stuffed Flatbread, 🧆 Falafel, 🥚 Egg, 🍳 Cooking, 🥘 Shallow Pan of Food, 🍲 Pot of Food, 🫕 Fondue, 🥣 Bowl with Spoon, 🥗 Green Salad, 🍿 Popcorn, 🧈 Butter, 🧂 Salt, 🥫 Canned Food, 🍱 Bento Box, 🍘 Rice Cracker, 🍙 Rice Ball, 🍚 Cooked Rice, 🍛 Curry Rice, 🍜 Steaming Bowl, 🍝 Spaghetti, 🍠 Roasted Sweet Potato, 🍢 Oden, 🍣 Sushi, 🍤 Fried Shrimp, 🍥 Fish Cake with Swirl, 🥮 Moon Cake, 🍡 Dango, 🥟 Dumpling, 🥠 Fortune Cookie, 🥡 Takeout Box, 🦪 Oyster, 🍦 Soft Ice Cream, 🍧 Shaved Ice, 🍨 Ice Cream, 🍩 Doughnut, 🍪 Cookie, 🎂 Birthday Cake, 🍰 Shortcake, 🧁 Cupcake, 🥧 Pie, 🍫 Chocolate Bar, 🍬 Candy, 🍭 Lollipop, 🍮 Custard, 🍯 Honey Pot, 🍼 Baby Bottle, 🥛 Glass of Milk, ☕ Hot Beverage, 🫖 Teapot, 🍵 Teacup Without Handle, 🍶 Sake, 🍾 Bottle with Popping Cork, 🍷 Wine Glass, 🍸 Cocktail Glass, 🍹 Tropical Drink, 🍺 Beer Mug, 🍻 Clinking Beer Mugs, 🥂 Clinking Glasses, 🥃 Tumbler Glass, 🫗 Pouring Liquid, 🥤 Cup with Straw, 🧋 Bubble Tea, 🧃 Beverage Box, 🧉 Mate, 🧊 Ice, 🥢 Chopsticks, 🍽️ Fork and Knife with Plate, 🍴 Fork and Knife, 🥄 Spoon, 🫙 Jar") """)