feat(client_web): local bot (random strategy)
This commit is contained in:
parent
4ecb222e55
commit
e414e28047
10 changed files with 141 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1400,6 +1400,7 @@ dependencies = [
|
||||||
"gloo-storage",
|
"gloo-storage",
|
||||||
"leptos",
|
"leptos",
|
||||||
"leptos_i18n",
|
"leptos_i18n",
|
||||||
|
"rand 0.9.2",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"trictrac-store",
|
"trictrac-store",
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ leptos = { version = "0.7", features = ["csr"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
|
rand = "0.9"
|
||||||
gloo-storage = "0.3"
|
gloo-storage = "0.3"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
|
|
||||||
|
|
@ -33,5 +33,7 @@
|
||||||
"jan_false_hit_big": "False hit (big jan)",
|
"jan_false_hit_big": "False hit (big jan)",
|
||||||
"jan_contre_two": "Contre two tables",
|
"jan_contre_two": "Contre two tables",
|
||||||
"jan_contre_mezeas": "Contre mezeas",
|
"jan_contre_mezeas": "Contre mezeas",
|
||||||
"jan_helpless_man": "Helpless man"
|
"jan_helpless_man": "Helpless man",
|
||||||
|
"play_vs_bot": "Play vs Bot",
|
||||||
|
"vs_bot_label": "vs Bot"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,5 +33,7 @@
|
||||||
"jan_false_hit_big": "Battage à faux (grand jan)",
|
"jan_false_hit_big": "Battage à faux (grand jan)",
|
||||||
"jan_contre_two": "Contre deux tables",
|
"jan_contre_two": "Contre deux tables",
|
||||||
"jan_contre_mezeas": "Contre mezeas",
|
"jan_contre_mezeas": "Contre mezeas",
|
||||||
"jan_helpless_man": "Dame impuissante"
|
"jan_helpless_man": "Dame impuissante",
|
||||||
|
"play_vs_bot": "Jouer contre le bot",
|
||||||
|
"vs_bot_label": "contre le bot"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,12 @@ use leptos::task::spawn_local;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use backbone_lib::session::{ConnectError, GameSession, RoomConfig, RoomRole, SessionEvent};
|
use backbone_lib::session::{ConnectError, GameSession, RoomConfig, RoomRole, SessionEvent};
|
||||||
use backbone_lib::traits::ViewStateUpdate;
|
use backbone_lib::traits::{BackEndArchitecture, BackendCommand, ViewStateUpdate};
|
||||||
|
|
||||||
use crate::components::{ConnectingScreen, GameScreen, LoginScreen};
|
use crate::components::{ConnectingScreen, GameScreen, LoginScreen};
|
||||||
use crate::i18n::I18nContextProvider;
|
use crate::i18n::I18nContextProvider;
|
||||||
use crate::trictrac::backend::TrictracBackend;
|
use crate::trictrac::backend::TrictracBackend;
|
||||||
|
use crate::trictrac::bot_local::bot_decide;
|
||||||
use crate::trictrac::types::{GameDelta, PlayerAction, ViewState};
|
use crate::trictrac::types::{GameDelta, PlayerAction, ViewState};
|
||||||
|
|
||||||
const RELAY_URL: &str = "ws://127.0.0.1:8080/ws";
|
const RELAY_URL: &str = "ws://127.0.0.1:8080/ws";
|
||||||
|
|
@ -24,6 +25,7 @@ pub struct GameUiState {
|
||||||
/// 0 = host, 1 = guest
|
/// 0 = host, 1 = guest
|
||||||
pub player_id: u16,
|
pub player_id: u16,
|
||||||
pub room_id: String,
|
pub room_id: String,
|
||||||
|
pub is_bot_game: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Which screen is currently shown.
|
/// Which screen is currently shown.
|
||||||
|
|
@ -49,6 +51,7 @@ pub enum NetCommand {
|
||||||
token: u64,
|
token: u64,
|
||||||
host_state: Option<Vec<u8>>,
|
host_state: Option<Vec<u8>>,
|
||||||
},
|
},
|
||||||
|
PlayVsBot,
|
||||||
Action(PlayerAction),
|
Action(PlayerAction),
|
||||||
Disconnect,
|
Disconnect,
|
||||||
}
|
}
|
||||||
|
|
@ -109,11 +112,13 @@ pub fn App() -> impl IntoView {
|
||||||
|
|
||||||
spawn_local(async move {
|
spawn_local(async move {
|
||||||
loop {
|
loop {
|
||||||
// Wait for a connect/reconnect command.
|
// Wait for a connect/reconnect command (or PlayVsBot).
|
||||||
let (config, is_reconnect) = loop {
|
// None means "play vs bot"; Some((config, is_reconnect)) means "connect to relay".
|
||||||
|
let remote_config: Option<(RoomConfig, bool)> = loop {
|
||||||
match cmd_rx.next().await {
|
match cmd_rx.next().await {
|
||||||
|
Some(NetCommand::PlayVsBot) => break None,
|
||||||
Some(NetCommand::CreateRoom { room }) => {
|
Some(NetCommand::CreateRoom { room }) => {
|
||||||
break (
|
break Some((
|
||||||
RoomConfig {
|
RoomConfig {
|
||||||
relay_url: RELAY_URL.to_string(),
|
relay_url: RELAY_URL.to_string(),
|
||||||
game_id: GAME_ID.to_string(),
|
game_id: GAME_ID.to_string(),
|
||||||
|
|
@ -124,10 +129,10 @@ pub fn App() -> impl IntoView {
|
||||||
host_state: None,
|
host_state: None,
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
Some(NetCommand::JoinRoom { room }) => {
|
Some(NetCommand::JoinRoom { room }) => {
|
||||||
break (
|
break Some((
|
||||||
RoomConfig {
|
RoomConfig {
|
||||||
relay_url: RELAY_URL.to_string(),
|
relay_url: RELAY_URL.to_string(),
|
||||||
game_id: GAME_ID.to_string(),
|
game_id: GAME_ID.to_string(),
|
||||||
|
|
@ -138,7 +143,7 @@ pub fn App() -> impl IntoView {
|
||||||
host_state: None,
|
host_state: None,
|
||||||
},
|
},
|
||||||
false,
|
false,
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
Some(NetCommand::Reconnect {
|
Some(NetCommand::Reconnect {
|
||||||
relay_url,
|
relay_url,
|
||||||
|
|
@ -147,7 +152,7 @@ pub fn App() -> impl IntoView {
|
||||||
token,
|
token,
|
||||||
host_state,
|
host_state,
|
||||||
}) => {
|
}) => {
|
||||||
break (
|
break Some((
|
||||||
RoomConfig {
|
RoomConfig {
|
||||||
relay_url,
|
relay_url,
|
||||||
game_id,
|
game_id,
|
||||||
|
|
@ -158,12 +163,19 @@ pub fn App() -> impl IntoView {
|
||||||
host_state,
|
host_state,
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
);
|
));
|
||||||
}
|
}
|
||||||
_ => {} // Ignore game commands while disconnected.
|
_ => {} // Ignore game commands while disconnected.
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if remote_config.is_none() {
|
||||||
|
run_local_bot_game(screen, &mut cmd_rx).await;
|
||||||
|
screen.set(Screen::Login { error: None });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let (config, is_reconnect) = remote_config.unwrap();
|
||||||
|
|
||||||
screen.set(Screen::Connecting);
|
screen.set(Screen::Connecting);
|
||||||
|
|
||||||
let room_id_for_storage = config.room_id.clone();
|
let room_id_for_storage = config.room_id.clone();
|
||||||
|
|
@ -228,6 +240,7 @@ pub fn App() -> impl IntoView {
|
||||||
view_state: vs.clone(),
|
view_state: vs.clone(),
|
||||||
player_id,
|
player_id,
|
||||||
room_id: room_id_for_storage.clone(),
|
room_id: room_id_for_storage.clone(),
|
||||||
|
is_bot_game: false,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Some(SessionEvent::Disconnected(reason)) => {
|
Some(SessionEvent::Disconnected(reason)) => {
|
||||||
|
|
@ -254,3 +267,60 @@ pub fn App() -> impl IntoView {
|
||||||
</I18nContextProvider>
|
</I18nContextProvider>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn run_local_bot_game(
|
||||||
|
screen: RwSignal<Screen>,
|
||||||
|
cmd_rx: &mut futures::channel::mpsc::UnboundedReceiver<NetCommand>,
|
||||||
|
) {
|
||||||
|
let mut backend = TrictracBackend::new(0);
|
||||||
|
backend.player_arrival(0);
|
||||||
|
backend.player_arrival(1);
|
||||||
|
|
||||||
|
let mut vs = ViewState::default_with_names("You", "Bot");
|
||||||
|
drain_and_update(&mut backend, &mut vs, screen);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match cmd_rx.next().await {
|
||||||
|
Some(NetCommand::Action(action)) => {
|
||||||
|
backend.inform_rpc(0, action);
|
||||||
|
}
|
||||||
|
_ => break,
|
||||||
|
}
|
||||||
|
|
||||||
|
drain_and_update(&mut backend, &mut vs, screen);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match bot_decide(backend.get_game()) {
|
||||||
|
None => break,
|
||||||
|
Some(action) => {
|
||||||
|
backend.inform_rpc(1, action);
|
||||||
|
drain_and_update(&mut backend, &mut vs, screen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain_and_update(
|
||||||
|
backend: &mut TrictracBackend,
|
||||||
|
vs: &mut ViewState,
|
||||||
|
screen: RwSignal<Screen>,
|
||||||
|
) {
|
||||||
|
for cmd in backend.drain_commands() {
|
||||||
|
match cmd {
|
||||||
|
BackendCommand::ResetViewState => {
|
||||||
|
*vs = backend.get_view_state().clone();
|
||||||
|
}
|
||||||
|
BackendCommand::Delta(delta) => {
|
||||||
|
vs.apply_delta(&delta);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
screen.set(Screen::Playing(GameUiState {
|
||||||
|
view_state: vs.clone(),
|
||||||
|
player_id: 0,
|
||||||
|
room_id: String::new(),
|
||||||
|
is_bot_game: true,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -115,12 +115,17 @@ pub fn GameScreen(state: GameUiState) -> impl IntoView {
|
||||||
let stage = vs.stage.clone();
|
let stage = vs.stage.clone();
|
||||||
let turn_stage = vs.turn_stage.clone();
|
let turn_stage = vs.turn_stage.clone();
|
||||||
let room_id = state.room_id.clone();
|
let room_id = state.room_id.clone();
|
||||||
|
let is_bot_game = state.is_bot_game;
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<div class="game-container">
|
<div class="game-container">
|
||||||
// ── Top bar ──────────────────────────────────────────────────────
|
// ── Top bar ──────────────────────────────────────────────────────
|
||||||
<div class="top-bar">
|
<div class="top-bar">
|
||||||
<span>{move || t_string!(i18n, room_label, id = room_id.as_str())}</span>
|
<span>{move || if is_bot_game {
|
||||||
|
t_string!(i18n, vs_bot_label).to_owned()
|
||||||
|
} else {
|
||||||
|
t_string!(i18n, room_label, id = room_id.as_str())
|
||||||
|
}}</span>
|
||||||
<div class="lang-switcher">
|
<div class="lang-switcher">
|
||||||
<button
|
<button
|
||||||
class:lang-active=move || i18n.get_locale() == Locale::en
|
class:lang-active=move || i18n.get_locale() == Locale::en
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,8 @@ pub fn LoginScreen(error: Option<String>) -> impl IntoView {
|
||||||
.expect("UnboundedSender<NetCommand> not found in context");
|
.expect("UnboundedSender<NetCommand> not found in context");
|
||||||
|
|
||||||
let cmd_tx_create = cmd_tx.clone();
|
let cmd_tx_create = cmd_tx.clone();
|
||||||
let cmd_tx_join = cmd_tx;
|
let cmd_tx_join = cmd_tx.clone();
|
||||||
|
let cmd_tx_bot = cmd_tx;
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<div class="login-container">
|
<div class="login-container">
|
||||||
|
|
@ -62,6 +63,15 @@ pub fn LoginScreen(error: Option<String>) -> impl IntoView {
|
||||||
>
|
>
|
||||||
{t!(i18n, join_room)}
|
{t!(i18n, join_room)}
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="btn btn-bot"
|
||||||
|
on:click=move |_| {
|
||||||
|
cmd_tx_bot.unbounded_send(NetCommand::PlayVsBot).ok();
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{t!(i18n, play_vs_bot)}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,12 @@ impl TrictracBackend {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TrictracBackend {
|
||||||
|
pub fn get_game(&self) -> &GameState {
|
||||||
|
&self.game
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl BackEndArchitecture<PlayerAction, GameDelta, ViewState> for TrictracBackend {
|
impl BackEndArchitecture<PlayerAction, GameDelta, ViewState> for TrictracBackend {
|
||||||
fn new(_rule_variation: u16) -> Self {
|
fn new(_rule_variation: u16) -> Self {
|
||||||
let mut game = GameState::new(false);
|
let mut game = GameState::new(false);
|
||||||
|
|
|
||||||
30
client_web/src/trictrac/bot_local.rs
Normal file
30
client_web/src/trictrac/bot_local.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
use rand::prelude::IndexedRandom;
|
||||||
|
use trictrac_store::{CheckerMove, Color, GameState, MoveRules, TurnStage};
|
||||||
|
|
||||||
|
use crate::trictrac::types::PlayerAction;
|
||||||
|
|
||||||
|
const GUEST_PLAYER_ID: u64 = 2;
|
||||||
|
|
||||||
|
/// Returns the next action for the bot (mp_player 1 / guest), or None if it is not the bot's turn.
|
||||||
|
pub fn bot_decide(game: &GameState) -> Option<PlayerAction> {
|
||||||
|
if game.active_player_id != GUEST_PLAYER_ID {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
match game.turn_stage {
|
||||||
|
TurnStage::RollDice => Some(PlayerAction::Roll),
|
||||||
|
TurnStage::HoldOrGoChoice => Some(PlayerAction::Go),
|
||||||
|
TurnStage::Move => {
|
||||||
|
let rules = MoveRules::new(&Color::Black, &game.board, game.dice);
|
||||||
|
let sequences = rules.get_possible_moves_sequences(true, vec![]);
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
let (m1, m2) = sequences
|
||||||
|
.choose(&mut rng)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or((CheckerMove::default(), CheckerMove::default()));
|
||||||
|
// MoveRules with Color::Black mirrors the board internally, so
|
||||||
|
// returned move coordinates are in mirrored (White) space — mirror back.
|
||||||
|
Some(PlayerAction::Move(m1.mirror(), m2.mirror()))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod backend;
|
pub mod backend;
|
||||||
|
pub mod bot_local;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue