araeyn commited on
Commit
55cc794
·
verified ·
1 Parent(s): 627c00c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
app.py CHANGED
@@ -13,41 +13,44 @@ json.dump({}, open("b.json", "w"))
13
  async def echo(websocket):
14
  timout = 2500
15
  async for message in websocket:
16
- jm = json.loads(message)
17
- print(jm)
18
- if jm["token"] == None:
19
- await websocket.send(json.dumps({"type": "error", "data": "No token"}))
20
- p = json.load(open("p.json"))
21
- b = json.load(open("p.json"))
22
- if jm["type"] == "login":
23
- if jm["token"] not in p.keys():
24
- p.update({jm["token"]: {"pos": {"x": 0,"y": 0, "vy": 0}}})
25
- p[jm["token"]]["time"] = time.time() * 1000
26
- json.dump(p, open("p.json", "w"))
27
- await websocket.send(json.dumps({"type": "init", "data": p[jm["token"]]}))
28
- elif jm["type"] == "update":
29
- if time.time() * 1000 - p[jm["token"]]["time"] >= timout:
30
- await websocket.send(json.dumps({"type": "reconnect"}))
31
- else:
32
- p.update({jm["token"]: jm["data"]})
33
  json.dump(p, open("p.json", "w"))
34
- players = []
35
- bullets = []
36
- for key, value in p.items():
37
- if key != jm["token"]:
38
- if time.time() * 1000 - value["time"] < timout:
39
- players.append({"pos": value["pos"], "name": value["name"], "color": value["color"]})
40
- for key, value in b.items():
41
- if key != jm["token"]:
42
- if (time.time() - value["time"] / 1000) * 60 <= 420:
43
- bullets.append(value)
44
- await websocket.send(json.dumps({"type": "bullet", "data": bullets}))
45
- await websocket.send(json.dumps({"type": "update", "data": {"players": players}}))
46
- elif jm["type"] == "bullet":
47
- if jm["token"] not in b.keys():
48
- b.update({jm["token"]: []})
49
- b[jm["token"]].append(jm["data"])
50
- json.dump(b, open("b.json"))
 
 
 
 
 
 
 
 
 
 
51
 
52
  async def main():
53
  async with serve(echo, "0.0.0.0", 7860):
 
13
  async def echo(websocket):
14
  timout = 2500
15
  async for message in websocket:
16
+ try:
17
+ jm = json.loads(message)
18
+ if jm["token"] == None:
19
+ await websocket.send(json.dumps({"type": "error", "data": "No token"}))
20
+ p = json.load(open("p.json"))
21
+ b = json.load(open("p.json"))
22
+ if jm["type"] == "login":
23
+ if jm["token"] not in p.keys():
24
+ p.update({jm["token"]: {"pos": {"x": 0,"y": 0, "vy": 0}}})
25
+ p[jm["token"]]["time"] = time.time() * 1000
 
 
 
 
 
 
 
26
  json.dump(p, open("p.json", "w"))
27
+ await websocket.send(json.dumps({"type": "init", "data": p[jm["token"]]}))
28
+ elif jm["type"] == "update":
29
+ if time.time() * 1000 - p[jm["token"]]["time"] >= timout:
30
+ await websocket.send(json.dumps({"type": "reconnect"}))
31
+ else:
32
+ p.update({jm["token"]: jm["data"]})
33
+ json.dump(p, open("p.json", "w"))
34
+ players = []
35
+ bullets = []
36
+ for key, value in p.items():
37
+ if key != jm["token"]:
38
+ if time.time() * 1000 - value["time"] < timout:
39
+ players.append({"pos": value["pos"], "name": value["name"], "color": value["color"]})
40
+ for key, value in b.items():
41
+ if key != jm["token"]:
42
+ if (time.time() - value["time"] / 1000) * 60 <= 420:
43
+ bullets.append(value)
44
+ await websocket.send(json.dumps({"type": "bullet", "data": bullets}))
45
+ await websocket.send(json.dumps({"type": "update", "data": {"players": players}}))
46
+ elif jm["type"] == "bullet":
47
+ if jm["token"] not in b.keys():
48
+ b.update({jm["token"]: []})
49
+ b[jm["token"]].append(jm["data"])
50
+ json.dump(b, open("b.json"))
51
+ except Exception as e:
52
+ print(e)
53
+ print(message)
54
 
55
  async def main():
56
  async with serve(echo, "0.0.0.0", 7860):