Skip to content

Commit 678e438

Browse files
committed
增加游戏超时删除机制
1 parent 5958569 commit 678e438

6 files changed

Lines changed: 34 additions & 8 deletions

File tree

__pycache__/test1.cpython-38.pyc

251 Bytes
Binary file not shown.
22 Bytes
Binary file not shown.

douzero/env/game.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def step(self, data):
259259
self.get_acting_player_position()
260260
self.game_infoset = self.get_infoset()
261261

262+
self.timestamp = time.time() # 更新时间
263+
262264
# print(f"result:{result} confidence:{confidence}")
263265
return result, confidence
264266

24 Bytes
Binary file not shown.

douzero/evaluation/simulation.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def init(data):
176176
if env.game_over:
177177
# print("{}win, game over!\n".format("farmer" if env.winner == "farmer" else "landlord"))
178178
res_game_over = True
179-
env = None
179+
del env_list[pid]
180180

181181
res_data = {
182182
"pid": pid,
@@ -319,7 +319,8 @@ def init(data):
319319
}
320320
res_action = "play"
321321

322-
env_list[pid] = env
322+
if not res_game_over:
323+
env_list[pid] = env
323324
res_status = "ok"
324325
except Exception as err:
325326
res_action = "init"
@@ -373,7 +374,7 @@ def next(data): # 收到他人出牌
373374
env.step(data) # 上报玩家出牌
374375
if env.game_over:
375376
res_game_over = True
376-
env = None
377+
del env_list[pid]
377378
return {"action": "receive", "data": {}}
378379

379380
else:
@@ -385,15 +386,15 @@ def next(data): # 收到他人出牌
385386
if env.game_over:
386387
# print("{}win, game over!\n".format("farmer" if env.winner == "farmer" else "landlord"))
387388
res_game_over = True
388-
env = None
389+
del env_list[pid]
389390
else:
390391
if env.acting_player_position in list(
391392
env.players.keys()
392393
): # 第二位出牌的还是ai,也就是双ai时
393394
cards_po, confidence_po = env.step(data)
394395
if env.game_over:
395396
res_game_over = True
396-
env = None
397+
del env_list[pid]
397398
else: # 单ai时
398399
pass
399400

@@ -416,7 +417,8 @@ def next(data): # 收到他人出牌
416417
res_data = {"pid": pid, "game_over": res_game_over}
417418
res_action = "receive"
418419

419-
env_list[pid] = env
420+
if not res_game_over :
421+
env_list[pid] = env
420422
res_status = "ok"
421423
except Exception as err:
422424
res_action = "step"

start.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
port = 24813
2+
timeout = 18000 # 超过此时长游戏未继续进行则删除游戏进程
23
###############################################################################################
3-
version = 1.0
4+
version = 1.1
45
###############################################################################################
56
import os
67
import time
78
import json
8-
from douzero.evaluation.simulation import init, next
9+
import threading
10+
from douzero.evaluation.simulation import init, next, env_list
911
from http.server import HTTPServer, BaseHTTPRequestHandler
1012

13+
lock = threading.Lock()
14+
1115
"""post data
1216
{
1317
action: "", #init/play
@@ -114,13 +118,31 @@ def do_POST(self):
114118
self.wfile.write(res)
115119

116120

121+
class TimeCheck(threading.Thread):
122+
def run(self):
123+
while True:
124+
global env_list
125+
with lock:
126+
timestamp = time.time()
127+
tmp_env_list = dict(env_list.items())
128+
for k, v in tmp_env_list.items():
129+
existed_times = timestamp - v.timestamp
130+
if existed_times > timeout:
131+
del env_list[k]
132+
print(f"pid:{k} game timeout, deleted.")
133+
time.sleep(300)
134+
135+
117136
if __name__ == "__main__":
118137
print(f"DouZero_API . ver{version}")
119138

120139
# 才疏学浅,不知道这两行的用途与意义,暂且保留
121140
os.environ["KMP_DUPLICATE_LIB_OK"] = "True"
122141
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
123142

143+
time_check = TimeCheck()
144+
time_check.start()
145+
124146
host = ("127.0.0.1", port)
125147
print(f"listen at port {port}")
126148
server = HTTPServer(host, Request)

0 commit comments

Comments
 (0)