Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions scripts_of_tribute/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _cleanup_processes(self):

self.processes.clear()

def _download_game_runner(self, version="v1.0.0"):
def _download_game_runner(self, version=None):
system = platform.system()
if system == "Windows":
zip_name = f"GameRunner-win-x64.zip"
Expand All @@ -125,6 +125,13 @@ def _download_game_runner(self, version="v1.0.0"):
zip_name = f"GameRunner-osx-x64.zip"
else:
raise NotImplementedError(f"Unsupported platform: {system}")

if version is None:
print("Fetching latest GameRunner version...")
response = requests.get("https://api.github.com/repos/ScriptsOfTribute/ScriptsOfTribute-Core/releases/latest")
response.raise_for_status()
version = response.json()["tag_name"]
print(f"Latest version: {version}")

url = f"https://github.com/ScriptsOfTribute/ScriptsOfTribute-Core/releases/download/{version}/{zip_name}"

Expand All @@ -135,9 +142,8 @@ def _download_game_runner(self, version="v1.0.0"):

print(f"Downloading GameRunner from {url}...")
response = requests.get(url)
response.raise_for_status() # Raise an error for bad status codes
response.raise_for_status()

# Save the zip file
with open(zip_path, "wb") as f:
f.write(response.content)

Expand Down
2 changes: 2 additions & 0 deletions scripts_of_tribute/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def run_game_runner(path: Path, bot1: str, bot2: str, runs=1, threads=1, enable_
cwd=str(path.parent)
)
print(f"{result.stdout}")
if result.stderr:
print(result.stderr)
except subprocess.CalledProcessError as e:
print(f"Error running GameRunner:\n{e.stderr}")
raise RuntimeError(e)
Expand Down
23 changes: 19 additions & 4 deletions scripts_of_tribute/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import Tuple
import grpc
from concurrent import futures
Expand Down Expand Up @@ -43,10 +44,17 @@ def GameEnd(self, request, context):
return main_pb2.Empty()

def CloseServer(self, request, context):
print("Received CloseServer request. Shutting down server...")
self.server_instance.bot_disconnected()
print(f"Received CloseServer request from {self.ai.bot_name}. Shutting down server...")
context.set_code(grpc.StatusCode.OK)
context.set_details(f"Bot {self.ai.bot_name}'s connection closed.")
def delayed_shutdown():
time.sleep(0.1)
self.server_instance.bot_disconnected()
print(f"Bot {self.ai.bot_name}'s connection closed.")

import threading
threading.Thread(target=delayed_shutdown, daemon=True).start()

return main_pb2.Empty()


Expand All @@ -55,6 +63,7 @@ class Server:
def __init__(self):
self.active_bots = 0
self.server = None

def add_bot(self):
self.active_bots += 1

Expand Down Expand Up @@ -99,6 +108,12 @@ def run_grpc_server(
server2.server.start()

if bot1 is not None:
server1.server.wait_for_termination()
try:
server1.server.wait_for_termination(timeout=2)
except grpc.FutureTimeoutError:
print("Server didn't terminate cleanly in time.")
if bot2 is not None:
server2.server.wait_for_termination()
try:
server2.server.wait_for_termination(timeout=2)
except grpc.FutureTimeoutError:
print("Server didn't terminate cleanly in time.")