mirror of
https://github.com/Pandipipas/scoreko-dev.git
synced 2026-06-06 03:32:06 +00:00
refactor: architecture base
- Moved NodeCG context management to a dedicated context module. - Introduced message handling utilities for better message listening and sending. - Updated startgg integration to use new message handling methods. - Removed deprecated replicant utilities and replaced them with a new structure. - Refactored replicant imports in graphics components to align with new structure. - Added new pack-related types and schemas for better type safety. - Cleaned up unused files and consolidated pack configuration into a single module. - Updated TypeScript configurations to reflect new directory structure.
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { computed, ref } from 'vue';
|
||||
import { scoreboardReplicant } from '../../nodecg/browser/replicants';
|
||||
import type { Schemas } from '../../types';
|
||||
import { readStorageSnapshot, syncStateWithReplicant } from './store-sync';
|
||||
|
||||
type Scoreboard = Schemas.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', () => {
|
||||
const scoreboard = ref<Scoreboard>({ ...defaultScoreboard });
|
||||
const replicant = scoreboardReplicant;
|
||||
const storageSnapshot = readStorageSnapshot(STORAGE_KEY, normalizeScoreboard);
|
||||
if (storageSnapshot) {
|
||||
scoreboard.value = storageSnapshot;
|
||||
}
|
||||
|
||||
syncStateWithReplicant(scoreboard, replicant, normalizeScoreboard, STORAGE_KEY);
|
||||
|
||||
const setScoreboard = (value: Scoreboard) => {
|
||||
scoreboard.value = normalizeScoreboard(value);
|
||||
};
|
||||
|
||||
const swapPlayers = () => {
|
||||
scoreboard.value = {
|
||||
...scoreboard.value,
|
||||
leftPlayerId: scoreboard.value.rightPlayerId,
|
||||
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 = () => {
|
||||
scoreboard.value = {
|
||||
...scoreboard.value,
|
||||
leftScore: 0,
|
||||
rightScore: 0,
|
||||
};
|
||||
};
|
||||
|
||||
const leftScore = computed({
|
||||
get: () => scoreboard.value.leftScore,
|
||||
set: (value: number) => {
|
||||
scoreboard.value = {
|
||||
...scoreboard.value,
|
||||
leftScore: Math.max(0, Math.floor(value)),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const rightScore = computed({
|
||||
get: () => scoreboard.value.rightScore,
|
||||
set: (value: number) => {
|
||||
scoreboard.value = {
|
||||
...scoreboard.value,
|
||||
rightScore: Math.max(0, Math.floor(value)),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
scoreboard,
|
||||
leftScore,
|
||||
rightScore,
|
||||
setScoreboard,
|
||||
swapPlayers,
|
||||
resetScores,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user