feat: implement replicant state synchronization for commentary, players, scoreboard, and graphics settings

- Added a new service for synchronizing state with replicants in `replicant-state-service.ts`.
- Refactored commentary store to utilize the new synchronization service.
- Created a new graphics settings store that syncs with replicants.
- Introduced a packs store for managing installed packs and their states.
- Updated players and scoreboard stores to use the new synchronization service.
- Created shared services for managing replicated state in graphics components.
- Refactored existing components to use the new shared services for replicant state.
- Added normalization and default values for commentary, graphics settings, players, and scoreboard.
- Improved type safety and organization in shared domain files for better maintainability.
This commit is contained in:
2026-05-30 21:22:48 +02:00
parent 02a108f983
commit b32c0e4560
33 changed files with 929 additions and 608 deletions
+85
View File
@@ -0,0 +1,85 @@
# Phase 2 Summary
## Scope
Executed the state and replicants phase only.
This phase focused on isolating state logic, normalizing Pinia stores, encapsulating browser-side replicant access, and moving side effects behind services without changing UX, visual design, overlay CSS, or public NodeCG contracts.
## Completed
- Added pure state/domain modules:
- `src/shared/domain/scoreboard`
- `src/shared/domain/commentary`
- `src/shared/domain/graphics`
- `src/shared/domain/players/state.ts`
- `src/shared/domain/packs/characters.ts`
- Moved normalization and pure state transitions out of dashboard stores.
- Replaced direct dashboard replicant imports with `src/dashboard/services/replicant-state-service.ts`.
- Added `useGraphicsSettingsStore` and moved dashboard graphics skin writes through the store.
- Reworked scoreboard, players and commentary stores to use shared domain normalizers and service-based replicant sync.
- Replaced the pack registry singleton composable with a normalized `usePacksStore`.
- Moved pack replicant listeners and NodeCG pack messages into `src/dashboard/services/pack-service.ts`.
- Removed Vue reactivity and mutable pack registration from `src/shared/fighting-characters.ts`.
- Modeled installed pack manifests as explicit store state instead of hidden module state.
- Centralized registry auto-refresh timer in the packs store.
- Routed integration NodeCG messages through `src/dashboard/services/integration-message-service.ts`.
- Added `src/graphics/shared/services/replicated-state.ts` so graphics read replicants through a service layer.
- Removed the redundant `src/dashboard/stores/store-sync.ts`.
## Preserved
- Public replicant names were unchanged.
- Public message names were unchanged.
- Existing dashboard UX was preserved.
- Overlay markup, CSS, positioning and animation logic were not intentionally changed.
- The existing `usePackRegistry` import path remains as a compatibility wrapper over the packs store.
- The legacy `src/shared/fighting-characters.ts` path remains as a compatibility export, but no longer owns mutable runtime state.
## Realtime Flow After This Phase
```text
schemas
-> nodecg/browser
-> dashboard services / graphics services
-> Pinia stores or overlay computed state
-> components
```
Pack runtime flow:
```text
pack replicants
-> pack service
-> packs store
-> pack registry compatibility composable
-> game / character UI
```
## Verification
- `pnpm.cmd exec vue-tsc -p tsconfig.browser.json --noEmit`: passed.
- `pnpm.cmd exec tsc -b tsconfig.extension.json --pretty false`: passed.
- `pnpm.cmd exec eslint`: passed with 0 errors and existing Vue formatting warnings.
- `pnpm.cmd run build`: passed.
- Searched dashboard, graphics and shared for direct NodeCG/message/replicant imports:
- remaining browser NodeCG access is contained in services and `nodecg/browser`.
- direct component/view replicant imports were removed.
- Searched for `any` in touched runtime areas:
- no new TypeScript `any` usage was added.
## Notes and Limits
- This phase did not split large views like `Players.vue` or `Settings.vue`.
- This phase did not refactor overlay internals beyond replacing direct replicant imports with a read service.
- This phase did not rewrite extension-side `pack-manager.ts`.
- This phase did not rename public messages to the future canonical names; compatibility was preserved.
- Existing Vue lint warnings remain formatting-only and were not addressed because they are outside this phase.
## Remaining For Later Phases
- Controlled rewrite of `pack-manager.ts`.
- Full split of `useIntegration` into provider clients, OAuth client, temporary players and import modules.
- Divide `Players.vue` and `Settings.vue`.
- Extract overlay view models and visual helpers after visual baseline.
- Add tests for pure normalizers and pack state derivations.
@@ -83,12 +83,12 @@ const updateRound = () => {
return; return;
} }
if (customActive.value) { if (customActive.value) {
scoreboardStore.scoreboard.round = customText.value.trim(); scoreboardStore.setRound(customText.value.trim());
return; return;
} }
const prefix = bracketSide.value ? `${bracketSide.value} ` : ''; const prefix = bracketSide.value ? `${bracketSide.value} ` : '';
scoreboardStore.scoreboard.round = `${prefix}${stage.value}`.trim(); scoreboardStore.setRound(`${prefix}${stage.value}`.trim());
}; };
watch([stage, bracketSide, customText, customActive], updateRound); watch([stage, bracketSide, customText, customActive], updateRound);
@@ -1,5 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue'; import { computed } from 'vue';
import { stripTwitterPrefix } from '../../../shared/domain/commentary';
import { t } from '../i18n'; import { t } from '../i18n';
import { useCommentaryStore } from '../../stores/commentary'; import { useCommentaryStore } from '../../stores/commentary';
@@ -17,25 +18,18 @@ const twitterRules = [
!val || TWITTER_VALID_CHARS.test(val) || t('commentaryTwitterInvalidChars'), !val || TWITTER_VALID_CHARS.test(val) || t('commentaryTwitterInvalidChars'),
]; ];
function stripAt(value: string): string {
return value.startsWith('@') ? value.slice(1) : value;
}
function handleLeftTwitterInput(value: string | number | null) { function handleLeftTwitterInput(value: string | number | null) {
commentaryStore.leftCommentatorTwitter = value ? stripAt(String(value)) : ''; commentaryStore.leftCommentatorTwitter = value ? stripTwitterPrefix(String(value)) : '';
} }
function handleRightTwitterInput(value: string | number | null) { function handleRightTwitterInput(value: string | number | null) {
commentaryStore.rightCommentatorTwitter = value ? stripAt(String(value)) : ''; commentaryStore.rightCommentatorTwitter = value ? stripTwitterPrefix(String(value)) : '';
} }
// --- Clear --- // --- Clear ---
function clearAll() { function clearAll() {
commentaryStore.leftCommentator = ''; commentaryStore.clearCommentary();
commentaryStore.leftCommentatorTwitter = '';
commentaryStore.rightCommentator = '';
commentaryStore.rightCommentatorTwitter = '';
} }
const isAnythingFilled = computed(() => const isAnythingFilled = computed(() =>
@@ -74,8 +74,7 @@ const character = computed({
? scoreboardStore.scoreboard.leftCharacter ? scoreboardStore.scoreboard.leftCharacter
: scoreboardStore.scoreboard.rightCharacter), : scoreboardStore.scoreboard.rightCharacter),
set: (v) => { set: (v) => {
if (isLeft.value) scoreboardStore.scoreboard.leftCharacter = v; scoreboardStore.setSideCharacter(props.side, v);
else scoreboardStore.scoreboard.rightCharacter = v;
}, },
}); });
@@ -21,28 +21,24 @@ const {
// Refresca el catálogo al montar y luego cada 15 segundos automáticamente. // Refresca el catálogo al montar y luego cada 15 segundos automáticamente.
// Si Gitea no está disponible se usa la caché persistida del replicante. // Si Gitea no está disponible se usa la caché persistida del replicante.
onMounted(() => { onMounted(() => {
packRegistry.fetchRegistry(); packRegistry.startRegistryRefresh();
}); });
const refreshInterval = setInterval(() => {
packRegistry.fetchRegistry();
}, 15_000);
onUnmounted(() => { onUnmounted(() => {
clearInterval(refreshInterval); packRegistry.stopRegistryRefresh();
}); });
const adjustLeftScore = (delta: number) => { const adjustLeftScore = (delta: number) => {
scoreboardStore.leftScore = Math.max(0, scoreboardStore.leftScore + delta); scoreboardStore.adjustScore('left', delta);
}; };
const adjustRightScore = (delta: number) => { const adjustRightScore = (delta: number) => {
scoreboardStore.rightScore = Math.max(0, scoreboardStore.rightScore + delta); scoreboardStore.adjustScore('right', delta);
}; };
/** Tras una descarga exitosa, activa el juego en el store. */ /** Tras una descarga exitosa, activa el juego en el store. */
const onPackDownloaded = (gameName: string) => { const onPackDownloaded = (gameName: string) => {
scoreboardStore.scoreboard.game = gameName; scoreboardStore.setGame(gameName);
}; };
// ── Estado del diálogo de actualización ─────────────────────────────────────── // ── Estado del diálogo de actualización ───────────────────────────────────────
@@ -13,14 +13,14 @@
// ───────────────────────────────────────────────────────────────────────────── // ─────────────────────────────────────────────────────────────────────────────
import { computed, ref, watch, type InjectionKey, type Ref } from 'vue'; import { computed, ref, watch, type InjectionKey, type Ref } from 'vue';
import { getCharactersByGame, getDefaultCharactersByGame, installedPacksRevision } from '../../../shared/fighting-characters'; import type { FightingCharacterOption } from '../../../shared/domain/packs/characters';
import type { GameSelectOption, PackRegistryEntry } from '../../../shared/domain/packs/types'; import type { GameSelectOption, PackRegistryEntry } from '../../../shared/domain/packs/types';
import { useScoreboardStore } from '../../stores/scoreboard'; import { useScoreboardStore } from '../../stores/scoreboard';
import { usePackRegistry } from './usePackRegistry'; import { usePackRegistry } from './usePackRegistry';
// ── Types ───────────────────────────────────────────────────────────────────── // ── Types ─────────────────────────────────────────────────────────────────────
export type CharacterOption = ReturnType<typeof getCharactersByGame>[number]; export type CharacterOption = FightingCharacterOption;
export type CharacterGameContext = ReturnType<typeof useCharacterGame>; export type CharacterGameContext = ReturnType<typeof useCharacterGame>;
export const CHARACTER_GAME_KEY: InjectionKey<CharacterGameContext> = Symbol('characterGame'); export const CHARACTER_GAME_KEY: InjectionKey<CharacterGameContext> = Symbol('characterGame');
@@ -62,7 +62,7 @@ export function useCharacterGame() {
*/ */
const handleGameSelect = (gameName: string) => { const handleGameSelect = (gameName: string) => {
if (!gameName) { if (!gameName) {
scoreboardStore.scoreboard.game = ''; scoreboardStore.setGame('');
return; return;
} }
if (!packRegistry.isGameAvailable(gameName)) { if (!packRegistry.isGameAvailable(gameName)) {
@@ -72,17 +72,13 @@ export function useCharacterGame() {
// Do NOT update the store — the game isn't installed // Do NOT update the store — the game isn't installed
return; return;
} }
scoreboardStore.scoreboard.game = gameName; scoreboardStore.setGame(gameName);
}; };
// ── Character state ─────────────────────────────────────────────────────── // ── Character state ───────────────────────────────────────────────────────
const characterOptions = computed(() => { const characterOptions = computed(() => {
// Subscribing to installedPacksRevision forces Vue to re-evaluate this return packRegistry.getCharactersByGame(scoreboardStore.scoreboard.game);
// computed whenever a pack is registered/unregistered at runtime, even
// though scoreboardStore.scoreboard.game itself hasn't changed.
void installedPacksRevision.value;
return getCharactersByGame(scoreboardStore.scoreboard.game);
}); });
const leftCharacterOptions = ref<CharacterOption[]>([]); const leftCharacterOptions = ref<CharacterOption[]>([]);
const rightCharacterOptions = ref<CharacterOption[]>([]); const rightCharacterOptions = ref<CharacterOption[]>([]);
@@ -155,11 +151,11 @@ export function useCharacterGame() {
}; };
} }
const options = getCharactersByGame(newGame); const options = packRegistry.getCharactersByGame(newGame);
// If the game is set but has no options yet, the pack is still loading // If the game is set but has no options yet, the pack is still loading
// (installed pack whose registerInstalledPack() hasn't run yet). // (installed pack whose manifest has not been loaded into the pack store yet).
// Bail out — the installedPacksRevision watcher below will restore state // Bail out — the characterOptions watcher below will restore state
// once the pack becomes available. // once the pack becomes available.
if (newGame && options.length === 0) return; if (newGame && options.length === 0) return;
@@ -176,7 +172,7 @@ export function useCharacterGame() {
if (!allowed.has(nextRight)) nextRight = ''; if (!allowed.has(nextRight)) nextRight = '';
if ((!nextLeft || !nextRight) && (!curLeft || !curRight)) { if ((!nextLeft || !nextRight) && (!curLeft || !curRight)) {
const defaults = getDefaultCharactersByGame(newGame); const defaults = packRegistry.getDefaultCharactersByGame(newGame);
if (defaults) { if (defaults) {
if (!nextLeft) nextLeft = allowed.has(defaults.leftCharacter) ? defaults.leftCharacter : ''; if (!nextLeft) nextLeft = allowed.has(defaults.leftCharacter) ? defaults.leftCharacter : '';
if (!nextRight) nextRight = allowed.has(defaults.rightCharacter) ? defaults.rightCharacter : ''; if (!nextRight) nextRight = allowed.has(defaults.rightCharacter) ? defaults.rightCharacter : '';
@@ -184,16 +180,16 @@ export function useCharacterGame() {
} }
if (allowed.has(nextLeft)) { if (allowed.has(nextLeft)) {
scoreboardStore.scoreboard.leftCharacter = nextLeft; scoreboardStore.setSideCharacter('left', nextLeft);
} else if (!allowed.has(scoreboardStore.scoreboard.leftCharacter)) { } else if (!allowed.has(scoreboardStore.scoreboard.leftCharacter)) {
scoreboardStore.scoreboard.leftCharacter = ''; scoreboardStore.setSideCharacter('left', '');
leftCharacterInput.value = ''; leftCharacterInput.value = '';
} }
if (allowed.has(nextRight)) { if (allowed.has(nextRight)) {
scoreboardStore.scoreboard.rightCharacter = nextRight; scoreboardStore.setSideCharacter('right', nextRight);
} else if (!allowed.has(scoreboardStore.scoreboard.rightCharacter)) { } else if (!allowed.has(scoreboardStore.scoreboard.rightCharacter)) {
scoreboardStore.scoreboard.rightCharacter = ''; scoreboardStore.setSideCharacter('right', '');
rightCharacterInput.value = ''; rightCharacterInput.value = '';
} }
}, },
@@ -232,14 +228,12 @@ export function useCharacterGame() {
{ immediate: true }, { immediate: true },
); );
// When an installed pack becomes available (e.g. after page refresh while // When an installed pack manifest becomes available, re-validate characters
// the pack loads asynchronously), re-validate and restore the characters // already present in the replicated scoreboard state.
// that are already in the store but couldn't be confirmed before. watch(characterOptions, (options) => {
watch(installedPacksRevision, () => {
const game = scoreboardStore.scoreboard.game; const game = scoreboardStore.scoreboard.game;
if (!game) return; if (!game) return;
const options = getCharactersByGame(game);
if (options.length === 0) return; if (options.length === 0) return;
const allowed = new Set(options.map((o) => o.value)); const allowed = new Set(options.map((o) => o.value));
@@ -251,14 +245,14 @@ export function useCharacterGame() {
if (leftCharacter && allowed.has(leftCharacter)) { if (leftCharacter && allowed.has(leftCharacter)) {
leftCharacterInput.value = options.find((o) => o.value === leftCharacter)?.label ?? ''; leftCharacterInput.value = options.find((o) => o.value === leftCharacter)?.label ?? '';
} else if (leftCharacter && !allowed.has(leftCharacter)) { } else if (leftCharacter && !allowed.has(leftCharacter)) {
scoreboardStore.scoreboard.leftCharacter = ''; scoreboardStore.setSideCharacter('left', '');
leftCharacterInput.value = ''; leftCharacterInput.value = '';
} }
if (rightCharacter && allowed.has(rightCharacter)) { if (rightCharacter && allowed.has(rightCharacter)) {
rightCharacterInput.value = options.find((o) => o.value === rightCharacter)?.label ?? ''; rightCharacterInput.value = options.find((o) => o.value === rightCharacter)?.label ?? '';
} else if (rightCharacter && !allowed.has(rightCharacter)) { } else if (rightCharacter && !allowed.has(rightCharacter)) {
scoreboardStore.scoreboard.rightCharacter = ''; scoreboardStore.setSideCharacter('right', '');
rightCharacterInput.value = ''; rightCharacterInput.value = '';
} }
}); });
@@ -1,5 +1,5 @@
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'; import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue';
import { sendNodecgMessage } from '../../../nodecg/browser/messages'; import { sendIntegrationMessage } from '../../services/integration-message-service';
// ─── Tipos ───────────────────────────────────────────────────────────────────── // ─── Tipos ─────────────────────────────────────────────────────────────────────
@@ -66,8 +66,6 @@ export interface UseIntegrationOptions {
playersStore: PlayersStore; playersStore: PlayersStore;
} }
// ─── Utilidad para mensajes NodeCG ─────────────────────────────────────────────
// ─── Composable ──────────────────────────────────────────────────────────────── // ─── Composable ────────────────────────────────────────────────────────────────
export function useIntegration(options: UseIntegrationOptions) { export function useIntegration(options: UseIntegrationOptions) {
@@ -155,8 +153,9 @@ export function useIntegration(options: UseIntegrationOptions) {
tournamentsError.value = ''; tournamentsError.value = '';
loadingTournaments.value = true; loadingTournaments.value = true;
try { try {
const tournaments = await sendNodecgMessage<IntegrationTournament[]>( const tournaments = await sendIntegrationMessage<IntegrationTournament[]>(
`${messagePrefix}:fetchRecentTournaments`, messagePrefix,
'fetchRecentTournaments',
{ token: currentToken }, { token: currentToken },
); );
hasValidatedToken.value = true; hasValidatedToken.value = true;
@@ -194,8 +193,9 @@ export function useIntegration(options: UseIntegrationOptions) {
players.value = []; players.value = [];
try { try {
const importedPlayers = await sendNodecgMessage<IntegrationPlayer[]>( const importedPlayers = await sendIntegrationMessage<IntegrationPlayer[]>(
`${messagePrefix}:fetchTournamentPlayers`, messagePrefix,
'fetchTournamentPlayers',
{ token: token.value.trim(), slug: tournament.slug }, { token: token.value.trim(), slug: tournament.slug },
); );
players.value = importedPlayers; players.value = importedPlayers;
@@ -315,8 +315,9 @@ export function useIntegration(options: UseIntegrationOptions) {
if (!oauthSessionId.value) return; if (!oauthSessionId.value) return;
try { try {
const status = await sendNodecgMessage<OAuthStatusResponse>( const status = await sendIntegrationMessage<OAuthStatusResponse>(
`${messagePrefix}:getOAuthSessionStatus`, messagePrefix,
'getOAuthSessionStatus',
{ sessionId: oauthSessionId.value }, { sessionId: oauthSessionId.value },
); );
@@ -352,8 +353,9 @@ export function useIntegration(options: UseIntegrationOptions) {
stopPolling(); stopPolling();
try { try {
const session = await sendNodecgMessage<OAuthSessionResponse>( const session = await sendIntegrationMessage<OAuthSessionResponse>(
`${messagePrefix}:createOAuthSession`, messagePrefix,
'createOAuthSession',
{}, {},
); );
oauthSessionId.value = session.sessionId; oauthSessionId.value = session.sessionId;
@@ -1,235 +1,68 @@
// src/dashboard/scoreboard/composables/usePackRegistry.ts import { storeToRefs } from 'pinia';
// ───────────────────────────────────────────────────────────────────────────── import type { InjectionKey, Ref } from 'vue';
// Singleton composable. The first caller sets up NodeCG replicant listeners; import { usePacksStore } from '../../stores/packs';
// subsequent calls return the same reactive state. This avoids duplicate event import type { DefaultCharacterPair, FightingCharacterOption } from '../../../shared/domain/packs/characters';
// listeners when multiple components call usePackRegistry().
// ─────────────────────────────────────────────────────────────────────────────
import { computed, ref, type ComputedRef, type InjectionKey } from 'vue';
import {
registerInstalledPack,
unregisterInstalledPack,
} from '../../../shared/fighting-characters';
import { sendNodecgCommand, sendNodecgMessage } from '../../../nodecg/browser/messages';
import { createPackBrowserReplicants } from '../../../nodecg/browser/packReplicants';
import { messageNames } from '../../../nodecg/messageNames';
import type { import type {
GameSelectOption, GameSelectOption,
PackDownloadState, PackDownloadState,
PackManifest,
PackRegistry, PackRegistry,
PackUpdateInfo, PackUpdateInfo,
} from '../../../shared/domain/packs/types'; } from '../../../shared/domain/packs/types';
// ── NodeCG global type declarations ──────────────────────────────────────────
// NodeCG injects these into the browser window via its bundle script.
// ── Module-level singleton state ──────────────────────────────────────────────
let initialized = false;
const registry = ref<PackRegistry | null>(null);
const installedPackIds = ref<string[]>([]);
const downloadStates = ref<Record<string, PackDownloadState>>({});
const availableUpdates = ref<Record<string, PackUpdateInfo>>({});
// Tracks which installed pack manifests have been loaded into fighting-characters.ts
const loadedManifestIds = new Set<string>();
// ── Helpers ───────────────────────────────────────────────────────────────────
const formatBytes = (bytes: number): string => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
/**
* Asks the NodeCG extension to read the local manifest.json for an installed
* pack and registers the characters in fighting-characters.ts.
*/
const loadInstalledManifest = (packId: string): void => {
if (loadedManifestIds.has(packId)) return;
sendNodecgMessage<PackManifest>(messageNames.packs.readLocalManifest, packId)
.then((manifest) => {
registerInstalledPack(manifest);
loadedManifestIds.add(packId);
})
.catch((err: unknown) => {
console.error(`[usePackRegistry] Failed to load manifest for "${packId}":`, err);
});
};
// ── Replicant setup (runs once) ───────────────────────────────────────────────
const initReplicants = (): void => {
if (initialized) return;
initialized = true;
const { registryRep, installedRep, statesRep, updatesRep, waitUntilReady } =
createPackBrowserReplicants();
waitUntilReady().then(() => {
// Hydrate initial values
registry.value = registryRep.value;
installedPackIds.value = installedRep.value ?? [];
downloadStates.value = statesRep.value ?? {};
availableUpdates.value = updatesRep.value ?? {};
// Load manifests for all installed packs
for (const id of installedPackIds.value) {
loadInstalledManifest(id);
}
// Subscribe to changes
registryRep.on('change', (val) => {
registry.value = val;
});
installedRep.on('change', (newVal, oldVal) => {
const next = newVal ?? [];
const prev = oldVal ?? [];
installedPackIds.value = next;
// Load manifests for newly installed packs
const added = next.filter((id) => !prev.includes(id));
for (const id of added) {
loadInstalledManifest(id);
}
// Unregister packs that were removed
const removed = prev.filter((id) => !next.includes(id));
for (const id of removed) {
const gameName = getGameNameById(id);
unregisterInstalledPack(gameName);
loadedManifestIds.delete(id);
}
});
statesRep.on('change', (val) => {
downloadStates.value = val ?? {};
});
updatesRep.on('change', (val) => {
availableUpdates.value = val ?? {};
});
});
};
/**
* Given a pack ID (e.g. "street-fighter-6"), returns the matching game name
* from the current registry, or an empty string if the registry isn't loaded.
*/
const getGameNameById = (packId: string): string =>
registry.value?.packs.find((p) => p.id === packId)?.name ?? '';
// ── Public composable ─────────────────────────────────────────────────────────
export interface PackRegistryContext { export interface PackRegistryContext {
/** Full registry fetched from Gitea (null until first fetch). */ registry: Ref<PackRegistry | null>;
registry: typeof registry; installedPackIds: Ref<string[]>;
/** IDs of packs installed on disk (bundled packs are NOT in this list). */ downloadStates: Ref<Record<string, PackDownloadState>>;
installedPackIds: typeof installedPackIds;
/** Per-pack download state. */
downloadStates: typeof downloadStates;
/** Checks if a game is available (bundled OR installed). */
isGameAvailable: (gameName: string) => boolean; isGameAvailable: (gameName: string) => boolean;
/** Returns the download state for a pack, or a default idle state. */
getDownloadState: (packId: string) => PackDownloadState; getDownloadState: (packId: string) => PackDownloadState;
/** All games from the registry, enriched with availability info. */ getCharactersByGame: (gameName: string) => FightingCharacterOption[];
allGameOptions: ReturnType<typeof buildAllGameOptions>; getDefaultCharactersByGame: (gameName: string) => DefaultCharacterPair | undefined;
/** Tells the extension to fetch the latest registry.json from Gitea. */ allGameOptions: Ref<GameSelectOption[]>;
fetchRegistry: () => void; fetchRegistry: () => void;
/** Tells the extension to download and install a pack. */ startRegistryRefresh: (intervalMs?: number) => void;
stopRegistryRefresh: () => void;
downloadPack: (packId: string) => void; downloadPack: (packId: string) => void;
/** Tells the extension to uninstall a pack and delete its files. */
uninstallPack: (packId: string) => void; uninstallPack: (packId: string) => void;
/** Tells the extension to download and apply an update for an installed pack. */
updatePack: (packId: string) => void; updatePack: (packId: string) => void;
/** Map of packId → version info for packs that have a newer version available. */ availableUpdates: Ref<Record<string, PackUpdateInfo>>;
availableUpdates: typeof availableUpdates; updateCount: Ref<number>;
/** Total number of packs with available updates. */ formatBytes: (bytes: number) => string;
updateCount: ComputedRef<number>;
/** Human-readable file size. */
formatBytes: typeof formatBytes;
/** Returns the URL for the pack's logo served by NodeCG (installed packs only). */
getLocalLogoUrl: (packId: string) => string; getLocalLogoUrl: (packId: string) => string;
} }
export const PACK_REGISTRY_KEY: InjectionKey<PackRegistryContext> = Symbol('packRegistry'); export const PACK_REGISTRY_KEY: InjectionKey<PackRegistryContext> = Symbol('packRegistry');
const buildAllGameOptions = () =>
computed<GameSelectOption[]>(() => {
// Registry not loaded yet — return empty list
if (!registry.value) return [];
return registry.value.packs.map((entry) => ({
label: entry.name,
value: entry.name,
available: installedPackIds.value.includes(entry.id),
registryEntry: entry,
updateInfo: availableUpdates.value[entry.id],
}));
});
export function usePackRegistry(): PackRegistryContext { export function usePackRegistry(): PackRegistryContext {
initReplicants(); const packsStore = usePacksStore();
packsStore.initialize();
const allGameOptions = buildAllGameOptions(); const {
registry,
const isGameAvailable = (gameName: string): boolean => { installedPackIds,
const entry = registry.value?.packs.find((p) => p.name === gameName); downloadStates,
if (!entry) return false; availableUpdates,
return installedPackIds.value.includes(entry.id); allGameOptions,
}; updateCount,
} = storeToRefs(packsStore);
const getDownloadState = (packId: string): PackDownloadState =>
downloadStates.value[packId] ?? { status: 'idle', progress: 0 };
const getLocalLogoUrl = (packId: string): string =>
`/packs/${packId}/logo.png`;
const fetchRegistry = (): void => {
sendNodecgCommand(messageNames.packs.fetchRegistry).catch((err: unknown) => {
console.error('[usePackRegistry] fetchPackRegistry failed:', err);
});
};
const downloadPack = (packId: string): void => {
sendNodecgCommand(messageNames.packs.download, packId).catch((err: unknown) => {
console.error(`[usePackRegistry] downloadPack "${packId}" failed:`, err);
});
};
const uninstallPack = (packId: string): void => {
sendNodecgCommand(messageNames.packs.uninstall, packId).catch((err: unknown) => {
console.error(`[usePackRegistry] uninstallPack "${packId}" failed:`, err);
});
};
const updatePack = (packId: string): void => {
sendNodecgCommand(messageNames.packs.update, packId).catch((err: unknown) => {
console.error(`[usePackRegistry] updatePack "${packId}" failed:`, err);
});
};
const updateCount = computed(() => Object.keys(availableUpdates.value).length);
return { return {
registry, registry,
installedPackIds, installedPackIds,
downloadStates, downloadStates,
isGameAvailable, isGameAvailable: packsStore.isGameAvailable,
getDownloadState, getDownloadState: packsStore.getDownloadState,
getCharactersByGame: packsStore.getCharactersByGame,
getDefaultCharactersByGame: packsStore.getDefaultCharactersByGame,
allGameOptions, allGameOptions,
fetchRegistry, fetchRegistry: packsStore.fetchRegistry,
downloadPack, startRegistryRefresh: packsStore.startRegistryRefresh,
uninstallPack, stopRegistryRefresh: packsStore.stopRegistryRefresh,
updatePack, downloadPack: packsStore.downloadPack,
uninstallPack: packsStore.uninstallPack,
updatePack: packsStore.updatePack,
availableUpdates, availableUpdates,
updateCount, updateCount,
formatBytes, formatBytes: packsStore.formatBytes,
getLocalLogoUrl, getLocalLogoUrl: packsStore.getLocalLogoUrl,
}; };
} }
@@ -2,6 +2,7 @@ import { computed, ref, watch, watchEffect } from 'vue';
import { useScoreboardStore } from '../../stores/scoreboard'; import { useScoreboardStore } from '../../stores/scoreboard';
import { usePlayersStore } from '../../stores/players'; import { usePlayersStore } from '../../stores/players';
import type { Schemas } from '../../../types'; import type { Schemas } from '../../../types';
import { createPlayerId, normalizePlayerName } from '../../../shared/domain/players/state';
import { t } from '../i18n'; import { t } from '../i18n';
import { useCountryFilter } from './useCountryFilter'; import { useCountryFilter } from './useCountryFilter';
@@ -16,34 +17,6 @@ export const CUSTOM_RIGHT_PLAYER_ID = '__custom_right_player__';
// Pure helpers (no Vue reactivity) // Pure helpers (no Vue reactivity)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const normalizeName = (value: string) => value.trim().toLowerCase();
/**
* Generates a unique slug-based player ID that does not collide with
* existing player keys in the store.
*/
const createPlayerId = (name: string, players: Schemas.Players): string => {
const base = name
.trim()
.toLowerCase()
.normalize('NFD')
.replace(/[^\w\s-]/g, '')
.replace(/[\u0300-\u036f]/g, '')
.replace(/\s+/g, '-') || 'player';
let index = 1;
let candidate = base;
while (players[candidate]) {
index += 1;
candidate = `${base}-${index}`;
}
return candidate;
};
// ---------------------------------------------------------------------------
// Composable
// ---------------------------------------------------------------------------
/** /**
* Encapsulates all reactive state and handlers for one side of the scoreboard * Encapsulates all reactive state and handlers for one side of the scoreboard
* (left or right). Call once per side inside the corresponding component. * (left or right). Call once per side inside the corresponding component.
@@ -63,32 +36,28 @@ export function usePlayerSide(side: 'left' | 'right') {
const playerId = computed({ const playerId = computed({
get: () => (isLeft ? scoreboardStore.scoreboard.leftPlayerId : scoreboardStore.scoreboard.rightPlayerId), get: () => (isLeft ? scoreboardStore.scoreboard.leftPlayerId : scoreboardStore.scoreboard.rightPlayerId),
set: (v) => { set: (v) => {
if (isLeft) scoreboardStore.scoreboard.leftPlayerId = v; scoreboardStore.setSidePlayerId(side, v);
else scoreboardStore.scoreboard.rightPlayerId = v;
}, },
}); });
const nameOverride = computed({ const nameOverride = computed({
get: () => (isLeft ? scoreboardStore.scoreboard.leftNameOverride : scoreboardStore.scoreboard.rightNameOverride), get: () => (isLeft ? scoreboardStore.scoreboard.leftNameOverride : scoreboardStore.scoreboard.rightNameOverride),
set: (v) => { set: (v) => {
if (isLeft) scoreboardStore.scoreboard.leftNameOverride = v; scoreboardStore.setSideNameOverride(side, v);
else scoreboardStore.scoreboard.rightNameOverride = v;
}, },
}); });
const teamOverride = computed({ const teamOverride = computed({
get: () => (isLeft ? scoreboardStore.scoreboard.leftTeamOverride : scoreboardStore.scoreboard.rightTeamOverride), get: () => (isLeft ? scoreboardStore.scoreboard.leftTeamOverride : scoreboardStore.scoreboard.rightTeamOverride),
set: (v) => { set: (v) => {
if (isLeft) scoreboardStore.scoreboard.leftTeamOverride = v; scoreboardStore.setSideTeamOverride(side, v);
else scoreboardStore.scoreboard.rightTeamOverride = v;
}, },
}); });
const countryOverride = computed({ const countryOverride = computed({
get: () => (isLeft ? scoreboardStore.scoreboard.leftCountryOverride : scoreboardStore.scoreboard.rightCountryOverride), get: () => (isLeft ? scoreboardStore.scoreboard.leftCountryOverride : scoreboardStore.scoreboard.rightCountryOverride),
set: (v) => { set: (v) => {
if (isLeft) scoreboardStore.scoreboard.leftCountryOverride = v; scoreboardStore.setSideCountryOverride(side, v);
else scoreboardStore.scoreboard.rightCountryOverride = v;
}, },
}); });
@@ -145,10 +114,10 @@ export function usePlayerSide(side: 'left' | 'right') {
}; };
const playerExistsByGamertag = (name: string): boolean => { const playerExistsByGamertag = (name: string): boolean => {
const normalized = normalizeName(name); const normalized = normalizePlayerName(name);
return Boolean(normalized) return Boolean(normalized)
&& Object.values(playersStore.players).some( && Object.values(playersStore.players).some(
(p) => normalizeName(p.gamertag || '') === normalized, (p) => normalizePlayerName(p.gamertag || '') === normalized,
); );
}; };
+6 -8
View File
@@ -2,7 +2,7 @@
import { useHead } from '@unhead/vue'; import { useHead } from '@unhead/vue';
import { computed, ref, watch } from 'vue'; import { computed, ref, watch } from 'vue';
import bundlePackage from '../../../../package.json'; import bundlePackage from '../../../../package.json';
import { graphicsSettingsReplicant } from '../../../nodecg/browser/replicants'; import { useGraphicsSettingsStore } from '../../stores/graphics-settings';
import { t } from '../i18n'; import { t } from '../i18n';
defineOptions({ name: 'GraphicsView' }); defineOptions({ name: 'GraphicsView' });
@@ -24,6 +24,7 @@ type GraphicCard = {
useHead(() => ({ title: t('graphicsTitle') })); useHead(() => ({ title: t('graphicsTitle') }));
const graphicsSettingsStore = useGraphicsSettingsStore();
const graphics = computed<GraphicConfig[]>(() => bundlePackage.nodecg?.graphics ?? []); const graphics = computed<GraphicConfig[]>(() => bundlePackage.nodecg?.graphics ?? []);
const baseUrl = computed(() => { const baseUrl = computed(() => {
@@ -60,7 +61,7 @@ const commentaryGraphic = computed(() =>
const selectedScoreboardSkin = ref<string>(''); const selectedScoreboardSkin = ref<string>('');
watch( watch(
[scoreboardGraphics, () => graphicsSettingsReplicant?.data?.scoreboardSkin], [scoreboardGraphics, () => graphicsSettingsStore.settings.scoreboardSkin],
([availableSkins, replicatedSkin]) => { ([availableSkins, replicatedSkin]) => {
if (availableSkins.length === 0) { if (availableSkins.length === 0) {
selectedScoreboardSkin.value = ''; selectedScoreboardSkin.value = '';
@@ -87,18 +88,15 @@ watch(
watch( watch(
selectedScoreboardSkin, selectedScoreboardSkin,
(value) => { (value) => {
if (!value || !graphicsSettingsReplicant) { if (!value) {
return; return;
} }
if (graphicsSettingsReplicant.data?.scoreboardSkin === value) { if (graphicsSettingsStore.settings.scoreboardSkin === value) {
return; return;
} }
graphicsSettingsReplicant.data = { graphicsSettingsStore.setScoreboardSkin(value);
scoreboardSkin: value,
};
graphicsSettingsReplicant.save();
}, },
{ immediate: true }, { immediate: true },
); );
@@ -0,0 +1,8 @@
import { sendNodecgMessage } from '../../nodecg/browser/messages';
export const sendIntegrationMessage = <T>(
messagePrefix: string,
action: string,
payload: unknown,
): Promise<T> =>
sendNodecgMessage<T>(`${messagePrefix}:${action}`, payload);
+79
View File
@@ -0,0 +1,79 @@
import { sendNodecgCommand, sendNodecgMessage } from '../../nodecg/browser/messages';
import { createPackBrowserReplicants } from '../../nodecg/browser/packReplicants';
import { messageNames } from '../../nodecg/messageNames';
import type {
PackDownloadState,
PackManifest,
PackRegistry,
PackUpdateInfo,
} from '../../shared/domain/packs/types';
export interface PackReplicantHandlers {
onRegistryChanged: (value: PackRegistry | null) => void;
onInstalledPacksChanged: (value: string[], previousValue: string[]) => void;
onDownloadStatesChanged: (value: Record<string, PackDownloadState>) => void;
onAvailableUpdatesChanged: (value: Record<string, PackUpdateInfo>) => void;
}
export interface PackService {
subscribe: (handlers: PackReplicantHandlers) => Promise<() => void>;
fetchRegistry: () => Promise<void>;
downloadPack: (packId: string) => Promise<void>;
uninstallPack: (packId: string) => Promise<void>;
updatePack: (packId: string) => Promise<void>;
readLocalManifest: (packId: string) => Promise<PackManifest>;
}
export const createPackService = (): PackService => {
const subscribe = async (handlers: PackReplicantHandlers): Promise<() => void> => {
const {
registryRep,
installedRep,
statesRep,
updatesRep,
waitUntilReady,
} = createPackBrowserReplicants();
await waitUntilReady();
handlers.onRegistryChanged(registryRep.value ?? null);
handlers.onInstalledPacksChanged(installedRep.value ?? [], []);
handlers.onDownloadStatesChanged(statesRep.value ?? {});
handlers.onAvailableUpdatesChanged(updatesRep.value ?? {});
const onRegistryChanged = (value: PackRegistry | null): void => {
handlers.onRegistryChanged(value ?? null);
};
const onInstalledPacksChanged = (value: string[], previousValue?: string[]): void => {
handlers.onInstalledPacksChanged(value ?? [], previousValue ?? []);
};
const onDownloadStatesChanged = (value: Record<string, PackDownloadState>): void => {
handlers.onDownloadStatesChanged(value ?? {});
};
const onAvailableUpdatesChanged = (value: Record<string, PackUpdateInfo>): void => {
handlers.onAvailableUpdatesChanged(value ?? {});
};
registryRep.on('change', onRegistryChanged);
installedRep.on('change', onInstalledPacksChanged);
statesRep.on('change', onDownloadStatesChanged);
updatesRep.on('change', onAvailableUpdatesChanged);
return () => {
registryRep.off('change', onRegistryChanged);
installedRep.off('change', onInstalledPacksChanged);
statesRep.off('change', onDownloadStatesChanged);
updatesRep.off('change', onAvailableUpdatesChanged);
};
};
return {
subscribe,
fetchRegistry: () => sendNodecgCommand(messageNames.packs.fetchRegistry),
downloadPack: (packId: string) => sendNodecgCommand(messageNames.packs.download, packId),
uninstallPack: (packId: string) => sendNodecgCommand(messageNames.packs.uninstall, packId),
updatePack: (packId: string) => sendNodecgCommand(messageNames.packs.update, packId),
readLocalManifest: (packId: string) =>
sendNodecgMessage<PackManifest>(messageNames.packs.readLocalManifest, packId),
};
};
@@ -1,4 +1,10 @@
import { ref, watch, type Ref } from 'vue'; import { ref, watch, type Ref } from 'vue';
import { commentaryReplicant, graphicsSettingsReplicant, playersReplicant, scoreboardReplicant } from '../../nodecg/browser/replicants';
import { normalizeCommentary } from '../../shared/domain/commentary';
import { normalizeGraphicsSettings } from '../../shared/domain/graphics';
import { normalizePlayers } from '../../shared/domain/players/state';
import { normalizeScoreboard } from '../../shared/domain/scoreboard';
import type { Schemas } from '../../types';
interface ReplicantLike<T> { interface ReplicantLike<T> {
data: T | undefined; data: T | undefined;
@@ -36,7 +42,7 @@ export const writeStorageSnapshot = <T>(storageKey: string, value: T): void => {
} }
}; };
export const syncStateWithReplicant = <T>( const syncStateWithReplicant = <T>(
state: Ref<T>, state: Ref<T>,
replicant: ReplicantLike<T> | undefined, replicant: ReplicantLike<T> | undefined,
normalize: (input: unknown) => T, normalize: (input: unknown) => T,
@@ -44,24 +50,21 @@ export const syncStateWithReplicant = <T>(
): void => { ): void => {
const isApplyingReplicant = ref(false); const isApplyingReplicant = ref(false);
const persistSnapshot = (value: T): void => { const persistSnapshot = (value: T): void => {
if (!storageKey) { if (storageKey) {
return; writeStorageSnapshot(storageKey, value);
} }
writeStorageSnapshot(storageKey, value);
}; };
watch( watch(
() => replicant?.data, () => replicant?.data,
(value) => { (value) => {
if (!value) { if (value === undefined) {
return; return;
} }
isApplyingReplicant.value = true; isApplyingReplicant.value = true;
state.value = normalize(value); state.value = normalize(value);
isApplyingReplicant.value = false; isApplyingReplicant.value = false;
persistSnapshot(state.value); persistSnapshot(state.value);
}, },
{ deep: true, immediate: true }, { deep: true, immediate: true },
@@ -70,16 +73,32 @@ export const syncStateWithReplicant = <T>(
watch( watch(
state, state,
(value) => { (value) => {
persistSnapshot(value); const normalized = normalize(value);
persistSnapshot(normalized);
if (isApplyingReplicant.value || !replicant) { if (isApplyingReplicant.value || !replicant) {
return; return;
} }
// Replicants remain the source of truth for server/browser synchronization. replicant.data = normalized;
replicant.data = normalize(value);
replicant.save(); replicant.save();
}, },
{ deep: true, flush: 'sync' }, { deep: true, flush: 'sync' },
); );
}; };
export const syncScoreboardState = (state: Ref<Schemas.Scoreboard>, storageKey: string): void => {
syncStateWithReplicant(state, scoreboardReplicant, normalizeScoreboard, storageKey);
};
export const syncPlayersState = (state: Ref<Schemas.Players>, storageKey: string): void => {
syncStateWithReplicant(state, playersReplicant, normalizePlayers, storageKey);
};
export const syncCommentaryState = (state: Ref<Schemas.Commentary>): void => {
syncStateWithReplicant(state, commentaryReplicant, normalizeCommentary);
};
export const syncGraphicsSettingsState = (state: Ref<Schemas.GraphicsSettings>): void => {
syncStateWithReplicant(state, graphicsSettingsReplicant, normalizeGraphicsSettings);
};
+20 -31
View File
@@ -1,32 +1,24 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import { commentaryReplicant } from '../../nodecg/browser/replicants'; import {
import type { Schemas } from '../../types'; defaultCommentary,
import { syncStateWithReplicant } from './store-sync'; normalizeCommentary,
swapCommentary,
type Commentary = Schemas.Commentary; type Commentary,
} from '../../shared/domain/commentary';
const defaultCommentary: Commentary = { import { syncCommentaryState } from '../services/replicant-state-service';
leftCommentator: '',
leftCommentatorTwitter: '',
rightCommentator: '',
rightCommentatorTwitter: '',
};
const normalizeCommentary = (input: unknown): Commentary => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
leftCommentator: typeof candidate.leftCommentator === 'string' ? candidate.leftCommentator : '',
leftCommentatorTwitter: typeof candidate.leftCommentatorTwitter === 'string' ? candidate.leftCommentatorTwitter : '',
rightCommentator: typeof candidate.rightCommentator === 'string' ? candidate.rightCommentator : '',
rightCommentatorTwitter: typeof candidate.rightCommentatorTwitter === 'string' ? candidate.rightCommentatorTwitter : '',
};
};
export const useCommentaryStore = defineStore('commentary', () => { export const useCommentaryStore = defineStore('commentary', () => {
const commentary = ref<Commentary>({ ...defaultCommentary }); const commentary = ref<Commentary>({ ...defaultCommentary });
const replicant = commentaryReplicant; syncCommentaryState(commentary);
syncStateWithReplicant(commentary, replicant, normalizeCommentary);
const setCommentary = (value: Commentary): void => {
commentary.value = normalizeCommentary(value);
};
const clearCommentary = (): void => {
commentary.value = { ...defaultCommentary };
};
const leftCommentator = computed({ const leftCommentator = computed({
get: () => commentary.value.leftCommentator, get: () => commentary.value.leftCommentator,
@@ -68,13 +60,8 @@ export const useCommentaryStore = defineStore('commentary', () => {
}, },
}); });
const swapCommentators = () => { const swapCommentators = (): void => {
commentary.value = { commentary.value = swapCommentary(commentary.value);
leftCommentator: commentary.value.rightCommentator,
leftCommentatorTwitter: commentary.value.rightCommentatorTwitter,
rightCommentator: commentary.value.leftCommentator,
rightCommentatorTwitter: commentary.value.leftCommentatorTwitter,
};
}; };
return { return {
@@ -83,6 +70,8 @@ export const useCommentaryStore = defineStore('commentary', () => {
leftCommentatorTwitter, leftCommentatorTwitter,
rightCommentator, rightCommentator,
rightCommentatorTwitter, rightCommentatorTwitter,
setCommentary,
clearCommentary,
swapCommentators, swapCommentators,
}; };
}); });
+31
View File
@@ -0,0 +1,31 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import {
defaultGraphicsSettings,
normalizeGraphicsSettings,
type GraphicsSettings,
} from '../../shared/domain/graphics';
import { syncGraphicsSettingsState } from '../services/replicant-state-service';
export const useGraphicsSettingsStore = defineStore('graphics-settings', () => {
const settings = ref<GraphicsSettings>({ ...defaultGraphicsSettings });
syncGraphicsSettingsState(settings);
const setSettings = (value: GraphicsSettings): void => {
settings.value = normalizeGraphicsSettings(value);
};
const setScoreboardSkin = (scoreboardSkin: string): void => {
settings.value = normalizeGraphicsSettings({
...settings.value,
scoreboardSkin,
});
};
return {
settings,
setSettings,
setScoreboardSkin,
};
});
+218
View File
@@ -0,0 +1,218 @@
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { createPackService } from '../services/pack-service';
import {
buildCharactersByGame,
buildDefaultCharactersByGame,
type DefaultCharacterPair,
type FightingCharacterOption,
} from '../../shared/domain/packs/characters';
import type {
GameSelectOption,
PackDownloadState,
PackManifest,
PackRegistry,
PackUpdateInfo,
} from '../../shared/domain/packs/types';
const packService = createPackService();
const formatBytes = (bytes: number): string => {
if (bytes < 1024) {
return `${bytes} B`;
}
if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(1)} KB`;
}
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
const getLocalLogoUrl = (packId: string): string => `/packs/${packId}/logo.png`;
export const usePacksStore = defineStore('packs', () => {
const initialized = ref(false);
const registry = ref<PackRegistry | null>(null);
const installedPackIds = ref<string[]>([]);
const downloadStates = ref<Record<string, PackDownloadState>>({});
const availableUpdates = ref<Record<string, PackUpdateInfo>>({});
const installedManifests = ref<Record<string, PackManifest>>({});
const loadingManifestIds = new Set<string>();
let unsubscribe: (() => void) | null = null;
let registryRefreshTimer: ReturnType<typeof setInterval> | null = null;
const installedManifestList = computed(() =>
installedPackIds.value
.map((packId) => installedManifests.value[packId])
.filter((manifest): manifest is PackManifest => Boolean(manifest)),
);
const charactersByGame = computed(() => buildCharactersByGame(installedManifestList.value));
const defaultCharactersByGame = computed(() => buildDefaultCharactersByGame(installedManifestList.value));
const allGameOptions = computed<GameSelectOption[]>(() => {
if (!registry.value) {
return [];
}
return registry.value.packs.map((entry) => ({
label: entry.name,
value: entry.name,
available: installedPackIds.value.includes(entry.id),
registryEntry: entry,
updateInfo: availableUpdates.value[entry.id],
}));
});
const updateCount = computed(() => Object.keys(availableUpdates.value).length);
const loadInstalledManifest = (packId: string): void => {
if (installedManifests.value[packId] || loadingManifestIds.has(packId)) {
return;
}
loadingManifestIds.add(packId);
packService.readLocalManifest(packId)
.then((manifest) => {
installedManifests.value = {
...installedManifests.value,
[packId]: manifest,
};
})
.catch((error: unknown) => {
console.error(`[packs] Failed to load manifest for "${packId}":`, error);
})
.finally(() => {
loadingManifestIds.delete(packId);
});
};
const syncInstalledManifests = (nextPackIds: string[]): void => {
const nextSet = new Set(nextPackIds);
const nextManifests: Record<string, PackManifest> = {};
Object.entries(installedManifests.value).forEach(([packId, manifest]) => {
if (nextSet.has(packId)) {
nextManifests[packId] = manifest;
}
});
installedManifests.value = nextManifests;
nextPackIds.forEach(loadInstalledManifest);
};
const initialize = (): void => {
if (initialized.value) {
return;
}
initialized.value = true;
packService.subscribe({
onRegistryChanged: (value) => {
registry.value = value;
},
onInstalledPacksChanged: (value) => {
installedPackIds.value = [...value];
syncInstalledManifests(value);
},
onDownloadStatesChanged: (value) => {
downloadStates.value = { ...value };
},
onAvailableUpdatesChanged: (value) => {
availableUpdates.value = { ...value };
},
})
.then((dispose) => {
unsubscribe = dispose;
})
.catch((error: unknown) => {
initialized.value = false;
console.error('[packs] Failed to subscribe to pack replicants:', error);
});
};
const dispose = (): void => {
unsubscribe?.();
unsubscribe = null;
if (registryRefreshTimer) {
clearInterval(registryRefreshTimer);
registryRefreshTimer = null;
}
initialized.value = false;
};
const runCommand = (label: string, command: () => Promise<void>): void => {
command().catch((error: unknown) => {
console.error(`[packs] ${label} failed:`, error);
});
};
const fetchRegistry = (): void => {
runCommand('fetchRegistry', packService.fetchRegistry);
};
const startRegistryRefresh = (intervalMs = 15_000): void => {
fetchRegistry();
if (registryRefreshTimer) {
return;
}
registryRefreshTimer = setInterval(fetchRegistry, intervalMs);
};
const stopRegistryRefresh = (): void => {
if (!registryRefreshTimer) {
return;
}
clearInterval(registryRefreshTimer);
registryRefreshTimer = null;
};
const downloadPack = (packId: string): void => {
runCommand(`downloadPack "${packId}"`, () => packService.downloadPack(packId));
};
const uninstallPack = (packId: string): void => {
runCommand(`uninstallPack "${packId}"`, () => packService.uninstallPack(packId));
};
const updatePack = (packId: string): void => {
runCommand(`updatePack "${packId}"`, () => packService.updatePack(packId));
};
const isGameAvailable = (gameName: string): boolean => {
const entry = registry.value?.packs.find((pack) => pack.name === gameName);
return entry ? installedPackIds.value.includes(entry.id) : false;
};
const getDownloadState = (packId: string): PackDownloadState =>
downloadStates.value[packId] ?? { status: 'idle', progress: 0 };
const getCharactersByGame = (gameName: string): FightingCharacterOption[] =>
charactersByGame.value[gameName] ?? [];
const getDefaultCharactersByGame = (gameName: string): DefaultCharacterPair | undefined =>
defaultCharactersByGame.value[gameName];
return {
registry,
installedPackIds,
downloadStates,
availableUpdates,
installedManifests,
allGameOptions,
updateCount,
initialize,
dispose,
fetchRegistry,
startRegistryRefresh,
stopRegistryRefresh,
downloadPack,
uninstallPack,
updatePack,
isGameAvailable,
getDownloadState,
getCharactersByGame,
getDefaultCharactersByGame,
formatBytes,
getLocalLogoUrl,
};
});
+11 -36
View File
@@ -1,61 +1,36 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import { playersReplicant } from '../../nodecg/browser/replicants'; import {
import type { Schemas } from '../../types'; normalizePlayer,
import { readStorageSnapshot, syncStateWithReplicant } from './store-sync'; normalizePlayers,
type Player,
type PlayersMap = Schemas.Players; type PlayersMap,
type Player = PlayersMap[string]; } from '../../shared/domain/players/state';
import { readStorageSnapshot, syncPlayersState } from '../services/replicant-state-service';
const STORAGE_KEY = 'scoreko-dev.players'; const STORAGE_KEY = 'scoreko-dev.players';
const normalizePlayer = (input: unknown): Player => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
gamertag: typeof candidate.gamertag === 'string' ? candidate.gamertag : '',
name: typeof candidate.name === 'string' ? candidate.name : '',
team: typeof candidate.team === 'string' ? candidate.team : '',
country: typeof candidate.country === 'string' ? candidate.country : '',
twitter: typeof candidate.twitter === 'string' ? candidate.twitter : '',
};
};
const normalizePlayers = (input: unknown): PlayersMap => {
if (typeof input !== 'object' || input === null) {
return {};
}
const result: PlayersMap = {};
Object.entries(input as Record<string, unknown>).forEach(([id, value]) => {
if (!id) {
return;
}
result[id] = normalizePlayer(value);
});
return result;
};
export const usePlayersStore = defineStore('players', () => { export const usePlayersStore = defineStore('players', () => {
const players = ref<PlayersMap>({}); const players = ref<PlayersMap>({});
const replicant = playersReplicant;
const storageSnapshot = readStorageSnapshot(STORAGE_KEY, normalizePlayers); const storageSnapshot = readStorageSnapshot(STORAGE_KEY, normalizePlayers);
if (storageSnapshot) { if (storageSnapshot) {
players.value = storageSnapshot; players.value = storageSnapshot;
} }
syncStateWithReplicant(players, replicant, normalizePlayers, STORAGE_KEY); syncPlayersState(players, STORAGE_KEY);
const setPlayers = (value: PlayersMap) => { const setPlayers = (value: PlayersMap): void => {
players.value = normalizePlayers(value); players.value = normalizePlayers(value);
}; };
const upsertPlayer = (id: string, player: Player) => { const upsertPlayer = (id: string, player: Player): void => {
players.value = { players.value = {
...players.value, ...players.value,
[id]: normalizePlayer(player), [id]: normalizePlayer(player),
}; };
}; };
const removePlayer = (id: string) => { const removePlayer = (id: string): void => {
const next = { ...players.value }; const next = { ...players.value };
delete next[id]; delete next[id];
players.value = next; players.value = next;
+73 -69
View File
@@ -1,107 +1,102 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { computed, ref } from 'vue'; import { computed, ref } from 'vue';
import { scoreboardReplicant } from '../../nodecg/browser/replicants'; import {
import type { Schemas } from '../../types'; adjustScoreboardScore,
import { readStorageSnapshot, syncStateWithReplicant } from './store-sync'; defaultScoreboard,
normalizeScoreboard,
type Scoreboard = Schemas.Scoreboard; resetScoreboardScores,
setScoreboardScore,
swapScoreboardPlayers,
type Scoreboard,
type ScoreboardSide,
} from '../../shared/domain/scoreboard';
import { readStorageSnapshot, syncScoreboardState } from '../services/replicant-state-service';
const STORAGE_KEY = 'scoreko-dev.scoreboard'; const STORAGE_KEY = 'scoreko-dev.scoreboard';
const defaultScoreboard: Scoreboard = {
leftPlayerId: '',
rightPlayerId: '',
leftNameOverride: '',
rightNameOverride: '',
leftTeamOverride: '',
rightTeamOverride: '',
leftCountryOverride: '',
rightCountryOverride: '',
leftCharacter: '',
rightCharacter: '',
leftScore: 0,
rightScore: 0,
round: '',
game: '',
};
const normalizeScoreboard = (input: unknown): Scoreboard => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
leftPlayerId: typeof candidate.leftPlayerId === 'string' ? candidate.leftPlayerId : '',
rightPlayerId: typeof candidate.rightPlayerId === 'string' ? candidate.rightPlayerId : '',
leftNameOverride: typeof candidate.leftNameOverride === 'string' ? candidate.leftNameOverride : '',
rightNameOverride: typeof candidate.rightNameOverride === 'string' ? candidate.rightNameOverride : '',
leftTeamOverride: typeof candidate.leftTeamOverride === 'string' ? candidate.leftTeamOverride : '',
rightTeamOverride: typeof candidate.rightTeamOverride === 'string' ? candidate.rightTeamOverride : '',
leftCountryOverride: typeof candidate.leftCountryOverride === 'string' ? candidate.leftCountryOverride : '',
rightCountryOverride: typeof candidate.rightCountryOverride === 'string' ? candidate.rightCountryOverride : '',
leftCharacter: typeof candidate.leftCharacter === 'string' ? candidate.leftCharacter : '',
rightCharacter: typeof candidate.rightCharacter === 'string' ? candidate.rightCharacter : '',
leftScore: typeof candidate.leftScore === 'number' ? Math.max(0, Math.floor(candidate.leftScore)) : 0,
rightScore: typeof candidate.rightScore === 'number' ? Math.max(0, Math.floor(candidate.rightScore)) : 0,
round: typeof candidate.round === 'string' ? candidate.round : '',
game: typeof candidate.game === 'string' ? candidate.game : '',
};
};
export const useScoreboardStore = defineStore('scoreboard', () => { export const useScoreboardStore = defineStore('scoreboard', () => {
const scoreboard = ref<Scoreboard>({ ...defaultScoreboard }); const scoreboard = ref<Scoreboard>({ ...defaultScoreboard });
const replicant = scoreboardReplicant;
const storageSnapshot = readStorageSnapshot(STORAGE_KEY, normalizeScoreboard); const storageSnapshot = readStorageSnapshot(STORAGE_KEY, normalizeScoreboard);
if (storageSnapshot) { if (storageSnapshot) {
scoreboard.value = storageSnapshot; scoreboard.value = storageSnapshot;
} }
syncStateWithReplicant(scoreboard, replicant, normalizeScoreboard, STORAGE_KEY); syncScoreboardState(scoreboard, STORAGE_KEY);
const setScoreboard = (value: Scoreboard) => { const setScoreboard = (value: Scoreboard): void => {
scoreboard.value = normalizeScoreboard(value); scoreboard.value = normalizeScoreboard(value);
}; };
const swapPlayers = () => { const setGame = (value: string): void => {
scoreboard.value = { ...scoreboard.value, game: value };
};
const setRound = (value: string): void => {
scoreboard.value = { ...scoreboard.value, round: value };
};
const setScore = (side: ScoreboardSide, value: number): void => {
scoreboard.value = setScoreboardScore(scoreboard.value, side, value);
};
const adjustScore = (side: ScoreboardSide, delta: number): void => {
scoreboard.value = adjustScoreboardScore(scoreboard.value, side, delta);
};
const setSidePlayerId = (side: ScoreboardSide, value: string): void => {
scoreboard.value = { scoreboard.value = {
...scoreboard.value, ...scoreboard.value,
leftPlayerId: scoreboard.value.rightPlayerId, [side === 'left' ? 'leftPlayerId' : 'rightPlayerId']: value,
rightPlayerId: scoreboard.value.leftPlayerId,
leftNameOverride: scoreboard.value.rightNameOverride,
rightNameOverride: scoreboard.value.leftNameOverride,
leftTeamOverride: scoreboard.value.rightTeamOverride,
rightTeamOverride: scoreboard.value.leftTeamOverride,
leftCountryOverride: scoreboard.value.rightCountryOverride,
rightCountryOverride: scoreboard.value.leftCountryOverride,
leftCharacter: scoreboard.value.rightCharacter,
rightCharacter: scoreboard.value.leftCharacter,
leftScore: scoreboard.value.rightScore,
rightScore: scoreboard.value.leftScore,
}; };
}; };
const resetScores = () => { const setSideNameOverride = (side: ScoreboardSide, value: string): void => {
scoreboard.value = { scoreboard.value = {
...scoreboard.value, ...scoreboard.value,
leftScore: 0, [side === 'left' ? 'leftNameOverride' : 'rightNameOverride']: value,
rightScore: 0,
}; };
}; };
const setSideTeamOverride = (side: ScoreboardSide, value: string): void => {
scoreboard.value = {
...scoreboard.value,
[side === 'left' ? 'leftTeamOverride' : 'rightTeamOverride']: value,
};
};
const setSideCountryOverride = (side: ScoreboardSide, value: string): void => {
scoreboard.value = {
...scoreboard.value,
[side === 'left' ? 'leftCountryOverride' : 'rightCountryOverride']: value,
};
};
const setSideCharacter = (side: ScoreboardSide, value: string): void => {
scoreboard.value = {
...scoreboard.value,
[side === 'left' ? 'leftCharacter' : 'rightCharacter']: value,
};
};
const swapPlayers = (): void => {
scoreboard.value = swapScoreboardPlayers(scoreboard.value);
};
const resetScores = (): void => {
scoreboard.value = resetScoreboardScores(scoreboard.value);
};
const leftScore = computed({ const leftScore = computed({
get: () => scoreboard.value.leftScore, get: () => scoreboard.value.leftScore,
set: (value: number) => { set: (value: number) => {
scoreboard.value = { setScore('left', value);
...scoreboard.value,
leftScore: Math.max(0, Math.floor(value)),
};
}, },
}); });
const rightScore = computed({ const rightScore = computed({
get: () => scoreboard.value.rightScore, get: () => scoreboard.value.rightScore,
set: (value: number) => { set: (value: number) => {
scoreboard.value = { setScore('right', value);
...scoreboard.value,
rightScore: Math.max(0, Math.floor(value)),
};
}, },
}); });
@@ -110,6 +105,15 @@ export const useScoreboardStore = defineStore('scoreboard', () => {
leftScore, leftScore,
rightScore, rightScore,
setScoreboard, setScoreboard,
setGame,
setRound,
setScore,
adjustScore,
setSidePlayerId,
setSideNameOverride,
setSideTeamOverride,
setSideCountryOverride,
setSideCharacter,
swapPlayers, swapPlayers,
resetScores, resetScores,
}; };
+2 -10
View File
@@ -1,19 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { useHead } from '@unhead/vue'; import { useHead } from '@unhead/vue';
import { computed } from 'vue'; import { computed } from 'vue';
import { commentaryReplicant } from '../../nodecg/browser/replicants'; import { useCommentaryReplicatedState } from '../shared/services/replicated-state';
import type { Schemas } from '../../types';
useHead({ title: 'Commentary' }); useHead({ title: 'Commentary' });
const defaultCommentary: Schemas.Commentary = { const { commentary } = useCommentaryReplicatedState();
leftCommentator: '',
leftCommentatorTwitter: '',
rightCommentator: '',
rightCommentatorTwitter: '',
};
const commentary = computed<Schemas.Commentary>(() => commentaryReplicant?.data ?? defaultCommentary);
const leftCommentator = computed(() => commentary.value.leftCommentator || 'COMMENTATOR 1'); const leftCommentator = computed(() => commentary.value.leftCommentator || 'COMMENTATOR 1');
const rightCommentator = computed(() => commentary.value.rightCommentator || 'COMMENTATOR 2'); const rightCommentator = computed(() => commentary.value.rightCommentator || 'COMMENTATOR 2');
+2 -10
View File
@@ -1,21 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { useHead } from '@unhead/vue'; import { useHead } from '@unhead/vue';
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'; import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
import { graphicsSettingsReplicant, playersReplicant, scoreboardReplicant } from '../../nodecg/browser/replicants'; import { useScoreboardReplicatedState } from '../shared/services/replicated-state';
import { resolveCountryCode } from '../../shared/domain/players/countries'; import { resolveCountryCode } from '../../shared/domain/players/countries';
import { getCharactersByGame } from '../../shared/fighting-characters'; import { getCharactersByGame } from '../../shared/fighting-characters';
import type { Schemas } from '../../types';
useHead({ title: 'Scoreboard 2XKO' }); useHead({ title: 'Scoreboard 2XKO' });
const defaultScoreboard: Schemas.Scoreboard = { const { players, scoreboard, scoreboardSkin } = useScoreboardReplicatedState('scoreboard-2xko/main.html');
leftPlayerId: '', rightPlayerId: '', leftNameOverride: '', rightNameOverride: '', leftTeamOverride: '', rightTeamOverride: '',
leftCountryOverride: '', rightCountryOverride: '', leftCharacter: '', rightCharacter: '', leftScore: 0, rightScore: 0, round: '', game: '',
};
const players = computed<Schemas.Players>(() => playersReplicant?.data ?? {});
const scoreboard = computed<Schemas.Scoreboard>(() => scoreboardReplicant?.data ?? defaultScoreboard);
const scoreboardSkin = computed(() => graphicsSettingsReplicant?.data?.scoreboardSkin ?? 'scoreboard-2xko/main.html');
watch( watch(
scoreboardSkin, scoreboardSkin,
+2 -22
View File
@@ -1,32 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { useHead } from '@unhead/vue'; import { useHead } from '@unhead/vue';
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'; import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { graphicsSettingsReplicant, playersReplicant, scoreboardReplicant } from '../../nodecg/browser/replicants'; import { useScoreboardReplicatedState } from '../shared/services/replicated-state';
import { resolveCountryCode } from '../../shared/domain/players/countries'; import { resolveCountryCode } from '../../shared/domain/players/countries';
import type { Schemas } from '../../types';
useHead({ title: 'Scoreboard' }); useHead({ title: 'Scoreboard' });
const defaultScoreboard: Schemas.Scoreboard = { const { players, scoreboard, scoreboardSkin } = useScoreboardReplicatedState('scoreboard/main.html');
leftPlayerId: '',
rightPlayerId: '',
leftNameOverride: '',
rightNameOverride: '',
leftTeamOverride: '',
rightTeamOverride: '',
leftCountryOverride: '',
rightCountryOverride: '',
leftCharacter: '',
rightCharacter: '',
leftScore: 0,
rightScore: 0,
round: '',
game: '',
};
const players = computed<Schemas.Players>(() => playersReplicant?.data ?? {});
const scoreboard = computed<Schemas.Scoreboard>(() => scoreboardReplicant?.data ?? defaultScoreboard);
const scoreboardSkin = computed(() => graphicsSettingsReplicant?.data?.scoreboardSkin ?? 'scoreboard/main.html');
watch( watch(
scoreboardSkin, scoreboardSkin,
@@ -0,0 +1,25 @@
import { computed } from 'vue';
import { commentaryReplicant, graphicsSettingsReplicant, playersReplicant, scoreboardReplicant } from '../../../nodecg/browser/replicants';
import { defaultCommentary } from '../../../shared/domain/commentary';
import { defaultScoreboard } from '../../../shared/domain/scoreboard';
import type { Schemas } from '../../../types';
export const useScoreboardReplicatedState = (defaultSkin: string) => {
const players = computed<Schemas.Players>(() => playersReplicant?.data ?? {});
const scoreboard = computed<Schemas.Scoreboard>(() => scoreboardReplicant?.data ?? defaultScoreboard);
const scoreboardSkin = computed(() => graphicsSettingsReplicant?.data?.scoreboardSkin ?? defaultSkin);
return {
players,
scoreboard,
scoreboardSkin,
};
};
export const useCommentaryReplicatedState = () => {
const commentary = computed<Schemas.Commentary>(() => commentaryReplicant?.data ?? defaultCommentary);
return {
commentary,
};
};
+1 -1
View File
@@ -9,7 +9,7 @@ import { replicantNames } from '../replicantNames';
interface BrowserReplicant<T> { interface BrowserReplicant<T> {
value: T; value: T;
on(event: 'change', handler: (newVal: T, oldVal?: T) => void): void; on(event: 'change', handler: (newVal: T, oldVal?: T) => void): void;
off(event: string, handler: (...args: unknown[]) => void): void; off(event: 'change', handler: (newVal: T, oldVal?: T) => void): void;
} }
export interface PackBrowserReplicants { export interface PackBrowserReplicants {
+1
View File
@@ -0,0 +1 @@
export * from './state';
+32
View File
@@ -0,0 +1,32 @@
import type { Schemas } from '../../../types';
export type Commentary = Schemas.Commentary;
export const defaultCommentary: Commentary = {
leftCommentator: '',
leftCommentatorTwitter: '',
rightCommentator: '',
rightCommentatorTwitter: '',
};
const toString = (value: unknown): string => (typeof value === 'string' ? value : '');
export const normalizeCommentary = (input: unknown): Commentary => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
leftCommentator: toString(candidate.leftCommentator),
leftCommentatorTwitter: toString(candidate.leftCommentatorTwitter),
rightCommentator: toString(candidate.rightCommentator),
rightCommentatorTwitter: toString(candidate.rightCommentatorTwitter),
};
};
export const swapCommentary = (commentary: Commentary): Commentary => ({
leftCommentator: commentary.rightCommentator,
leftCommentatorTwitter: commentary.rightCommentatorTwitter,
rightCommentator: commentary.leftCommentator,
rightCommentatorTwitter: commentary.leftCommentatorTwitter,
});
export const stripTwitterPrefix = (value: string): string =>
value.startsWith('@') ? value.slice(1) : value;
+1
View File
@@ -0,0 +1 @@
export * from './state';
+16
View File
@@ -0,0 +1,16 @@
import type { Schemas } from '../../../types';
export type GraphicsSettings = Schemas.GraphicsSettings;
export const defaultGraphicsSettings: GraphicsSettings = {
scoreboardSkin: 'scoreboard/main.html',
};
export const normalizeGraphicsSettings = (input: unknown): GraphicsSettings => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
scoreboardSkin: typeof candidate.scoreboardSkin === 'string'
? candidate.scoreboardSkin
: defaultGraphicsSettings.scoreboardSkin,
};
};
+48
View File
@@ -0,0 +1,48 @@
import type { PackManifest } from './types';
export interface FightingCharacterOption {
label: string;
value: string;
image: string;
dlc?: boolean;
}
export interface DefaultCharacterPair {
leftCharacter: string;
rightCharacter: string;
}
export const BUNDLED_GAME_NAMES = new Set<string>();
export const buildCharactersForManifest = (manifest: PackManifest): FightingCharacterOption[] =>
manifest.characters.map((character) => ({
label: character.name,
value: character.slug,
image: `/packs/${manifest.id}/characters/${character.slug}.png`,
dlc: character.dlc ?? false,
}));
export const buildCharactersByGame = (
manifests: readonly PackManifest[],
): Record<string, FightingCharacterOption[]> => {
const charactersByGame: Record<string, FightingCharacterOption[]> = {};
manifests.forEach((manifest) => {
charactersByGame[manifest.name] = buildCharactersForManifest(manifest);
});
return charactersByGame;
};
export const buildDefaultCharactersByGame = (
manifests: readonly PackManifest[],
): Record<string, DefaultCharacterPair> => {
const defaultsByGame: Record<string, DefaultCharacterPair> = {};
manifests.forEach((manifest) => {
if (manifest.defaultPair) {
defaultsByGame[manifest.name] = {
leftCharacter: manifest.defaultPair.left,
rightCharacter: manifest.defaultPair.right,
};
}
});
return defaultsByGame;
};
+1
View File
@@ -1,2 +1,3 @@
export * from './config'; export * from './config';
export * from './characters';
export * from './types'; export * from './types';
+51
View File
@@ -0,0 +1,51 @@
import type { Schemas } from '../../../types';
export type PlayersMap = Schemas.Players;
export type Player = PlayersMap[string];
const toString = (value: unknown): string => (typeof value === 'string' ? value : '');
export const normalizePlayer = (input: unknown): Player => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
gamertag: toString(candidate.gamertag),
name: toString(candidate.name),
team: toString(candidate.team),
country: toString(candidate.country),
twitter: toString(candidate.twitter),
};
};
export const normalizePlayers = (input: unknown): PlayersMap => {
if (typeof input !== 'object' || input === null) {
return {};
}
const result: PlayersMap = {};
Object.entries(input as Record<string, unknown>).forEach(([id, value]) => {
if (id) {
result[id] = normalizePlayer(value);
}
});
return result;
};
export const normalizePlayerName = (value: string): string => value.trim().toLowerCase();
export const createPlayerId = (name: string, players: PlayersMap): string => {
const base = name
.trim()
.toLowerCase()
.normalize('NFD')
.replace(/[^\w\s-]/g, '')
.replace(/[\u0300-\u036f]/g, '')
.replace(/\s+/g, '-') || 'player';
let index = 1;
let candidate = base;
while (players[candidate]) {
index += 1;
candidate = `${base}-${index}`;
}
return candidate;
};
+1
View File
@@ -0,0 +1 @@
export * from './state';
+88
View File
@@ -0,0 +1,88 @@
import type { Schemas } from '../../../types';
export type Scoreboard = Schemas.Scoreboard;
export type ScoreboardSide = 'left' | 'right';
export const defaultScoreboard: Scoreboard = {
leftPlayerId: '',
rightPlayerId: '',
leftNameOverride: '',
rightNameOverride: '',
leftTeamOverride: '',
rightTeamOverride: '',
leftCountryOverride: '',
rightCountryOverride: '',
leftCharacter: '',
rightCharacter: '',
leftScore: 0,
rightScore: 0,
round: '',
game: '',
};
const toString = (value: unknown): string => (typeof value === 'string' ? value : '');
const normalizeScore = (value: unknown): number =>
typeof value === 'number' ? Math.max(0, Math.floor(value)) : 0;
export const normalizeScoreboard = (input: unknown): Scoreboard => {
const candidate = typeof input === 'object' && input !== null ? (input as Record<string, unknown>) : {};
return {
leftPlayerId: toString(candidate.leftPlayerId),
rightPlayerId: toString(candidate.rightPlayerId),
leftNameOverride: toString(candidate.leftNameOverride),
rightNameOverride: toString(candidate.rightNameOverride),
leftTeamOverride: toString(candidate.leftTeamOverride),
rightTeamOverride: toString(candidate.rightTeamOverride),
leftCountryOverride: toString(candidate.leftCountryOverride),
rightCountryOverride: toString(candidate.rightCountryOverride),
leftCharacter: toString(candidate.leftCharacter),
rightCharacter: toString(candidate.rightCharacter),
leftScore: normalizeScore(candidate.leftScore),
rightScore: normalizeScore(candidate.rightScore),
round: toString(candidate.round),
game: toString(candidate.game),
};
};
export const setScoreboardScore = (
scoreboard: Scoreboard,
side: ScoreboardSide,
value: number,
): Scoreboard => ({
...scoreboard,
[side === 'left' ? 'leftScore' : 'rightScore']: Math.max(0, Math.floor(value)),
});
export const adjustScoreboardScore = (
scoreboard: Scoreboard,
side: ScoreboardSide,
delta: number,
): Scoreboard =>
setScoreboardScore(
scoreboard,
side,
(side === 'left' ? scoreboard.leftScore : scoreboard.rightScore) + delta,
);
export const swapScoreboardPlayers = (scoreboard: Scoreboard): Scoreboard => ({
...scoreboard,
leftPlayerId: scoreboard.rightPlayerId,
rightPlayerId: scoreboard.leftPlayerId,
leftNameOverride: scoreboard.rightNameOverride,
rightNameOverride: scoreboard.leftNameOverride,
leftTeamOverride: scoreboard.rightTeamOverride,
rightTeamOverride: scoreboard.leftTeamOverride,
leftCountryOverride: scoreboard.rightCountryOverride,
rightCountryOverride: scoreboard.leftCountryOverride,
leftCharacter: scoreboard.rightCharacter,
rightCharacter: scoreboard.leftCharacter,
leftScore: scoreboard.rightScore,
rightScore: scoreboard.leftScore,
});
export const resetScoreboardScores = (scoreboard: Scoreboard): Scoreboard => ({
...scoreboard,
leftScore: 0,
rightScore: 0,
});
+11 -111
View File
@@ -1,112 +1,12 @@
// src/shared/fighting-characters.ts export {
// ───────────────────────────────────────────────────────────────────────────── BUNDLED_GAME_NAMES,
// Todo el contenido de personajes viene de packs descargados desde Gitea. buildCharactersByGame,
// No hay datos bundled — el proyecto arranca vacío y se rellena en runtime. buildCharactersForManifest,
// ───────────────────────────────────────────────────────────────────────────── buildDefaultCharactersByGame,
type DefaultCharacterPair,
type FightingCharacterOption,
} from './domain/packs/characters';
import type { DefaultCharacterPair, FightingCharacterOption } from './domain/packs/characters';
import { ref } from 'vue'; export const getCharactersByGame = (_game?: string): FightingCharacterOption[] => [];
import type { PackManifest } from './domain/packs/types'; export const getDefaultCharactersByGame = (_game?: string): DefaultCharacterPair | undefined => undefined;
export interface FightingCharacterOption {
label: string;
value: string;
image: string;
dlc?: boolean;
}
// ── Runtime registry ──────────────────────────────────────────────────────────
const installedPackCharacters: Record<string, FightingCharacterOption[]> = {};
const installedPackDefaults: Record<string, { leftCharacter: string; rightCharacter: string }> = {};
/**
* Incrementado cada vez que se registra o elimina un pack.
* Los composables se suscriben a este ref para que Vue invalide los computed
* que dependen de installedPackCharacters (objeto plano, no reactivo).
*/
export const installedPacksRevision = ref(0);
/**
* Vacío — ya no hay juegos bundled.
* Mantenido por compatibilidad con usePackRegistry.
*/
export const BUNDLED_GAME_NAMES = new Set<string>();
// ── Placeholder SVG ───────────────────────────────────────────────────────────
const toDataUrl = (svg: string): string =>
`data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
const buildPlaceholder = (game: string, character: string, start: string, end: string): string => {
const initials = character
.split(/\s+/)
.map((p) => p[0])
.join('')
.slice(0, 2)
.toUpperCase();
const svg = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 220" role="img" aria-label="${character}">
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stop-color="${start}"/>
<stop offset="100%" stop-color="${end}"/>
</linearGradient>
</defs>
<rect width="480" height="220" fill="url(#bg)" rx="18"/>
<circle cx="90" cy="110" r="64" fill="rgba(255,255,255,0.13)"/>
<text x="90" y="130" text-anchor="middle" fill="#ffffff" font-family="Arial, sans-serif" font-size="56" font-weight="700">${initials}</text>
<text x="170" y="96" fill="#e2e8f0" font-family="Arial, sans-serif" font-size="20" font-weight="700">${game}</text>
<text x="170" y="145" fill="#ffffff" font-family="Arial, sans-serif" font-size="38" font-weight="700">${character}</text>
</svg>`.trim();
return toDataUrl(svg);
};
// ── Pack registration ─────────────────────────────────────────────────────────
/**
* Registra un pack instalado para que getCharactersByGame() lo devuelva.
* Llamado por usePackRegistry cuando carga el manifest.json local de un pack.
*/
export const registerInstalledPack = (manifest: PackManifest): void => {
const { id, name, palette, characters, defaultPair } = manifest;
installedPackCharacters[name] = characters.map((char) => ({
label: char.name,
value: char.slug,
image: `/packs/${id}/characters/${char.slug}.png`,
dlc: char.dlc ?? false,
// Fallback inline por si la imagen no se encuentra en disco
_placeholder: buildPlaceholder(name, char.name, palette.start, palette.end),
}));
if (defaultPair) {
installedPackDefaults[name] = {
leftCharacter: defaultPair.left,
rightCharacter: defaultPair.right,
};
}
installedPacksRevision.value++;
};
/**
* Elimina un pack del registro en memoria.
* Llamado por usePackRegistry cuando el usuario desinstala un pack.
*/
export const unregisterInstalledPack = (gameName: string): void => {
delete installedPackCharacters[gameName];
delete installedPackDefaults[gameName];
installedPacksRevision.value++;
};
// ── Public API ────────────────────────────────────────────────────────────────
export const getCharactersByGame = (game: string): FightingCharacterOption[] =>
installedPackCharacters[game] ?? [];
export const getDefaultCharactersByGame = (
game: string,
): { leftCharacter: string; rightCharacter: string } | undefined =>
installedPackDefaults[game];