Add standardized turn summary messages.

Same logic for both player and AI
This commit is contained in:
Joel Therrien 2023-09-15 21:01:49 -07:00
parent e5b2e4139e
commit e7c46c8efd
2 changed files with 36 additions and 11 deletions

View file

@ -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,
},

View file

@ -94,8 +94,7 @@ export function Game(props: {
if(result.response_type === "ERR") {
logDispatch(<div><em>{(result.value as string)}</em></div>);
} else {
const action = result.value as {type: "ExchangeTiles"; tiles_exchanged: number;};
logDispatch(<div><em>You exchanged {action.tiles_exchanged} tiles.</em></div>);
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(<div>{playerName} received {word.score} points for playing '{word.word}.'</div>);
}
logDispatch(<div>{playerName} received a total of <strong>{result.total} points</strong> for their turn.</div>);
} else if(action.type == "ExchangeTiles") {
logDispatch(<div>{playerName} exchanged {action.tiles_exchanged} tile{action.tiles_exchanged > 1 ? 's' : ''} for their turn.</div>);
}
else if(action.type == "Pass"){
logDispatch(<div>{playerName} passed.</div>);
}
}
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(<div><em>{msg}</em></div>);
setConfirmedScorePoints(total_points);
}
@ -258,7 +277,7 @@ export function Game(props: {
}}>Return Tiles</button>
<button onClick={() => {
props.wasm.skip_turn();
logDispatch(<div><em>Player skipped their turn</em></div>);
handlePlayerAction({type: "Pass"}, props.settings.playerName);
setTurnCount(turnCount + 1);
}}>Skip Turn</button>
</>;