araeyn commited on
Commit
ac49cb1
·
verified ·
1 Parent(s): 3ac3831

Update app.py

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