QoL changes (non vibed)
This commit is contained in:
parent
3138dfc99f
commit
33efd24b26
2 changed files with 8 additions and 9 deletions
|
|
@ -31,10 +31,11 @@ from quarto.shared.game import (
|
||||||
|
|
||||||
|
|
||||||
class ClientApp:
|
class ClientApp:
|
||||||
def __init__(self, host: str, port: int, name: str) -> None:
|
def __init__(self, host: str, port: int, name: str, timeout: float = 5.0) -> None:
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.timeout = timeout
|
||||||
self.conn = ClientConnection(host, port)
|
self.conn = ClientConnection(host, port)
|
||||||
self.ai = QuartoAI()
|
self.ai = QuartoAI()
|
||||||
self.state: Optional[GameState] = None
|
self.state: Optional[GameState] = None
|
||||||
|
|
@ -89,7 +90,7 @@ class ClientApp:
|
||||||
# If we are player1, we must immediately choose initial piece
|
# If we are player1, we must immediately choose initial piece
|
||||||
if self.our_player == Player.PLAYER1:
|
if self.our_player == Player.PLAYER1:
|
||||||
assert self.state is not None
|
assert self.state is not None
|
||||||
init_move = self.ai.choose_initial_piece(self.state, 5.0)
|
init_move = self.ai.choose_initial_piece(self.state, self.timeout)
|
||||||
# Do NOT apply locally; wait for server broadcast MOVE~M
|
# Do NOT apply locally; wait for server broadcast MOVE~M
|
||||||
self.conn.send_line(format_move_initial(init_move.chosen_piece))
|
self.conn.send_line(format_move_initial(init_move.chosen_piece))
|
||||||
|
|
||||||
|
|
@ -154,7 +155,7 @@ class ClientApp:
|
||||||
return
|
return
|
||||||
|
|
||||||
# Use AI to choose move within 5 seconds
|
# Use AI to choose move within 5 seconds
|
||||||
move = self.ai.choose_normal_move(state, 5.0)
|
move = self.ai.choose_normal_move(state, self.timeout)
|
||||||
|
|
||||||
# Validate locally via simulation before sending
|
# Validate locally via simulation before sending
|
||||||
tmp_state = self._clone_state(state)
|
tmp_state = self._clone_state(state)
|
||||||
|
|
@ -205,9 +206,11 @@ def main() -> None:
|
||||||
parser.add_argument("--port", type=int, default=12345)
|
parser.add_argument("--port", type=int, default=12345)
|
||||||
parser.add_argument("--name", default="QuartoBot",
|
parser.add_argument("--name", default="QuartoBot",
|
||||||
help="Client name/description to send in HELLO")
|
help="Client name/description to send in HELLO")
|
||||||
|
parser.add_argument("--timeout", default=5.0, type=float,
|
||||||
|
help="The maximum think time for the AI")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
client = ClientApp(host=args.host, port=args.port, name=args.name)
|
client = ClientApp(host=args.host, port=args.port, name=args.name, timeout=args.timeout)
|
||||||
client.run()
|
client.run()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ class ClientConnection:
|
||||||
|
|
||||||
def connect(self) -> None:
|
def connect(self) -> None:
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
s.settimeout(self.timeout)
|
|
||||||
s.connect((self.host, self.port))
|
s.connect((self.host, self.port))
|
||||||
self.sock = s
|
self.sock = s
|
||||||
|
|
||||||
|
|
@ -31,10 +30,7 @@ class ClientConnection:
|
||||||
if b"\n" in self._buffer:
|
if b"\n" in self._buffer:
|
||||||
line, self._buffer = self._buffer.split(b"\n", 1)
|
line, self._buffer = self._buffer.split(b"\n", 1)
|
||||||
return line.decode("utf-8", errors="replace").rstrip("\r")
|
return line.decode("utf-8", errors="replace").rstrip("\r")
|
||||||
try:
|
|
||||||
chunk = self.sock.recv(4096)
|
chunk = self.sock.recv(4096)
|
||||||
except socket.timeout:
|
|
||||||
return None
|
|
||||||
if not chunk:
|
if not chunk:
|
||||||
return None
|
return None
|
||||||
self._buffer += chunk
|
self._buffer += chunk
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue