Skip to content

Commit df60f76

Browse files
v0.4.8 update :
- Open release download page if client is outdated - Server checks client verson - Great stability improvment
1 parent e1e57e9 commit df60f76

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
lines changed

lelonmo/main_online.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from lelonmo import persist_data, socket_client
44

5-
5+
server_ip = str()
66
def main_online(host=''):
7+
global server_ip
78
try:
89
if not host:
910
if bool(persist_data.DATA["debug"]["RANDOMIZE_UUID"]):
@@ -13,14 +14,17 @@ def main_online(host=''):
1314
if not host:
1415
host = persist_data.DATA['online']['last_ip']
1516
persist_data.update_key("last_ip", host, "online")
17+
socket_client.atexit.register(socket_client.exit_server)
1618
socket_client.main(host)
1719
while socket_client.human_to_bool("Do you want to play again ?\n"):
1820
socket_client.main(host)
1921
exit()
2022
except KeyboardInterrupt:
23+
socket_client.exit_server()
2124
print("Exiting ...")
2225
exit(0)
2326
except Exception as e:
27+
socket_client.exit_server()
2428
print("Something went wrong :", e)
2529
exit(-1)
2630

lelonmo/persist_data.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
save_file = home + ".lelonmo_save.json"
88

99
default_data = dict(
10-
version="0.4.7",
10+
version="00.4.8",
11+
update_url='https://github.com/claj-ndc/LeLonMo/releases/latest',
1112
online=dict(
1213
uuid=str(uuid.uuid4()),
1314
name="",
@@ -68,6 +69,8 @@ def _repair_data():
6869
if DATA[key1] != default_data[key1]:
6970
print("Version changed")
7071
DATA[key1] = default_data[key1]
72+
elif key1 == "update_url":
73+
DATA[key1] = default_data[key1]
7174
else:
7275
for key2 in default_data[key1].keys():
7376
if not key2 in DATA[key1]:

lelonmo/socket_client.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import json
32
import sys
43
import socket
@@ -23,21 +22,26 @@ def _send_data(data: str, host: str):
2322

2423
# Allow identifying users at anytime
2524
s.send(
26-
bytearray(f"{persist_data.DATA['online']['uuid']}%{data}", "utf-8"))
25+
bytearray(
26+
"%llm_client%" \
27+
f"{persist_data.DATA['version'].replace('.', str())}%" \
28+
f"{persist_data.DATA['online']['uuid']}%" \
29+
f"{data}",
30+
"utf-8"
31+
)
32+
)
2733
# UUID is never shared with other players
2834
v = s.recv(1024)
2935
s.close()
3036
return v
3137
except ConnectionRefusedError:
3238
print("Server error : Connexion refused")
33-
exit_server()
3439
s.close()
35-
sys.exit()
40+
exit()
3641
except Exception as e:
37-
exit_server()
3842
s.close()
3943
print("Server error :", e)
40-
sys.exit(0)
44+
exit(0)
4145

4246

4347
def _status(host): # Small function to get server status
@@ -53,8 +57,12 @@ def _wait_for_status(key, host): # Wait for a specific status in order to conti
5357
def _join_game(wb, ip="localhost"): # Initiate the connexion between client and server
5458
player_id = _send_data("join%" + persist_data.DATA["online"]["name"], ip)
5559
if player_id == b"started":
60+
wb.clear()
5661
wb.add("The game you tried to join already started")
57-
exit()
62+
wb.add("Press [ENTER] to try again")
63+
wait(1) # Avoid spam
64+
input()
65+
return _join_game(wb, ip)
5866
elif player_id == b"wait%":
5967
wb.add("Waiting for admin to restart the game ...")
6068
while player_id == b"wait%":
@@ -64,6 +72,16 @@ def _join_game(wb, ip="localhost"): # Initiate the connexion between client and
6472
return int(player_id)
6573
elif player_id == b"":
6674
wb.add("Error while joining, try again later")
75+
elif player_id == b"outdated%":
76+
wb.add("Your client is outdated, please download the latest version")
77+
import webbrowser
78+
webbrowser.open(persist_data.DATA["update_url"])
79+
exit_server()
80+
elif player_id == b"outdated%update":
81+
wb.add("Your client is outdated and automatic update install is not available")
82+
import webbrowser
83+
webbrowser.open(persist_data.DATA["update_url"])
84+
exit_server()
6785
else:
6886
try:
6987
int(player_id)
@@ -229,6 +247,7 @@ def main(host="localhost"):
229247
playerboard = PlayerUpdate(wb, host)
230248
playerboard.start()
231249
_wait_for_status("results", host)
250+
wait(0.25) # Avoid replay spam for the last player if it has low ping
232251
playerboard.enable = False
233252
wb.updatable = ""
234253

@@ -249,13 +268,12 @@ def exit_server():
249268
try:
250269
s.connect((server_ip, PORT))
251270
s.send(
252-
bytearray(f"{persist_data.DATA['online']['uuid']}%{'leave%'}", "utf-8"))
271+
bytearray(f"%llm_client%{persist_data.DATA['version'].replace('.', str())}%{persist_data.DATA['online']['uuid']}%leave%", "utf-8"))
253272
s.close()
254273
except:
255274
pass
256-
257-
258-
atexit.register(exit_server)
275+
finally:
276+
s.close()
259277

