Skip to content

Commit

Permalink
Remove JsGameMove & GameMoveType
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Oct 1, 2023
1 parent acb3d0d commit 8c9dd2c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
13 changes: 6 additions & 7 deletions src/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import enums from './enum.json' assert { type: 'json' };
import { randint } from './util.js';
import * as wasm from './rs/pkg/etg.js';

const GameMoveType = ['end', 'cast', 'accept', 'mulligan', 'foe', 'resign'];

export default class Game {
constructor(data) {
this.game = new wasm.Game(
Expand Down Expand Up @@ -90,9 +92,9 @@ export default class Game {
aiSearch() {
const cmd = this.aisearch();
return {
x: wasm.GameMoveType[cmd.x],
c: cmd.c,
t: cmd.t,
x: GameMoveType[cmd[0]],
c: cmd[1],
t: cmd[2],
};
}
countPlies() {
Expand All @@ -105,7 +107,7 @@ export default class Game {
}
nextCmd(cmd, fx = true) {
if (this.replay) this.replay.push(cmd);
return this.next(wasm.GameMoveType[cmd.x], cmd.c | 0, cmd.t | 0, fx);
return this.next(GameMoveType.indexOf(cmd.x), cmd.c | 0, cmd.t | 0, fx);
}
withMoves(moves) {
const newgame = new Game(this.data);
Expand Down Expand Up @@ -133,9 +135,6 @@ export default class Game {
const pos = this.tgt_to_pos(id, p1id);
return pos === 0 ? null : { x: pos & 4095, y: pos >> 12 };
}
info(id) {
return wasm.thingText(this.game, id);
}
}

for (const k of Object.getOwnPropertyNames(wasm.Game.prototype)) {
Expand Down
70 changes: 24 additions & 46 deletions src/rs/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(non_upper_case_globals)]

use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::vec;
Expand Down Expand Up @@ -195,52 +196,29 @@ pub enum GameMove {
Resign(i32),
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Clone, Copy)]
pub enum GameMoveType {
end = 0,
cast = 1,
accept = 2,
mulligan = 3,
foe = 4,
resign = 5,
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Clone, Copy)]
pub struct JsGameMove {
pub x: GameMoveType,
pub c: i32,
pub t: i32,
}

impl From<GameMove> for JsGameMove {
fn from(cmd: GameMove) -> JsGameMove {
let triplet = match cmd {
GameMove::End(t) => (GameMoveType::end, 0, t),
GameMove::Cast(c, t) => (GameMoveType::cast, c, t),
GameMove::Accept => (GameMoveType::accept, 0, 0),
GameMove::Mulligan => (GameMoveType::mulligan, 0, 0),
GameMove::Foe(t) => (GameMoveType::foe, 0, t),
GameMove::Resign(c) => (GameMoveType::resign, c, 0),
};
JsGameMove {
x: triplet.0,
c: triplet.1,
t: triplet.2,
impl From<GameMove> for [i32; 3] {
fn from(cmd: GameMove) -> [i32; 3] {
match cmd {
GameMove::End(t) => [0, 0, t],
GameMove::Cast(c, t) => [1, c, t],
GameMove::Accept => [2, 0, 0],
GameMove::Mulligan => [3, 0, 0],
GameMove::Foe(t) => [4, 0, t],
GameMove::Resign(c) => [5, c, 0],
}
}
}

impl From<JsGameMove> for GameMove {
fn from(cmd: JsGameMove) -> GameMove {
match cmd.x {
GameMoveType::end => GameMove::End(cmd.t),
GameMoveType::cast => GameMove::Cast(cmd.c, cmd.t),
GameMoveType::accept => GameMove::Accept,
GameMoveType::mulligan => GameMove::Mulligan,
GameMoveType::foe => GameMove::Foe(cmd.t),
GameMoveType::resign => GameMove::Resign(cmd.c),
impl From<[i32; 3]> for GameMove {
fn from(cmd: [i32; 3]) -> GameMove {
match cmd[0] {
0 => GameMove::End(cmd[2]),
1 => GameMove::Cast(cmd[1], cmd[2]),
2 => GameMove::Accept,
3 => GameMove::Mulligan,
4 => GameMove::Foe(cmd[2]),
5 => GameMove::Resign(cmd[1]),
_ => GameMove::Mulligan,
}
}
}
Expand Down Expand Up @@ -997,9 +975,9 @@ impl Game {
})
}

pub fn next(&mut self, x: GameMoveType, c: i32, t: i32, fx: bool) -> Option<Vec<i32>> {
pub fn next(&mut self, x: i32, c: i32, t: i32, fx: bool) -> Option<Vec<i32>> {
self.fx = if fx { Some(Fxs::new()) } else { None };
self.r#move(JsGameMove { x, c, t }.into());
self.r#move([x, c, t].into());
self.fx.take().map(|fxs| fxs.js())
}

Expand Down Expand Up @@ -1136,9 +1114,9 @@ impl Game {
.unwrap_or(false)
}

pub fn aisearch(&self) -> JsGameMove {
pub fn aisearch(&self) -> Box<[i32]> {
use crate::aisearch::search;
search(self).into()
Box::<[i32; 3]>::new(search(self).into()) as Box<[i32]>
}

pub fn aieval(&self) -> f32 {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/soi.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function App() {
const golemId = game
.visible_instances(1)
.find(id => game.get_kind(id) === Kind.Creature);
return golemId ? game.info(golemId) : 'No Shard Golem spawned';
return golemId ? game.thingText(golemId) : 'No Shard Golem spawned';
} else return 'No Shard of Integrity to cast';
} else return 'Too many cards';
};
Expand Down
2 changes: 1 addition & 1 deletion src/views/Match.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ export default function Match(props) {
targeting().filter(id) &&
game().actinfo(targeting().src, id);
setTooltip({
text: `${game().info(id)}${actinfo ? '\n' + actinfo : ''}`,
text: `${game().thingText(id)}${actinfo ? '\n' + actinfo : ''}`,
style: `position:absolute;left:${e.pageX}px;top:${e.pageY}px;z-index:5`,
});
if (game().get_kind(id) !== Kind.Player) setCard(e, game().getCard(id));
Expand Down

0 comments on commit 8c9dd2c

Please sign in to comment.