return ints from suggest() and interpret them in worker.js

This commit is contained in:
milo 2024-03-09 17:01:08 -05:00
parent 4b0f62aa59
commit f92c586cfc
2 changed files with 12 additions and 17 deletions

View File

@ -3,7 +3,7 @@ use wasm_bindgen_macro::wasm_bindgen;
use fish::{Bot, Weights};
use mino::srs::{PieceType, Queue};
use mino::{MatBuf, Rot};
use mino::MatBuf;
struct State {
weights: Weights,
@ -45,7 +45,7 @@ pub fn get_config() -> Vec<i32> {
}
#[wasm_bindgen]
pub fn suggest(matrix: String, hold: String, next: String) -> Result<String, String> {
pub fn suggest(matrix: String, hold: String, next: String) -> Result<Vec<i32>, String> {
let matrix = parse_matrix(&matrix);
let hold = parse_hold(&hold)?;
let next = parse_queue(&next)?;
@ -59,18 +59,12 @@ pub fn suggest(matrix: String, hold: String, next: String) -> Result<String, Str
bot.suggest().ok_or("No suggestion found")?
};
Ok(format!(
"{},{},{},{}",
res.loc.x,
res.loc.y,
match res.loc.r {
Rot::N => "spawn",
Rot::E => "right",
Rot::S => "reverse",
Rot::W => "left",
},
res.ty.name(),
))
Ok(vec![
res.loc.x as i16 as i32,
res.loc.y as i16 as i32,
res.loc.r as i8 as i32,
res.ty as u8 as i32,
])
}
fn parse_matrix(s: &str) -> MatBuf {

View File

@ -19,9 +19,10 @@ async function handle(msg) {
case 'suggest':
{
let res = suggest(msg.state.matrix, msg.state.hold, msg.state.next);
let [x, y, r, type] = res.split(',');
x = +x, y = +y;
let [x, y, r, type] = suggest(msg.state.matrix, msg.state.hold, msg.state.next);
console.log(x,y,r,type);
r = ['north', 'right', 'south', 'west'][r];
type = 'IJLOSTZ'[type];
return { x, y, r, type };
}