simplify config storage, since its never mutated anyways

This commit is contained in:
milo 2024-03-09 17:47:42 -05:00
parent 32d199933c
commit 1b2b330645
2 changed files with 14 additions and 26 deletions

View File

@ -1,34 +1,22 @@
use std::{cell::RefCell, mem::MaybeUninit};
use wasm_bindgen_macro::wasm_bindgen;
use fish::{Bot, Weights};
use mino::srs::{PieceType, Queue};
use mino::MatBuf;
struct State {
struct Config {
weights: Weights,
max_iters: u32,
}
static mut STATE: MaybeUninit<RefCell<State>> = MaybeUninit::uninit();
impl State {
fn new() -> RefCell<State> {
RefCell::new(State {
weights: Weights::default(),
max_iters: 10_000,
})
}
fn get() -> &'static RefCell<State> {
unsafe { STATE.assume_init_ref() }
}
}
static CONFIG: Config = Config {
weights: Weights::DEFAULT,
max_iters: 50_000,
};
#[wasm_bindgen(start)]
fn start() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
unsafe { STATE.write(State::new()) };
}
#[wasm_bindgen]
@ -38,9 +26,9 @@ pub fn get_version() -> String {
#[wasm_bindgen]
pub fn get_config() -> Vec<i32> {
let state = State::get().borrow();
let mut config = Vec::from(state.weights.0);
config.push(state.max_iters as i32);
let mut config = Vec::from(CONFIG.weights.0);
config.push(Weights::PER_PIECE);
config.push(CONFIG.max_iters as i32);
config
}
@ -51,11 +39,9 @@ pub fn suggest(matrix: String, hold: String, next: String) -> Result<Vec<i32>, S
let next = parse_queue(&next)?;
let queue = Queue::new(hold, &next);
let state = State::get().borrow();
let res = {
let mut bot = Bot::new(&state.weights, &matrix, queue);
bot.think(state.max_iters);
let mut bot = Bot::new(&CONFIG.weights, &matrix, queue);
bot.think(CONFIG.max_iters);
bot.suggest().ok_or("No suggestion found")?
};

View File

@ -12,8 +12,10 @@ async function handle(msg) {
case 'init':
{
let version = getVersion();
let weights = Array.from(getConfig());
let maxIters = weights.pop();
let config = Array.from(getConfig());
let maxIters = config.pop();
let weightFactor = config.pop();
let weights = config.map(w => w / weightFactor);
return { version, config: { weights, maxIters } };
}