better client scripts

This commit is contained in:
ajuvercr 2020-03-30 11:32:26 +02:00
parent 22cf663367
commit 9460323b48
7 changed files with 35 additions and 6 deletions

1
client/.gitignore vendored
View file

@ -1,2 +1,3 @@
target/
Cargo.lock
bots/

2
client/game_start.json Normal file
View file

@ -0,0 +1,2 @@
{"nop":2,"name":"Dick But","map":"maps/hex.json","max_turns":2000}

View file

@ -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

16
client/run_all.py Normal file
View file

@ -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()

View file

@ -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)

View file

@ -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]

4
client/start.sh Executable file
View file

@ -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']))"