planet-wars/frontend/src/lib.rs

48 lines
919 B
Rust
Raw Normal View History

2019-09-13 21:18:11 +00:00
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
2019-09-13 20:54:21 +00:00
mod utils;
2019-09-13 21:18:11 +00:00
mod types;
2019-09-13 20:54:21 +00:00
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
2019-09-14 08:48:20 +00:00
#[wasm_bindgen]
pub struct Game {
states : Vec<types::State>,
/* put extra shit here */
2019-09-13 21:18:11 +00:00
}
2019-09-13 20:54:21 +00:00
#[wasm_bindgen]
2019-09-14 08:48:20 +00:00
impl Game {
pub fn new(file: &str) -> Self {
utils::set_panic_hook();
// First line is fucked but we just filter out things that cannot parse
let states = file.split("\n").filter_map(|line|
serde_json::from_str(line).ok()
).collect();
Self {
states
}
}
pub fn turn_count(&self) -> usize {
self.states.len()
}
2019-09-13 20:54:21 +00:00
}
2019-09-14 08:48:20 +00:00
2019-09-13 20:54:21 +00:00
#[wasm_bindgen]
2019-09-14 08:48:20 +00:00
extern {
fn alert(s: &str);
2019-09-13 20:54:21 +00:00
}