simplify config storage, since its never mutated anyways
This commit is contained in:
parent
32d199933c
commit
1b2b330645
|
@ -1,34 +1,22 @@
|
||||||
use std::{cell::RefCell, mem::MaybeUninit};
|
|
||||||
use wasm_bindgen_macro::wasm_bindgen;
|
use wasm_bindgen_macro::wasm_bindgen;
|
||||||
|
|
||||||
use fish::{Bot, Weights};
|
use fish::{Bot, Weights};
|
||||||
use mino::srs::{PieceType, Queue};
|
use mino::srs::{PieceType, Queue};
|
||||||
use mino::MatBuf;
|
use mino::MatBuf;
|
||||||
|
|
||||||
struct State {
|
struct Config {
|
||||||
weights: Weights,
|
weights: Weights,
|
||||||
max_iters: u32,
|
max_iters: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
static mut STATE: MaybeUninit<RefCell<State>> = MaybeUninit::uninit();
|
static CONFIG: Config = Config {
|
||||||
|
weights: Weights::DEFAULT,
|
||||||
impl State {
|
max_iters: 50_000,
|
||||||
fn new() -> RefCell<State> {
|
};
|
||||||
RefCell::new(State {
|
|
||||||
weights: Weights::default(),
|
|
||||||
max_iters: 10_000,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get() -> &'static RefCell<State> {
|
|
||||||
unsafe { STATE.assume_init_ref() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[wasm_bindgen(start)]
|
#[wasm_bindgen(start)]
|
||||||
fn start() {
|
fn start() {
|
||||||
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
|
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||||
unsafe { STATE.write(State::new()) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
|
@ -38,9 +26,9 @@ pub fn get_version() -> String {
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn get_config() -> Vec<i32> {
|
pub fn get_config() -> Vec<i32> {
|
||||||
let state = State::get().borrow();
|
let mut config = Vec::from(CONFIG.weights.0);
|
||||||
let mut config = Vec::from(state.weights.0);
|
config.push(Weights::PER_PIECE);
|
||||||
config.push(state.max_iters as i32);
|
config.push(CONFIG.max_iters as i32);
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,11 +39,9 @@ pub fn suggest(matrix: String, hold: String, next: String) -> Result<Vec<i32>, S
|
||||||
let next = parse_queue(&next)?;
|
let next = parse_queue(&next)?;
|
||||||
let queue = Queue::new(hold, &next);
|
let queue = Queue::new(hold, &next);
|
||||||
|
|
||||||
let state = State::get().borrow();
|
|
||||||
|
|
||||||
let res = {
|
let res = {
|
||||||
let mut bot = Bot::new(&state.weights, &matrix, queue);
|
let mut bot = Bot::new(&CONFIG.weights, &matrix, queue);
|
||||||
bot.think(state.max_iters);
|
bot.think(CONFIG.max_iters);
|
||||||
bot.suggest().ok_or("No suggestion found")?
|
bot.suggest().ok_or("No suggestion found")?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,10 @@ async function handle(msg) {
|
||||||
case 'init':
|
case 'init':
|
||||||
{
|
{
|
||||||
let version = getVersion();
|
let version = getVersion();
|
||||||
let weights = Array.from(getConfig());
|
let config = Array.from(getConfig());
|
||||||
let maxIters = weights.pop();
|
let maxIters = config.pop();
|
||||||
|
let weightFactor = config.pop();
|
||||||
|
let weights = config.map(w => w / weightFactor);
|
||||||
return { version, config: { weights, maxIters } };
|
return { version, config: { weights, maxIters } };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue