From 7210bf367de8973d3710f324b65ad31b9fc4e7bd Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 21 Apr 2023 13:15:06 +0300 Subject: [PATCH] feat(client): impl api calls for game endpoints --- client/src/api/game/create.rs | 19 ++++++++ client/src/api/game/create_action.rs | 40 +++++++++++++++++ client/src/api/game/details.rs | 19 ++++++++ client/src/api/game/forming.rs | 19 ++++++++ client/src/api/game/join.rs | 18 ++++++++ client/src/api/game/mod.rs | 65 ++++++++++++++++++++++++++++ client/src/api/game/my_games.rs | 19 ++++++++ client/src/api/mod.rs | 2 + 8 files changed, 201 insertions(+) create mode 100644 client/src/api/game/create.rs create mode 100644 client/src/api/game/create_action.rs create mode 100644 client/src/api/game/details.rs create mode 100644 client/src/api/game/forming.rs create mode 100644 client/src/api/game/join.rs create mode 100644 client/src/api/game/mod.rs create mode 100644 client/src/api/game/my_games.rs diff --git a/client/src/api/game/create.rs b/client/src/api/game/create.rs new file mode 100644 index 0000000..75d2951 --- /dev/null +++ b/client/src/api/game/create.rs @@ -0,0 +1,19 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use crate::{post_request_auth, NetworkingOptions}; + +use super::Game; + +pub type CreateResponse = Result; + +pub fn create(no: &NetworkingOptions) -> CreateResponse { + let res = post_request_auth!(no, "/game", &{}); + + Ok(res.json().unwrap()) +} diff --git a/client/src/api/game/create_action.rs b/client/src/api/game/create_action.rs new file mode 100644 index 0000000..460dd40 --- /dev/null +++ b/client/src/api/game/create_action.rs @@ -0,0 +1,40 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use serde::Serialize; + +use crate::{NetworkingOptions, post_request_auth}; + +use super::{Command, Action}; + +pub type CreateActionResponse = Result; + +#[derive(Serialize)] +struct CreateActionPost { + game_id: String, + invoker: String, + target: String, + command: Command, +} + +pub fn create_action( + no: &NetworkingOptions, + game_id: &str, + invoker: &str, + target: &str, + command: &Command +) -> CreateActionResponse { + let res = post_request_auth!(no, "/game/action", &CreateActionPost { + game_id: game_id.to_string(), + invoker: invoker.to_string(), + target: target.to_string(), + command: command.clone(), + }); + + Ok(res.json().unwrap()) +} diff --git a/client/src/api/game/details.rs b/client/src/api/game/details.rs new file mode 100644 index 0000000..d68f471 --- /dev/null +++ b/client/src/api/game/details.rs @@ -0,0 +1,19 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use crate::{NetworkingOptions, get_request_auth}; + +use super::Game; + +pub type DetailsResponse = Result; + +pub fn details(no: &NetworkingOptions, game_id: &str) -> DetailsResponse { + let res = get_request_auth!(no, &format!("/game/{}", game_id)); + + Ok(res.json().unwrap()) +} diff --git a/client/src/api/game/forming.rs b/client/src/api/game/forming.rs new file mode 100644 index 0000000..fed8c71 --- /dev/null +++ b/client/src/api/game/forming.rs @@ -0,0 +1,19 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use crate::{NetworkingOptions, get_request_auth}; + +use super::Game; + +pub type FormingResponse = Result, ()>; + +pub fn forming(no: &NetworkingOptions) -> FormingResponse { + let res = get_request_auth!(no, "/game/forming"); + + Ok(res.json().unwrap()) +} diff --git a/client/src/api/game/join.rs b/client/src/api/game/join.rs new file mode 100644 index 0000000..09821c8 --- /dev/null +++ b/client/src/api/game/join.rs @@ -0,0 +1,18 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use crate::{NetworkingOptions, post_request_auth}; + +// NOTE: this response isn't actually used +type JoinResponse = Result; + +pub fn join(no: &NetworkingOptions, game_id: &str) -> JoinResponse { + let res = post_request_auth!(no, &format!("/game/{}/join)", &game_id), &{}); + + Ok(res.text().unwrap()) +} diff --git a/client/src/api/game/mod.rs b/client/src/api/game/mod.rs new file mode 100644 index 0000000..a80716c --- /dev/null +++ b/client/src/api/game/mod.rs @@ -0,0 +1,65 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use serde::{Deserialize, Serialize}; + +use super::user::User; + +mod forming; +pub use forming::*; + +mod my_games; +pub use my_games::*; + +mod join; +pub use join::*; + +mod create; +pub use create::*; + +mod create_action; +pub use create_action::*; + +mod details; +pub use details::*; + +#[derive(Deserialize, Serialize, Clone)] +pub enum Command { + /// draw N amount of cards from deck + Draw { amount: u32 }, + /// discard card from hand in slot N + Discard { index: u32 }, + /// shuffle discard pile to deck + ShuffleDiscardToDeck { seed: u32 }, +} + +#[derive(Deserialize)] +pub struct Action { + pub id: String, + pub created_at: String, + pub updated_at: String, + pub game_id: String, + pub invoker: String, + pub target: String, + pub command: Command, +} + +#[derive(Deserialize)] +pub struct Game { + pub id: String, + pub created_at: String, + pub updated_at: String, + pub ended_at: String, + pub host_id: String, + pub host: Option, + pub guest_id: String, + pub guest: Option, + pub state: u8, + pub turn: u8, + pub actions: Option>, +} diff --git a/client/src/api/game/my_games.rs b/client/src/api/game/my_games.rs new file mode 100644 index 0000000..79db557 --- /dev/null +++ b/client/src/api/game/my_games.rs @@ -0,0 +1,19 @@ +/* + * This file is part of laurelin_client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use crate::{NetworkingOptions, get_request_auth}; + +use super::Game; + +pub type MyGamesResponse = Result, ()>; + +pub fn my_games(no: &NetworkingOptions) -> MyGamesResponse { + let res = get_request_auth!(no, "/game/my_games"); + + Ok(res.json().unwrap()) +} diff --git a/client/src/api/mod.rs b/client/src/api/mod.rs index c0c5c1d..ff9554d 100644 --- a/client/src/api/mod.rs +++ b/client/src/api/mod.rs @@ -7,3 +7,5 @@ */ pub mod user; + +pub mod game; -- 2.44.1