diff --git a/backend/static/bot/runner.py b/backend/static/bot/runner.py new file mode 100755 index 0000000..3fb2cde --- /dev/null +++ b/backend/static/bot/runner.py @@ -0,0 +1,59 @@ +#! /usr/bin/env python + +import socket, sys, subprocess, argparse, io, threading, json + + +def execute(cmd): + popen = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) + yield(popen.stdin) + + for stdout_line in iter(popen.stdout.readline, "\n"): + yield stdout_line + popen.stdout.close() + return_code = popen.wait() + if return_code: + raise subprocess.CalledProcessError(return_code, cmd) + +def connect(host, port, id): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((host, port)) + s.sendall(f"{id}\n".encode("utf8")) + return s + +def handle_input(it, socket): + for line in it: + socket.sendall(line.encode('utf8')) + +def main(): + parser = argparse.ArgumentParser(description='Run MOZAIC bot') + parser.add_argument('--id', '-i', required=True, + help='The bot\'s ID') + parser.add_argument('--host', default="localhost", + help='What host to connect to') + parser.add_argument('--port', '-p', default=6666, type=int, + help='What port to connect to') + parser.add_argument('arguments', nargs=argparse.REMAINDER, + help='How to run the bot') + args = parser.parse_args() + + sock = connect(args.host, args.port, args.id) + f = sock.makefile("rw") + + it = execute(args.arguments) + stdin = next(it) + + threading.Thread(target=lambda: handle_input(it, sock), daemon=True).start() + + line = f.readline() + content = "Nothing" + while line: + content = json.loads(line) + if content["type"] == "game_state": + stdin.write(json.dumps(content["content"])+"\n") + stdin.flush() + line = f.readline() + + print(content) + +if __name__ == "__main__": + main() diff --git a/backend/static/bot/simple.py b/backend/static/bot/simple.py new file mode 100644 index 0000000..02d6fc4 --- /dev/null +++ b/backend/static/bot/simple.py @@ -0,0 +1,29 @@ +import sys, json, random + +def move(command): + record = { 'moves': [command] } + print(json.dumps(record)) + sys.stdout.flush() + +f_name = f"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] + other_planets = [p for p in state['planets'] if p['owner'] != 1] + + if not my_planets or not other_planets: + move(None) + else: + planet = max(my_planets, key=lambda p: p['ship_count']) + dest = min(other_planets, key=lambda p: p['ship_count']) + move({ + 'origin': planet['name'], + 'destination': dest['name'], + 'ship_count': planet['ship_count'] - 1 + }) diff --git a/backend/static/style/base.css b/backend/static/style/base.css index 8d4310f..120e3b4 100644 --- a/backend/static/style/base.css +++ b/backend/static/style/base.css @@ -39,7 +39,7 @@ } body { - height: 100vh; + min-height: 100vh; display: flex; flex-direction: column; align-items: stretch; @@ -60,6 +60,7 @@ body { .info { max-width: 80%; width: 1000px; + margin: 70px auto; } .info a { @@ -78,11 +79,15 @@ body { color: antiquewhite; } -.info div h3, .info div h1 { +.info div h3 { color: #ff7f00; margin-bottom: 10px; } +.info div h1 { + color: #ff7f00; + margin-bottom: 20px; +} .info div p { /* color: #ff7f00; */ margin: 5px 0; diff --git a/backend/static/style/style.css b/backend/static/style/style.css index c43d159..c4382fc 100644 --- a/backend/static/style/style.css +++ b/backend/static/style/style.css @@ -12,7 +12,6 @@ body { display: flex; width: 100%; height: 100%; - overflow: hidden; } .lobby { diff --git a/backend/templates/index.html.tera b/backend/templates/index.html.tera index 06c443a..31e0658 100644 --- a/backend/templates/index.html.tera +++ b/backend/templates/index.html.tera @@ -49,6 +49,16 @@
The debug station is nothing special, it just shows the current state of the underlying MOZAICP infrastructure.
+Build a bot! All information about the planetwars game, Please visit. You can find an example bot here.
+Optionally, you can create your own custom with the mapbuilder
+Start a game instance in the lobby, don't forget to name it, watching the visualization afterwars is the only way to know who won atm.
+Start up your bot and connect to the game instance. In the lobby you will see keys corresponding to players in the game. You can use the botrunner to run your bot, with the command python runner.py --host mozaic.zeus.gent -p 9142 -i {your key} {The command to start you bot}. +
Invite friends!
+Bash your friends because you have the superior bot, according to the visualizer
+There is a lot that can still be done. Difficulty ranging from easy, like creating favicon, to very challenging, like add blockly client back from BottleBats.