forked from Roflin/gamenight
Adds own/disown game logic
This commit is contained in:
36
gamenight-cli/src/flows/disown.rs
Normal file
36
gamenight-cli/src/flows/disown.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use gamenight_api_client_rs::{apis::default_api::disown_post, models::GameId};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{Flow, FlowOutcome, FlowResult, GamenightState};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Disown {
|
||||
game_id: Uuid
|
||||
}
|
||||
|
||||
impl Disown {
|
||||
pub fn new(game_id: Uuid) -> Self {
|
||||
Self {
|
||||
game_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Flow<'a> for Disown {
|
||||
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
||||
let _ = disown_post(&state.api_configuration, Some(GameId{game_id: self.game_id.to_string()})).await?;
|
||||
|
||||
clear_screen::clear();
|
||||
Ok((FlowOutcome::Successful, state))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Disown {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Disown")
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ mod list_games;
|
||||
mod add_game;
|
||||
mod view_game;
|
||||
mod rename_game;
|
||||
mod own;
|
||||
mod disown;
|
||||
|
||||
pub struct GamenightState {
|
||||
api_configuration: Configuration,
|
||||
|
||||
36
gamenight-cli/src/flows/own.rs
Normal file
36
gamenight-cli/src/flows/own.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use gamenight_api_client_rs::{apis::default_api::own_post, models::GameId};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{Flow, FlowOutcome, FlowResult, GamenightState};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Own {
|
||||
game_id: Uuid
|
||||
}
|
||||
|
||||
impl Own {
|
||||
pub fn new(game_id: Uuid) -> Self {
|
||||
Self {
|
||||
game_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Flow<'a> for Own {
|
||||
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
||||
let _ = own_post(&state.api_configuration, Some(GameId{game_id: self.game_id.to_string()})).await?;
|
||||
|
||||
clear_screen::clear();
|
||||
Ok((FlowOutcome::Successful, state))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Own {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Own")
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
|
||||
use gamenight_api_client_rs::{apis::default_api::game_get, models::GameId};
|
||||
use gamenight_api_client_rs::{apis::default_api::{game_get, owned_games_get}, models::GameId};
|
||||
use inquire::Select;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{domain::game::Game, flows::{exit::Exit, rename_game::RenameGame}};
|
||||
use crate::{domain::game::Game, flows::{disown::Disown, exit::Exit, own::Own, rename_game::RenameGame}};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -27,10 +28,24 @@ impl<'a> Flow<'a> for ViewGame {
|
||||
|
||||
println!("{}", game);
|
||||
|
||||
let owned_games: Vec<Uuid> = owned_games_get(&state.api_configuration).await?.iter().map(|x| -> Result<Uuid, FlowError> { Ok(Uuid::parse_str(&x)?) }).collect::<Result<Vec<Uuid>, FlowError>>()?;
|
||||
|
||||
println!("game_id {:?}, owned_games {:?}", game.id, owned_games);
|
||||
|
||||
let own_or_disown: Box<dyn Flow<'a> + Send> =
|
||||
if owned_games.into_iter().find(|x| *x == game.id) != None {
|
||||
Box::new(Disown::new(self.game.id))
|
||||
}
|
||||
else {
|
||||
Box::new(Own::new(self.game.id))
|
||||
};
|
||||
|
||||
let options: Vec<Box<dyn Flow<'a> + Send>> = vec![
|
||||
own_or_disown,
|
||||
Box::new(RenameGame::new(game.clone())),
|
||||
Box::new(Exit::new())
|
||||
];
|
||||
|
||||
let choice = Select::new("What do you want to do:", options)
|
||||
.prompt_skippable()?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user