awacke1's picture
Update app.py
c82718a verified
import streamlit as st
import random
import time
from datetime import datetime
# Constants
EMOJIS = ["🎯", "🚀", "🐔", "🌮", "🍩", "🤖", "🎩"]
OBJECTS_MONSTERS = ["Spork", "Magic Sock", "Laughing Potion", "Ticklish Dragon", "Dancing Goblin", "Elf with Bad Jokes", "Orc who thinks he's a Chef"]
CHAT_HISTORY_LENGTH = 5
ACTION_HISTORY_LENGTH = 5
SCORE_HISTORY_LENGTH = 5
# Ensure data files exist
def ensure_data_files():
files = ['players.txt', 'chat.txt', 'actions.txt', 'objects.txt', 'scores.txt']
for f in files:
try:
with open(f, 'r') as file:
pass
except FileNotFoundError:
with open(f, 'w') as file:
file.write('')
# Add player
def add_player(name):
with open('players.txt', 'a') as file:
file.write(name + '\n')
# Get all players
def get_all_players():
with open('players.txt', 'r') as file:
return [line.strip() for line in file.readlines()]
# Add chat message
def add_chat_message(name, message):
with open('chat.txt', 'a') as file:
file.write(name + ': ' + message + '\n')
# Get recent chat messages
def get_recent_chat_messages():
with open('chat.txt', 'r') as file:
return file.readlines()[-CHAT_HISTORY_LENGTH:]
# Add action
def add_action(name, action):
with open('actions.txt', 'a') as file:
file.write(name + ': ' + action + '\n')
# Get recent actions
def get_recent_actions():
with open('actions.txt', 'r') as file:
return file.readlines()[-ACTION_HISTORY_LENGTH:]
# Add score
def add_score(name, score):
with open('scores.txt', 'a') as file:
file.write(name + ': ' + str(score) + '\n')
# Get recent scores
def get_recent_scores(name):
with open('scores.txt', 'r') as file:
return [line for line in file.readlines() if name in line][-SCORE_HISTORY_LENGTH:]
# Streamlit interface
def app():
st.title("Emoji Fun Battle!")
# Ensure data files exist
ensure_data_files()
# Player name input
player_name = st.text_input("Enter your name:")
if player_name and player_name not in get_all_players():
add_player(player_name)
players = get_all_players()
st.write(f"Players: {', '.join(players)}")
# Display timer and emoji buttons
st.write("Click on the emoji when the timer reaches 0 for a surprise!")
timer = st.empty()
for i in range(3, 0, -1):
timer.write(f"Timer: {i}")
time.sleep(1)
timer.write("Timer: 0")
chosen_emoji = random.choice(EMOJIS)
emoji_clicked = st.button(chosen_emoji)
if emoji_clicked and player_name:
event = random.choice(["won a dance battle", "ate too many tacos", "got tickled by a ghost", "found a magic spork"])
action_message = f"{player_name} {event} with {chosen_emoji}"
add_action(player_name, action_message)
# Display recent actions
st.write("Recent Hilarious Actions:")
for action in get_recent_actions():
st.write(action.strip())
# Interactions with objects and monsters
interaction = st.selectbox("Choose a funny interaction:", OBJECTS_MONSTERS)
if st.button("Interact") and player_name:
dice_roll = random.randint(1, 6)
outcome = f"rolled a {dice_roll} and"
if dice_roll > 4:
outcome += f" successfully made friends with {interaction}!"
score = 10
else:
outcome += f" had a laugh-off with {interaction}!"
score = 5
action_message = f"{player_name} {outcome}"
add_action(player_name, action_message)
add_score(player_name, score)
# Chat
chat_message = st.text_input("Send a funny message:")
if st.button("Send") and player_name:
add_chat_message(player_name, chat_message)
st.write("Recent chat messages:")
for message in get_recent_chat_messages():
st.write(message.strip())
# Display recent scores
if player_name:
st.write(f"{player_name}'s Recent Scores:")
for score in get_recent_scores(player_name):
st.write(score.strip())
# Refresh every 5 seconds for better user experience
st.write("Refreshing in 5 seconds...")
time.sleep(5)
st.rerun()
# Run the Streamlit app
app()