Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
import time
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
# Constants
|
7 |
+
EMOJIS = ["🎯", "🚀", "🐔", "🌮", "🍩", "🤖", "🎩"]
|
8 |
+
OBJECTS_MONSTERS = ["Spork", "Magic Sock", "Laughing Potion", "Ticklish Dragon", "Dancing Goblin", "Elf with Bad Jokes", "Orc who thinks he's a Chef"]
|
9 |
+
CHAT_HISTORY_LENGTH = 5
|
10 |
+
ACTION_HISTORY_LENGTH = 5
|
11 |
+
SCORE_HISTORY_LENGTH = 5
|
12 |
+
|
13 |
+
# Ensure data files exist
|
14 |
+
def ensure_data_files():
|
15 |
+
files = ['players.txt', 'chat.txt', 'actions.txt', 'objects.txt', 'scores.txt']
|
16 |
+
for f in files:
|
17 |
+
try:
|
18 |
+
with open(f, 'r') as file:
|
19 |
+
pass
|
20 |
+
except FileNotFoundError:
|
21 |
+
with open(f, 'w') as file:
|
22 |
+
file.write('')
|
23 |
+
|
24 |
+
# Add player
|
25 |
+
def add_player(name):
|
26 |
+
with open('players.txt', 'a') as file:
|
27 |
+
file.write(name + '\n')
|
28 |
+
|
29 |
+
# Get all players
|
30 |
+
def get_all_players():
|
31 |
+
with open('players.txt', 'r') as file:
|
32 |
+
return [line.strip() for line in file.readlines()]
|
33 |
+
|
34 |
+
# Add chat message
|
35 |
+
def add_chat_message(name, message):
|
36 |
+
with open('chat.txt', 'a') as file:
|
37 |
+
file.write(name + ': ' + message + '\n')
|
38 |
+
|
39 |
+
# Get recent chat messages
|
40 |
+
def get_recent_chat_messages():
|
41 |
+
with open('chat.txt', 'r') as file:
|
42 |
+
return file.readlines()[-CHAT_HISTORY_LENGTH:]
|
43 |
+
|
44 |
+
# Add action
|
45 |
+
def add_action(name, action):
|
46 |
+
with open('actions.txt', 'a') as file:
|
47 |
+
file.write(name + ': ' + action + '\n')
|
48 |
+
|
49 |
+
# Get recent actions
|
50 |
+
def get_recent_actions():
|
51 |
+
with open('actions.txt', 'r') as file:
|
52 |
+
return file.readlines()[-ACTION_HISTORY_LENGTH:]
|
53 |
+
|
54 |
+
# Add score
|
55 |
+
def add_score(name, score):
|
56 |
+
with open('scores.txt', 'a') as file:
|
57 |
+
file.write(name + ': ' + str(score) + '\n')
|
58 |
+
|
59 |
+
# Get recent scores
|
60 |
+
def get_recent_scores(name):
|
61 |
+
with open('scores.txt', 'r') as file:
|
62 |
+
return [line for line in file.readlines() if name in line][-SCORE_HISTORY_LENGTH:]
|
63 |
+
|
64 |
+
# Streamlit interface
|
65 |
+
def app():
|
66 |
+
st.title("Emoji Fun Battle!")
|
67 |
+
|
68 |
+
# Ensure data files exist
|
69 |
+
ensure_data_files()
|
70 |
+
|
71 |
+
# Player name input
|
72 |
+
player_name = st.text_input("Enter your name:")
|
73 |
+
if player_name and player_name not in get_all_players():
|
74 |
+
add_player(player_name)
|
75 |
+
|
76 |
+
players = get_all_players()
|
77 |
+
st.write(f"Players: {', '.join(players)}")
|
78 |
+
|
79 |
+
# Display timer and emoji buttons
|
80 |
+
st.write("Click on the emoji when the timer reaches 0 for a surprise!")
|
81 |
+
timer = st.empty()
|
82 |
+
for i in range(3, 0, -1):
|
83 |
+
timer.write(f"Timer: {i}")
|
84 |
+
time.sleep(1)
|
85 |
+
timer.write("Timer: 0")
|
86 |
+
|
87 |
+
chosen_emoji = random.choice(EMOJIS)
|
88 |
+
emoji_clicked = st.button(chosen_emoji)
|
89 |
+
if emoji_clicked and player_name:
|
90 |
+
event = random.choice(["won a dance battle", "ate too many tacos", "got tickled by a ghost", "found a magic spork"])
|
91 |
+
action_message = f"{player_name} {event} with {chosen_emoji}"
|
92 |
+
add_action(player_name, action_message)
|
93 |
+
|
94 |
+
# Display recent actions
|
95 |
+
st.write("Recent Hilarious Actions:")
|
96 |
+
for action in get_recent_actions():
|
97 |
+
st.write(action.strip())
|
98 |
+
|
99 |
+
# Interactions with objects and monsters
|
100 |
+
interaction = st.selectbox("Choose a funny interaction:", OBJECTS_MONSTERS)
|
101 |
+
if st.button("Interact") and player_name:
|
102 |
+
dice_roll = random.randint(1, 6)
|
103 |
+
outcome = f"rolled a {dice_roll} and"
|
104 |
+
if dice_roll > 4:
|
105 |
+
outcome += f" successfully made friends with {interaction}!"
|
106 |
+
score = 10
|
107 |
+
else:
|
108 |
+
outcome += f" had a laugh-off with {interaction}!"
|
109 |
+
score = 5
|
110 |
+
action_message = f"{player_name} {outcome}"
|
111 |
+
add_action(player_name, action_message)
|
112 |
+
add_score(player_name, score)
|
113 |
+
|
114 |
+
# Chat
|
115 |
+
chat_message = st.text_input("Send a funny message:")
|
116 |
+
if st.button("Send") and player_name:
|
117 |
+
add_chat_message(player_name, chat_message)
|
118 |
+
|
119 |
+
st.write("Recent chat messages:")
|
120 |
+
for message in get_recent_chat_messages():
|
121 |
+
st.write(message.strip())
|
122 |
+
|
123 |
+
# Display recent scores
|
124 |
+
if player_name:
|
125 |
+
st.write(f"{player_name}'s Recent Scores:")
|
126 |
+
for score in get_recent_scores(player_name):
|
127 |
+
st.write(score.strip())
|
128 |
+
|
129 |
+
# Refresh every 5 seconds for better user experience
|
130 |
+
st.write("Refreshing in 5 seconds...")
|
131 |
+
time.sleep(5)
|
132 |
+
st.experimental_rerun()
|
133 |
+
|
134 |
+
# Run the Streamlit app
|
135 |
+
app()
|