DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

7210bf367de8973d3710f324b65ad31b9fc4e7bd — Jonni Liljamo 1 year, 5 months ago f79dbd3
feat(client): impl api calls for game endpoints
A client/src/api/game/create.rs => client/src/api/game/create.rs +19 -0
@@ 0,0 1,19 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{post_request_auth, NetworkingOptions};

use super::Game;

pub type CreateResponse = Result<Game, ()>;

pub fn create(no: &NetworkingOptions) -> CreateResponse {
    let res = post_request_auth!(no, "/game", &{});

    Ok(res.json().unwrap())
}

A client/src/api/game/create_action.rs => client/src/api/game/create_action.rs +40 -0
@@ 0,0 1,40 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * 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<Action, ()>;

#[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())
}

A client/src/api/game/details.rs => client/src/api/game/details.rs +19 -0
@@ 0,0 1,19 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{NetworkingOptions, get_request_auth};

use super::Game;

pub type DetailsResponse = Result<Game, ()>;

pub fn details(no: &NetworkingOptions, game_id: &str) -> DetailsResponse {
    let res = get_request_auth!(no, &format!("/game/{}", game_id));

    Ok(res.json().unwrap())
}

A client/src/api/game/forming.rs => client/src/api/game/forming.rs +19 -0
@@ 0,0 1,19 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{NetworkingOptions, get_request_auth};

use super::Game;

pub type FormingResponse = Result<Vec<Game>, ()>;

pub fn forming(no: &NetworkingOptions) -> FormingResponse {
    let res = get_request_auth!(no, "/game/forming");

    Ok(res.json().unwrap())
}

A client/src/api/game/join.rs => client/src/api/game/join.rs +18 -0
@@ 0,0 1,18 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * 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<String, ()>;

pub fn join(no: &NetworkingOptions, game_id: &str) -> JoinResponse {
    let res = post_request_auth!(no, &format!("/game/{}/join)", &game_id), &{});

    Ok(res.text().unwrap())
}

A client/src/api/game/mod.rs => client/src/api/game/mod.rs +65 -0
@@ 0,0 1,65 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * 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<User>,
    pub guest_id: String,
    pub guest: Option<User>,
    pub state: u8,
    pub turn: u8,
    pub actions: Option<Vec<Action>>,
}

A client/src/api/game/my_games.rs => client/src/api/game/my_games.rs +19 -0
@@ 0,0 1,19 @@
/*
 * This file is part of laurelin_client
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use crate::{NetworkingOptions, get_request_auth};

use super::Game;

pub type MyGamesResponse = Result<Vec<Game>, ()>;

pub fn my_games(no: &NetworkingOptions) -> MyGamesResponse {
    let res = get_request_auth!(no, "/game/my_games");

    Ok(res.json().unwrap())
}

M client/src/api/mod.rs => client/src/api/mod.rs +2 -0
@@ 7,3 7,5 @@
 */

pub mod user;

pub mod game;