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};
pub mod gamemaster;
use crate::gamemaster::*;
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)
}
#[allow(dead_code)]
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 {
name: "Adam".to_string(),
role: PlayerRole::Host,
white: vec![],
black: vec![],
};
let test_player1 = CAHPlayer {
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(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(())
}
// Our shared state
pub struct AppState {
// We require unique usernames. This tracks which usernames have been taken.
user_set: Mutex>,
// Channel used to send messages to all connected clients.
tx: broadcast::Sender,
// Master card decks
all_cards: Mutex>,
// Games list
games: Mutex>,
}
#[tokio::main]
async fn main() -> Result<(), Box> {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "cards=trace".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
// Set up application state for use with with_state().
let user_set = Mutex::new(HashSet::new());
let (tx, _rx) = broadcast::channel(100);
// 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 all_cards = Mutex::new(load_json(cards_input_path)?);
let games = Mutex::new(vec![]);
let app_state = Arc::new(AppState {
user_set,
tx,
all_cards,
games,
});
let app = Router::new()
.route("/", get(index))
.route("/test", get(spawnclients))
.route("/websocket", get(websocket_handler))
.with_state(app_state);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3030").await?;
tracing::debug!("listening on {}", listener.local_addr()?);
axum::serve(listener, app).await?;
Ok(())
}
// Include utf-8 file at **compile** time.
async fn index() -> Html<&'static str> {
Html(std::include_str!("../test_client.html"))
}
async fn spawnclients() -> Html<&'static str> {
Html(std::include_str!("../spawnclients.html"))
}