-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
577 lines (515 loc) · 19.9 KB
/
ui.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from cfg import *
import json
from typing import *
import time
import osu_irc
from utils import *
app = Flask(__name__)
socketio = SocketIO(app)
ui_cfg = uiConfig()
rms_cfg = roomsConfig()
TEAM_RED = 1
TEAM_BLUE = 2
TEAM_NONE = 0
NOTIF_TYPE_INFO = 'info'
NOTIF_TYPE_WARNING = 'warning'
NOTIF_TYPE_ERROR = 'error'
NOTIF_TYPE_SUCCESS = 'success'
NOTIF_TYPE_DEFAULT = ''
team_map = {
'red': TEAM_RED,
'blue': TEAM_BLUE,
'none': TEAM_NONE
}
# TODO: Implement cfg for this and settings, and make it PMs only
debug_block_list = [
"BLOCKED_USER_DEBUG"
]
debug_block_list = [name.lower() for name in debug_block_list]
debug_flag = False
# ---------------------
# Helper Functions
# ---------------------
def create_notif(content: str, duration: int = 5000, notif_type: str = NOTIF_TYPE_DEFAULT) -> None:
"""
Create a notification with content and duration in ms.
"""
emit('notif', {
"content": content,
"duration": duration,
"type": notif_type
})
def start_chat(channel_name: str, channel_type: int) -> None:
"""
Start a chat with a channel name and type from the UI server's side.
"""
create_notif(f"Opening chat {channel_name}...", 5000, notif_type=NOTIF_TYPE_INFO)
emit('cmd_req_ch', {
'channel': channel_name,
'type': channel_type
}, broadcast=True)
def close_chat(channel_name: str) -> None:
"""
Close a chat with a channel name from the UI server's side.
"""
create_notif(f"Closing chat {channel_name}...", 5000, notif_type=NOTIF_TYPE_INFO)
emit('cmd_part', {
'channel': channel_name
}, broadcast=True)
def command_parse(command: str) -> None:
"""
Parse a command string and execute it.
Commands: query, part, clear, timer, matchtimer
"""
parts = command.strip().split(" ")
command = parts[0].lower()
args = parts[1:]
aliases = {
"/q": "/query",
"/pm": "/query",
"/chat": "/query",
"/join": "/query",
"/l": "/part",
"/leave": "/part",
"/close": "/part",
"/t": "/timer",
"/mt": "/matchtimer",
"/s": "/savelog"
}
command = aliases.get(command, command)
if command != "/query" and chats.current_chat is None:
create_notif("No chat open.", notif_type=NOTIF_TYPE_WARNING)
return
if command == "/query":
if len(args) == 0:
# TODO: make this work, the <> is currently escaped in HTML
create_notif("Usage: /query <channel>", notif_type=NOTIF_TYPE_WARNING)
else:
start_chat(args[0], osu_irc.CHANNEL_TYPE_ROOM if args[0].startswith("#") else osu_irc.CHANNEL_TYPE_PM)
elif command == "/part":
close_chat(chats.current_chat) if len(args) == 0 else close_chat(args[0])
elif command == "/clear":
chats.get_current_chat.clear_messages()
emit('cmd_clear', {}, broadcast=True)
elif command == "/timer":
if len(args) == 0:
handle_send_msg({
"content": f"!mp timer {chats.get_current_chat.timer}"
})
else:
handle_send_msg({
"content": f"!mp timer {args[0]}"
})
chats.get_current_chat.set_timer(args[0])
emit('set_timer_input', {
'timer': args[0]
})
elif command == "/matchtimer":
if len(args) == 0:
handle_send_msg({
"content": f"!mp start {chats.get_current_chat.match_timer}"
})
else:
chats.get_current_chat.set_match_timer(args[0])
emit('set_match_timer_input', {
'timer': args[0]
})
# TODO: match timer logic here
handle_send_msg({
"content": f"!mp start {args[0]}"
})
elif command == "/savelog":
if len(args) == 0:
emit('cmd_savelog_response', {
'messages': chats.get_messages(chats.get_current_chat.channel_name),
'channel': chats.get_current_chat.channel_name
})
else:
ch_insensitive = case_insensitive_get(chats.chats, args[0])
if ch_insensitive:
args[0] = ch_insensitive
else:
create_notif(f"Channel {args[0]} not found.", notif_type=NOTIF_TYPE_ERROR)
return
emit('cmd_savelog_response', {
'messages': chats.get_messages(args[0]),
'channel': args[0]
})
else:
create_notif(f"Command {command} not found.", notif_type=NOTIF_TYPE_WARNING)
# ---------------------
# Chat Classes
# ---------------------
class Chat():
def __init__(self, channel_name: str, channel_type: int, **kwargs):
"""
Class for a chat channel. Should only ever be created by the Chats class.
"""
self.type = channel_type
self.channel_name = channel_name
self.alias = self.channel_name
self.messages: List[List] = []
# TODO: implement this or delete it depending on feedback
# 1 = red, 2 = blue, 0 = none
self.teams: Dict[str, int] = {}
self.timer = kwargs['timer'] if 'timer' in kwargs else 120
self.match_timer = kwargs['match_timer'] if 'match_timer' in kwargs else 5
def add_message(self, message: Dict[str, Any]) -> None:
"""
Add a message to the chat channel.
"""
# Regex for team changes here (must be issued by BanchoBot)
if message['room_name'] == self.channel_name:
self.messages.append([message['time_recv']*1000, message['user_name'], message['content']])
else:
create_notif(f"Something has gone terribly wrong, code MSG-001", notif_type=NOTIF_TYPE_ERROR)
raise ValueError(f"Message not in channel {self.channel_name}")
# TODO: Implement this or delete it depending on feedback
def team_change(self, user: str, team: int) -> None:
"""
Change a user's team in the chat channel.
"""
# Should only be 0, 1, 2
self.teams[user] = team
emit('team_change', {
'username': user,
'team': team,
'channel': self.channel_name
}, broadcast=True)
def set_timer(self, timer: int) -> None:
"""
Set the timer for the chat channel.
"""
self.timer = timer
def set_match_timer(self, match_timer: int) -> None:
"""
Set the start timer for the chat channel.
"""
self.match_timer = match_timer
def set_alias(self, alias: str) -> None:
"""
Set the alias for the chat channel.
"""
self.alias = alias
def __repr__(self) -> str:
return f"<{self.__class__.__name__} channel_name='{self.channel_name}' num_messages={len(self.messages)}>"
def __str__(self) -> str:
return f"Chat on channel {self.channel_name} with {len(self.messages)} messages."
@property
def message_count(self) -> int:
return len(self.messages)
def clear_messages(self):
self.messages.clear()
class Chats():
def __init__(self):
"""
Main class for handling chat messages. Should only ever be created once. Note that username is read directly from the cfg. file and not validated for case by the API.
"""
self.username: str = None
self.chats: Dict[str, Chat] = {}
self.current_chat: str = None
# Fetch username from osu_irc
socketio.on('nickname', self.change_nickname)
def change_nickname(self, data: Dict[str, Any]) -> None:
"""
Pretty important function, since we start with not having a username. This is called when the user logs in.
"""
print(f"User logged in: {data['nickname']}")
self.username = data['nickname']
def add_chat(self, channel_name: str, channel_type: int, **kwargs) -> None:
"""
Add a chat channel to the chat list.
Called when a message comes in that isn't in the chat list or when a new chat is opened (query/add/etc)
"""
if case_insensitive_get(self.chats, channel_name):
return
self.chats[channel_name] = Chat(channel_name, channel_type, **kwargs)
# We only need to do this here because all other methods of opening a channel result
# in a bounce to here anyway
rms_cfg.add_room(channel_name)
emit('bounce_tab_open', {
'channel': channel_name
}, broadcast=True)
def remove_chat(self, channel_name: str) -> None:
"""
Remove a chat channel from the chat list. Bounces the tab close event to the IRC client so it can PART
"""
if channel_name in self.chats:
self.chats.pop(channel_name)
socketio.emit('bounce_tab_close', {
'channel': channel_name
})
if self.chat_count == 0:
self.current_chat = None
else:
create_notif(f"Something has gone terribly wrong, code CHN-001", notif_type=NOTIF_TYPE_ERROR)
raise ValueError(f"Channel {channel_name} not found.")
def get_chat(self, channel_name: str) -> Chat:
"""
Get a chat channel from the chat list.
"""
return self.chats.get(channel_name, None)
def add_message(self, message: Dict[str, Any]) -> None:
"""
Called when a message is received from the IRC client. Adds the message to the appropriate chat channel.
Bounces it to the UI client if the chat is currently open.
Also bounces the tab open event if the chat is not currently open.
"""
if case_insensitive_get(self.chats, message['room_name']):
message['room_name'] = case_insensitive_get(self.chats, message['room_name'])
if message['room_name'] not in self.chats:
self.add_chat(message['room_name'], message['channel_type'])
self.chats[message['room_name']].add_message(message)
if self.current_chat == message['room_name']:
emit('bounce_recv_msg', {
'time': message["time_recv"]*1000, # convert to ms
'user': message["user_name"],
'content': message["content"],
'team': self.chats[message['room_name']].teams.get(message["user_name"], TEAM_NONE)
}, broadcast=True)
else:
# TODO: unread indicator
# This should be conditional and only sent once based on the unread flag.
create_notif(f"New message in {message['room_name']}", notif_type=NOTIF_TYPE_INFO)
def set_current_chat(self, channel_name: str) -> None:
"""
Set the current chat channel.
"""
self.current_chat = channel_name
def get_messages(self, channel_name: str) -> List[List]:
return self.chats[channel_name].messages
@property
def get_current_chat(self) -> Chat:
"""
Get the current chat channel from the chat list.
"""
return self.chats.get(self.current_chat, None)
@property
def chat_count(self) -> int:
return len(self.chats)
@property
def message_count(self) -> int:
return sum([chat.message_count for chat in self.chats.values()])
@property
def channel_names(self) -> List[str]:
return list(self.chats.keys())
def __repr__(self) -> str:
return f"<{self.__class__.__name__} num_chats={len(self.chats)}>"
def __str__(self) -> str:
return f"Chats with {len(self.chats)} channels, {self.chats}. Current chat: {self.current_chat}"
chats = Chats()
# ---------------------
# Main Routes
# ---------------------
@app.route('/')
def chat():
cur_theme = ui_cfg.get_theme()
# TODO: conditionall reroute to login if not logged in
return render_template('chat.html', cur_theme=cur_theme, themes=THEMES)
@app.route('/login')
def login():
cur_theme = ui_cfg.get_theme()
return render_template('login.html', cur_theme=cur_theme, themes=THEMES)
@app.route('/settings')
def settings():
cur_theme = ui_cfg.get_theme()
return render_template('settings.html', cur_theme=cur_theme, themes=THEMES)
# ---------------------
# Layout SIO Routes
# ---------------------
@socketio.on('theme')
def set_theme(data: Dict[str, Any]):
# default to dark theme
theme: str = data.get('theme', 'dark')
if theme in THEMES:
ui_cfg.set_theme(theme)
else:
# I'd be shocked if this ever happens; it means the user edited some code and didn't know what they were doing
raise ValueError(f"Theme {theme} not found.")
# ---------------------
# Chat SIO Routes
# ---------------------
@socketio.on('connect')
def handle_connect():
# For the sake of simplicity we won't have
# any ident procedures
for chat in chats.chats.values():
emit('bounce_tab_open', {
'channel': chat.channel_name
})
if debug_flag:
debug_connect()
@socketio.on('nickname')
def handle_nickname(data: Dict[str, Any]):
chats.username = data['nickname']
@socketio.on('disconnect')
def handle_disconnect():
print('A Client disconnected')
@socketio.on('tab_open')
def handle_tab_open(data: Dict[str, Any]):
chats.add_chat(data['channel'], data['type'])
@socketio.on('send_msg')
def handle_send_msg(data: Dict[str, Any]):
# Prevent sending attempting to send messages to a nonexistent chat unless
# It's a slash command because /q is a thing
if (chats.current_chat is None or "") and (data["content"][0] != "/"):
create_notif("No chat open.", notif_type=NOTIF_TYPE_WARNING)
return
# Failsafe, this case occurs actually not infrequently, eg. the buttons
if "channel" not in data:
data["channel"] = chats.current_chat
# Prevent a message send if the chat is not created yet
elif data["channel"] not in chats.chats:
create_notif(f"Chat {data['channel']} not found.", notif_type=NOTIF_TYPE_ERROR)
return
# Slash commands
if data["content"][0] == "/":
command_parse(data["content"])
return
emit('bounce_send_msg', {
'content': data["content"],
'channel': data["channel"],
'type': chats.get_chat(data["channel"]).type
}, broadcast=True)
chats.add_message({
'room_name': data["channel"],
'time_recv': time.time(),
'user_name': chats.username,
'content': data["content"]
})
@socketio.on('recv_msg')
def handle_recv_msg(data: Dict[str, Any]):
if data["user_name"].lower() in debug_block_list:
return
if data["channel_type"] == osu_irc.CHANNEL_TYPE_ROOM:
data["room_name"] = f"#{data['room_name']}"
if str(data["room_name"]).lower() == chats.username.lower():
data["room_name"] = data["user_name"]
chats.add_message(data)
# TODO: implement blocking, sound alerts, any required regex here
if data["user_name"].lower() == "banchobot":
create = osu_irc.ReCreateMatch.match(data["content"])
if create:
start_chat(f"#mp_{create.group(1)}", osu_irc.CHANNEL_TYPE_ROOM)
return
# TODO: detect a tournament acronym here
slot = osu_irc.ReSlot.match(data["content"])
if slot:
username = slot.group(1).strip().replace(" ", "_")
team = team_map.get(slot.group(2).strip().lower(), TEAM_NONE)
chats.get_chat(data['room_name']).team_change(username, team)
return
join = osu_irc.ReJoinSlot.match(data["content"])
if join:
username = join.group(1).strip().replace(" ", "_")
team = team_map.get(join.group(2).strip().lower(), TEAM_NONE)
chats.get_chat(data['room_name']).team_change(username, team)
return
change = osu_irc.ReChangeTeam.match(data["content"])
if change:
username = change.group(1).strip().strip().replace(" ", "_")
team = team_map.get(change.group(2).strip().lower(), TEAM_NONE)
chats.get_chat(data['room_name']).team_change(username, team)
return
@socketio.on('tab_swap')
def handle_tab_swap(data: Dict[str, Any]):
chats.set_current_chat(data['channel'])
messages = chats.get_messages(data['channel'])
emit('tab_swap_response', {
'alias': chats.get_chat(data['channel']).alias,
'messages': messages,
'timer': chats.get_chat(data['channel']).timer,
'match_timer': chats.get_chat(data['channel']).match_timer,
'teams': json.dumps(chats.get_chat(data['channel']).teams),
'recent_rooms': rms_cfg.rooms
})
@socketio.on('tab_close')
def handle_tab_close(data: Dict[str, Any]):
chats.remove_chat(data['channel'])
@socketio.on('set_timer')
def handle_set_timer(data: Dict[str, Any]):
chats.get_current_chat.set_timer(data['timer'])
@socketio.on('set_match_timer')
def handle_set_match_timer(data: Dict[str, Any]):
chats.get_current_chat.set_match_timer(data['timer'])
@socketio.on('change_alias')
def change_alias(data: Dict[str, Any]):
chats.get_chat(data['channel']).set_alias(data['alias'])
@socketio.on('debug')
def debug(data: Dict[str, Any]):
print(f'debug flag: {debug_flag}')
print(data)
print(rms_cfg)
print(chats)
# ---------------------
# Starting webserver
# ---------------------
def debug_run():
# This is meant when the UI is being run standalone, so we need to make some fake chats
# Adding chats is optional, since any non empty chat will automatically be added
# However the fake mp is empty so we add it here
global debug_flag
debug_flag = True
socketio.run(app, debug=True, host='localhost', port=5000)
def debug_connect():
global debug_flag
# Careful to only run this once
debug_flag = False
chats.username = "HijiriS"
chats.add_chat("#testchat1", osu_irc.CHANNEL_TYPE_ROOM, timer=120, match_timer=5)
chats.add_chat("testpm1", osu_irc.CHANNEL_TYPE_PM)
chats.add_chat("#mp_12345678", osu_irc.CHANNEL_TYPE_ROOM, timer=90, match_timer=10)
chats.add_message({
'room_name': "#testchat1",
'time_recv': htime("12:00:01 PM"),
'user_name': "HijiriS",
'content': "Test Message Test Message Test Message Test Message Test Message Test Message Test Message Test Message Test Message Test Message Test Message Test Message",
'channel_type': osu_irc.CHANNEL_TYPE_ROOM
})
chats.add_message({
'room_name': "#testchat1",
'time_recv': htime("12:00:01 PM"),
'user_name': "HijiriS",
'content': "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
'channel_type': osu_irc.CHANNEL_TYPE_ROOM
})
chats.add_message({
'room_name': "#testchat1",
'time_recv': mtime("18:00:02"),
'user_name': "Test User",
'content': "TourniRC Test Message",
'channel_type': osu_irc.CHANNEL_TYPE_ROOM
})
chats.add_message({
'room_name': "testpm1",
'time_recv': htime("8:00:01 AM"),
'user_name': "HijiriS",
'content': "Test PM",
'channel_type': osu_irc.CHANNEL_TYPE_PM
})
chats.add_message({
'room_name': "#mp_12345678",
'time_recv': htime("8:00:01 AM"),
'user_name': "HijiriS",
'content': "Test Message for Teams",
'channel_type': osu_irc.CHANNEL_TYPE_ROOM
})
chats.add_message({
'room_name': "#mp_12345678",
'time_recv': htime("8:00:01 AM"),
'user_name': "Pof",
'content': "Test Message for Teams",
'channel_type': osu_irc.CHANNEL_TYPE_ROOM
})
chats.get_chat("#mp_12345678").team_change("HijiriS", TEAM_RED)
chats.get_chat("#mp_12345678").team_change("Pof", TEAM_BLUE)
def prod_run():
socketio.run(app, debug=False, host='localhost', port=5000)
# ---------------------
# Used for debug since we will call all methods from main.py normally
# ---------------------
if __name__ == "__main__":
print(f"http://localhost:5000")
debug_run()