Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "gfcore"
description = "Go Fish card game engine"
version = "0.0.1"
version = "0.0.2"
edition = "2024"
rust-version = "1.85"
license = "MIT OR Apache-2.0"
Expand Down
21 changes: 21 additions & 0 deletions src/game/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,27 @@ impl Game {
})
}

/// Like [`Self::state`] but always reveals `observer`'s hand regardless of whose
/// turn it is. Used by the WASM layer so the human's hand stays visible
/// throughout bot turns.
///
/// # Errors
///
/// Returns [`GfError::InvalidTarget`] if `observer` is out of range for the current player list.
pub fn state_as_observer(&self, observer: usize) -> Result<GameState, GfError> {
let players = PlayerView::from_perspective(&self.players, observer)?;

Ok(GameState {
phase: self.phase.clone(),
current_player: self.current_player,
players,
draw_pile_size: self.draw_pile.len(),
last_event: self.last_event.clone(),
winner: self.winner,
ask_log: self.ask_log.clone(),
})
}

/// Returns the index of the player whose turn it is.
///
/// # Examples
Expand Down
30 changes: 29 additions & 1 deletion src/wasm_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,26 @@ pub fn get_state() -> String {
})
}

/// Like [`get_state`] but always returns state from player 0's perspective so
/// the human hand remains visible during bot turns.
#[must_use]
#[wasm_bindgen]
pub fn get_human_state() -> String {
GAME.with(|cell| {
let borrow = cell.borrow();
match borrow.as_ref() {
None => error_json("no game in progress"),
Some(game) => match game.state_as_observer(0) {
Ok(s) => match serde_json::to_string(&s) {
Ok(j) => j,
Err(e) => error_json(&e.to_string()),
},
Err(e) => error_json(&e.to_string()),
},
}
})
}

/// If the current player is a bot, computes and applies their action.
///
/// Returns:
Expand Down Expand Up @@ -359,6 +379,12 @@ pub fn step_bot() -> String {
profile.decide(&hand, &state.players, &state.ask_log)
};

// Serialize the action before it is consumed by game.act().
let action_json = match serde_json::to_string(&action) {
Ok(j) => j,
Err(e) => return error_json(&e.to_string()),
};

// Apply the action.
let event_json = GAME.with(|cell| {
let mut borrow = cell.borrow_mut();
Expand All @@ -383,7 +409,9 @@ pub fn step_bot() -> String {
return event_json;
}

format!("{{\"done\":false,\"event\":{event_json}}}")
format!(
"{{\"done\":false,\"player\":{current_player},\"action\":{action_json},\"event\":{event_json}}}"
)
}

/// Returns the full game history as YAML.
Expand Down
Loading