This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·266 lines (230 loc) · 9.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
# std
import logging
import random
from typing import Optional, Union
import traceback
# 3rd
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
# ours
from codenames.users import User
from codenames.messages import Message
from codenames.util import handle_raw_input
from codenames.room import Room
from codenames.words import interpret_query
from codenames.playground import Playground
app = Flask(__name__)
app.config["SECRET_KEY"] = "vnkdjnfjknfl1232#"
socketio = SocketIO(app)
number_of_rooms = 10
rooms = [Room(k) for k in range(number_of_rooms)]
@app.route("/")
def sessions():
return render_template(
"session.html",
)
def messageReceived(methods=("GET", "POST")):
print("message was received!!!")
@socketio.on("user_connect")
def handle_user_connect(json, methods=("GET", "POST")):
"""Gets triggered by the frontend if a user connected, i.e., the browser
window was opened by someone (not necessarily a login yet)."""
app.logger.info("user connected: " + str(json))
@socketio.on("login")
def handle_user_login_event(json, methods=("GET", "POST")):
"""Gets triggered by the frontend if a user logs in. We trigger 'user_login'
on the frontend of the client who made the request to tell whether the
login was successful or not and update our user database and write a chat
message to everyone.
"""
app.logger.info("User logged in: " + str(json))
success = True
if not json["user"]:
success = False
if "team" not in json or not json["team"]:
success = False
if "role" not in json or not json["role"]:
success = False
if "room" not in json:
success = False
username = handle_raw_input(json["user"])
return_json = {"user": json["user"], "success": success} # escaped unicode!
app.logger.debug("Returning json: " + str(return_json))
emit("user_login", return_json, callback=messageReceived)
if success and 0 <= int(json["room"]) < number_of_rooms:
room = rooms[int(json["room"])]
room.users.add_user(User(username, team=json["team"], role=json["role"]))
# todo: Format team and role
msg = "{user} has joined team {team} as {role}.".format(
user=room.users[username].to_html(),
team=json["team"],
role=json["role"],
)
write_chat_message(msg, room)
update_team_info(room)
@socketio.on("chat_message_received")
def handle_chat_message_received(json, methods=("GET", "POST")):
"""Gets triggered by the frontend if a user submits a chat message.
We add it to the list of chat messages and trigger a rebuild of the chat
history in all clients."""
app.logger.info("Received chat message: " + str(json))
if json["user"].strip() and json["message"].strip():
write_chat_message(
handle_raw_input(json["message"]),
rooms[int(json["room"])],
user=handle_raw_input(json["user"]),
)
@socketio.on("game_restart")
def reset_game(json, methods=("GET", "POST")):
room_number = int(json["room"])
app.logger.info(f"Restart game in room {room_number}")
room = rooms[room_number]
write_chat_message(
"{user} has restarted the game".format(
user=room.users[json["user"]].to_html()
),
room,
)
room.restart()
# fixme: don't use private
room.users._username2user = {}
socketio.emit("force_reload_page", {"room": room_number})
def write_chat_message(
message: str, room: Room, user: Optional[Union[str, User]] = None
) -> None:
"""Write a chat message to everyone."""
if isinstance(user, str):
user = room.users[user]
room.messages.add_message(Message(message, user=user))
update_chat_messages(room)
if message.startswith("/newwords "):
query = message.replace("/newwords ", "").strip()
try:
words = interpret_query(query)
except:
msg = "An exception occurred.\n Traceback:"
msg += traceback.format_exc()
print(msg)
room.messages.add_message(Message(msg))
update_chat_messages(room)
else:
room.playground = Playground.generate_from_words(words)
ask_all_sessions_to_request_playground_update(room)
def update_chat_messages(room):
"""Triggers an update of the chat history box in all clients."""
return_json = {"message": room.messages.to_html(), "room": room.number}
socketio.emit("update_chat_messages", return_json, callback=messageReceived)
def update_team_info(room):
"""Triggers an update of the team overview box in all clients."""
out = ""
for team in ["red", "blue"]:
out += f'<span class="team_info_{team}">'
out += "<b>Team {team} ({score})</b>".format(
team=team.capitalize(), score=room.playground.get_score()[team]
)
out += "<div>"
for member in room.users.get_by_team(team):
out += member.to_html("user-role") + " "
out += "</div>"
out += "</span>"
return_json = {"team_overview_html": out, "room": room.number}
app.logger.debug("Update team overview " + str(return_json))
socketio.emit("update_teams", return_json)
@socketio.on("tile_clicked")
def handle_tile_clicked_event(json, methods=("GET", "POST")):
"""Gets triggered by the frontend if someone clicks on one of the tiles of
the playground. We check if the corresponding user triggers anything by
clicking and if yes, ask all sessions to request a playground update."""
app.logger.info("Tile clicked: " + str(json))
room = rooms[int(json["room"])]
user = room.users[handle_raw_input(json["user"])]
if room.playground.get_winner():
print("Winner: ", room.playground.get_winner())
return
if user.role == "guesser":
tile = room.playground.tiles[json["index"]]
tile.clicked_by = user
msg = (
'<span class="badge {team}">{name}</span> clicked '
"'{content}'. ".format(
team=user.team,
name=user.name.capitalize(),
content=tile.content,
)
)
write_chat_message(msg, room)
msg = '<span class="badge badge-secondary">Bot</span> '
if tile.correctly_clicked:
congratulations = [
"And that was the right decision! Congratulations! 😉 ",
"Good job! I expected nothing less. 😎 ",
"Nice one! 😇 ",
]
msg += random.choice(congratulations)
else:
if tile.type != "bomb":
insults = [
"Booooo! ",
"Hope you're proud of yourself... 😒 ",
"Really? I expected better of you. 😦 ",
"I'm pretty disappointed, but oh well. 🙄 ",
]
msg += random.choice(insults)
else:
insults = [
"I'm speechless. 🤮",
"I don't even have words for this. 🤮",
"A disgrace. 🤮",
]
msg += random.choice(insults)
write_chat_message(msg, room)
winner = room.playground.get_winner()
if winner:
msg = f"Team {winner} won! Congratulations!"
write_chat_message(msg, room)
ask_all_sessions_to_request_playground_update(room)
update_team_info(room) # score was updated
# todo: If bomb, the game should be over
else:
msg = (
"Tile clicking ignored, because user {name} is not "
"of role 'guesser', but of role '{role}'.".format(
name=user.name, role=user.role
)
)
app.logger.info(msg)
@socketio.on("request_update_playground")
def update_playground(json, methods=("GET", "POST")):
"""Gets triggered by the frontend (not from the backend!) and sends back
the html for the playground.
The reason that this should only be triggered by the backend is that we
need to know the user's role before serving them html (a 'guesser' gets
different html than an 'explainer') and we only have the username if they
make the request, not we.
"""
room = rooms[int(json["room"])]
user = room.users[handle_raw_input(json["user"])]
role = user.role
app.logger.info(
"Handing HTML for viewer role {role} to user {name} in room {number}".format(
role=role, name=user.name, number=room.number
)
)
emit(
"update_playground",
{
"playground_html": room.playground.to_html(user_role=role),
"room": room.number,
},
)
def ask_all_sessions_to_request_playground_update(room: Room):
"""If `update_playground` has to be called by the backend, how do we force
everyone to update the playground? We kindly ask them to submit an
update themselves."""
socketio.emit(
"ask_all_sessions_to_request_update_playground", {"room": room.number}
)
if __name__ == "__main__":
app.logger.setLevel(logging.DEBUG)
socketio.run(app, debug=True)