-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.py
241 lines (193 loc) · 6.98 KB
/
minesweeper.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import random
import json
import os
import threading
import asyncio
from fastapi import APIRouter, WebSocket
from gunicorn.workers.base import Worker
import shortuuid
# from socketio import AsyncServer
# from ..main import socket_manager as sm
from starlette.websockets import WebSocketDisconnect
import redis
from utils.mine import generate_nickname, generate_color
from decouple import config
router = APIRouter()
r = redis.Redis(
host=str(config('REDIS_HOST')),
port=int(config('REDIS_PORT')),
db=int(config('MINE_REDIS_DB')),
charset="utf-8",
decode_responses=True,
)
pubsub = r.pubsub()
pubsub.subscribe('mine')
active_connections_key = 'active_connections'
profiles_key = 'profiles'
history_key = 'history'
size_key = 'size'
bomb_key = 'bomb'
bomb_coords_key = 'bomb_coords'
def get_redis_size():
size = int(r.get(size_key) or 0)
return size
def get_redis_bomb():
bomb = int(r.get(bomb_key) or 0)
return bomb
def get_redis_bomb_coords():
bomb_coords = r.get(bomb_coords_key) or '[]'
return bomb_coords
def get_history():
history = []
for history_str in r.lrange(history_key, 0, -1):
history.append(json.loads(history_str))
return history
def game_reset():
r.delete(history_key)
bomb_coords = []
r.set(size_key, 12)
r.set(bomb_key, 20)
size = int(r.get(size_key) or 0)
bombs = int(r.get(bomb_key) or 0)
for _ in range(bombs):
row = random.randint(0, size - 1)
col = random.randint(0, size - 1)
bomb_coords.append((row, col))
r.set(bomb_coords_key, json.dumps(bomb_coords))
game_reset()
class ConnectionManager:
def __init__(self):
listener = threading.Thread(target=self.between_worker)
listener.start()
self.websockets: dict[str, WebSocket] = {}
async def connect(self, websocket: WebSocket, sid: str):
profile = {
"sid": sid,
"name": generate_nickname(),
"color": generate_color(),
}
self.websockets[sid] = websocket
r.set(sid, json.dumps(profile))
r.lpush(profiles_key, json.dumps(profile))
await websocket.send_json({"type": "profile", **profile})
async def disconnect(self, sid: str):
if sid in self.websockets:
del self.websockets[sid]
await self.mine_players()
async def mine_players(self):
print(os.getpid(), 'mine_players')
players = []
for profile_str in r.lrange(profiles_key, 0, -1):
profile = json.loads(profile_str)
players.append({"sid": profile['sid'], "name": profile["name"], "color": profile["color"]})
for ws in self.websockets.values():
await ws.send_json({"type": "players", "players": players})
async def mine_start(self, websocket: WebSocket):
await websocket.send_json(
{
"type": "start",
"size": get_redis_size(),
"bomb_coords": get_redis_bomb_coords(),
"history": get_history(),
}
)
async def mine_action(self, sid: str, data: dict):
profile_str = r.get(sid)
if not profile_str:
return
profile = json.loads(profile_str)
name = profile['name']
color = profile['color']
response_data = {**data, "name": name, "color": color}
r.lpush(history_key, json.dumps(response_data))
r.publish('mine', json.dumps({"type": "action", **response_data}))
async def mine_reset(self):
global size
response_data = {
"type": "restart",
"size": get_redis_size(),
"bomb_coords": get_redis_bomb_coords(),
"history": [],
}
for profile_str in r.lrange(profiles_key, 0, -1):
profile = json.loads(profile_str)
if profile['sid'] in self.websockets:
await self.websockets[profile['sid']].send_json(response_data)
async def broadcast(self, data: dict):
if data['type'] == 'action':
profile = json.loads(r.get(data['sid']) or '')
response_data = {
'type': 'action',
'action': data['action'],
'x': data['x'],
'y': data['y'],
'name': profile['name'],
'color': profile['color'],
"history": get_history(),
}
for profile_str in r.lrange(profiles_key, 0, -1):
profile = json.loads(profile_str)
if profile['sid'] in self.websockets:
await self.websockets[profile['sid']].send_json(response_data)
elif data['type'] == 'disconnect':
await self.disconnect(data['sid'])
async def worker(self):
for message in pubsub.listen():
if message['type'] == 'message':
data = json.loads(message['data'])
if data['type'] == 'reset':
await self.mine_reset()
elif data['type'] == 'players':
await self.mine_players()
elif data['type'] == 'action':
await self.broadcast(data)
elif data['type'] == 'disconnect':
await self.disconnect(data['sid'])
time.sleep(0.001)
def between_worker(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.worker())
loop.close()
connection_manager = ConnectionManager()
import time
@router.websocket("/ws/mine_connect")
async def connect(websocket: WebSocket):
await websocket.accept()
sid = shortuuid.uuid()
try:
await connection_manager.connect(websocket, sid)
while True:
data = await websocket.receive_json()
if data['type'] == "start":
await connection_manager.mine_start(websocket)
elif data['type'] == "players":
r.publish(
'mine',
json.dumps({"type": "players"}),
)
elif data['type'] == "action":
sid = data['sid']
await connection_manager.mine_action(sid, data)
elif data['type'] == 'reset':
game_reset()
r.publish(
'mine',
json.dumps(
{
"type": "reset",
"size": get_redis_size(),
"bomb_coords": get_redis_bomb_coords(),
"history": [],
}
),
)
except WebSocketDisconnect:
r.delete(sid)
list_values = r.lrange(profiles_key, 0, -1)
for value in list_values:
if json.loads(value)['sid'] == sid:
r.lrem(profiles_key, 0, value)
r.publish('mine', json.dumps({"type": "disconnect", "sid": sid}))
await connection_manager.broadcast({"type": "disconnect", "sid": sid})
return