woo! application state!

This commit is contained in:
Max Bradbury 2020-08-23 12:19:20 +01:00
parent 73074c8b75
commit 1c1d183dbe
3 changed files with 60 additions and 19 deletions

View File

@ -10,5 +10,6 @@ edition = "2018"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
"bitsy-parser" = "^0.71.1" "bitsy-parser" = "^0.72.0"
"lazy_static" = "^1.4.0"
"wasm-bindgen" = "^0.2.64" "wasm-bindgen" = "^0.2.64"

View File

@ -1,13 +1,27 @@
import init, {merge} from './pkg/bitsy_merge.js'; import init, {load_main, load_additional, merge} from './pkg/mixsy.js';
async function run() { async function run() {
await init(); await init();
document.getElementById('merge').addEventListener('click', () => { const main = document.getElementById('a');
let a = document.getElementById('a').value; const additional = document.getElementById('b');
let b = document.getElementById('b').value; const output = document.getElementById('c');
document.getElementById('c').value = merge(a, b); const merge_button = document.getElementById('merge');
})
main.addEventListener('change', () => {
console.debug(load_main(main.value));
});
additional.addEventListener('change', () => {
console.debug(load_additional(additional.value));
});
function mix() {
output.value = merge();
}
merge_button.addEventListener('click', mix);
merge_button.addEventListener('touchend', mix);
} }
run(); run();

View File

@ -1,22 +1,48 @@
use wasm_bindgen::prelude::*;
use bitsy_parser::game::Game; use bitsy_parser::game::Game;
use lazy_static::lazy_static;
use std::sync::Mutex;
use wasm_bindgen::prelude::*;
struct State {
main: Option<Game>,
additional: Option<Game>,
}
lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(State { main: None, additional: None});
}
// TODO DEDUPE
#[wasm_bindgen] #[wasm_bindgen]
pub fn merge(a: String, b: String) -> String { pub fn load_main(game_data: String) -> String {
let main = Game::from(a); let game = Game::from(game_data);
let additional = Game::from(b);
if main.is_err() { if game.is_ok() {
return "Couldn't parse main game.".to_string(); STATE.lock().unwrap().main = Some(game.unwrap());
"OK!"
} else {
"Could not parse game data"
}.to_string()
} }
if additional.is_err() { #[wasm_bindgen]
return "Couldn't parse additional game.".to_string(); pub fn load_additional(game_data: String) -> String {
let game = Game::from(game_data);
if game.is_ok() {
STATE.lock().unwrap().additional = Some(game.unwrap());
"OK!"
} else {
"Could not parse game data"
}.to_string()
} }
let mut main = main.unwrap(); #[wasm_bindgen]
let additional = additional.unwrap(); pub fn merge() -> String {
let state = STATE.lock().unwrap();
let mut main = state.main.clone().unwrap();
let additional = state.additional.clone().unwrap();
main.merge(additional); main.merge(additional);
main.dedupe_tiles(); main.dedupe_tiles();
main.to_string() main.to_string()