Skip to content

Commit

Permalink
update template
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenhs committed Dec 28, 2023
1 parent a94e11f commit 468dba7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 81 deletions.
64 changes: 18 additions & 46 deletions assets/templates/javascript.node.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,29 @@ bot directory to play.
// ⚠️ Only modify this file if you know what you're doing!
const { Bot } = require("./bot");

function send(channel, botInstanceId, payload) {
let message = `\n<<zilch>>.${channel}`;

if (botInstanceId) {
message += "." + botInstanceId;
}

if (payload) {
message += "." + payload;
}

message += "\n";

process.stderr.write(message);
function send(channel, payload) {
process.stderr.write(
`\n<<zilch>>.${channel}${payload ? "." + payload : ""}\n`
);
}

function parsePayload(payload) {
const [eastPaddleX, eastPaddleY, westPaddleX, westPaddleY, ballX, ballY] =
payload.split(",").map((value) => {
return parseFloat(value);
});
const [paddleX, paddleY, ballX, ballY] = payload
.split(",")
.map((value) => parseFloat(value));

return [
{
x: eastPaddleX,
y: eastPaddleY,
},
{
x: westPaddleX,
y: westPaddleY,
},
{
x: ballX,
y: ballY,
},
{ x: paddleX, y: paddleY },
{ x: ballX, y: ballY },
];
}

const bots = new Map();
let bot;

process.stdin.on("data", async (chunk) => {
const data = chunk.toString().trim();
const [channel, botInstanceId] = data.split(".", 2);
const payload = data.slice(channel.length + botInstanceId.length + 2);
const channel = data.split(".")[0];
const payload = data.slice(channel.length + 1);

if (channel === "start") {
const standardCustomConfigSplit = payload.indexOf(".");
Expand All @@ -71,33 +50,29 @@ process.stdin.on("data", async (chunk) => {
.split(",");

const config = {
botInstanceId,
gameTimeLimit: parseInt(standardConfigParts[0]),
turnTimeLimit: parseInt(standardConfigParts[1]),
paddle: standardConfigParts[2] === "0" ? "east" : "west",
};

bots.set(botInstanceId, new Bot(config));
bot = new Bot(config);

send("start", botInstanceId);
send("start");
return;
}

const bot = bots.get(botInstanceId);

if (!bot) {
throw new Error("No bot runner with id " + botInstanceId);
throw new Error("Bot not yet initialized.");
}

if (channel === "move") {
const move = await bot.move(...parsePayload(payload));
send("move", botInstanceId, move);
send("move", move);
return;
}

if (channel === "end") {
await bot.end(...parsePayload(payload));
bots.delete(botInstanceId);
return;
}
});
Expand All @@ -116,10 +91,7 @@ class Bot {
console.log("Hello world!", this.config);
}

move(eastPaddle, westPaddle, ball) {
// Determine which paddle you control.
const paddle = this.config.paddle === "east" ? eastPaddle : westPaddle;

move(paddle, ball) {
// This prints the position of your paddle and the ball to the bot terminal.
// Use these values to determine which direction your paddle should move so
// you hit the ball!
Expand All @@ -131,7 +103,7 @@ class Bot {
return "none";
}

end(eastPaddle, westPaddle, ball) {
end(paddle, ball) {
console.log("Good game!");
}
}
Expand Down
36 changes: 11 additions & 25 deletions assets/templates/python.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
python3 -u ./main.py
```

```md file=/README.md hidden=true
```md file=/README.md
Check if you have Python 3 installed on your system like this:

\`\`\`
python --version
python3 --version
\`\`\`

If you receive a `command not found` error follow the instructions
Expand All @@ -20,14 +20,10 @@ import sys
from bot import Bot

def send(channel: str, *args):
bot_instance_id = args[0] if len(args) > 0 else None
payload = args[1] if len(args) > 1 else None
payload = args[0] if len(args) > 0 else None

message = "\n<<zilch>>." + channel

if bot_instance_id is not None:
message += "." + bot_instance_id

if payload is not None:
message += "." + payload

Expand All @@ -37,6 +33,7 @@ def send(channel: str, *args):

def parse_payload(payload):
parts = payload.split(",")

return [
{
"x": float(parts[0]),
Expand All @@ -45,44 +42,36 @@ def parse_payload(payload):
{
"x": float(parts[2]),
"y": float(parts[3])
},
{
"x": float(parts[4]),
"y": float(parts[5])
}
]

send("ready")

bots: "dict[str, Bot]" = dict([])
bot: Bot = None

while True:
data = sys.stdin.readline().strip()
channel, bot_instance_id, payload = data.split(".", 2)
channel, payload = data.split(".", 1)

if channel == "start":
standard_config, custom_config = payload.split(".", 1)
game_time_limit, turn_time_limit, player = standard_config.split(",", 2)
config = {
"bot_instance_id": bot_instance_id,
"game_time_limit": int(game_time_limit),
"turn_time_limit": int(turn_time_limit),
"paddle": "east" if player == "0" else "west"
}
bots[bot_instance_id] = Bot(config)
send("start", bot_instance_id)
bot = Bot(config)
send("start")
continue

if channel == "move":
bot = bots[bot_instance_id]
move = bot.move(*parse_payload(payload))
send("move", bot_instance_id, move)
send("move", move)
continue

if channel == "end":
bot = bots[bot_instance_id]
bot.end(*parse_payload(payload))
bots.pop(bot_instance_id)
continue
```

Expand All @@ -96,10 +85,7 @@ class Bot:
print("Hello World!", config)
self.config = config

def move(self, eastPaddle, westPaddle, ball):
# Determine which paddle you control.
paddle = eastPaddle if self.config["paddle"] == "east" else westPaddle

def move(self, paddle, ball):
# This prints the position of your paddle and the ball to the bot terminal.
# Use these values to determine which direction your paddle should move so
# you hit the ball!
Expand All @@ -110,7 +96,7 @@ class Bot:
# "north" "south" "east" "west" or "none"
return "none"

def end(self, eastPaddle, westPaddle, ball):
def end(self, paddle, ball):
print("Good game!")
```

Expand Down
16 changes: 6 additions & 10 deletions src/play.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bot, BotOutcome } from "zilch-game-engine";
import { Chalk } from "chalk";
import { Hit, Point, State } from "./config";
import { Hit, Player, Point, State } from "./config";
import { getBallZ, getCircleLineIntercept, zMultiplier } from "./ui/math";

const chalk = new Chalk({ level: 3 });
Expand Down Expand Up @@ -65,8 +65,6 @@ Zilch.play = async function* (game) {
const previousP2Moves: Move[] = [];

while (true) {
const payload = createMovePayload(state);

state.index++;

const logMove = (bot: Bot) => {
Expand All @@ -83,11 +81,11 @@ Zilch.play = async function* (game) {
game.bots[1].writeln(chalk.dim(`Start move`));
const [p1Move, p2Move] = await Promise.all([
game.bots[0]
.move(payload)
.move(createMovePayload(state, "p1"))
.then(parseMoveResponse)
.then(logMove(game.bots[0])),
game.bots[1]
.move(payload)
.move(createMovePayload(state, "p2"))
.then(parseMoveResponse)
.then(logMove(game.bots[1])),
]);
Expand Down Expand Up @@ -359,12 +357,10 @@ function parseMoveResponse(response: string): Move | Error {
}
}

function createMovePayload(state: State) {
function createMovePayload(state: State, player: "p1" | "p2") {
return [
state.p1.position.x,
state.p1.position.y,
state.p2.position.x,
state.p2.position.y,
state[player].position.x,
state[player].position.y,
state.ball.position.x,
state.ball.position.y,
].join(",");
Expand Down

0 comments on commit 468dba7

Please sign in to comment.