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
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
2
+ import uvicorn
3
+ import json
4
+ import os
5
+ import uuid
6
+
7
+ app = FastAPI()
8
+
9
+ PLAYERS_FILE = "players.json"
10
+ ENTREPRISES_FILE = "entreprises.json"
11
+
12
+ clients = {}
13
+ logged_users = {}
14
+ player_positions = {}
15
+
16
+ # ======================================================
17
+ # PLAYERS
18
+ # ======================================================
19
+
20
+ def load_players():
21
+
22
+ if os.path.exists(PLAYERS_FILE):
23
+
24
+ with open(
25
+ PLAYERS_FILE,
26
+ "r",
27
+ encoding="utf-8"
28
+ ) as f:
29
+
30
+ return json.load(f)
31
+
32
+ return {}
33
+
34
+ def save_players():
35
+
36
+ with open(
37
+ PLAYERS_FILE,
38
+ "w",
39
+ encoding="utf-8"
40
+ ) as f:
41
+
42
+ json.dump(
43
+ players_data,
44
+ f,
45
+ indent=4,
46
+ ensure_ascii=False
47
+ )
48
+
49
+ # ======================================================
50
+ # ENTREPRISES
51
+ # ======================================================
52
+
53
+ def load_entreprises():
54
+
55
+ if os.path.exists(
56
+ ENTREPRISES_FILE
57
+ ):
58
+
59
+ with open(
60
+ ENTREPRISES_FILE,
61
+ "r",
62
+ encoding="utf-8"
63
+ ) as f:
64
+
65
+ return json.load(f)
66
+
67
+ return {}
68
+
69
+ def save_entreprises():
70
+
71
+ with open(
72
+ ENTREPRISES_FILE,
73
+ "w",
74
+ encoding="utf-8"
75
+ ) as f:
76
+
77
+ json.dump(
78
+ entreprises_data,
79
+ f,
80
+ indent=4,
81
+ ensure_ascii=False
82
+ )
83
+
84
+ players_data = load_players()
85
+ entreprises_data = load_entreprises()
86
+
87
+ # ======================================================
88
+ # WEBSOCKET
89
+ # ======================================================
90
+
91
+ @app.websocket("/ws")
92
+ async def websocket_endpoint(
93
+ ws: WebSocket
94
+ ):
95
+
96
+ await ws.accept()
97
+
98
+ session_id = str(
99
+ uuid.uuid4()
100
+ )
101
+
102
+ clients[session_id] = ws
103
+
104
+ username = None
105
+
106
+ await ws.send_text(json.dumps({
107
+ "type":"welcome",
108
+ "id":session_id
109
+ }))
110
+
111
+ try:
112
+
113
+ while True:
114
+
115
+ text = await ws.receive_text()
116
+
117
+ data = json.loads(text)
118
+
119
+ msg_type = data.get(
120
+ "type",
121
+ ""
122
+ )
123
+
124
+ # =====================================
125
+ # LOGIN
126
+ # =====================================
127
+
128
+ if msg_type == "login":
129
+
130
+ user = str(
131
+ data.get(
132
+ "username",
133
+ ""
134
+ )
135
+ ).strip()
136
+
137
+ password = str(
138
+ data.get(
139
+ "password",
140
+ ""
141
+ )
142
+ ).strip()
143
+
144
+ if (
145
+ user == ""
146
+ or
147
+ password == ""
148
+ ):
149
+
150
+ await ws.send_text(
151
+ json.dumps({
152
+ "type":"login_failed"
153
+ })
154
+ )
155
+
156
+ continue
157
+
158
+ if user not in players_data:
159
+
160
+ players_data[user] = {
161
+ "password":password,
162
+ "money":0,
163
+ "progress":1
164
+ }
165
+
166
+ save_players()
167
+
168
+ elif (
169
+ players_data[user]["password"]
170
+ != password
171
+ ):
172
+
173
+ await ws.send_text(
174
+ json.dumps({
175
+ "type":"login_failed"
176
+ })
177
+ )
178
+
179
+ continue
180
+
181
+ username = user
182
+
183
+ logged_users[
184
+ session_id
185
+ ] = username
186
+
187
+ await ws.send_text(
188
+ json.dumps({
189
+ "type":"login_success"
190
+ })
191
+ )
192
+
193
+ await ws.send_text(
194
+ json.dumps({
195
+ "type":"player_data",
196
+ "money":
197
+ players_data[user]["money"],
198
+ "progress":
199
+ players_data[user]["progress"]
200
+ })
201
+ )
202
+
203
+ for sid in clients.keys():
204
+
205
+ if sid == session_id:
206
+ continue
207
+
208
+ await ws.send_text(
209
+ json.dumps({
210
+ "type":"player_join",
211
+ "id":sid
212
+ })
213
+ )
214
+
215
+ for sid, client in clients.items():
216
+
217
+ if sid == session_id:
218
+ continue
219
+
220
+ try:
221
+
222
+ await client.send_text(
223
+ json.dumps({
224
+ "type":"player_join",
225
+ "id":session_id
226
+ })
227
+ )
228
+
229
+ except:
230
+ pass
231
+
232
+ for path, owner in (
233
+ entreprises_data.items()
234
+ ):
235
+
236
+ await ws.send_text(
237
+ json.dumps({
238
+ "type":"entreprise_owner",
239
+ "path":path,
240
+ "owner":owner
241
+ })
242
+ )
243
+
244
+ # =====================================
245
+ # POSITION
246
+ # =====================================
247
+
248
+ elif msg_type == "player_update":
249
+
250
+ player_positions[
251
+ session_id
252
+ ] = {
253
+
254
+ "x":
255
+ data.get("x",0),
256
+
257
+ "y":
258
+ data.get("y",0),
259
+
260
+ "z":
261
+ data.get("z",0)
262
+ }
263
+
264
+ data["id"] = session_id
265
+
266
+ for sid, client in clients.items():
267
+
268
+ if sid == session_id:
269
+ continue
270
+
271
+ try:
272
+
273
+ await client.send_text(
274
+ json.dumps(
275
+ data
276
+ )
277
+ )
278
+
279
+ except:
280
+ pass
281
+
282
+ # =====================================
283
+ # MONEY
284
+ # =====================================
285
+
286
+ elif msg_type == "save_money":
287
+
288
+ if username:
289
+
290
+ players_data[
291
+ username
292
+ ]["money"] = int(
293
+ data.get(
294
+ "money",
295
+ 0
296
+ )
297
+ )
298
+
299
+ save_players()
300
+
301
+ # =====================================
302
+ # PROGRESS
303
+ # =====================================
304
+
305
+ elif msg_type == "save_progress":
306
+
307
+ if username:
308
+
309
+ players_data[
310
+ username
311
+ ]["progress"] = int(
312
+ data.get(
313
+ "progress",
314
+ 1
315
+ )
316
+ )
317
+
318
+ save_players()
319
+
320
+ # =====================================
321
+ # ENTREPRISE
322
+ # =====================================
323
+
324
+ elif msg_type == "save_entreprise":
325
+
326
+ path = data.get(
327
+ "path",
328
+ ""
329
+ )
330
+
331
+ owner = data.get(
332
+ "owner",
333
+ ""
334
+ )
335
+
336
+ entreprises_data[
337
+ path
338
+ ] = owner
339
+
340
+ save_entreprises()
341
+
342
+ # =====================================
343
+ # CHANGE OWNER
344
+ # =====================================
345
+
346
+ elif msg_type == "set_entreprise_owner":
347
+
348
+ path = data.get(
349
+ "path",
350
+ ""
351
+ )
352
+
353
+ owner = data.get(
354
+ "owner",
355
+ ""
356
+ )
357
+
358
+ entreprises_data[
359
+ path
360
+ ] = owner
361
+
362
+ save_entreprises()
363
+
364
+ broadcast = {
365
+ "type":"entreprise_owner",
366
+ "path":path,
367
+ "owner":owner
368
+ }
369
+
370
+ for client in clients.values():
371
+
372
+ try:
373
+
374
+ await client.send_text(
375
+ json.dumps(
376
+ broadcast
377
+ )
378
+ )
379
+
380
+ except:
381
+ pass
382
+
383
+ except WebSocketDisconnect:
384
+
385
+ pass
386
+
387
+ finally:
388
+
389
+ # Libérer les entreprises du joueur
390
+
391
+ to_clear = []
392
+
393
+ for path, owner in entreprises_data.items():
394
+
395
+ if owner == session_id:
396
+
397
+ to_clear.append(path)
398
+
399
+ for path in to_clear:
400
+
401
+ entreprises_data[path] = ""
402
+
403
+ for client in clients.values():
404
+
405
+ try:
406
+
407
+ await client.send_text(
408
+ json.dumps({
409
+ "type":"entreprise_owner",
410
+ "path":path,
411
+ "owner":""
412
+ })
413
+ )
414
+
415
+ except:
416
+ pass
417
+
418
+ save_entreprises()
419
+
420
+ if session_id in clients:
421
+ del clients[session_id]
422
+
423
+ if session_id in logged_users:
424
+ del logged_users[session_id]
425
+
426
+ if session_id in player_positions:
427
+ del player_positions[session_id]
428
+
429
+ for client in clients.values():
430
+
431
+ try:
432
+
433
+ await client.send_text(
434
+ json.dumps({
435
+ "type":"player_leave",
436
+ "id":session_id
437
+ })
438
+ )
439
+
440
+ except:
441
+ pass
442
+
443
+ # ======================================================
444
+ # HUGGING FACE
445
+ # ======================================================
446
+
447
+ if __name__ == "__main__":
448
+
449
+ port = int(
450
+ os.environ.get(
451
+ "PORT",
452
+ 7860
453
+ )
454
+ )
455
+
456
+ uvicorn.run(
457
+ app,
458
+ host="0.0.0.0",
459
+ port=port
460
+ )