Compare commits

...

4 Commits

4 changed files with 28 additions and 83 deletions

View File

@ -8,7 +8,7 @@ version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if",
"once_cell", "once_cell",
"version_check", "version_check",
"zerocopy", "zerocopy",
@ -32,18 +32,22 @@ version = "3.15.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b" checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]] [[package]]
name = "fish" name = "fish"
version = "0.1.0" version = "0.1.0"
@ -60,11 +64,11 @@ dependencies = [
name = "fish-webworker" name = "fish-webworker"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"console_error_panic_hook",
"fish", "fish",
"mino", "mino",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-macro", "wasm-bindgen-macro",
"wee_alloc",
] ]
[[package]] [[package]]
@ -83,24 +87,12 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.20" version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]] [[package]]
name = "mino" name = "mino"
version = "0.1.0" version = "0.1.0"
@ -235,7 +227,7 @@ version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
@ -283,40 +275,6 @@ version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838"
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "zerocopy" name = "zerocopy"
version = "0.7.32" version = "0.7.32"

View File

@ -13,6 +13,4 @@ mino = { path = "../mino" }
wasm-bindgen = "0.2" wasm-bindgen = "0.2"
wasm-bindgen-macro = "0.2" wasm-bindgen-macro = "0.2"
wee_alloc = "0.4" console_error_panic_hook = "0.1.7"
# console_error_panic_hook = "0.1.7"

View File

@ -3,10 +3,7 @@ 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, Rot}; use mino::MatBuf;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
struct State { struct State {
weights: Weights, weights: Weights,
@ -30,6 +27,7 @@ impl State {
#[wasm_bindgen(start)] #[wasm_bindgen(start)]
fn start() { fn start() {
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
unsafe { STATE.write(State::new()) }; unsafe { STATE.write(State::new()) };
} }
@ -47,7 +45,7 @@ pub fn get_config() -> Vec<i32> {
} }
#[wasm_bindgen] #[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 matrix = parse_matrix(&matrix);
let hold = parse_hold(&hold)?; let hold = parse_hold(&hold)?;
let next = parse_queue(&next)?; let next = parse_queue(&next)?;
@ -61,22 +59,12 @@ pub fn suggest(matrix: String, hold: String, next: String) -> Result<String, Str
bot.suggest().ok_or("No suggestion found")? bot.suggest().ok_or("No suggestion found")?
}; };
Ok(format!( Ok(vec![
"{x},{y},{r},{ty}", res.loc.x as i16 as i32,
x = res.loc.x, res.loc.y as i16 as i32,
y = res.loc.y, res.loc.r as i8 as i32,
r = rot(res.loc.r), res.ty as u8 as i32,
ty = res.ty ])
))
}
fn rot(r: Rot) -> &'static str {
match r {
Rot::N => "spawn",
Rot::E => "right",
Rot::S => "reverse",
Rot::W => "left",
}
} }
fn parse_matrix(s: &str) -> MatBuf { fn parse_matrix(s: &str) -> MatBuf {

View File

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