diff --git a/server/src/main.rs b/server/src/main.rs index 50076c2..af8e8da 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -15,7 +15,7 @@ pub mod api; use crate::api::*; /// Parse json for card data -fn load_json(path: &str) -> Result> { +fn load_cards_from_json(path: &str) -> Result> { let data: String = read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?; let jayson: Vec = @@ -24,69 +24,18 @@ fn load_json(path: &str) -> Result> { Ok(jayson) } -fn load_names(path: &str) -> Vec { - let f = File::open(path).unwrap(); +/// Parse name list +fn load_names(path: &str) -> Result> { + let f = File::open(path).with_context(|| format!("Invalid names path: \"{}\"", path))?; let f = BufReader::new(f); let mut buf = vec![]; for line in f.lines() { - buf.push(line.unwrap()) + buf.push(line?) } - buf -} - -// this is still around just for reference -#[allow(dead_code)] -fn test() -> Result<()> { - // choose decks - let cards_input_path: &str = "../data/cah-cards-full.json"; - - // TODO: this should be a master card database and pointers - // to the cards should be passed to the game instead of actual cards - let chosen_packs: Vec = load_json(cards_input_path)?; - println!("{}", &chosen_packs.len()); - - let test_player0 = Player { - name: "Adam".to_string(), - role: PlayerRole::Host, - white: vec![], - black: vec![], - }; - - let test_player1 = Player { - name: "Ferris".to_string(), - role: PlayerRole::Player, - white: vec![], - black: vec![], - }; - - // make some games - // use hashmap? - let mut games: Vec = vec![]; - - // create game with/for player 0 - let test_game0 = NewGameRequest { - name: "Test0".to_string(), - host: test_player0, - // packs: chosen_packs, - packs: vec![0], - }; - - games.push(Game::new(test_game0)?); - - // a new game request struct but this player is a player - games[0].create_player(test_player1)?; - - // start round - games[0].game_start()?; - - println!("----------------------"); - for card in &games[0].players[0].white { - println!("{}", card.text); - } - Ok(()) + Ok(buf) } // Our shared state @@ -96,7 +45,7 @@ pub struct AppState { // Channel used to send messages to all connected clients. tx: broadcast::Sender, // Master card decks - // all_cards: Mutex>, + all_cards: Vec, // Games list games: Mutex>, first_names: Vec, @@ -115,18 +64,17 @@ async fn main() -> Result<()> { .init(); // Set up application state for use with with_state(). - // let user_set = Mutex::new(HashSet::new()); let (tx, _rx) = broadcast::channel(100); - // let cards_input_path: &str = "data/cah-cards-full.json"; let users = Mutex::new(HashMap::::new()); - // let all_cards = Mutex::new(load_json(cards_input_path)?); + let all_cards = load_cards_from_json("data/cah-cards-full.json")?; let games = Mutex::new(vec![]); - let first_names = load_names("data/first.txt"); - let last_names = load_names("data/last.txt"); + let first_names = load_names("data/first.txt")?; + let last_names = load_names("data/last.txt")?; + let app_state = Arc::new(AppState { users, tx, - // all_cards, + all_cards, games, first_names, last_names,