Add the game creation function to the app

This commit is contained in:
2025-12-18 16:53:00 -05:00
committed by savanni
parent f4250cdafa
commit e7bfbb035a
6 changed files with 133 additions and 30 deletions

View File

@@ -15726,4 +15726,3 @@ rec {
#
};
}

View File

@@ -326,6 +326,41 @@ impl App {
todo!()
}
pub async fn create_game(
&self,
user_id: &UserId,
name: String,
system: String,
) -> Result<Result<GameId, Error>, Fatal> {
let state = self.inner.read().await;
let db = match state.database.open() {
Ok(db) => db,
Err(_) => return Ok(Err(Error::CannotOpen)),
};
let game_id = GameId::default();
let game = Game {
id: game_id.clone(),
name,
system,
gm: user_id.clone(),
players: vec![],
background: "".to_owned(),
};
db.save_game(game)?;
Ok(Ok(game_id))
}
pub async fn link_player(
&self,
_gm_id: &UserId,
_game_id: &GameId,
_user_id: &UserId,
) -> Result<Result<(), Error>, Fatal> {
todo!()
}
pub async fn set_tabletop_image(
&self,
user_id: &UserId,
@@ -743,9 +778,7 @@ impl App {
None => (game.name.clone(), game.background.clone()),
};
eprintln!("sending title");
socket.sender.send(GameMessage::Title(title)).unwrap();
eprintln!("sending background");
socket
.sender
.send(GameMessage::Background(background))

View File

@@ -3,7 +3,9 @@ use visions_types::*;
use crate::{
app::Error,
test_utils::{TestSupport, VAKARIAN_ID, VAKARIAN_PASSWORD, VAKARIAN_USERNAME},
test_utils::{
ALENKO_ID, TSONI_ID, TestSupport, VAKARIAN_ID, VAKARIAN_PASSWORD, VAKARIAN_USERNAME,
},
};
/// SCENARIO: The app can check the user's password
@@ -105,7 +107,6 @@ async fn deleting_a_deleted_session_returns_an_error() {
);
}
/*
/// SCENARIO: A user can be retrieved by user ID
/// GIVEN: The app has already been populated with data
/// WHEN: Requesting a user by ID
@@ -118,11 +119,11 @@ async fn retrieve_a_user() {
// WHEN: Requesting a user by ID
// THEN: The applicant gets back the user information
assert_matches!(app.user(&UserId::from(VAKARIAN_ID)).await, Ok(Some(user)) => {
assert_matches!(app.user(&UserId::from(VAKARIAN_ID)).await, Ok(Ok(Some(user))) => {
assert_eq!(user.id, UserId::from(VAKARIAN_ID));
});
assert_matches!(app.user(&UserId::from("arbitrary-id")).await, Ok(None));
assert_matches!(app.user(&UserId::from("arbitrary-id")).await, Ok(Ok(None)));
}
/// SCENARIO: A user can create a new game
@@ -144,12 +145,13 @@ async fn create_a_game() {
"candela".to_owned(),
)
.await
.unwrap()
.unwrap();
// THEN: The game is created with the specified title and system,
// and the user is assigned as the GM
let game = app.game(&game_id).await;
assert_matches!(game, Ok(Some(game)) => {
assert_matches!(game, Ok(Ok(Some(game))) => {
assert_eq!(game.name, "The Eternal Atheneum");
assert_eq!(game.system, "candela".to_owned());
assert_eq!(game.gm, UserId::from(VAKARIAN_ID));
@@ -175,6 +177,7 @@ async fn link_user() {
"candela".to_owned(),
)
.await
.unwrap()
.unwrap();
// WHEN: The GM assigns a user to a game
@@ -188,11 +191,11 @@ async fn link_user() {
// THEN: The game now has the user in it
let game = app.game(&game_id).await;
assert_matches!(game, Ok(Some(game)) => {
assert_matches!(game, Ok(Ok(Some(game))) => {
assert_eq!(game.name, "The Eternal Atheneum");
assert_eq!(game.system, "candela".to_owned());
assert_eq!(game.gm, UserId::from(VAKARIAN_ID));
assert!(game.contains_player(&UserId::from(ALENKO_ID)));
assert!(game.contains_user(&UserId::from(ALENKO_ID)));
});
}
@@ -215,22 +218,22 @@ async fn link_user_denied() {
"candela".to_owned(),
)
.await
.unwrap()
.unwrap();
// WHEN: A regular user assigns a user to a game
assert_matches!(
app.link_player(&UserId::from(TSONI_ID), &game_id, &UserId::from(ALENKO_ID),)
app.link_player(&UserId::from(TSONI_ID), &game_id, &UserId::from(ALENKO_ID))
.await,
ResultExt::Err(_)
Ok(Err(Error::Forbidden))
);
// THEN: The game does not have the user
let game = app.game(&game_id).await;
assert_matches!(game, Ok(Some(game)) => {
assert_matches!(game, Ok(Ok(Some(game))) => {
assert_eq!(game.name, "The Eternal Atheneum");
assert_eq!(game.system, "candela".to_owned());
assert_eq!(game.gm, UserId::from(VAKARIAN_ID));
assert!(!game.contains_player(&UserId::from(ALENKO_ID)));
assert!(!game.contains_user(&UserId::from(ALENKO_ID)));
});
}
*/

