This commit is contained in:
Eden Kirin
2023-03-27 13:17:09 +02:00
parent d660e3a586
commit cf0605c83c
2 changed files with 167 additions and 0 deletions

43
javascript/demo.js Normal file
View File

@ -0,0 +1,43 @@
const sdk = require("./fh_sdk");
const FAIRHOPPER_HOST = "http://127.0.0.1";
const FAIRHOPPER_PORT = 8010;
async function main() {
const fh = new sdk.FairHopper(FAIRHOPPER_HOST, FAIRHOPPER_PORT);
await fh.ping();
game = await fh.startGame("Mirko");
console.log("Board dimensions:", game.board);
console.log("Destination position:", game.destination.position);
console.log();
const playerUuid = game.player.uuid;
console.log("Current position is", game.player.position);
let moveResult;
try {
console.log("Trying to move right");
moveResult = await fh.move(playerUuid, "right");
console.log("Successfully moved to", moveResult.player.position);
console.log("Trying to move right");
moveResult = await fh.move(playerUuid, "right");
console.log("Successfully moved to", moveResult.player.position);
console.log("Trying to move right");
moveResult = await fh.move(playerUuid, "right");
console.log("Successfully moved to", moveResult.player.position);
console.log("Trying to move down");
moveResult = await fh.move(playerUuid, "down");
console.log("Successfully moved to", moveResult.player.position);
} catch (err) {
if (err instanceof sdk.PositionError) {
console.error("Cant't move in this direction");
}
}
}
main();