fix(viewer): show bank settlement winners with player badges

This commit is contained in:
coyotte508 2026-11-19 08:11:24 +00:00
commit 5928bcb798
3 changed files with 76 additions and 13 deletions

View file

@ -832,7 +832,7 @@ import { colorBadgeHtml } from '../color-blind';
import { playerColors, playerTextColor, playerSymbol } from '../player-colors';
import { journalPieces } from '../journal-pieces';
import { journalOrder } from '../journal-order';
import { offshoreJournal } from '../offshore-journal';
import { offshoreJournal, offshoreWinJournal } from '../offshore-journal';
import { Vue, Component, Prop, Watch, Provide, ProvideReactive } from 'vue-property-decorator';
import { bidRevision, MoveName, ended, isLoanReversal, move as engineMove } from 'container-engine';
import type { GameState, Move } from 'container-engine';
@ -2143,7 +2143,11 @@ export default class Game extends Vue {
if (log.event.name == GameEventName.Upkeep) {
logReversed.push(
'New event: ' +
journalPieces(log.event.interest, this.preferences.colorBlind, this.playerColors)
journalPieces(
offshoreWinJournal(log.event.interest, this.G!) ?? log.event.interest,
this.preferences.colorBlind,
this.playerColors
)
);
} else {
logReversed.push('New event: ' + log.event.name);

View file

@ -13,15 +13,7 @@ export function offshoreJournal(
): string | undefined {
const text = (value: unknown) => (rich ? escapeJournal(String(value)) : String(value));
const name = state.players?.[entry.player]?.name || String(entry.player + 1);
// Match the ordinary engine badge; journalPieces applies the user's seat colors.
const badge = rich ? document.createElement('span') : undefined;
if (badge) {
badge.style.backgroundColor = playerColors[entry.player] || '';
badge.style.fontWeight = 'bold';
badge.style.padding = '0 3px';
badge.textContent = name;
}
const player = badge?.outerHTML || name;
const player = rich ? playerBadge(name, entry.player) : name;
const container = (color: unknown) =>
rich && containerColors.includes(color as ContainerColor)
? journalContainerIcon(color as ContainerColor, colorBlind)
@ -58,6 +50,28 @@ export function offshoreJournal(
}
}
/** Bank settlements are legacy Upkeep strings, not moves with a player ID.
* Match the complete public name and suffix rather than replacing name substrings.
* Ambiguous or unknown names keep their original prose without guessing a seat.
*/
export function offshoreWinJournal(message: string, state: GameState): string | undefined {
const match = /^(.*)( wins bank (?:containers|cash) lot [1-3])$/.exec(message);
if (!match) return undefined;
const players = state.players.filter((player) => player.name === match[1]);
if (players.length !== 1) return escapeJournal(message);
return playerBadge(match[1], players[0].id) + match[2];
}
function playerBadge(name: string, seat: number): string {
// Match the ordinary engine badge; journalPieces applies the user's seat colors.
const badge = document.createElement('span');
badge.style.backgroundColor = playerColors[seat] || '';
badge.style.fontWeight = 'bold';
badge.style.padding = '0 3px';
badge.textContent = name;
return badge.outerHTML;
}
export function escapeJournal(text: string): string {
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

View file

@ -2,10 +2,10 @@ import Game from '@/components/Game.vue';
import { journalContainerIcon, journalPieces } from '@/journal-pieces';
import fr from '@/localization/fr.json';
import { mountLocalization } from '@/localization/runtime.js';
import { offshoreJournal } from '@/offshore-journal';
import { offshoreJournal, offshoreWinJournal } from '@/offshore-journal';
import { setup, stripSecret } from 'container-engine/src/engine';
import { ContainerColor, ShipPosition } from 'container-engine/src/gamestate';
import { LogMove } from 'container-engine/src/log';
import { GameEventName, LogMove } from 'container-engine/src/log';
import { Move, MoveName } from 'container-engine/src/move';
import { describe, expect, it } from 'vitest';
@ -24,6 +24,51 @@ function render(log: LogMove, state = fixture(), blind = true) {
}
describe('Offshore rich journal', () => {
it.each(['containers', 'cash'])(
'badges %s winners in the rich journal without changing engine or plain logs',
(type) => {
const state = fixture();
state.players[0].name = 'You';
const message = `You wins bank ${type} lot 1`;
state.log = [{ type: 'event', event: { name: GameEventName.Upkeep, interest: message } }];
const context = { G: state, preferences: { colorBlind: true }, playerColors: ['#123456'] };
const root = document.createElement('div');
root.innerHTML = (Game as any).options.computed.logReversed.get.call(context)[0];
const badge = root.querySelector('[data-localized-journal] > span') as HTMLElement;
expect(badge.textContent).toBe('You');
expect(badge.style.backgroundColor).toBe('rgb(18, 52, 86)');
expect(badge.style.fontWeight).toBe('bold');
expect(root.textContent).toBe(`New event: ${message}`);
expect((Game as any).options.computed.logReversedSimple.get.call(context)).toEqual([
`New event: ${message}`,
]);
expect((state.log[0] as any).event.interest).toBe(message);
const localization = mountLocalization(root, { fr }, 'fr');
expect(root.textContent).toContain('remporte');
expect(badge.textContent).toBe('You');
localization.setLocale('en');
expect(root.textContent).toContain(message);
localization.destroy();
}
);
it('escapes winner names, matches whole names and leaves unrelated upkeep alone', () => {
const state = fixture();
const name = state.players[0].name;
state.players[1].name = 'Draw';
const root = document.createElement('div');
root.innerHTML = offshoreWinJournal(`${name} wins bank cash lot 3`, state)!;
expect(root.querySelector('span')!.textContent).toBe(name);
expect(root.querySelector('img')).toBeNull();
expect(offshoreWinJournal('Draw pays $1 interest', state)).toBeUndefined();
expect(offshoreWinJournal('Unknown wins bank cash lot 1', state)).toBe('Unknown wins bank cash lot 1');
state.players[1].name = name;
root.innerHTML = offshoreWinJournal(`${name} wins bank cash lot 3`, state)!;
expect(root.querySelector('span')).toBeNull();
expect(root.querySelector('img')).toBeNull();
expect(root.textContent).toBe(`${name} wins bank cash lot 3`);
});
it('matches ordinary badges, applies custom colors, escapes names exactly once and lists escrow pictograms', () => {
const state = fixture();
const root = render(cashCall(), state);