diff --git a/client/.gitignore b/client/.gitignore index 2c96eb1..c409813 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -1,2 +1,3 @@ target/ Cargo.lock +bots/ diff --git a/client/game_start.json b/client/game_start.json new file mode 100644 index 0000000..f768030 --- /dev/null +++ b/client/game_start.json @@ -0,0 +1,2 @@ + +{"nop":2,"name":"Dick But","map":"maps/hex.json","max_turns":2000} diff --git a/client/run.sh b/client/run.sh index ef34c29..761b1b1 100755 --- a/client/run.sh +++ b/client/run.sh @@ -1,3 +1,4 @@ #!/bin/bash -python bot.py -p 9142 -i $1 python simple.py +echo "Using token $1" +python runner.py --host ${HOST:-localhost} -p 9142 -i $1 python simple.py diff --git a/client/run_all.py b/client/run_all.py new file mode 100644 index 0000000..f02fbeb --- /dev/null +++ b/client/run_all.py @@ -0,0 +1,16 @@ +import requests, json, subprocess, os + +host = os.getenv("HOST") or "localhost:8000" +headers = {'content-type': 'application/json'} + +r = requests.post(f"http://{host}/lobby", data=open('game_start.json').read(), headers=headers) +data = r.json() +processes = [] + +for player in data["players"]: + processes.append( + subprocess.Popen(["./run.sh", str(player)]) + ) + +for p in processes: + p.wait() diff --git a/client/bot.py b/client/runner.py similarity index 86% rename from client/bot.py rename to client/runner.py index 3fb2cde..ae578f4 100755 --- a/client/bot.py +++ b/client/runner.py @@ -15,9 +15,10 @@ def execute(cmd): raise subprocess.CalledProcessError(return_code, cmd) def connect(host, port, id): + print(host, port) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) - s.sendall(f"{id}\n".encode("utf8")) + s.sendall(f"{id.strip()}\n".encode("utf8")) return s def handle_input(it, socket): @@ -51,6 +52,10 @@ def main(): if content["type"] == "game_state": stdin.write(json.dumps(content["content"])+"\n") stdin.flush() + if content["type"] == "player_action": + if content["content"]["type"] == "parse_error": + sys.stderr.write(content["content"]["value"] + '\n') + sys.stderr.flush() line = f.readline() print(content) diff --git a/client/simple.py b/client/simple.py index 02d6fc4..7f4afd5 100644 --- a/client/simple.py +++ b/client/simple.py @@ -2,16 +2,16 @@ import sys, json, random def move(command): record = { 'moves': [command] } + f.write(json.dumps(record) + '\n') + f.flush() print(json.dumps(record)) sys.stdout.flush() -f_name = f"bot{random.randint(0, 10)}.txt" +f_name = f"bots/bot{random.randint(0, 10)}.txt" f = open(f_name,"w+") -f.write("start") f.flush() + for line in sys.stdin: - f.write(line) - f.flush() state = json.loads(line) # find planet with most ships my_planets = [p for p in state['planets'] if p['owner'] == 1] diff --git a/client/start.sh b/client/start.sh new file mode 100755 index 0000000..5c9fc86 --- /dev/null +++ b/client/start.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +curl -X POST -H "Content-Type: application/json" -d @game_start.json ${HOST:-localhost:8000}/lobby | \ + python3 -c "import sys, json; print('\n'.join(str(x) for x in json.load(sys.stdin)['players']))"