-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessGame.py
54 lines (41 loc) · 1.6 KB
/
ProcessGame.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
import Game
import socket
from MessageParser import MessageParser, GameMessageParser
import sys
from Config import Config
import time
class ProcessGame(object):
def __init__(self):
self.game = getattr(Game, Config.GAME_NAME)()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.msg_parser = GameMessageParser()
def run(self):
# connect master to get worker's id
self.sock.connect((Config.MASTER_IP, Config.MASTER_PORT))
worker_id = MessageParser().decode_recv(self.sock)
print('Game recv worker id', worker_id)
self.sock.shutdown(2)
self.sock.close()
# try to connect worker
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
self.sock.connect(('localhost', Config.WORKER_BASE_PORT+worker_id))
break
except Exception as e:
print(e)
time.sleep(1)
self.msg_parser.send_game_model_info(self.sock, self.game.get_game_model_info())
state, reward, done, next_state = self.game.reset()
while True:
self.msg_parser.send_sample(self.sock, [state, reward, done, next_state])
if not done:
action = self.msg_parser.recv_action(self.sock)
state, reward, done, next_state = self.game.step(action)
else:
state, reward, done, next_state = self.game.reset()
if __name__ == '__main__':
if not Config.GAME_PUSH_ALGORITHM:
sys.exit(0)
game = ProcessGame()
game.run()