cleanup and load cards
This commit is contained in:
parent
d878682805
commit
f5ee3357a5
1 changed files with 12 additions and 64 deletions
|
@ -15,7 +15,7 @@ pub mod api;
|
||||||
use crate::api::*;
|
use crate::api::*;
|
||||||
|
|
||||||
/// Parse json for card data
|
/// 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 =
|
let data: String =
|
||||||
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
||||||
let jayson: Vec<CardSet> =
|
let jayson: Vec<CardSet> =
|
||||||
|
@ -24,69 +24,18 @@ fn load_json(path: &str) -> Result<Vec<CardSet>> {
|
||||||
Ok(jayson)
|
Ok(jayson)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_names(path: &str) -> Vec<String> {
|
/// Parse name list
|
||||||
let f = File::open(path).unwrap();
|
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 f = BufReader::new(f);
|
||||||
|
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
|
|
||||||
for line in f.lines() {
|
for line in f.lines() {
|
||||||
buf.push(line.unwrap())
|
buf.push(line?)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf
|
Ok(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(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our shared state
|
// Our shared state
|
||||||
|
@ -96,7 +45,7 @@ pub struct AppState {
|
||||||
// Channel used to send messages to all connected clients.
|
// Channel used to send messages to all connected clients.
|
||||||
tx: broadcast::Sender<String>,
|
tx: broadcast::Sender<String>,
|
||||||
// Master card decks
|
// Master card decks
|
||||||
// all_cards: Mutex<Vec<CardSet>>,
|
all_cards: Vec<CardSet>,
|
||||||
// Games list
|
// Games list
|
||||||
games: Mutex<Vec<Game>>,
|
games: Mutex<Vec<Game>>,
|
||||||
first_names: Vec<String>,
|
first_names: Vec<String>,
|
||||||
|
@ -115,18 +64,17 @@ async fn main() -> Result<()> {
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
// Set up application state for use with with_state().
|
// Set up application state for use with with_state().
|
||||||
// let user_set = Mutex::new(HashSet::new());
|
|
||||||
let (tx, _rx) = broadcast::channel(100);
|
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 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 games = Mutex::new(vec![]);
|
||||||
let first_names = load_names("data/first.txt");
|
let first_names = load_names("data/first.txt")?;
|
||||||
let last_names = load_names("data/last.txt");
|
let last_names = load_names("data/last.txt")?;
|
||||||
|
|
||||||
let app_state = Arc::new(AppState {
|
let app_state = Arc::new(AppState {
|
||||||
users,
|
users,
|
||||||
tx,
|
tx,
|
||||||
// all_cards,
|
all_cards,
|
||||||
games,
|
games,
|
||||||
first_names,
|
first_names,
|
||||||
last_names,
|
last_names,
|
||||||
|
|
Loading…
Add table
Reference in a new issue