File size: 3,458 Bytes
7092f63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import json

with open('index.html', 'rb') as fp:
    html = fp.read()

house = {}


async def application(scope, receive, send):
    if scope['type'] == 'websocket':
        event = await receive()
        if event['type'] != 'websocket.connect':
            return
        await send({'type': 'websocket.accept'})
        event = await receive()
        data = json.loads(event['text'])
        if data['type'] != 'EnterRoom' or not data['id'] or not data['room']:
            await send({'type': 'websocket.close', 'code': 403})
            return
        room_id = data['room']
        user_id = data['id']
        if room_id not in house:
            house[room_id] = {
                'black': None,
                'white': None,
                'pieces': [],
                'sends': [],
                'users': [],
            }
        room = house[room_id]
        old = False
        if room['black'] == user_id or room['white'] == user_id:
            old = True
            if user_id in room['users']:
                old_send = room['sends'][room['users'].index(user_id)]
                room['sends'].remove(old_send)
                room['users'].remove(user_id)
                await old_send({'type': 'websocket.close', 'code': 4000})
        else:
            if room['black'] is None:
                room['black'] = user_id
            elif room['white'] is None:
                room['white'] = user_id
        visiting = room['black'] != user_id and room['white'] != user_id
        room['sends'].append(send)
        room['users'].append(user_id)
        await send({'type': 'websocket.send', 'text': json.dumps({
            'type': 'InitializeRoomState',
            'pieces': room['pieces'],
            'visiting': visiting,
            'black': room['black'] == user_id if not visiting else bool(len(room['pieces']) % 2),
            'ready': bool(room['black'] and room['white']),
        })})
        if not old and (room['black'] == user_id or room['white'] == user_id):
            for _send in room['sends']:
                if _send == send:
                    continue
                await _send({'type': 'websocket.send', 'text': json.dumps({
                    'type': 'AddPlayer',
                    'ready': bool(room['black'] and room['white']),
                })})
        while True:
            event = await receive()
            if event['type'] == 'websocket.disconnect':
                if send in room['sends']:
                    room['sends'].remove(send)
                    room['users'].remove(user_id)
                    if len(room['pieces']) == 0 and len(room['sends']) == 0:
                        del house[room_id]
                break
            data = json.loads(event['text'])
            if data['type'] == 'DropPiece':
                room['pieces'].append((data['x'], data['y']))
                for _send in room['sends']:
                    if _send == send:
                        continue
                    await _send({'type': 'websocket.send', 'text': json.dumps({
                        'type': 'DropPiece',
                        'x': data['x'],
                        'y': data['y'],
                    })})
    elif scope['type'] == 'http':
        request = await receive()
        if request['type'] == 'http.request':
            await send({'type': 'http.response.start', 'status': 200})
            await send({'type': 'http.response.body', 'body': html})