cleanup and load cards

This commit is contained in:
Adam 2024-07-29 00:34:17 -04:00
parent d878682805
commit f5ee3357a5

View file

@ -15,7 +15,7 @@ pub mod api;
use crate::api::*;
/// Parse json for card data
fn load_json(path: &str) -> Result<Vec<CardSet>> {
fn load_cards_from_json(path: &str) -> Result<Vec<CardSet>> {
let data: String =
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
let jayson: Vec<CardSet> =
@ -24,69 +24,18 @@ fn load_json(path: &str) -> Result<Vec<CardSet>> {
Ok(jayson)
}
fn load_names(path: &str) -> Vec<String> {
let f = File::open(path).unwrap();
/// Parse name list
fn load_names(path: &str) -> Result<Vec<String>> {
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<CardSet> = 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<Game> = 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<String>,
// Master card decks
// all_cards: Mutex<Vec<CardSet>>,
all_cards: Vec<CardSet>,
// Games list
games: Mutex<Vec<Game>>,
first_names: Vec<String>,
@ -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::<SocketAddr, User>::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,