use axum::{ response::{Html}, routing::get, Router, }; use std::{ collections::HashSet, sync::{Arc, Mutex}, }; use std::{error::Error, fs, result::Result}; use tokio::sync::broadcast; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[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() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "example_chat=trace".into()), ) .with(tracing_subscriber::fmt::layer()) .init(); let _ = test(); // Set up application state for use with with_state(). let user_set = Mutex::new(HashSet::new()); let (tx, _rx) = broadcast::channel(100); let app_state = Arc::new(AppState { user_set, tx }); let app = Router::new() .route("/", get(index)) .route("/websocket", get(websocket_handler)) .with_state(app_state); let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") .await .unwrap(); tracing::debug!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } // Include utf-8 file at **compile** time. async fn index() -> Html<&'static str> { Html(std::include_str!("../chat.html")) }