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"; } } const Direction = { LEFT: "left", RIGHT: "right", UP: "up", DOWN: "down", }; 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, Direction, };