-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer_list.py
More file actions
79 lines (63 loc) · 2.53 KB
/
player_list.py
File metadata and controls
79 lines (63 loc) · 2.53 KB
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
from listeners import Listener
class PlayerList(Listener):
def __init__(self):
connection_pattern = R"Player (?P<type>dis)?connected: (?P<username>.+), xuid: (?P<xuid>\d+)"
super().__init__(connection_pattern)
self.players = set()
def handler(self, gui, matches):
self.update_list(matches)
self.update_gui(gui)
def update_gui(self, gui):
gui.clear_textbox(gui.player_list)
gui.write_textbox(gui.player_list, "\n".join(self.players))
def update_list(self, matches):
player_name = matches.group('username')
quitting = (matches.group('type') == "dis")
if player_name:
if quitting:
try:
self.players.remove(player_name)
except KeyError as error:
print(f"Attempted to remove someone ({error}) who never joined!")
else:
self.players.add(player_name)
if __name__ == "__main__":
print(f"Testing {__file__}")
class FakeGui():
def __init__(self):
self.player_list = "the player textbox"
def clear_textbox(self, textbox):
print(f"Cleared {textbox}.")
def write_textbox(self, textbox, text):
print(f"Wrote '{text}' in {textbox}.")
def connect_string(name, id):
return f"Player connected: {name}, xuid: {id}"
def disconnect_string(name, id):
return f"Player disconnected: {name}, xuid: {id}"
gui = FakeGui()
pl_listener = PlayerList()
# Test with random string.
pl_listener(gui, "Play0r eez deehzconnaykt.")
# Test with correct connection string.
test_string = connect_string("foo", 1337)
pl_listener(gui, test_string)
# Test with incorrect disconnection string.
test_string = disconnect_string("bar", 9001)
pl_listener(gui, test_string)
# Test with correct disconnection string.
test_string = disconnect_string("foo", 1337)
pl_listener(gui, test_string)
# Test with players named "connected", "xuid" and "dis"
test_string = connect_string("connected", 8)
pl_listener(gui, test_string)
test_string = connect_string("xuid", 9)
pl_listener(gui, test_string)
test_string = connect_string("dis", 10)
pl_listener(gui, test_string)
test_string = disconnect_string("xuid", 9)
pl_listener(gui, test_string)
test_string = disconnect_string("connected", 8)
pl_listener(gui, test_string)
test_string = disconnect_string("dis", 10)
pl_listener(gui, test_string)
print("Test completed.")