From 5e8981953f166b6625263a9a8ca8d345c4ca6268 Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Mon, 27 Mar 2023 13:33:23 +0200 Subject: [PATCH] Python SDK demo --- javascript/demo.js | 23 ++++++++++++++--------- javascript/fh_sdk.js | 8 ++++++++ python/demo.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 python/demo.py diff --git a/javascript/demo.js b/javascript/demo.js index 4064538..897e08c 100644 --- a/javascript/demo.js +++ b/javascript/demo.js @@ -5,37 +5,42 @@ const FAIRHOPPER_PORT = 8010; async function main() { const fh = new sdk.FairHopper(FAIRHOPPER_HOST, FAIRHOPPER_PORT); - await fh.ping(); + + console.log(`Pinging FairHopper server on ${FAIRHOPPER_HOST}:${FAIRHOPPER_PORT}`); + const pingResult = await fh.ping(); + console.log("Ping result:", pingResult); + console.log(); 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); + console.log("Initial player position is", game.player.position); let moveResult; + const playerUuid = game.player.uuid; + try { console.log("Trying to move right"); - moveResult = await fh.move(playerUuid, "right"); + moveResult = await fh.move(playerUuid, sdk.Direction.RIGHT); console.log("Successfully moved to", moveResult.player.position); console.log("Trying to move right"); - moveResult = await fh.move(playerUuid, "right"); + moveResult = await fh.move(playerUuid, sdk.Direction.RIGHT); console.log("Successfully moved to", moveResult.player.position); console.log("Trying to move right"); - moveResult = await fh.move(playerUuid, "right"); + moveResult = await fh.move(playerUuid, sdk.Direction.RIGHT); console.log("Successfully moved to", moveResult.player.position); console.log("Trying to move down"); - moveResult = await fh.move(playerUuid, "down"); + moveResult = await fh.move(playerUuid, sdk.Direction.DOWN); console.log("Successfully moved to", moveResult.player.position); } catch (err) { if (err instanceof sdk.PositionError) { console.error("Cant't move in this direction"); + } else { + console.error(err); } } } diff --git a/javascript/fh_sdk.js b/javascript/fh_sdk.js index 5ef7ac3..3096983 100644 --- a/javascript/fh_sdk.js +++ b/javascript/fh_sdk.js @@ -28,6 +28,13 @@ class PositionError extends Error { } } +const Direction = { + LEFT: "left", + RIGHT: "right", + UP: "up", + DOWN: "down", +}; + class FairHopper { constructor(host, port) { this.host = host; @@ -121,4 +128,5 @@ module.exports = { PlayerNotFoundError, PlayerInactiveError, PositionError, + Direction, }; diff --git a/python/demo.py b/python/demo.py new file mode 100644 index 0000000..de2f986 --- /dev/null +++ b/python/demo.py @@ -0,0 +1,44 @@ +from fh_sdk import Direction, FairHopper, PositionError + +FAIRHOPPER_HOST = "http://127.0.0.1" +FAIRHOPPER_PORT = 8010 + + +def main() -> None: + fh = FairHopper(FAIRHOPPER_HOST, FAIRHOPPER_PORT) + + print(f"Pinging FairHopper server on {FAIRHOPPER_HOST}:{FAIRHOPPER_PORT}") + res = fh.ping() + print("Ping result:", res) + print() + + game = fh.start_game(player_name="Mirko") + print("Board dimensions:", game.board) + print("Destination position:", game.destination.position) + print() + print("Initial player position is", game.player.position) + + player_uuid = game.player.uuid + + try: + print("Trying to move right") + moveResult = fh.move(player_uuid, Direction.RIGHT) + print("Successfully moved to", moveResult.player.position) + + print("Trying to move right") + moveResult = fh.move(player_uuid, Direction.RIGHT) + print("Successfully moved to", moveResult.player.position) + + print("Trying to move right") + moveResult = fh.move(player_uuid, Direction.RIGHT) + print("Successfully moved to", moveResult.player.position) + + print("Trying to move down") + moveResult = fh.move(player_uuid, Direction.DOWN) + print("Successfully moved to", moveResult.player.position) + except PositionError: + print("Cant't move in this direction") + + +if __name__ == "__main__": + main()