Add How to play section

This commit is contained in:
ajuvercr 2020-03-29 11:28:57 +02:00
parent 53778462c1
commit e8e24be595
5 changed files with 105 additions and 3 deletions

59
backend/static/bot/runner.py Executable file
View file

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

View file

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

View file

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

View file

@ -12,7 +12,6 @@ body {
display: flex;
width: 100%;
height: 100%;
overflow: hidden;
}
.lobby {

View file

@ -49,6 +49,16 @@
<p>The debug station is nothing special, it just shows the current state of the underlying MOZAICP infrastructure.</p>
</div>
<div>
<h3>How to play?</h3>
<p>Build a bot! All information about the planetwars game, <a target="_blank" href="https://docs.google.com/presentation/d/1ZwFlXGm7WZ4urTFxXdyoEz7n19PjwkO4Z-iVYLWCDmg/edit#slide=id.p">Please visit.</a> You can find an example bot <a href="bot/simple.py">here</a>.</p>
<p>Optionally, you can create your own custom with the mapbuilder</p>
<p>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.</p>
<p>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 <a href="bot/runner.py">the botrunner</a> to run your bot, with the command <span style="font-style: italic;">python runner.py --host mozaic.zeus.gent -p 9142 -i {your key} {The command to start you bot}</span>.
<p>Invite friends!</p>
<p>Bash your friends because you have the superior bot, according to the visualizer</p>
</div>
<div>
<h3>Contribute!</h3>
<p>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.</p>