260278

261279
if __name__ == "__main__":

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import Quartz
5959
import objc
6060
except:
61-
print("Quartz and objc is not installed, trying to install it automatically ...")
61+
print("Quartz and objc are not installed, trying to install it automatically ...")
6262
import subprocess
6363
import sys
6464
command = [sys.executable, "-m", "pip", "install",

server/persist_data.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
save_file = home + ".lelonmo_server_save.json"
88

99
default_data = dict(
10-
version="0.4.7",
10+
version="0.4.8",
11+
client_version="0048",
1112
online=dict(
1213
uuid=str(uuid.uuid4()),
1314
name="",
@@ -59,6 +60,8 @@ def _repair_data():
5960
if DATA[key1] != default_data[key1]:
6061
print("Version changed")
6162
DATA[key1] = default_data[key1]
63+
elif key1 in ["client_version", "update_url"]:
64+
DATA[key1] = default_data[key1]
6265
else:
6366
for key2 in default_data[key1].keys():
6467
if not key2 in DATA[key1]:

server/socket_server.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
import threading
55
import socket
66

7-
from server import letter_generator
7+
from server import letter_generator, persist_data
88
from server import word_check
99

1010

1111
class Game():
12-
def __init__(self, LANGUAGE):
12+
def __init__(self, LANGUAGE, MAX_PLAYERS = 10):
1313
self.state = 0
1414
print("[I] Waiting for admin ...")
1515
self.game_data = dict(players=list())
1616
self.LANGUAGE = LANGUAGE
17+
self.MAX_PLAYERS = MAX_PLAYERS
1718

1819
def _new_player(self, uuid, name, ip, status="Connected"):
1920
if name in [i['name'] for i in self.game_data["players"]]:
@@ -28,9 +29,11 @@ def _new_player(self, uuid, name, ip, status="Connected"):
2829
elif name == "":
2930
print("[V] Refused", uuid, ": Empty name")
3031
return -1
32+
elif len(self.game_data["players"]) > self.MAX_PLAYERS:
33+
print("Refused", uuid, ": Too many people")
3134
else:
3235
self.game_data["players"].append(
33-
dict(uuid=uuid, name=name, ip=ip, word="", status=status, computer_afk="false"))
36+
dict(uuid=uuid, name=name, ip=ip, word="", status=status, computer_afk=False))
3437
print("[I] Added player", name, "with uuid", uuid)
3538
return len(self.game_data["players"])-1
3639

@@ -85,9 +88,21 @@ def _reset(self):
8588
self.game_data = dict(players=list())
8689

8790
def handle_data(self, data: str, client_socket, client_thread):
88-
r = re.search("(.{36})%(.*)", data)
89-
uuid = r.group(1)
90-
msg = r.group(2)
91+
try:
92+
r = re.search("%llm_client%(.{4})%(.{36})%(.*)", data)
93+
version = r.group(1)
94+
uuid = r.group(2)
95+
msg = r.group(3)
96+
except:
97+
print("[W] Invalid request :", data)
98+
return data, client_socket, client_socket
99+
try:
100+
if int(version) < int(persist_data.DATA["client_version"]):
101+
self._answer("outdated%", client_socket)
102+
return
103+
except:
104+
self._answer("outdated%", client_socket)
105+
return
91106
try:
92107
admin = uuid == self.game_data["admin"]["uuid"]
93108
except KeyError:
@@ -145,7 +160,7 @@ def handle_data(self, data: str, client_socket, client_thread):
145160
f'[I] {self.game_data["players"][self._get_player_id(uuid)]["name"]} finished')
146161
all_finished = True
147162
for i in self.game_data["players"]:
148-
if i["status"] != "Finished":
163+
if i["status"] != "Finished" and not i["computer_afk"]:
149164
all_finished = False
150165
break
151166
if all_finished:
@@ -166,6 +181,7 @@ def handle_data(self, data: str, client_socket, client_thread):
166181
self.handle_data(data, client_socket, client_thread)
167182
elif msg.startswith("join%"):
168183
self._answer("wait%", client_socket)
184+
return
169185

170186

171187
class ClientThread(threading.Thread):
@@ -177,8 +193,11 @@ def __init__(self, ip, port, clientsocket, game):
177193
self.clientsocket = clientsocket
178194

179195
def run(self):
180-
data = self.clientsocket.recv(1024)
181-
self.game.handle_data(data.decode("utf-8"), self.clientsocket, self)
196+
data = self.clientsocket.recv(1024).decode("utf-8")
197+
if data.startswith("%llm_client%"):
198+
self.game.handle_data(data, self.clientsocket, self)
199+
else:
200+
self.game._answer("outdated%", self.clientsocket)
182201

183202

184203
class MainThread(threading.Thread):

0 commit comments

Comments
 (0)