Support custom player colours across the Container viewer

This commit is contained in:
coyotte508 2026-10-02 01:38:31 +02:00
commit d0c9537cca
6 changed files with 53 additions and 11 deletions

View file

@ -191,7 +191,7 @@
:owner="ship.owner"
:ownerName="G.players[ship.owner].name"
:position="ship.position"
:color="ship.color"
:color="playerColors[ship.owner]"
/>
<template v-for="container in containers">
@ -649,7 +649,14 @@
<tr>
<th><div>Player</div></th>
<th v-for="player in G.players" :key="'FS' + player.id">
<div :style="'background-color: ' + playerColors[player.id]">{{ player.name }}</div>
<div
:style="{
backgroundColor: playerColors[player.id],
color: playerTextColor(playerColors[player.id]),
}"
>
{{ player.name }}
</div>
</th>
</tr>
<tr
@ -697,6 +704,7 @@ import InlineLog from './InlineLog.vue';
import { islandValue } from '../island-value';
import ColorBlindButton from './buttons/ColorBlindButton.vue';
import { colorBadgeHtml } from '../color-blind';
import { playerColors, playerTextColor } from '../player-colors';
import { journalPieces } from '../journal-pieces';
import { Vue, Component, Prop, Watch, Provide, ProvideReactive } from 'vue-property-decorator';
import { MoveName, ended, isLoanReversal, move as engineMove } from 'container-engine';
@ -785,7 +793,10 @@ export default class Game extends Vue {
loanCards: Piece[] = [];
ships: ShipType[] = [];
playerColors = ['dodgerblue', 'red', 'yellow', 'limegreen', 'mediumorchid'];
get playerColors() {
return playerColors(this.preferences);
}
playerTextColor = playerTextColor;
animationQueue: Array<Function> = [];
@ -1025,7 +1036,7 @@ export default class Game extends Vue {
// Ships
this.ships = [];
this.G?.players.forEach((player, pi) => {
const color = ['dodgerblue', 'red', 'yellow', 'limegreen', 'mediumorchid'][pi];
const color = this.playerColors[pi];
switch (player.ship.shipPosition) {
case ShipPosition.OpenSea:
this.ships.push({
@ -1754,13 +1765,14 @@ export default class Game extends Vue {
} else if (log.type == 'event') {
if (log.event.name == GameEventName.Upkeep) {
logReversed.push(
'New event: ' + journalPieces(log.event.interest, this.preferences.colorBlind)
'New event: ' +
journalPieces(log.event.interest, this.preferences.colorBlind, this.playerColors)
);
} else {
logReversed.push('New event: ' + log.event.name);
}
} else if (log.type == 'move') {
logReversed.push(journalPieces(log.pretty, this.preferences.colorBlind));
logReversed.push(journalPieces(log.pretty, this.preferences.colorBlind, this.playerColors));
}
});

View file

@ -8,7 +8,7 @@
:x="supporterBadge ? 23 : 5"
y="10"
font-weight="600"
fill="black"
:fill="playerTextColor(color)"
:text-decoration="isCurrentPlayer ? 'underline' : ''"
>{{ preferences.colorBlind ? owner + 1 + '. ' : '' }}{{ getPlayerName() }}</text
>
@ -206,6 +206,7 @@
</g>
</template>
<script lang="ts">
import { playerTextColor } from '../player-colors';
import type { Preferences } from '../types/ui-data';
import { Player } from 'container-engine';
import { Vue, Component, Prop, InjectReactive } from 'vue-property-decorator';
@ -217,6 +218,7 @@ import DropZone from './DropZone.vue';
},
})
export default class PlayerBoard extends Vue {
playerTextColor = playerTextColor;
@InjectReactive() readonly preferences!: Preferences;
get supporterBadge() {
return this.owner !== undefined && this.preferences.bgs?.players?.[this.owner]?.pro

View file

@ -1,9 +1,10 @@
import { ChatController, chatSegments, type ChatMessage } from '@boardgamers/protocol/chat';
import { mountChat } from '@boardgamers/protocol/chat/dom';
import { attachChat, type ViewerEmitter } from '@boardgamers/protocol/viewer';
import { playerColors } from 'container-engine/src/engine';
import { playerColors as resolvePlayerColors, playerTextColor } from './player-colors';
type ChatEmitter = Pick<ViewerEmitter<any, any>, 'on' | 'emit'>;
export function mountGameChat(emitter: ViewerEmitter<any, any>, host: Element): () => void {
let playerColors = resolvePlayerColors({});
const chat = new ChatController();
const detach = attachChat(emitter, chat);
const slot = host.querySelector<HTMLElement>('.chat-host') || document.createElement('div');
@ -138,7 +139,7 @@ export function mountGameChat(emitter: ViewerEmitter<any, any>, host: Element):
(message.author === 'You' ? localPlayer : players.find((p) => p.name === message.author)?.id);
if (index !== undefined && playerColors[index]) {
author.style.backgroundColor = playerColors[index];
author.style.color = author.style.backgroundColor === 'brown' ? '#fff' : '#111';
author.style.color = playerTextColor(playerColors[index]);
}
return author;
}
@ -164,6 +165,8 @@ export function mountGameChat(emitter: ViewerEmitter<any, any>, host: Element):
() => mobile.removeEventListener('change', expandMobilePanels),
detach,
emitter.on('preferences', (preferences) => {
playerColors = resolvePlayerColors(preferences as Parameters<typeof resolvePlayerColors>[0]);
view.refresh();
chatNotifications = preferences.chatNotifications !== false;
updateShortcut();
}),

View file

@ -1,4 +1,6 @@
import { playerColors as defaults } from 'container-engine/src/engine';
import { colorMarkSvg } from './color-blind';
import { playerTextColor } from './player-colors';
function pieceIcon(kind: string, color = '', colorBlind = false): string {
const label = `${color === 'darkslategray' ? 'dark green' : color} ${kind}`.trim();
const shape =
@ -12,10 +14,15 @@ function pieceIcon(kind: string, color = '', colorBlind = false): string {
}</svg></span>`;
}
export function journalPieces(html: string, colorBlind = false): string {
export function journalPieces(html: string, colorBlind = false, playerColors: string[] = []): string {
const root = document.createElement('div');
root.innerHTML = html;
for (const badge of Array.from(root.querySelectorAll('span'))) {
const seat = defaults.indexOf(badge.style.backgroundColor);
if (seat >= 0 && playerColors[seat]) {
badge.style.backgroundColor = playerColors[seat];
badge.style.color = playerTextColor(playerColors[seat]);
}
const color = badge.textContent || '';
const tail = badge.nextSibling;
if (

View file

@ -0,0 +1,18 @@
import { playerColors as defaults } from 'container-engine/src/engine';
import type { Preferences } from './types/ui-data';
export function playerColors(preferences: Pick<Preferences, 'bgs'>): string[] {
return defaults.map((color, index) => {
const custom = preferences.bgs?.playerColors?.[index];
return custom && /^#[a-f0-9]{6}$/i.test(custom) ? custom : color;
});
}
export function playerTextColor(color = ''): string {
if (!/^#[a-f0-9]{6}$/i.test(color)) return '#111';
const luminance = [1, 3, 5].map((offset) => {
const channel = parseInt(color.slice(offset, offset + 2), 16) / 255;
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
});
return luminance[0] * 0.2126 + luminance[1] * 0.7152 + luminance[2] * 0.0722 > 0.179 ? '#111' : '#fff';
}

View file

@ -9,7 +9,7 @@ export interface UIData {
}
export type Preferences = {
bgs?: { players?: { pro: boolean }[]; supporterBadge?: { url: string; label: string } };
bgs?: { playerColors?: string[]; players?: { pro: boolean }[]; supporterBadge?: { url: string; label: string } };
analysis?: boolean;
sound: boolean;
colorBlind?: boolean;