File size: 2,925 Bytes
1942a04
 
 
457db31
1942a04
 
362f420
6da28a7
362f420
6da28a7
1942a04
930de32
c40a0b5
1942a04
c40a0b5
1942a04
9c33b9e
3ac3831
 
 
ac49cb1
 
 
 
 
 
 
 
 
 
 
55cc794
ac49cb1
 
 
 
 
 
 
 
 
 
 
 
 
adaa0f6
ac49cb1
f108bc8
1ad18e5
 
 
ac49cb1
 
 
 
 
 
 
 
 
 
 
 
1942a04
0b217aa
1942a04
 
 
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
import asyncio
import json
from websockets.server import serve
import traceback
import time

print("-------")
print("started")
print("-------")

json.dump({}, open("p.json", "w"))
json.dump({}, open("b.json", "w"))

async def echo(websocket):
    timout = 2500
    async for message in websocket:
        if message == "data":
            p = json.load(open("p.json"))
            b = json.load(open("b.json"))
            await websocket.send(str(b))
        else:
            try:
                p = json.load(open("p.json"))
                b = json.load(open("b.json"))
                jm = json.loads(message)
                if jm["token"] == None:
                    await websocket.send(json.dumps({"type": "error", "data": "No token"}))
                if jm["type"] == "login":
                    if jm["token"] not in p.keys():
                        p.update({jm["token"]: {"pos": {"x": 0,"y": 0, "vy": 0}}})
                    p[jm["token"]]["time"] = time.time() * 1000
                    json.dump(p, open("p.json", "w"))
                    await websocket.send(json.dumps({"type": "init", "data": p[jm["token"]]}))
                elif jm["type"] == "update":
                    if time.time() * 1000 - p[jm["token"]]["time"] >= timout:
                        await websocket.send(json.dumps({"type": "reconnect"}))
                    else:
                        p.update({jm["token"]: jm["data"]})
                        json.dump(p, open("p.json", "w"))
                        players = []
                        bullets = []
                        for key, value in p.items():
                            if key != jm["token"]:
                                if time.time() * 1000 - value["time"] < timout:
                                    players.append({"pos": value["pos"], "name": value["name"], "color": value["color"]})
                        await websocket.send(json.dumps({"type": "update", "data": {"players": players}}))
                        for key, value in b.items():
                            if type(value) is list:
                                for i in value:
                                    if (time.time() - i["time"] / 1000) * 60 <= 420:
                                        bullets.append(value)
                        await websocket.send(json.dumps({"type": "bullet", "data": bullets}))
                elif jm["type"] == "bullet":
                    print(jm)
                    if jm["token"] not in b.keys():
                        b.update({jm["token"]: []})
                    b[jm["token"]].append(jm["data"])
                    json.dump(b, open("b.json", "w"))
            except Exception:
                print(traceback.format_exc())
                print(message)
                print(p)
                print(b)
async def main():
    async with serve(echo, "0.0.0.0", 7860):
        await asyncio.Future()

asyncio.run(main())