From e7c46c8efd05432ee2eb75d22a856dfb33544e3a Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Fri, 15 Sep 2023 21:01:49 -0700 Subject: [PATCH] Add standardized turn summary messages. Same logic for both player and AI --- src/game.rs | 8 +++++++- ui/src/Game.tsx | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/game.rs b/src/game.rs index fbcd3ac..9cf0180 100644 --- a/src/game.rs +++ b/src/game.rs @@ -152,13 +152,19 @@ impl Game { }) .collect(); + let ai_length = ai_difficulties.len(); for (i, ai_difficulty) in ai_difficulties.into_iter().enumerate() { let ai = AI::new(ai_difficulty, &dictionary); let mut tray = Tray::new(TRAY_LENGTH); tray.fill(&mut letters); + let ai_player_name = if ai_length > 1 { + format!("AI {}", i+1) + } else { + "AI".to_string() + }; player_states.push(PlayerState { player: Player::AI { - name: format!("AI {}", i), + name: ai_player_name, difficulty: ai_difficulty, object: ai, }, diff --git a/ui/src/Game.tsx b/ui/src/Game.tsx index 04b9f0b..a3f5ce3 100644 --- a/ui/src/Game.tsx +++ b/ui/src/Game.tsx @@ -94,8 +94,7 @@ export function Game(props: { if(result.response_type === "ERR") { logDispatch(
{(result.value as string)}
); } else { - const action = result.value as {type: "ExchangeTiles"; tiles_exchanged: number;}; - logDispatch(
You exchanged {action.tiles_exchanged} tiles.
); + handlePlayerAction(result.value as TurnAction, props.settings.playerName); setTurnCount(turnCount + 1); } @@ -108,7 +107,13 @@ export function Game(props: { function runAI() { const result: TurnAdvanceResult = props.wasm.advance_turn(); - console.info({result}); + if(result.type == "AIMove"){ + handlePlayerAction(result.action, "AI"); + } else { + // this would be quite surprising + console.error({result}); + } + setTurnCount(turnCount + 1); } @@ -172,7 +177,24 @@ export function Game(props: { if (logDivRef.current != null) { logDivRef.current.scrollTo(0, logDivRef.current.scrollHeight); // scroll down } - }, [logInfo]) + }, [logInfo]); + + function handlePlayerAction(action: TurnAction, playerName: string) { + + if (action.type == "PlayTiles"){ + const result = action.result; + result.words.sort((a, b) => b.score - a.score); + for(let word of result.words) { + logDispatch(
{playerName} received {word.score} points for playing '{word.word}.'
); + } + logDispatch(
{playerName} received a total of {result.total} points for their turn.
); + } else if(action.type == "ExchangeTiles") { + logDispatch(
{playerName} exchanged {action.tiles_exchanged} tile{action.tiles_exchanged > 1 ? 's' : ''} for their turn.
); + } + else if(action.type == "Pass"){ + logDispatch(
{playerName} passed.
); + } + } return <> @@ -232,15 +254,12 @@ export function Game(props: { const score_result = (result.value as { type: "PlayTiles"; result: ScoreResult }).result; const total_points = score_result.total; - let msg: string; + if (confirmedScorePoints > -1) { - msg = `You scored ${total_points} points.`; + handlePlayerAction({type: "PlayTiles", result: score_result}, props.settings.playerName); setTurnCount(turnCount + 1); } - - logDispatch(
{msg}
); - setConfirmedScorePoints(total_points); } @@ -258,7 +277,7 @@ export function Game(props: { }}>Return Tiles ;