use std::fs::{read, read_to_string}; use std::{error::Error, fs, result::Result}; use warp::Filter; #[allow(non_snake_case)] pub mod CAHd_game; use crate::CAHd_game::*; pub mod api; use crate::api::*; /// Parse json for card data fn load_json(path: &str) -> Result, Box> { let data: String = fs::read_to_string(path).expect("Error reading file"); let jayson: Vec = serde_json::from_str(&data)?; Ok(jayson) } fn test() -> Result<(), Box> { // 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 = CAHPlayer { player_name: "Adam".to_string(), role: PlayerRole::Host, white: vec![], black: vec![], }; let test_player1 = CAHPlayer { 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, }; games.push(CAHGame::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(()) } #[tokio::main] async fn main() -> Result<(), Box> { pretty_env_logger::init(); test()?; // Keep track of all connected users, key is usize, value // is a websocket sender. let users = api::Users::default(); // Turn our "state" into a new Filter... let users = warp::any().map(move || users.clone()); // GET /chat -> websocket upgrade let chat = warp::path("chat") // The `ws()` filter will prepare Websocket handshake... .and(warp::ws()) .and(users) .map(|ws: warp::ws::Ws, users| { // This will call our function if the handshake succeeds. ws.on_upgrade(move |socket| on_user_connected(socket, users)) }); // GET / -> index html let index = warp::get() .and(warp::path::end()) .and(warp::fs::file("./test_client.html")); let routes = index.or(chat); warp::serve(routes).run(([0, 0, 0, 0], 3030)).await; Ok(()) }