Add standardized turn summary messages.
Same logic for both player and AI
This commit is contained in:
parent
e5b2e4139e
commit
e7c46c8efd
2 changed files with 36 additions and 11 deletions
|
@ -152,13 +152,19 @@ impl Game {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
let ai_length = ai_difficulties.len();
|
||||||
for (i, ai_difficulty) in ai_difficulties.into_iter().enumerate() {
|
for (i, ai_difficulty) in ai_difficulties.into_iter().enumerate() {
|
||||||
let ai = AI::new(ai_difficulty, &dictionary);
|
let ai = AI::new(ai_difficulty, &dictionary);
|
||||||
let mut tray = Tray::new(TRAY_LENGTH);
|
let mut tray = Tray::new(TRAY_LENGTH);
|
||||||
tray.fill(&mut letters);
|
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_states.push(PlayerState {
|
||||||
player: Player::AI {
|
player: Player::AI {
|
||||||
name: format!("AI {}", i),
|
name: ai_player_name,
|
||||||
difficulty: ai_difficulty,
|
difficulty: ai_difficulty,
|
||||||
object: ai,
|
object: ai,
|
||||||
},
|
},
|
||||||
|
|
|
@ -94,8 +94,7 @@ export function Game(props: {
|
||||||
if(result.response_type === "ERR") {
|
if(result.response_type === "ERR") {
|
||||||
logDispatch(<div><em>{(result.value as string)}</em></div>);
|
logDispatch(<div><em>{(result.value as string)}</em></div>);
|
||||||
} else {
|
} else {
|
||||||
const action = result.value as {type: "ExchangeTiles"; tiles_exchanged: number;};
|
handlePlayerAction(result.value as TurnAction, props.settings.playerName);
|
||||||
logDispatch(<div><em>You exchanged {action.tiles_exchanged} tiles.</em></div>);
|
|
||||||
setTurnCount(turnCount + 1);
|
setTurnCount(turnCount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +107,13 @@ export function Game(props: {
|
||||||
|
|
||||||
function runAI() {
|
function runAI() {
|
||||||
const result: TurnAdvanceResult = props.wasm.advance_turn();
|
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);
|
setTurnCount(turnCount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +177,24 @@ export function Game(props: {
|
||||||
if (logDivRef.current != null) {
|
if (logDivRef.current != null) {
|
||||||
logDivRef.current.scrollTo(0, logDivRef.current.scrollHeight); // scroll down
|
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 <>
|
return <>
|
||||||
|
@ -232,15 +254,12 @@ export function Game(props: {
|
||||||
const score_result = (result.value as { type: "PlayTiles"; result: ScoreResult }).result;
|
const score_result = (result.value as { type: "PlayTiles"; result: ScoreResult }).result;
|
||||||
const total_points = score_result.total;
|
const total_points = score_result.total;
|
||||||
|
|
||||||
let msg: string;
|
|
||||||
if (confirmedScorePoints > -1) {
|
if (confirmedScorePoints > -1) {
|
||||||
msg = `You scored ${total_points} points.`;
|
handlePlayerAction({type: "PlayTiles", result: score_result}, props.settings.playerName);
|
||||||
setTurnCount(turnCount + 1);
|
setTurnCount(turnCount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
logDispatch(<div><em>{msg}</em></div>);
|
|
||||||
|
|
||||||
setConfirmedScorePoints(total_points);
|
setConfirmedScorePoints(total_points);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,7 +277,7 @@ export function Game(props: {
|
||||||
}}>Return Tiles</button>
|
}}>Return Tiles</button>
|
||||||
<button onClick={() => {
|
<button onClick={() => {
|
||||||
props.wasm.skip_turn();
|
props.wasm.skip_turn();
|
||||||
logDispatch(<div><em>Player skipped their turn</em></div>);
|
handlePlayerAction({type: "Pass"}, props.settings.playerName);
|
||||||
setTurnCount(turnCount + 1);
|
setTurnCount(turnCount + 1);
|
||||||
}}>Skip Turn</button>
|
}}>Skip Turn</button>
|
||||||
</>;
|
</>;
|
||||||
|
|
Loading…
Reference in a new issue