View File

@@ -1,21 +1,30 @@
use std::path::PathBuf;
use visions_core::{database::Database, types::Game};
use visions_core::{
database::Database,
types::{Game, User},
};
use visions_types::*;
pub const ZELL_ID: &str = "zell-id";
pub async fn populate_database(db: &Database) {
let db = db.open().unwrap();
for card in cards() {
db.save_card(card).expect("card should have been saved");
}
for character in charsheets() {
db.save_character(character).expect("save character");
for user in users() {
db.save_user(user).expect("save user");
}
for game in games() {
db.save_game(game).expect("save game")
db.save_game(game).expect("save game");
}
for card in cards() {
db.save_card(card).expect("card should have been saved");
}
for character in charsheets() {
db.save_character(character).expect("save character");
}
}
@@ -48,6 +57,67 @@ async fn main() -> Result<(), String> {
Ok(())
}
pub fn users() -> Vec<User> {
vec![
User {
id: UserId::from("savanni"),
email: "savanni@nowhere.com".to_owned(),
name: "Savanni".to_owned(),
admin: true,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
User {
id: UserId::from("vakarian-id"),
email: "vakarian@nowhere.com".to_owned(),
name: "Garrus Vakarian".to_owned(),
admin: false,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
User {
id: UserId::from("joker-id"),
email: "joker@nowhere.com".to_owned(),
name: "Jeff Moroeu".to_owned(),
admin: false,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
User {
id: UserId::from("alenko-id"),
email: "alenko@nowhere.com".to_owned(),
name: "Kaiden Alenko".to_owned(),
admin: false,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
User {
id: UserId::from("tali-id"),
email: "tali@nowhere.com".to_owned(),
name: "Tali Zora Vas Raya".to_owned(),
admin: false,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
User {
id: UserId::from("tsoni-id"),
email: "tsoni@nowhere.com".to_owned(),
name: "Liara Tsoni".to_owned(),
admin: false,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
User {
id: UserId::from("samara-id"),
email: "samara@nowhere.com".to_owned(),
name: "Samara".to_owned(),
admin: false,
status: AccountStatus::Ok,
password: "aoeu".to_owned(),
},
]
}
pub fn cards() -> Vec<Card> {
vec![
Card{

View File

@@ -16,12 +16,9 @@ pub async fn get_game(app: App, headers: HeaderMap, game_id: GameId) -> Response
.await
}
pub async fn get_games(_app: App, _headers: HeaderMap) -> Response<Body> {
// auth_required(app.clone(), headers, move |_user| {
// clone!(app, async move {
// http_adapter(no_error(app.game_overviews())).await
// })
// })
// .await
todo!()
pub async fn get_games(app: App, headers: HeaderMap) -> Response<Body> {
auth_required(app.clone(), headers, move |_user| {
clone!(app, async move { http_adapter(app.game_overviews()).await })
})
.await
}

View File

@@ -2,6 +2,7 @@
name = "visions-ui"
version = "0.1.0"
edition = "2021"
crate-type = ["cdylib", "rlib"]
[dependencies]
async-std.workspace = true