-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (94 loc) · 3.87 KB
/
main.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
import asyncio
import logging
import random
import sys
import time
import aiohttp
from game import Game
from minimax import Minimax
logging.basicConfig(level=logging.DEBUG)
class ApiTester:
def __init__(self, loop, player_num):
self._api_url = 'http://localhost:8081'
self._game = Game()
self._session = aiohttp.ClientSession()
self._player_num = player_num
self._loop = loop
self._last_move = []
async def _prepare_player(self, name):
async with self._session.post(
f'{self._api_url}/game',
params={'team_name': name}
) as resp:
res = (await resp.json())['data']
self._player_num = 1 if res['color'] == 'RED' else 2
self._player = {
'color': res['color'],
'token': res['token']
}
self.minimax = Minimax(self._player_num)
print("PLAYER_NUM", self._player_num)
async def _make_move(self, move):
json = {'move': move}
headers = {'Authorization': f'Token {self._player["token"]}'}
async with self._session.post(
f'{self._api_url}/move',
json=json,
headers=headers
) as resp:
print(await resp.text())
resp = (await resp.json())['data']
logging.info(f'Made move {move}, response: {resp}')
async def _get_game(self):
async with self._session.get(f'{self._api_url}/game') as resp:
return (await resp.json())['data']
async def _play_game(self):
current_game_progress = await self._get_game()
is_finished = current_game_progress['is_finished']
is_started = current_game_progress['is_started']
while is_started and not is_finished:
logging.info(f"Move of {current_game_progress['whose_turn']}")
if current_game_progress['last_move'] is not None and \
current_game_progress['last_move']['last_moves'] != self._last_move:
last_move = current_game_progress['last_move']['last_moves']
logging.info(f"Move from server: {last_move}")
moves = []
for m in last_move:
if m not in self._last_move:
moves.append(m)
for move in moves:
self._game.move(move)
self._last_move = last_move
if self._player['color'] == current_game_progress['whose_turn']:
start = time.time()
move = self.heuristic()
print("TOTAL TIME FOR TURN: ", time.time() - start)
logging.info(f"New move: {move}")
await self._make_move(move)
current_game_progress = await self._get_game()
is_finished = current_game_progress['is_finished']
is_started = current_game_progress['is_started']
await asyncio.sleep(0.2)
async def start(self):
logging.info('API Tester initialized, test will start in 2 secs')
await self._prepare_player(self._player_num)
logging.info('Game started, players initialized')
logging.info(f'Players: {self._player}')
await asyncio.sleep(0.5)
await self._play_game()
logging.info('Game finished')
last_game_progress = await self._get_game()
logging.info(str(last_game_progress))
await self._session.close()
def heuristic(self):
return self.minimax.best_move(self._game, 5)
async def start_bot(player_name):
loop = asyncio.get_event_loop()
player = ApiTester(loop, player_name)
await player.start()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
if sys.argv.__len__() > 1 and sys.argv[1]:
loop.run_until_complete(start_bot(sys.argv[1]))
else:
loop.run_until_complete(start_bot("BUBILDA"))