diff --git a/javascript/demo.js b/javascript/demo.js new file mode 100644 index 0000000..4064538 --- /dev/null +++ b/javascript/demo.js @@ -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(); diff --git a/javascript/fh_sdk.js b/javascript/fh_sdk.js new file mode 100644 index 0000000..5ef7ac3 --- /dev/null +++ b/javascript/fh_sdk.js @@ -0,0 +1,124 @@ +class PlayerNotFoundError extends Error { + constructor(message) { + if (!message) { + message = "Player not found"; + } + super(message); + this.name = "PlayerNotFoundError"; + } +} + +class PlayerInactiveError extends Error { + constructor(message) { + if (!message) { + message = "Player kicked out due to inactivity"; + } + super(message); + this.name = "PlayerInactiveError"; + } +} + +class PositionError extends Error { + constructor(message) { + if (!message) { + message = "Position out of bounds or collision with an object"; + } + super(message); + this.name = "PositionError"; + } +} + +class FairHopper { + constructor(host, port) { + this.host = host; + this.port = port; + + this.defaultHeaders = { + Accept: "application/json", + "Content-Type": "application/json", + }; + } + + formatUrl(path) { + return `${this.host}:${this.port}${path}`; + } + + async ping() { + const r = await fetch(this.formatUrl("/ping"), { + headers: this.defaultHeaders, + }); + return await r.json(); + } + + async startGame(playerName) { + const payload = { + player_name: playerName, + }; + const r = await fetch(this.formatUrl("/game"), { + method: "post", + headers: this.defaultHeaders, + body: JSON.stringify(payload), + }); + return await r.json(); + } + + async getGameInfo() { + const r = await fetch(this.formatUrl("/game"), { + headers: this.defaultHeaders, + }); + return await r.json(); + } + + async getPlayerInfo(playerUuid) { + const r = await fetch(this.formatUrl(`/player/${playerUuid}`), { + headers: this.defaultHeaders, + }); + switch (r.status) { + case 403: + throw new PlayerInactiveError(); + case 404: + throw new PlayerNotFoundError(); + } + return await r.json(); + } + + async moveLeft(playerUuid) { + return await this.move(playerUuid, "left"); + } + + async moveRight(playerUuid) { + return await this.move(playerUuid, "right"); + } + + async moveUp(playerUuid) { + return await this.move(playerUuid, "up"); + } + + async moveDown(playerUuid) { + return await this.move(playerUuid, "down"); + } + + async move(playerUuid, direction) { + const url = this.formatUrl(`/player/${playerUuid}/move/${direction}`); + const r = await fetch(url, { + method: "post", + headers: this.defaultHeaders, + }); + switch (r.status) { + case 403: + throw new PlayerInactiveError(); + case 404: + throw new PlayerNotFoundError(); + case 409: + throw new PositionError(); + } + return await r.json(); + } +} + +module.exports = { + FairHopper, + PlayerNotFoundError, + PlayerInactiveError, + PositionError, +};