From f871e2a3388e782d543f9d2aa4e92d1b6a9d8ce0 Mon Sep 17 00:00:00 2001 From: ajuvercr Date: Fri, 13 Sep 2019 23:18:11 +0200 Subject: [PATCH] jumpstart --- frontend/Cargo.toml | 4 ++++ frontend/src/lib.rs | 23 +++++++++++++++++++++-- frontend/src/types.rs | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 frontend/src/types.rs diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 2f7baad..7c3cc19 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -25,6 +25,10 @@ console_error_panic_hook = { version = "0.1.1", optional = true } # # Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now. wee_alloc = { version = "0.4.2", optional = true } +serde = "1.0.99" +serde_derive = "1.0.99" +serde_json = "1.0" +lazy_static = "1.4.0" [dev-dependencies] wasm-bindgen-test = "0.2" diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 80ffdf7..2a34709 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -1,4 +1,15 @@ +extern crate serde; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; + +#[macro_use] +extern crate lazy_static; + +use std::sync::{Arc, Mutex}; + mod utils; +mod types; use wasm_bindgen::prelude::*; @@ -8,12 +19,20 @@ use wasm_bindgen::prelude::*; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; +lazy_static! { + static ref STATE: Mutex> = Mutex::new(Box::new(types::State { + planets: Vec::new(), + expeditions: Vec::new(), + })); +} + #[wasm_bindgen] extern { fn alert(s: &str); } #[wasm_bindgen] -pub fn greet(name: &str) { - alert(&format!("Hello, {}!", name)); +pub fn set_state(state: &str) { + let deserialized: types::State = serde_json::from_str(state).unwrap(); + *STATE.lock().unwrap() = Box::new(deserialized); } diff --git a/frontend/src/types.rs b/frontend/src/types.rs new file mode 100644 index 0000000..260030b --- /dev/null +++ b/frontend/src/types.rs @@ -0,0 +1,25 @@ +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Expedition { + pub id: u64, + pub ship_count: u64, + pub origin: String, + pub destination: String, + pub owner: u64, + pub turns_remaining: u64, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Planet { + pub ship_count: u64, + pub x: f64, + pub y: f64, + pub owner: Option, + pub name: String, +} + + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct State { + pub planets: Vec, + pub expeditions: Vec, +}