Skip to content

Commit 8c401fb

Browse files
committed
On god it works
1 parent 24e99b8 commit 8c401fb

29 files changed

+735
-160
lines changed

AIService/Server.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
from threading import Lock
2-
from typing import Dict, List, Type
2+
from typing import List
33
import grpc
44
from concurrent import futures
5+
56
from Protos import main_pb2_grpc, main_pb2
67
from AIService.base_ai import BaseAI
8+
from ScriptsOfTribute.board import build_game_state
9+
from ScriptsOfTribute.move import from_proto_move
10+
from ScriptsOfTribute.enums import PatronId
711

812
class AIService(main_pb2_grpc.AIServiceServicer):
913
def __init__(self, ai: BaseAI, server_instance):
1014
self.ai = ai
1115
self.server_instance = server_instance
16+
self.engine_service_stub = None
17+
18+
def set_engine_service_stub(self, stub):
19+
self.engine_service_stub = stub
1220

1321
def RegisterBot(self, request, context):
1422
return main_pb2.RegistrationStatus(name=self.ai.bot_name, message="")
@@ -18,18 +26,21 @@ def PregamePrepare(self, request, context):
1826
return main_pb2.Empty()
1927

2028
def SelectPatron(self, request, context):
21-
patron = self.ai.select_patron(request.availablePatrons)
22-
return main_pb2.PatronIdMessage(patronId=patron)
29+
patrons = [PatronId(patron) for patron in request.availablePatrons]
30+
patron = self.ai.select_patron(patrons)
31+
return main_pb2.PatronIdMessage(patronId=patron.value)
2332

2433
def Play(self, request, context):
25-
import pdb; pdb.set_trace()
26-
move = self.ai.play(request.gameState, request.possibleMoves)
27-
return move
34+
game_state = build_game_state(request.gameState, self.engine_service_stub)
35+
# game_state.debug_print()
36+
moves = [from_proto_move(proto_move) for proto_move in request.possibleMoves]
37+
move = self.ai.play(game_state, moves)
38+
return move.to_proto()
2839

2940
def GameEnd(self, request, context):
3041
self.ai.game_end(request)
3142
return main_pb2.Empty()
32-
43+
3344
def CloseServer(self, request, context):
3445
print("Received CloseServer request. Shutting down server...")
3546
self.server_instance.bot_disconnected()
@@ -75,7 +86,15 @@ def run_grpc_server(self, ai_instances: List[BaseAI], port=50000, debug_prints=T
7586
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
7687
for i, ai in enumerate(ai_instances):
7788
self.add_bot()
78-
main_pb2_grpc.add_AIServiceServicer_to_server(AIService(ai, self), self.server)
79-
self.server.add_insecure_port(f"[::]:{port+i}")
89+
ai_service = AIService(ai, self)
90+
assigned_port = port + i
91+
self.server.add_insecure_port(f"localhost:{assigned_port}")
92+
engine_service_channel = grpc.insecure_channel(f"localhost:{assigned_port}")
93+
engine_service_stub = main_pb2_grpc.EngineServiceStub(engine_service_channel)
94+
if debug_prints:
95+
print(f"Bot {ai.bot_name} listening on localhost:{assigned_port}")
96+
ai_service.set_engine_service_stub(engine_service_stub)
97+
98+
main_pb2_grpc.add_AIServiceServicer_to_server(ai_service, self.server)
8099
self.server.start()
81100
return self.server

AIService/base_ai.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
from typing import List
2+
3+
from ScriptsOfTribute.board import GameState
4+
from ScriptsOfTribute.enums import PatronId
5+
from ScriptsOfTribute.move import BasicMove
6+
17
class BaseAI:
28
def __init__(self, bot_name):
39
self.bot_name = bot_name
410

511
def pregame_prepare(self):
612
pass
713

8-
def select_patron(self, available_patrons):
14+
def select_patron(self, available_patrons: List[PatronId]):
915
raise NotImplementedError
1016

11-
def play(self, game_state, possible_moves):
17+
def play(self, game_state: GameState, possible_moves: List[BasicMove]) -> BasicMove:
1218
raise NotImplementedError
1319

1420
def game_end(self, final_state):

Bots/MaxPrestigeBot.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import random
2+
3+
from AIService.base_ai import BaseAI
4+
5+
class MaxPrestigeBot(BaseAI):
6+
7+
def select_patron(self, available_patrons):
8+
#print(f"MaxPrestigeBot select patron: {available_patrons}")
9+
return random.choice(available_patrons)
10+
11+
def play(self, game_state, possible_moves):
12+
#print(f"MaxPrestigeBot play")
13+
# game_state.debug_print()
14+
return random.choice(possible_moves)
15+
16+
def game_end(self, final_state):
17+
pass

Bots/RandomBot.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import random
2+
3+
from AIService.base_ai import BaseAI
4+
5+
class RandomBot(BaseAI):
6+
7+
def select_patron(self, available_patrons):
8+
print("RandomBot select patron")
9+
return random.choice(available_patrons)
10+
11+
def play(self, game_state, possible_moves):
12+
game_state.debug_print()
13+
return random.choice(possible_moves)
14+
15+
def game_end(self, final_state):
16+
pass

Game/__init__.py

Whitespace-only changes.

GameRunner/GameRunner.dll

512 Bytes
Binary file not shown.

GameRunner/GameRunner.exe

0 Bytes
Binary file not shown.

GameRunner/GameRunner.pdb

68 Bytes
Binary file not shown.

GameRunner/TalesOfTribute.dll

-1 KB
Binary file not shown.

GameRunner/TalesOfTribute.pdb

-220 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)