import os import pandas as pd import streamlit as st # Set page title and icon st.set_page_config(page_title="Dice Roller", page_icon="🎲") # Set page title st.title("🎲 Dice Roller") # Check if username is already set, if not, create a text input to set it if 'username' not in st.session_state: st.session_state.username = st.text_input("👤 Username", max_chars=50) # Define the dice types dice_types = { "🎲 Six-sided dice": {"sides": 6, "name": "d6", "emoji": "🎲"}, "🎲 Thirty-sided dice": {"sides": 30, "name": "d30", "emoji": "🎲"}, "🎲 Twenty-sided dice": {"sides": 20, "name": "d20", "emoji": "🎲"}, "🎲 One hundred-sided dice": {"sides": 100, "name": "d100", "emoji": "💯"}, } # Create a selectbox to choose the dice type dice_type = st.selectbox("🎲 Select a dice type", options=list(dice_types.keys()), format_func=lambda x: x.split(' ')[-2]) # Create a slider to choose the number of rolls num_rolls = st.slider("🎲 How many times do you want to roll the dice?", 1, 1000000, 100) # Create a string with the rolls for the selected dice type rolls = [str(pd.Series([dice_types[dice_type]["sides"]] * num_rolls).apply(pd.Series.sample, replace=True))] rolls = rolls[0].strip('[]').split('\n') # Create a button to roll the dice if st.button("🎲 Roll Dice"): # Display the rolls display_roll = [int(i) for i in rolls] st.write(f"{dice_types[dice_type]['emoji']} You rolled {len(display_roll)} {dice_types[dice_type]['name']} dice: {', '.join(map(str, display_roll))}.") # Check if the count is a multiple of the dice sides to see if there's a bonus match bonus_match = None count = sum(display_roll) if count % dice_types[dice_type]['sides'] == 0: bonus_match = dice_types[dice_type]['name'] bonus_emoji = dice_types[dice_type]['emoji'] st.write(f"🎉 Bonus match! You rolled a multiple of {dice_types[dice_type]['sides']}.") # Create a DataFrame with the rolls, count, and dice number of sides df = pd.DataFrame({'Roll': rolls, 'Count': count, 'DiceNumberOfSides': dice_types[dice_type]['sides']}) # Check if the file exists file_exists = os.path.exists("SharedState.csv") # Append the new rolls to the file if file_exists: df.to_csv("SharedState.csv", mode='a', header=False, index=False) # If the file doesn't exist, create it and write the headers else: df.to_csv("SharedState.csv", mode='w', header=True, index=False) # Create a checkbox to download the roll history download_roll_history = st.checkbox("📥 Download Roll History", value=True) # Check if the file exists and display it as a download link if os.path.exists("SharedState.csv"): # Read the file into a DataFrame data = pd.read_csv("SharedState.csv") # Check if the user wants to include their name in the output if st.checkbox("💻 Include Username in Output", value=True): # Add the username to the DataFrame data["DiceRollerName"] = st.session_state.username # Check if there is a bonus match and add the bonus match column to the DataFrame if bonus_match: data["BonusMatchToDiceName"] = bonus_match data["BonusMatchToDiceEmoji"] = bonus_emoji # Check if the user wants to download the file if download_roll_history: st.download_button("📥 Download Roll History", data.to_csv(index=False), f"shared_state_{st.session_state.username}.csv", "text/csv") else: # Remove the username from the DataFrame data = data.drop(columns=["DiceRollerName"]) # Check if there is a bonus match and add the bonus match column to the DataFrame if bonus_match: data["BonusMatchToDiceName"] = bonus_match data["BonusMatchToDiceEmoji"] = bonus_emoji # Check if the user wants to download the file if download_roll_history: st.download_button("📥 Download Roll History", data.to_csv(index=False), "shared_state.csv", "text/csv") else: st.write("📜 No roll history available.") 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") """)