Add How to play section
This commit is contained in:
parent
53778462c1
commit
e8e24be595
5 changed files with 105 additions and 3 deletions
59
backend/static/bot/runner.py
Executable file
59
backend/static/bot/runner.py
Executable 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()
|
29
backend/static/bot/simple.py
Normal file
29
backend/static/bot/simple.py
Normal 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
|
||||||
|
})
|
|
@ -39,7 +39,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
height: 100vh;
|
min-height: 100vh;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: stretch;
|
align-items: stretch;
|
||||||
|
@ -60,6 +60,7 @@ body {
|
||||||
.info {
|
.info {
|
||||||
max-width: 80%;
|
max-width: 80%;
|
||||||
width: 1000px;
|
width: 1000px;
|
||||||
|
margin: 70px auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.info a {
|
.info a {
|
||||||
|
@ -78,11 +79,15 @@ body {
|
||||||
color: antiquewhite;
|
color: antiquewhite;
|
||||||
}
|
}
|
||||||
|
|
||||||
.info div h3, .info div h1 {
|
.info div h3 {
|
||||||
color: #ff7f00;
|
color: #ff7f00;
|
||||||
margin-bottom: 10px;
|
margin-bottom: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.info div h1 {
|
||||||
|
color: #ff7f00;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
.info div p {
|
.info div p {
|
||||||
/* color: #ff7f00; */
|
/* color: #ff7f00; */
|
||||||
margin: 5px 0;
|
margin: 5px 0;
|
||||||
|
|
|
@ -12,7 +12,6 @@ body {
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
overflow: hidden;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.lobby {
|
.lobby {
|
||||||
|
|
|
@ -49,6 +49,16 @@
|
||||||
<p>The debug station is nothing special, it just shows the current state of the underlying MOZAICP infrastructure.</p>
|
<p>The debug station is nothing special, it just shows the current state of the underlying MOZAICP infrastructure.</p>
|
||||||
</div>
|
</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>
|
<div>
|
||||||
<h3>Contribute!</h3>
|
<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>
|
<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>
|
||||||
|
|
Loading…
Reference in a new issue