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"]
[dependencies]
"bitsy-parser" = "^0.71.1"
"bitsy-parser" = "^0.72.0"
"lazy_static" = "^1.4.0"
"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() {
await init();
document.getElementById('merge').addEventListener('click', () => {
let a = document.getElementById('a').value;
let b = document.getElementById('b').value;
document.getElementById('c').value = merge(a, b);
})
const main = document.getElementById('a');
const additional = document.getElementById('b');
const output = document.getElementById('c');
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();

View File

@ -1,22 +1,48 @@
use wasm_bindgen::prelude::*;
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]
pub fn merge(a: String, b: String) -> String {
let main = Game::from(a);
let additional = Game::from(b);
pub fn load_main(game_data: String) -> String {
let game = Game::from(game_data);
if main.is_err() {
return "Couldn't parse main game.".to_string();
}
if game.is_ok() {
STATE.lock().unwrap().main = Some(game.unwrap());
"OK!"
} else {
"Could not parse game data"
}.to_string()
}
if additional.is_err() {
return "Couldn't parse additional game.".to_string();
}
#[wasm_bindgen]
pub fn load_additional(game_data: String) -> String {
let game = Game::from(game_data);
let mut main = main.unwrap();
let additional = additional.unwrap();
if game.is_ok() {
STATE.lock().unwrap().additional = Some(game.unwrap());
"OK!"
} else {
"Could not parse game data"
}.to_string()
}
#[wasm_bindgen]
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.dedupe_tiles();
main.to_string()