2026-03-25 16:18:17 +01:00
|
|
|
use futures::channel::mpsc;
|
|
|
|
|
use futures::{FutureExt, StreamExt};
|
|
|
|
|
use gloo_storage::{LocalStorage, Storage};
|
|
|
|
|
use leptos::prelude::*;
|
|
|
|
|
use leptos::task::spawn_local;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
use backbone_lib::session::{ConnectError, GameSession, RoomConfig, RoomRole, SessionEvent};
|
2026-03-29 19:19:33 +02:00
|
|
|
use backbone_lib::traits::{BackEndArchitecture, BackendCommand, ViewStateUpdate};
|
2026-03-25 16:18:17 +01:00
|
|
|
|
|
|
|
|
use crate::components::{ConnectingScreen, GameScreen, LoginScreen};
|
2026-03-29 17:15:22 +02:00
|
|
|
use crate::i18n::I18nContextProvider;
|
2026-03-25 16:18:17 +01:00
|
|
|
use crate::trictrac::backend::TrictracBackend;
|
2026-03-29 19:19:33 +02:00
|
|
|
use crate::trictrac::bot_local::bot_decide;
|
2026-04-12 21:02:59 +02:00
|
|
|
use crate::trictrac::types::{
|
2026-04-17 22:22:50 +02:00
|
|
|
GameDelta, JanEntry, PlayerAction, ScoredEvent, SerStage, SerTurnStage, ViewState,
|
2026-04-12 21:02:59 +02:00
|
|
|
};
|
2026-04-09 20:17:21 +02:00
|
|
|
use trictrac_store::CheckerMove;
|
2026-04-05 18:43:13 +02:00
|
|
|
|
|
|
|
|
use std::collections::VecDeque;
|
2026-03-25 16:18:17 +01:00
|
|
|
|
2026-04-23 20:54:52 +02:00
|
|
|
const RELAY_URL: &str = "ws://localhost:8080/ws";
|
2026-03-25 16:18:17 +01:00
|
|
|
const GAME_ID: &str = "trictrac";
|
|
|
|
|
const STORAGE_KEY: &str = "trictrac_session";
|
|
|
|
|
|
2026-04-23 17:37:10 +02:00
|
|
|
// In debug builds trunk serves on 9091, relay is on 8080.
|
|
|
|
|
// In release the game is served by the relay itself — use relative paths.
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
const HTTP_BASE: &str = "http://localhost:8080";
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
const HTTP_BASE: &str = "";
|
|
|
|
|
|
2026-03-25 16:18:17 +01:00
|
|
|
/// The state the UI needs to render the game screen.
|
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
|
pub struct GameUiState {
|
|
|
|
|
pub view_state: ViewState,
|
|
|
|
|
/// 0 = host, 1 = guest
|
|
|
|
|
pub player_id: u16,
|
2026-03-26 21:13:24 +01:00
|
|
|
pub room_id: String,
|
2026-03-29 19:19:33 +02:00
|
|
|
pub is_bot_game: bool,
|
2026-04-05 18:43:13 +02:00
|
|
|
/// True when this state is a buffered snapshot awaiting player confirmation.
|
|
|
|
|
pub waiting_for_confirm: bool,
|
|
|
|
|
/// Why we are paused — drives the status-bar message in GameScreen.
|
|
|
|
|
pub pause_reason: Option<PauseReason>,
|
2026-04-07 21:32:35 +02:00
|
|
|
/// Points scored by this player in the transition to this state (if any).
|
|
|
|
|
pub my_scored_event: Option<ScoredEvent>,
|
|
|
|
|
pub opp_scored_event: Option<ScoredEvent>,
|
2026-04-09 20:17:21 +02:00
|
|
|
/// Checker moves to animate on this render. None when board is unchanged.
|
|
|
|
|
pub last_moves: Option<(CheckerMove, CheckerMove)>,
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reason the UI is paused waiting for the player to click Continue.
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
|
pub enum PauseReason {
|
|
|
|
|
AfterOpponentRoll,
|
|
|
|
|
AfterOpponentGo,
|
|
|
|
|
AfterOpponentMove,
|
2026-04-17 22:22:50 +02:00
|
|
|
/// Opponent rolled their die in the pre-game ceremony.
|
|
|
|
|
AfterOpponentPreGameRoll,
|
2026-03-25 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Which screen is currently shown.
|
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
|
pub enum Screen {
|
|
|
|
|
Login { error: Option<String> },
|
|
|
|
|
Connecting,
|
|
|
|
|
Playing(GameUiState),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Commands sent from UI event handlers into the network task.
|
|
|
|
|
pub enum NetCommand {
|
2026-03-26 21:13:24 +01:00
|
|
|
CreateRoom {
|
|
|
|
|
room: String,
|
|
|
|
|
},
|
|
|
|
|
JoinRoom {
|
|
|
|
|
room: String,
|
|
|
|
|
},
|
2026-03-25 16:18:17 +01:00
|
|
|
Reconnect {
|
|
|
|
|
relay_url: String,
|
|
|
|
|
game_id: String,
|
|
|
|
|
room_id: String,
|
|
|
|
|
token: u64,
|
|
|
|
|
host_state: Option<Vec<u8>>,
|
|
|
|
|
},
|
2026-03-29 19:19:33 +02:00
|
|
|
PlayVsBot,
|
2026-03-25 16:18:17 +01:00
|
|
|
Action(PlayerAction),
|
|
|
|
|
Disconnect,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Stored in localStorage to reconnect after a page refresh.
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
|
struct StoredSession {
|
|
|
|
|
relay_url: String,
|
|
|
|
|
game_id: String,
|
|
|
|
|
room_id: String,
|
|
|
|
|
token: u64,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
is_host: bool,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
view_state: Option<ViewState>,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 17:37:10 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct MeResponse {
|
|
|
|
|
username: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 16:18:17 +01:00
|
|
|
fn save_session(session: &StoredSession) {
|
|
|
|
|
LocalStorage::set(STORAGE_KEY, session).ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn load_session() -> Option<StoredSession> {
|
|
|
|
|
LocalStorage::get::<StoredSession>(STORAGE_KEY).ok()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clear_session() {
|
|
|
|
|
LocalStorage::delete(STORAGE_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 17:37:10 +02:00
|
|
|
/// Fire-and-forget: tell the relay server who won. Only called by the host.
|
|
|
|
|
async fn submit_game_result(room_code: String, game_state: ViewState) {
|
|
|
|
|
let [score_pl1, score_pl2] = game_state.scores;
|
|
|
|
|
let result_str = format!("{:?} - {:?}", score_pl1.holes, score_pl2.holes);
|
|
|
|
|
let outcomes = if score_pl1.holes < score_pl2.holes {
|
|
|
|
|
[("0", "loss"), ("1", "win")]
|
|
|
|
|
} else if score_pl2.holes < score_pl1.holes {
|
|
|
|
|
[("0", "win"), ("1", "loss")]
|
|
|
|
|
} else {
|
|
|
|
|
[("0", "draw"), ("1", "draw")]
|
|
|
|
|
};
|
|
|
|
|
let body = serde_json::json!({
|
|
|
|
|
"room_code": room_code,
|
|
|
|
|
"game_id": GAME_ID,
|
|
|
|
|
"result": result_str,
|
|
|
|
|
"outcomes": std::collections::HashMap::from(outcomes),
|
|
|
|
|
});
|
|
|
|
|
let _ = gloo_net::http::Request::post(&format!("{HTTP_BASE}/games/result"))
|
|
|
|
|
.credentials(web_sys::RequestCredentials::Include)
|
|
|
|
|
.json(&body)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.send()
|
|
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 16:18:17 +01:00
|
|
|
#[component]
|
|
|
|
|
pub fn App() -> impl IntoView {
|
|
|
|
|
let stored = load_session();
|
|
|
|
|
let initial_screen = if stored.is_some() {
|
|
|
|
|
Screen::Connecting
|
|
|
|
|
} else {
|
|
|
|
|
Screen::Login { error: None }
|
|
|
|
|
};
|
|
|
|
|
let screen = RwSignal::new(initial_screen);
|
|
|
|
|
|
2026-04-23 20:54:52 +02:00
|
|
|
// Auth: fetch once and expose to all child components via context.
|
|
|
|
|
let auth_username: RwSignal<Option<String>> = RwSignal::new(None);
|
|
|
|
|
provide_context(auth_username);
|
|
|
|
|
spawn_local(async move {
|
|
|
|
|
if let Ok(resp) = gloo_net::http::Request::get(&format!("{HTTP_BASE}/auth/me"))
|
|
|
|
|
.credentials(web_sys::RequestCredentials::Include)
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
if resp.status() == 200 {
|
|
|
|
|
if let Ok(me) = resp.json::<MeResponse>().await {
|
|
|
|
|
auth_username.set(Some(me.username));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-25 16:18:17 +01:00
|
|
|
let (cmd_tx, mut cmd_rx) = mpsc::unbounded::<NetCommand>();
|
2026-04-05 18:43:13 +02:00
|
|
|
let pending: RwSignal<VecDeque<GameUiState>> = RwSignal::new(VecDeque::new());
|
|
|
|
|
provide_context(pending);
|
2026-03-25 16:18:17 +01:00
|
|
|
provide_context(cmd_tx.clone());
|
|
|
|
|
|
|
|
|
|
if let Some(s) = stored {
|
|
|
|
|
let host_state = s
|
|
|
|
|
.view_state
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|vs| serde_json::to_vec(vs).ok());
|
|
|
|
|
cmd_tx
|
|
|
|
|
.unbounded_send(NetCommand::Reconnect {
|
|
|
|
|
relay_url: s.relay_url,
|
|
|
|
|
game_id: s.game_id,
|
|
|
|
|
room_id: s.room_id,
|
|
|
|
|
token: s.token,
|
|
|
|
|
host_state,
|
|
|
|
|
})
|
|
|
|
|
.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spawn_local(async move {
|
|
|
|
|
loop {
|
2026-03-29 19:19:33 +02:00
|
|
|
// Wait for a connect/reconnect command (or PlayVsBot).
|
|
|
|
|
// None means "play vs bot"; Some((config, is_reconnect)) means "connect to relay".
|
|
|
|
|
let remote_config: Option<(RoomConfig, bool)> = loop {
|
2026-03-25 16:18:17 +01:00
|
|
|
match cmd_rx.next().await {
|
2026-03-29 19:19:33 +02:00
|
|
|
Some(NetCommand::PlayVsBot) => break None,
|
2026-03-25 16:18:17 +01:00
|
|
|
Some(NetCommand::CreateRoom { room }) => {
|
2026-03-29 19:19:33 +02:00
|
|
|
break Some((
|
2026-03-25 16:18:17 +01:00
|
|
|
RoomConfig {
|
|
|
|
|
relay_url: RELAY_URL.to_string(),
|
|
|
|
|
game_id: GAME_ID.to_string(),
|
|
|
|
|
room_id: room,
|
|
|
|
|
rule_variation: 0,
|
|
|
|
|
role: RoomRole::Create,
|
|
|
|
|
reconnect_token: None,
|
|
|
|
|
host_state: None,
|
|
|
|
|
},
|
|
|
|
|
false,
|
2026-03-29 19:19:33 +02:00
|
|
|
));
|
2026-03-25 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
Some(NetCommand::JoinRoom { room }) => {
|
2026-03-29 19:19:33 +02:00
|
|
|
break Some((
|
2026-03-25 16:18:17 +01:00
|
|
|
RoomConfig {
|
|
|
|
|
relay_url: RELAY_URL.to_string(),
|
|
|
|
|
game_id: GAME_ID.to_string(),
|
|
|
|
|
room_id: room,
|
|
|
|
|
rule_variation: 0,
|
|
|
|
|
role: RoomRole::Join,
|
|
|
|
|
reconnect_token: None,
|
|
|
|
|
host_state: None,
|
|
|
|
|
},
|
|
|
|
|
false,
|
2026-03-29 19:19:33 +02:00
|
|
|
));
|
2026-03-25 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
Some(NetCommand::Reconnect {
|
|
|
|
|
relay_url,
|
|
|
|
|
game_id,
|
|
|
|
|
room_id,
|
|
|
|
|
token,
|
|
|
|
|
host_state,
|
|
|
|
|
}) => {
|
2026-03-29 19:19:33 +02:00
|
|
|
break Some((
|
2026-03-25 16:18:17 +01:00
|
|
|
RoomConfig {
|
|
|
|
|
relay_url,
|
|
|
|
|
game_id,
|
|
|
|
|
room_id,
|
|
|
|
|
rule_variation: 0,
|
|
|
|
|
role: RoomRole::Join,
|
|
|
|
|
reconnect_token: Some(token),
|
|
|
|
|
host_state,
|
|
|
|
|
},
|
|
|
|
|
true,
|
2026-03-29 19:19:33 +02:00
|
|
|
));
|
2026-03-25 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
_ => {} // Ignore game commands while disconnected.
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-29 19:19:33 +02:00
|
|
|
if remote_config.is_none() {
|
2026-03-30 22:29:34 +02:00
|
|
|
loop {
|
2026-04-05 18:43:13 +02:00
|
|
|
let restart = run_local_bot_game(screen, &mut cmd_rx, pending).await;
|
2026-04-12 21:02:59 +02:00
|
|
|
if !restart {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-03-30 22:29:34 +02:00
|
|
|
}
|
2026-04-05 18:43:13 +02:00
|
|
|
pending.update(|q| q.clear());
|
2026-03-29 19:19:33 +02:00
|
|
|
screen.set(Screen::Login { error: None });
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let (config, is_reconnect) = remote_config.unwrap();
|
|
|
|
|
|
2026-03-25 16:18:17 +01:00
|
|
|
screen.set(Screen::Connecting);
|
|
|
|
|
|
|
|
|
|
let room_id_for_storage = config.room_id.clone();
|
|
|
|
|
let mut session: GameSession<PlayerAction, GameDelta, ViewState> =
|
|
|
|
|
match GameSession::connect::<TrictracBackend>(config).await {
|
|
|
|
|
Ok(s) => s,
|
|
|
|
|
Err(ConnectError::WebSocket(e) | ConnectError::Handshake(e)) => {
|
|
|
|
|
if is_reconnect {
|
|
|
|
|
clear_session();
|
|
|
|
|
}
|
|
|
|
|
screen.set(Screen::Login { error: Some(e) });
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !session.is_host {
|
|
|
|
|
save_session(&StoredSession {
|
|
|
|
|
relay_url: RELAY_URL.to_string(),
|
|
|
|
|
game_id: GAME_ID.to_string(),
|
|
|
|
|
room_id: room_id_for_storage.clone(),
|
|
|
|
|
token: session.reconnect_token,
|
|
|
|
|
is_host: false,
|
|
|
|
|
view_state: None,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let is_host = session.is_host;
|
|
|
|
|
let player_id = session.player_id;
|
|
|
|
|
let reconnect_token = session.reconnect_token;
|
2026-04-18 16:13:45 +02:00
|
|
|
let mut vs = ViewState::default_with_names("Blancs", "Noirs");
|
2026-04-23 20:54:52 +02:00
|
|
|
let mut result_submitted = false;
|
2026-03-25 16:18:17 +01:00
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
futures::select! {
|
|
|
|
|
cmd = cmd_rx.next().fuse() => match cmd {
|
|
|
|
|
Some(NetCommand::Action(action)) => {
|
|
|
|
|
session.send_action(action);
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
clear_session();
|
|
|
|
|
session.disconnect();
|
2026-04-05 18:43:13 +02:00
|
|
|
pending.update(|q| q.clear());
|
2026-03-25 16:18:17 +01:00
|
|
|
screen.set(Screen::Login { error: None });
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
event = session.next_event().fuse() => match event {
|
|
|
|
|
Some(SessionEvent::Update(u)) => {
|
2026-04-05 18:43:13 +02:00
|
|
|
let prev_vs = vs.clone();
|
2026-03-25 16:18:17 +01:00
|
|
|
match u {
|
|
|
|
|
ViewStateUpdate::Full(state) => vs = state,
|
|
|
|
|
ViewStateUpdate::Incremental(delta) => vs.apply_delta(&delta),
|
|
|
|
|
}
|
2026-04-23 20:54:52 +02:00
|
|
|
|
|
|
|
|
// Host reports outcomes once per terminal game state.
|
|
|
|
|
if is_host && !result_submitted && vs.stage == SerStage::Ended {
|
|
|
|
|
result_submitted = true;
|
|
|
|
|
let room = room_id_for_storage.clone();
|
|
|
|
|
let gs = vs.clone();
|
|
|
|
|
spawn_local(submit_game_result(room, gs));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 16:18:17 +01:00
|
|
|
if is_host {
|
|
|
|
|
save_session(&StoredSession {
|
|
|
|
|
relay_url: RELAY_URL.to_string(),
|
|
|
|
|
game_id: GAME_ID.to_string(),
|
|
|
|
|
room_id: room_id_for_storage.clone(),
|
|
|
|
|
token: reconnect_token,
|
|
|
|
|
is_host: true,
|
|
|
|
|
view_state: Some(vs.clone()),
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-18 17:11:47 +02:00
|
|
|
let is_own_move = prev_vs.active_mp_player == Some(player_id);
|
2026-04-05 18:43:13 +02:00
|
|
|
push_or_show(
|
|
|
|
|
&prev_vs,
|
|
|
|
|
GameUiState {
|
|
|
|
|
view_state: vs.clone(),
|
|
|
|
|
player_id,
|
|
|
|
|
room_id: room_id_for_storage.clone(),
|
|
|
|
|
is_bot_game: false,
|
|
|
|
|
waiting_for_confirm: false,
|
|
|
|
|
pause_reason: None,
|
2026-04-07 21:32:35 +02:00
|
|
|
my_scored_event: None,
|
|
|
|
|
opp_scored_event: None,
|
2026-04-18 17:11:47 +02:00
|
|
|
last_moves: compute_last_moves(&prev_vs, &vs, is_own_move),
|
2026-04-05 18:43:13 +02:00
|
|
|
},
|
|
|
|
|
pending,
|
|
|
|
|
screen,
|
|
|
|
|
);
|
2026-03-25 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
Some(SessionEvent::Disconnected(reason)) => {
|
2026-04-05 18:43:13 +02:00
|
|
|
pending.update(|q| q.clear());
|
2026-03-25 16:18:17 +01:00
|
|
|
screen.set(Screen::Login { error: reason });
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
None => {
|
2026-04-05 18:43:13 +02:00
|
|
|
pending.update(|q| q.clear());
|
2026-03-25 16:18:17 +01:00
|
|
|
screen.set(Screen::Login { error: None });
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
view! {
|
2026-03-29 17:15:22 +02:00
|
|
|
<I18nContextProvider>
|
2026-04-05 18:43:13 +02:00
|
|
|
{move || {
|
|
|
|
|
let q = pending.get();
|
|
|
|
|
if let Some(front) = q.front() {
|
|
|
|
|
view! { <GameScreen state=front.clone() /> }.into_any()
|
|
|
|
|
} else {
|
|
|
|
|
match screen.get() {
|
|
|
|
|
Screen::Login { error } => view! { <LoginScreen error=error /> }.into_any(),
|
|
|
|
|
Screen::Connecting => view! { <ConnectingScreen /> }.into_any(),
|
|
|
|
|
Screen::Playing(state) => view! { <GameScreen state=state /> }.into_any(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-29 17:15:22 +02:00
|
|
|
}}
|
|
|
|
|
</I18nContextProvider>
|
2026-03-25 16:18:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-29 19:19:33 +02:00
|
|
|
|
2026-03-30 22:29:34 +02:00
|
|
|
/// Runs one local bot game. Returns `true` if the player wants to play again.
|
2026-03-29 19:19:33 +02:00
|
|
|
async fn run_local_bot_game(
|
|
|
|
|
screen: RwSignal<Screen>,
|
|
|
|
|
cmd_rx: &mut futures::channel::mpsc::UnboundedReceiver<NetCommand>,
|
2026-04-05 18:43:13 +02:00
|
|
|
pending: RwSignal<VecDeque<GameUiState>>,
|
2026-03-30 22:29:34 +02:00
|
|
|
) -> bool {
|
2026-03-29 19:19:33 +02:00
|
|
|
let mut backend = TrictracBackend::new(0);
|
|
|
|
|
backend.player_arrival(0);
|
|
|
|
|
backend.player_arrival(1);
|
|
|
|
|
|
|
|
|
|
let mut vs = ViewState::default_with_names("You", "Bot");
|
2026-04-07 21:32:35 +02:00
|
|
|
for cmd in backend.drain_commands() {
|
|
|
|
|
match cmd {
|
2026-04-12 21:02:59 +02:00
|
|
|
BackendCommand::ResetViewState => {
|
|
|
|
|
vs = backend.get_view_state().clone();
|
|
|
|
|
}
|
|
|
|
|
BackendCommand::Delta(delta) => {
|
|
|
|
|
vs.apply_delta(&delta);
|
|
|
|
|
}
|
2026-04-07 21:32:35 +02:00
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
screen.set(Screen::Playing(GameUiState {
|
|
|
|
|
view_state: vs.clone(),
|
|
|
|
|
player_id: 0,
|
|
|
|
|
room_id: String::new(),
|
|
|
|
|
is_bot_game: true,
|
|
|
|
|
waiting_for_confirm: false,
|
|
|
|
|
pause_reason: None,
|
|
|
|
|
my_scored_event: None,
|
|
|
|
|
opp_scored_event: None,
|
2026-04-09 20:17:21 +02:00
|
|
|
last_moves: None,
|
2026-04-07 21:32:35 +02:00
|
|
|
}));
|
2026-03-29 19:19:33 +02:00
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
match cmd_rx.next().await {
|
|
|
|
|
Some(NetCommand::Action(action)) => {
|
2026-04-07 21:32:35 +02:00
|
|
|
let prev_vs = vs.clone();
|
2026-03-29 19:19:33 +02:00
|
|
|
backend.inform_rpc(0, action);
|
2026-04-07 21:32:35 +02:00
|
|
|
for cmd in backend.drain_commands() {
|
|
|
|
|
if let BackendCommand::Delta(delta) = cmd {
|
|
|
|
|
vs.apply_delta(&delta);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let scored = compute_scored_event(&prev_vs, &vs, 0);
|
|
|
|
|
let opp_scored = compute_scored_event(&prev_vs, &vs, 1);
|
|
|
|
|
screen.set(Screen::Playing(GameUiState {
|
|
|
|
|
view_state: vs.clone(),
|
|
|
|
|
player_id: 0,
|
|
|
|
|
room_id: String::new(),
|
|
|
|
|
is_bot_game: true,
|
|
|
|
|
waiting_for_confirm: false,
|
|
|
|
|
pause_reason: None,
|
|
|
|
|
my_scored_event: scored,
|
|
|
|
|
opp_scored_event: opp_scored,
|
2026-04-18 17:11:47 +02:00
|
|
|
last_moves: compute_last_moves(&prev_vs, &vs, true),
|
2026-04-07 21:32:35 +02:00
|
|
|
}));
|
2026-03-29 19:19:33 +02:00
|
|
|
}
|
2026-03-30 22:29:34 +02:00
|
|
|
Some(NetCommand::PlayVsBot) => return true,
|
|
|
|
|
_ => return false,
|
2026-03-29 19:19:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loop {
|
2026-04-17 22:22:50 +02:00
|
|
|
let pgr = backend.get_view_state().pre_game_roll.clone();
|
|
|
|
|
match bot_decide(backend.get_game(), pgr.as_ref()) {
|
2026-03-29 19:19:33 +02:00
|
|
|
None => break,
|
|
|
|
|
Some(action) => {
|
|
|
|
|
backend.inform_rpc(1, action);
|
2026-04-17 22:22:50 +02:00
|
|
|
// Process each delta individually so intermediate ceremony
|
|
|
|
|
// states (both dice shown) can trigger a pause via push_or_show.
|
2026-04-05 18:43:13 +02:00
|
|
|
for cmd in backend.drain_commands() {
|
|
|
|
|
if let BackendCommand::Delta(delta) = cmd {
|
2026-04-17 22:22:50 +02:00
|
|
|
let delta_prev_vs = vs.clone();
|
2026-04-05 18:43:13 +02:00
|
|
|
vs.apply_delta(&delta);
|
2026-04-17 22:22:50 +02:00
|
|
|
push_or_show(
|
|
|
|
|
&delta_prev_vs,
|
|
|
|
|
GameUiState {
|
|
|
|
|
view_state: vs.clone(),
|
|
|
|
|
player_id: 0,
|
|
|
|
|
room_id: String::new(),
|
|
|
|
|
is_bot_game: true,
|
|
|
|
|
waiting_for_confirm: false,
|
|
|
|
|
pause_reason: None,
|
|
|
|
|
my_scored_event: None,
|
|
|
|
|
opp_scored_event: None,
|
2026-04-18 17:11:47 +02:00
|
|
|
last_moves: compute_last_moves(&delta_prev_vs, &vs, false),
|
2026-04-17 22:22:50 +02:00
|
|
|
},
|
|
|
|
|
pending,
|
|
|
|
|
screen,
|
|
|
|
|
);
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-29 19:19:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 20:17:21 +02:00
|
|
|
/// Returns the checker moves to animate when the board changed between two ViewStates.
|
|
|
|
|
/// Returns `None` when the board is unchanged or no real moves were recorded.
|
2026-04-18 17:11:47 +02:00
|
|
|
/// `own_move`: when true, m1 was already shown via staged-moves UI, so only animate m2.
|
2026-04-23 17:37:10 +02:00
|
|
|
fn compute_last_moves(
|
|
|
|
|
prev: &ViewState,
|
|
|
|
|
next: &ViewState,
|
|
|
|
|
own_move: bool,
|
|
|
|
|
) -> Option<(CheckerMove, CheckerMove)> {
|
2026-04-09 20:17:21 +02:00
|
|
|
if prev.board == next.board {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let (m1, m2) = next.dice_moves;
|
|
|
|
|
if m1 == CheckerMove::default() && m2 == CheckerMove::default() {
|
|
|
|
|
// Relies on the engine invariant: dice_moves is updated atomically with the board
|
|
|
|
|
// change in the Move event handler. Any future engine path that mutates the board
|
|
|
|
|
// without setting dice_moves would bypass this guard and replay stale animation.
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2026-04-18 17:11:47 +02:00
|
|
|
if own_move {
|
|
|
|
|
// m1 was already shown via the staged-moves overlay; only animate m2.
|
2026-04-23 17:37:10 +02:00
|
|
|
if m2 == CheckerMove::default() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2026-04-18 17:11:47 +02:00
|
|
|
return Some((m2, CheckerMove::default()));
|
|
|
|
|
}
|
2026-04-09 20:17:21 +02:00
|
|
|
Some((m1, m2))
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 21:32:35 +02:00
|
|
|
/// Computes a scoring event for `player_id` by comparing the previous and next
|
|
|
|
|
/// ViewState. Returns `None` when no points changed for that player.
|
|
|
|
|
fn compute_scored_event(prev: &ViewState, next: &ViewState, player_id: u16) -> Option<ScoredEvent> {
|
|
|
|
|
let prev_score = &prev.scores[player_id as usize];
|
|
|
|
|
let next_score = &next.scores[player_id as usize];
|
|
|
|
|
|
|
|
|
|
let holes_gained = next_score.holes.saturating_sub(prev_score.holes);
|
|
|
|
|
if holes_gained == 0 && prev_score.points == next_score.points {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let bredouille = holes_gained > 0 && prev_score.can_bredouille;
|
|
|
|
|
|
|
|
|
|
// Determine which dice_jans are "mine" depending on who was the active roller.
|
|
|
|
|
let my_jans: Vec<JanEntry> = if next.active_mp_player == Some(player_id)
|
|
|
|
|
&& prev.active_mp_player == Some(player_id)
|
|
|
|
|
{
|
|
|
|
|
// My own roll: positive totals are mine.
|
2026-04-12 21:02:59 +02:00
|
|
|
next.dice_jans
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|e| e.total > 0)
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect()
|
|
|
|
|
} else if next.active_mp_player == Some(player_id) && prev.active_mp_player != Some(player_id) {
|
2026-04-07 21:32:35 +02:00
|
|
|
// Opponent just moved: negative totals (their penalty) are scored for me.
|
|
|
|
|
next.dice_jans
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|e| e.total < 0)
|
2026-04-12 21:02:59 +02:00
|
|
|
.map(|e| JanEntry {
|
|
|
|
|
total: -e.total,
|
|
|
|
|
points_per: -e.points_per,
|
|
|
|
|
..e.clone()
|
|
|
|
|
})
|
2026-04-07 21:32:35 +02:00
|
|
|
.collect()
|
|
|
|
|
} else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let points_earned: u8 = my_jans
|
|
|
|
|
.iter()
|
|
|
|
|
.fold(0u8, |acc, e| acc.saturating_add(e.total.unsigned_abs()));
|
|
|
|
|
|
|
|
|
|
if points_earned == 0 && holes_gained == 0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(ScoredEvent {
|
|
|
|
|
points_earned,
|
|
|
|
|
holes_gained,
|
|
|
|
|
holes_total: next_score.holes,
|
|
|
|
|
bredouille,
|
|
|
|
|
jans: my_jans,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 18:43:13 +02:00
|
|
|
/// Either queues the state as a buffered confirmation step (when the transition
|
|
|
|
|
/// warrants a pause) or shows it immediately. Always updates `screen` to the
|
|
|
|
|
/// live state so the UI falls through to the right content once pending drains.
|
|
|
|
|
fn push_or_show(
|
|
|
|
|
prev_vs: &ViewState,
|
|
|
|
|
new_state: GameUiState,
|
|
|
|
|
pending: RwSignal<VecDeque<GameUiState>>,
|
|
|
|
|
screen: RwSignal<Screen>,
|
|
|
|
|
) {
|
2026-04-07 21:32:35 +02:00
|
|
|
let scored = compute_scored_event(prev_vs, &new_state.view_state, new_state.player_id);
|
|
|
|
|
let opp_scored = compute_scored_event(prev_vs, &new_state.view_state, 1 - new_state.player_id);
|
|
|
|
|
|
2026-04-05 18:43:13 +02:00
|
|
|
if let Some(reason) = infer_pause_reason(prev_vs, &new_state.view_state, new_state.player_id) {
|
2026-04-07 21:32:35 +02:00
|
|
|
// Scoring notifications go on the buffered (paused) state only.
|
2026-04-05 18:43:13 +02:00
|
|
|
pending.update(|q| {
|
|
|
|
|
q.push_back(GameUiState {
|
|
|
|
|
waiting_for_confirm: true,
|
|
|
|
|
pause_reason: Some(reason),
|
2026-04-07 21:32:35 +02:00
|
|
|
my_scored_event: scored,
|
|
|
|
|
opp_scored_event: opp_scored,
|
2026-04-05 18:43:13 +02:00
|
|
|
..new_state.clone()
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-04-09 20:17:21 +02:00
|
|
|
// Animation belongs to the buffered confirmation step; clear it on the
|
|
|
|
|
// fallback live state so it doesn't fire again after the queue drains.
|
2026-04-12 21:02:59 +02:00
|
|
|
screen.set(Screen::Playing(GameUiState {
|
|
|
|
|
last_moves: None,
|
|
|
|
|
..new_state
|
|
|
|
|
}));
|
2026-04-07 21:32:35 +02:00
|
|
|
} else {
|
|
|
|
|
// No pause: show scoring directly on the live state.
|
|
|
|
|
screen.set(Screen::Playing(GameUiState {
|
|
|
|
|
my_scored_event: scored,
|
|
|
|
|
opp_scored_event: opp_scored,
|
|
|
|
|
..new_state
|
|
|
|
|
}));
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Compares the previous and next ViewState to decide whether the transition
|
|
|
|
|
/// warrants a confirmation pause. Returns None when it is the local player's
|
|
|
|
|
/// own action (no pause needed).
|
|
|
|
|
fn infer_pause_reason(prev: &ViewState, next: &ViewState, player_id: u16) -> Option<PauseReason> {
|
|
|
|
|
let opponent_id = 1 - player_id;
|
|
|
|
|
|
2026-04-17 22:22:50 +02:00
|
|
|
// Pre-game ceremony: pause when both dice are revealed simultaneously
|
|
|
|
|
// (i.e. the second die was just rolled). Both players see this pause.
|
|
|
|
|
if next.stage == SerStage::PreGameRoll {
|
|
|
|
|
if let (Some(prev_pgr), Some(next_pgr)) = (&prev.pre_game_roll, &next.pre_game_roll) {
|
|
|
|
|
let both_now = next_pgr.host_die.is_some() && next_pgr.guest_die.is_some();
|
|
|
|
|
let both_before = prev_pgr.host_die.is_some() && prev_pgr.guest_die.is_some();
|
|
|
|
|
if both_now && !both_before {
|
|
|
|
|
return Some(PauseReason::AfterOpponentPreGameRoll);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't fire normal pause rules on the PreGameRoll → InGame transition.
|
|
|
|
|
if prev.stage == SerStage::PreGameRoll {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 18:43:13 +02:00
|
|
|
if next.active_mp_player == Some(opponent_id) {
|
|
|
|
|
// Dice changed → opponent just rolled.
|
|
|
|
|
if next.dice != prev.dice {
|
|
|
|
|
return Some(PauseReason::AfterOpponentRoll);
|
|
|
|
|
}
|
|
|
|
|
// Was at HoldOrGoChoice, now Move, opponent still active → opponent went.
|
2026-04-12 21:02:59 +02:00
|
|
|
if prev.turn_stage == SerTurnStage::HoldOrGoChoice && next.turn_stage == SerTurnStage::Move
|
2026-04-05 18:43:13 +02:00
|
|
|
{
|
|
|
|
|
return Some(PauseReason::AfterOpponentGo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Turn switched to us → opponent moved.
|
|
|
|
|
if next.active_mp_player == Some(player_id) && prev.active_mp_player == Some(opponent_id) {
|
|
|
|
|
return Some(PauseReason::AfterOpponentMove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::trictrac::types::{PlayerScore, SerStage, SerTurnStage};
|
|
|
|
|
|
|
|
|
|
fn score() -> PlayerScore {
|
2026-04-12 21:02:59 +02:00
|
|
|
PlayerScore {
|
|
|
|
|
name: String::new(),
|
|
|
|
|
points: 0,
|
|
|
|
|
holes: 0,
|
|
|
|
|
can_bredouille: false,
|
|
|
|
|
}
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn vs(dice: (u8, u8), turn_stage: SerTurnStage, active: Option<u16>) -> ViewState {
|
|
|
|
|
ViewState {
|
|
|
|
|
board: [0i8; 24],
|
|
|
|
|
stage: SerStage::InGame,
|
|
|
|
|
turn_stage,
|
|
|
|
|
active_mp_player: active,
|
|
|
|
|
scores: [score(), score()],
|
|
|
|
|
dice,
|
|
|
|
|
dice_jans: Vec::new(),
|
2026-04-09 20:17:21 +02:00
|
|
|
dice_moves: (CheckerMove::default(), CheckerMove::default()),
|
2026-04-17 22:22:50 +02:00
|
|
|
pre_game_roll: None,
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn dice_change_is_after_roll() {
|
|
|
|
|
let prev = vs((0, 0), SerTurnStage::RollDice, Some(1));
|
|
|
|
|
let next = vs((3, 5), SerTurnStage::Move, Some(1));
|
2026-04-12 21:02:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
infer_pause_reason(&prev, &next, 0),
|
|
|
|
|
Some(PauseReason::AfterOpponentRoll)
|
|
|
|
|
);
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn hold_to_move_is_after_go() {
|
|
|
|
|
let prev = vs((3, 5), SerTurnStage::HoldOrGoChoice, Some(1));
|
|
|
|
|
let next = vs((3, 5), SerTurnStage::Move, Some(1));
|
2026-04-12 21:02:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
infer_pause_reason(&prev, &next, 0),
|
|
|
|
|
Some(PauseReason::AfterOpponentGo)
|
|
|
|
|
);
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn turn_switch_is_after_move() {
|
|
|
|
|
let prev = vs((3, 5), SerTurnStage::Move, Some(1));
|
|
|
|
|
let next = vs((3, 5), SerTurnStage::RollDice, Some(0));
|
2026-04-12 21:02:59 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
infer_pause_reason(&prev, &next, 0),
|
|
|
|
|
Some(PauseReason::AfterOpponentMove)
|
|
|
|
|
);
|
2026-04-05 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn own_action_returns_none() {
|
|
|
|
|
let prev = vs((0, 0), SerTurnStage::RollDice, Some(0));
|
|
|
|
|
let next = vs((2, 4), SerTurnStage::Move, Some(0));
|
|
|
|
|
assert_eq!(infer_pause_reason(&prev, &next, 0), None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn no_active_player_returns_none() {
|
|
|
|
|
let mut prev = vs((0, 0), SerTurnStage::RollDice, None);
|
|
|
|
|
prev.stage = SerStage::PreGame;
|
|
|
|
|
let mut next = prev.clone();
|
|
|
|
|
next.active_mp_player = Some(0);
|
|
|
|
|
assert_eq!(infer_pause_reason(&prev, &next, 0), None);
|
|
|
|
|
}
|
|
|
|
|
}
|