jumpstart

This commit is contained in:
ajuvercr 2019-09-13 23:18:11 +02:00
parent ae7159889b
commit f871e2a338
3 changed files with 50 additions and 2 deletions

View file

@ -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"

View file

@ -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<Box<types::State>> = 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);
}

25
frontend/src/types.rs Normal file
View file

@ -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<u32>,
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct State {
pub planets: Vec<Planet>,
pub expeditions: Vec<Expedition>,
}