953 lines
35 KiB
TypeScript
953 lines
35 KiB
TypeScript
import { computed, ref, watch } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { MESSAGE_MAILBOX_NATIONAL_BASE, MESSAGE_MAILBOX_PUBLIC, type MessageType } from '@sammo-ts/logic';
|
|
import type { RealtimeEvent, RealtimeReadModelChanges } from '@sammo-ts/common';
|
|
import { trpc } from '../utils/trpc';
|
|
import { useMapViewerStore } from './mapViewer';
|
|
import { useSessionStore } from './session';
|
|
import { createLatestRefreshQueue } from '../utils/latestRefreshQueue';
|
|
import { createRateLimitedRefreshQueue } from '../utils/rateLimitedRefreshQueue';
|
|
import { structurallyShare } from '../utils/structuralShare';
|
|
import { createMergedReadModelRefreshQueue, resolveDashboardRefreshPlan } from '../utils/dashboardReadModel';
|
|
|
|
const REALTIME_FULL_REFRESH_MIN_INTERVAL_MS = 5_000;
|
|
|
|
const resolveErrorMessage = (value: unknown): string => {
|
|
if (value instanceof Error) {
|
|
return value.message;
|
|
}
|
|
if (typeof value === 'string') {
|
|
return value;
|
|
}
|
|
return 'unknown_error';
|
|
};
|
|
|
|
export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
|
type GeneralContext = Awaited<ReturnType<typeof trpc.general.me.query>>;
|
|
type PresentGeneralContext = NonNullable<GeneralContext>;
|
|
type LobbyInfo = Awaited<ReturnType<typeof trpc.lobby.info.query>>;
|
|
type WorldMapResult = Awaited<ReturnType<typeof trpc.world.getMap.query>>;
|
|
type MapLayout = Awaited<ReturnType<typeof trpc.world.getMapLayout.query>>;
|
|
type CommandTable = Awaited<ReturnType<typeof trpc.turns.getCommandTable.query>>;
|
|
type MessageBundle = Awaited<ReturnType<typeof trpc.messages.getRecent.query>>;
|
|
type MessageContacts = Awaited<ReturnType<typeof trpc.messages.getContacts.query>>;
|
|
type BoardAccess = Awaited<ReturnType<typeof trpc.board.getAccess.query>>;
|
|
type ReservedTurnView = Awaited<ReturnType<typeof trpc.turns.reserved.getGeneral.query>>['turns'][number];
|
|
type RecentRecord = Awaited<ReturnType<typeof trpc.general.getRecentRecords.query>>['global'][number];
|
|
type FrontStatus = Awaited<ReturnType<typeof trpc.general.getFrontStatus.query>>;
|
|
|
|
const loading = ref(false);
|
|
const refreshing = ref(false);
|
|
const error = ref<string | null>(null);
|
|
const recordsError = ref<string | null>(null);
|
|
const frontStatusError = ref<string | null>(null);
|
|
const realtimeEnabled = ref(true);
|
|
const realtimeStatus = ref<'idle' | 'connected' | 'paused'>('idle');
|
|
const realtimeActive = ref(false);
|
|
|
|
const general = ref<PresentGeneralContext['general'] | null>(null);
|
|
const city = ref<PresentGeneralContext['city'] | null>(null);
|
|
const nation = ref<PresentGeneralContext['nation'] | null>(null);
|
|
const lobbyInfo = ref<LobbyInfo | null>(null);
|
|
const worldMap = ref<WorldMapResult | null>(null);
|
|
const mapLayout = ref<MapLayout | null>(null);
|
|
const commandTable = ref<CommandTable | null>(null);
|
|
const messages = ref<MessageBundle | null>(null);
|
|
const messageContacts = ref<MessageContacts | null>(null);
|
|
const boardAccess = ref<BoardAccess | null>(null);
|
|
const reservedGeneralTurns = ref<ReservedTurnView[] | null>(null);
|
|
const reservedGeneralRevision = ref(0);
|
|
const globalRecords = ref<RecentRecord[]>([]);
|
|
const generalRecords = ref<RecentRecord[]>([]);
|
|
const worldHistory = ref<RecentRecord[]>([]);
|
|
const frontStatus = ref<FrontStatus | null>(null);
|
|
const surveyNotice = ref<NonNullable<FrontStatus['latestVote']> | null>(null);
|
|
let lastGeneralRecordId = 0;
|
|
let lastWorldHistoryId = 0;
|
|
let recordGeneralId: number | null = null;
|
|
let initialized = false;
|
|
|
|
const messageDraftText = ref('');
|
|
const targetMailbox = ref<number>(MESSAGE_MAILBOX_PUBLIC);
|
|
let initializedMailboxGeneralId: number | null = null;
|
|
|
|
const generalId = computed(() => general.value?.id ?? null);
|
|
const nationId = computed(() => nation.value?.id ?? null);
|
|
const mapViewer = useMapViewerStore();
|
|
const session = useSessionStore();
|
|
|
|
const selectedCity = computed(() => {
|
|
const layout = mapLayout.value;
|
|
const map = worldMap.value;
|
|
const selectedId = mapViewer.selectedCityId;
|
|
if (!layout || !map || !selectedId) {
|
|
return null;
|
|
}
|
|
const layoutCity = layout.cityList.find((city) => city.id === selectedId);
|
|
const mapEntry = map.cityList.find((entry) => entry[0] === selectedId);
|
|
if (!layoutCity || !mapEntry) {
|
|
return null;
|
|
}
|
|
const [, , state, nationIdValue, region, supplyFlag] = mapEntry;
|
|
const nationEntry = map.nationList.find((nationEntry) => nationEntry[0] === nationIdValue);
|
|
const regionName = layout.regionMap[region] ?? '-';
|
|
const levelName = layout.levelMap[layoutCity.level] ?? '-';
|
|
|
|
return {
|
|
id: layoutCity.id,
|
|
name: layoutCity.name,
|
|
level: layoutCity.level,
|
|
levelName,
|
|
region,
|
|
regionName,
|
|
nationId: nationIdValue,
|
|
nationName: nationEntry?.[1] ?? '무주',
|
|
nationColor: nationEntry?.[2] ?? '#444444',
|
|
state,
|
|
supply: supplyFlag > 0,
|
|
isCapital: nationEntry?.[3] === layoutCity.id,
|
|
isMyCity: map.myCity === layoutCity.id,
|
|
} as const;
|
|
});
|
|
|
|
const mailboxGroups = computed(() => {
|
|
type MailboxOption = {
|
|
label: string;
|
|
value: number;
|
|
disabled?: boolean;
|
|
color?: string;
|
|
};
|
|
type MailboxGroup = {
|
|
label: string;
|
|
color?: string;
|
|
options: MailboxOption[];
|
|
};
|
|
|
|
const ownNationId = nationId.value ?? 0;
|
|
const ownMailbox = MESSAGE_MAILBOX_NATIONAL_BASE + ownNationId;
|
|
const permission = messages.value?.permission ?? -1;
|
|
const contacts = messageContacts.value?.nation ?? [];
|
|
const ownNation = contacts.find((nation) => nation.mailbox === ownMailbox);
|
|
const groups: MailboxGroup[] = [
|
|
{
|
|
label: '즐겨찾기',
|
|
color: '#000000',
|
|
options: [
|
|
{
|
|
label: '【 아국 메세지 】',
|
|
value: ownMailbox,
|
|
color: ownNation?.color ?? '#000000',
|
|
},
|
|
{
|
|
label: '【 전체 메세지 】',
|
|
value: MESSAGE_MAILBOX_PUBLIC,
|
|
color: '#000000',
|
|
},
|
|
],
|
|
},
|
|
];
|
|
|
|
if (permission >= 4) {
|
|
groups.push({
|
|
label: '외교메시지',
|
|
color: '#000000',
|
|
options: contacts
|
|
.filter((nation) => nation.mailbox !== ownMailbox && nation.nationId > 0)
|
|
.map((nation) => ({
|
|
label: nation.name,
|
|
value: nation.mailbox,
|
|
color: nation.color,
|
|
})),
|
|
});
|
|
}
|
|
|
|
const sortedContacts = [...contacts].sort((left, right) => {
|
|
if (left.mailbox === ownMailbox) return -1;
|
|
if (right.mailbox === ownMailbox) return 1;
|
|
return left.mailbox - right.mailbox;
|
|
});
|
|
for (const nation of sortedContacts) {
|
|
const options = [...nation.general]
|
|
.filter(([id]) => id !== generalId.value)
|
|
.sort((left, right) => left[1].localeCompare(right[1], 'ko'))
|
|
.map(([id, name, flags]) => {
|
|
const ruler = Boolean(flags & 1);
|
|
const ambassador = Boolean(flags & 4);
|
|
return {
|
|
label: ruler ? `*${name}*` : ambassador ? `#${name}#` : name,
|
|
value: id,
|
|
disabled: permission === 4 && ambassador && nation.mailbox !== ownMailbox,
|
|
color: nation.color,
|
|
};
|
|
});
|
|
if (options.length > 0) {
|
|
groups.push({
|
|
label: nation.name,
|
|
color: nation.color,
|
|
options,
|
|
});
|
|
}
|
|
}
|
|
return groups;
|
|
});
|
|
|
|
const statusLine = computed(() => {
|
|
if (!lobbyInfo.value) {
|
|
return '상태 정보를 불러오는 중';
|
|
}
|
|
return `${lobbyInfo.value.year}년 ${lobbyInfo.value.month}월 · 턴 ${lobbyInfo.value.turnTerm}분`;
|
|
});
|
|
|
|
const realtimeLabel = computed(() => {
|
|
if (!realtimeEnabled.value) {
|
|
return '끔';
|
|
}
|
|
return realtimeStatus.value === 'connected' ? '연결됨' : '대기중';
|
|
});
|
|
|
|
const setRealtimeEnabled = (enabled: boolean) => {
|
|
realtimeEnabled.value = enabled;
|
|
if (!enabled) {
|
|
realtimeStatus.value = 'paused';
|
|
}
|
|
};
|
|
|
|
const updateFrontStatus = (nextStatus: FrontStatus) => {
|
|
frontStatus.value = structurallyShare(frontStatus.value, nextStatus);
|
|
const latestVote = nextStatus.latestVote;
|
|
if (!latestVote || latestVote.hasVoted || typeof window === 'undefined') {
|
|
surveyNotice.value = null;
|
|
return;
|
|
}
|
|
const serverId = session.profile?.split(':', 1)[0] ?? 'game';
|
|
const storageKey = `state.${serverId}.lastVote`;
|
|
const lastSeenVoteId = Number.parseInt(window.localStorage.getItem(storageKey) ?? '0', 10);
|
|
if (latestVote.id <= (Number.isFinite(lastSeenVoteId) ? lastSeenVoteId : 0)) {
|
|
surveyNotice.value = null;
|
|
return;
|
|
}
|
|
window.localStorage.setItem(storageKey, latestVote.id.toString());
|
|
surveyNotice.value = latestVote;
|
|
};
|
|
|
|
const dismissSurveyNotice = () => {
|
|
surveyNotice.value = null;
|
|
};
|
|
|
|
const mergeRecentRecords = (current: RecentRecord[], incoming: RecentRecord[]): RecentRecord[] => {
|
|
const merged = new Map(current.map((entry) => [entry.id, entry]));
|
|
for (const entry of incoming) {
|
|
merged.set(entry.id, entry);
|
|
}
|
|
return [...merged.values()].sort((left, right) => right.id - left.id).slice(0, 15);
|
|
};
|
|
|
|
const resetRecentRecords = (id: number | null) => {
|
|
globalRecords.value = [];
|
|
generalRecords.value = [];
|
|
worldHistory.value = [];
|
|
lastGeneralRecordId = 0;
|
|
lastWorldHistoryId = 0;
|
|
recordGeneralId = id;
|
|
frontStatus.value = null;
|
|
surveyNotice.value = null;
|
|
};
|
|
|
|
const applyRecentRecords = (
|
|
records: Awaited<ReturnType<typeof trpc.general.getRecentRecords.query>>
|
|
) => {
|
|
globalRecords.value = structurallyShare(
|
|
globalRecords.value,
|
|
mergeRecentRecords(globalRecords.value, records.global)
|
|
);
|
|
generalRecords.value = structurallyShare(
|
|
generalRecords.value,
|
|
mergeRecentRecords(generalRecords.value, records.general)
|
|
);
|
|
worldHistory.value = structurallyShare(
|
|
worldHistory.value,
|
|
mergeRecentRecords(worldHistory.value, records.history)
|
|
);
|
|
lastGeneralRecordId = Math.max(
|
|
lastGeneralRecordId,
|
|
records.global[0]?.id ?? 0,
|
|
records.general[0]?.id ?? 0
|
|
);
|
|
lastWorldHistoryId = Math.max(lastWorldHistoryId, records.history[0]?.id ?? 0);
|
|
};
|
|
|
|
const refreshMainData = async () => {
|
|
const isInitialLoad = !initialized;
|
|
if (isInitialLoad) {
|
|
loading.value = true;
|
|
} else {
|
|
refreshing.value = true;
|
|
}
|
|
error.value = null;
|
|
recordsError.value = null;
|
|
frontStatusError.value = null;
|
|
|
|
try {
|
|
const context = await trpc.general.me.query();
|
|
|
|
if (!context) {
|
|
general.value = null;
|
|
city.value = null;
|
|
nation.value = null;
|
|
reservedGeneralTurns.value = null;
|
|
reservedGeneralRevision.value = 0;
|
|
boardAccess.value = null;
|
|
resetRecentRecords(null);
|
|
initialized = true;
|
|
return;
|
|
}
|
|
|
|
const id = context.general.id;
|
|
if (recordGeneralId !== id) {
|
|
resetRecentRecords(id);
|
|
}
|
|
const layoutPromise = mapLayout.value ? Promise.resolve(mapLayout.value) : trpc.world.getMapLayout.query();
|
|
const generalTurnsPromise = trpc.turns.reserved.getGeneral.query({ generalId: id });
|
|
const recordsPromise = trpc.general.getRecentRecords
|
|
.query({
|
|
lastGeneralRecordId,
|
|
lastWorldHistoryId,
|
|
})
|
|
.catch((err: unknown) => {
|
|
recordsError.value = resolveErrorMessage(err);
|
|
return null;
|
|
});
|
|
const frontStatusPromise = trpc.general.getFrontStatus.query().catch((err: unknown) => {
|
|
frontStatusError.value = resolveErrorMessage(err);
|
|
return null;
|
|
});
|
|
const [
|
|
layout,
|
|
lobby,
|
|
map,
|
|
commands,
|
|
messageData,
|
|
contacts,
|
|
access,
|
|
generalTurns,
|
|
records,
|
|
nextFrontStatus,
|
|
] = await Promise.all([
|
|
layoutPromise,
|
|
trpc.lobby.info.query(),
|
|
trpc.world.getMap.query({ generalId: id, showMe: true, useCache: true }),
|
|
trpc.turns.getCommandTable.query({ generalId: id }),
|
|
trpc.messages.getRecent.query({ generalId: id }),
|
|
trpc.messages.getContacts.query({ generalId: id }),
|
|
trpc.board.getAccess.query(),
|
|
generalTurnsPromise,
|
|
recordsPromise,
|
|
frontStatusPromise,
|
|
]);
|
|
|
|
general.value = structurallyShare(general.value, context.general);
|
|
city.value = structurallyShare(city.value, context.city);
|
|
nation.value = structurallyShare(nation.value, context.nation);
|
|
mapLayout.value = structurallyShare(mapLayout.value, layout);
|
|
lobbyInfo.value = structurallyShare(lobbyInfo.value, lobby);
|
|
worldMap.value = structurallyShare(worldMap.value, map);
|
|
commandTable.value = structurallyShare(commandTable.value, commands);
|
|
messages.value = structurallyShare(messages.value, messageData);
|
|
messageContacts.value = structurallyShare(messageContacts.value, contacts);
|
|
boardAccess.value = structurallyShare(boardAccess.value, access);
|
|
reservedGeneralTurns.value = structurallyShare<unknown>(
|
|
reservedGeneralTurns.value,
|
|
generalTurns.turns
|
|
) as ReservedTurnView[];
|
|
reservedGeneralRevision.value = generalTurns.revision;
|
|
if (records) {
|
|
applyRecentRecords(records);
|
|
}
|
|
if (nextFrontStatus) {
|
|
updateFrontStatus(nextFrontStatus);
|
|
}
|
|
if (initializedMailboxGeneralId !== id) {
|
|
targetMailbox.value = MESSAGE_MAILBOX_NATIONAL_BASE + context.general.nationId;
|
|
initializedMailboxGeneralId = id;
|
|
}
|
|
initialized = true;
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
} finally {
|
|
if (isInitialLoad) {
|
|
loading.value = false;
|
|
} else {
|
|
refreshing.value = false;
|
|
}
|
|
}
|
|
};
|
|
|
|
const refreshQueue = createLatestRefreshQueue(refreshMainData);
|
|
const loadMainData = () => refreshQueue.request();
|
|
const realtimeRefreshQueue = createRateLimitedRefreshQueue(() => refreshQueue.request(), {
|
|
minIntervalMs: REALTIME_FULL_REFRESH_MIN_INTERVAL_MS,
|
|
});
|
|
|
|
const refreshChangedReadModels = async (changes: RealtimeReadModelChanges) => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
const plan = resolveDashboardRefreshPlan(changes, {
|
|
generalId: id,
|
|
cityId: city.value?.id ?? null,
|
|
nationId: nation.value?.id ?? null,
|
|
});
|
|
if (!Object.values(plan).some(Boolean)) {
|
|
return;
|
|
}
|
|
|
|
refreshing.value = true;
|
|
error.value = null;
|
|
if (plan.records) recordsError.value = null;
|
|
if (plan.frontStatus) frontStatusError.value = null;
|
|
try {
|
|
const contextPromise = plan.context
|
|
? trpc.general.me.query()
|
|
: Promise.resolve(undefined as GeneralContext | undefined);
|
|
const lobbyPromise = plan.lobby ? trpc.lobby.info.query() : Promise.resolve(undefined);
|
|
const mapPromise = plan.map
|
|
? trpc.world.getMap.query({ generalId: id, showMe: true, useCache: true })
|
|
: Promise.resolve(undefined);
|
|
const commandsPromise = plan.commands
|
|
? trpc.turns.getCommandTable.query({ generalId: id })
|
|
: Promise.resolve(undefined);
|
|
const contactsPromise = plan.contacts
|
|
? trpc.messages.getContacts.query({ generalId: id })
|
|
: Promise.resolve(undefined);
|
|
const boardPromise = plan.boardAccess ? trpc.board.getAccess.query() : Promise.resolve(undefined);
|
|
const reservedPromise = plan.reservedTurns
|
|
? trpc.turns.reserved.getGeneral.query({ generalId: id })
|
|
: Promise.resolve(undefined);
|
|
const recordsPromise = plan.records
|
|
? trpc.general.getRecentRecords
|
|
.query({ lastGeneralRecordId, lastWorldHistoryId })
|
|
.catch((err: unknown) => {
|
|
recordsError.value = resolveErrorMessage(err);
|
|
return null;
|
|
})
|
|
: Promise.resolve(undefined);
|
|
const frontPromise = plan.frontStatus
|
|
? trpc.general.getFrontStatus.query().catch((err: unknown) => {
|
|
frontStatusError.value = resolveErrorMessage(err);
|
|
return null;
|
|
})
|
|
: Promise.resolve(undefined);
|
|
|
|
const [context, lobby, map, commands, contacts, access, generalTurns, records, nextFrontStatus] =
|
|
await Promise.all([
|
|
contextPromise,
|
|
lobbyPromise,
|
|
mapPromise,
|
|
commandsPromise,
|
|
contactsPromise,
|
|
boardPromise,
|
|
reservedPromise,
|
|
recordsPromise,
|
|
frontPromise,
|
|
]);
|
|
|
|
if (context === null) {
|
|
general.value = null;
|
|
city.value = null;
|
|
nation.value = null;
|
|
reservedGeneralTurns.value = null;
|
|
reservedGeneralRevision.value = 0;
|
|
boardAccess.value = null;
|
|
resetRecentRecords(null);
|
|
return;
|
|
}
|
|
if (context !== undefined) {
|
|
general.value = structurallyShare(general.value, context.general);
|
|
city.value = structurallyShare(city.value, context.city);
|
|
nation.value = structurallyShare(nation.value, context.nation);
|
|
}
|
|
if (lobby !== undefined) lobbyInfo.value = structurallyShare(lobbyInfo.value, lobby);
|
|
if (map !== undefined) worldMap.value = structurallyShare(worldMap.value, map);
|
|
if (commands !== undefined) commandTable.value = structurallyShare(commandTable.value, commands);
|
|
if (contacts !== undefined) messageContacts.value = structurallyShare(messageContacts.value, contacts);
|
|
if (access !== undefined) boardAccess.value = structurallyShare(boardAccess.value, access);
|
|
if (generalTurns !== undefined) {
|
|
reservedGeneralTurns.value = structurallyShare<unknown>(
|
|
reservedGeneralTurns.value,
|
|
generalTurns.turns
|
|
) as ReservedTurnView[];
|
|
reservedGeneralRevision.value = generalTurns.revision;
|
|
}
|
|
if (records) applyRecentRecords(records);
|
|
if (nextFrontStatus) updateFrontStatus(nextFrontStatus);
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
} finally {
|
|
refreshing.value = false;
|
|
}
|
|
};
|
|
|
|
const readModelRefreshQueue = createMergedReadModelRefreshQueue(refreshChangedReadModels);
|
|
|
|
const refreshMessages = async () => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
try {
|
|
messages.value = structurallyShare(messages.value, await trpc.messages.getRecent.query({ generalId: id }));
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
}
|
|
};
|
|
|
|
const sendMessage = async () => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
const mailbox = targetMailbox.value;
|
|
const text = messageDraftText.value.trim();
|
|
if (!text) {
|
|
return;
|
|
}
|
|
if (mailbox <= 0) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
messageDraftText.value = '';
|
|
await trpc.messages.send.mutate({
|
|
generalId: id,
|
|
mailbox,
|
|
text,
|
|
});
|
|
await refreshMessages();
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
}
|
|
};
|
|
|
|
const loadOlderMessages = async (type: MessageType) => {
|
|
const id = generalId.value;
|
|
if (!id || !messages.value) {
|
|
return;
|
|
}
|
|
|
|
const bucket = messages.value[type] ?? [];
|
|
const oldest = bucket[bucket.length - 1];
|
|
if (!oldest) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const older = await trpc.messages.getOld.query({
|
|
generalId: id,
|
|
type,
|
|
to: oldest.id,
|
|
});
|
|
const merged = {
|
|
...messages.value,
|
|
[type]: [...bucket, ...older[type]],
|
|
} as MessageBundle;
|
|
messages.value = merged;
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
}
|
|
};
|
|
|
|
const respondToMessage = async (messageId: number, response: boolean) => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
try {
|
|
const result = await trpc.messages.respond.mutate({
|
|
generalId: id,
|
|
messageId,
|
|
response,
|
|
});
|
|
if (!result.result) {
|
|
error.value = result.reason;
|
|
}
|
|
await refreshMessages();
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
}
|
|
};
|
|
|
|
const readLatestMessage = async (type: 'private' | 'diplomacy', messageId: number) => {
|
|
const id = generalId.value;
|
|
if (!id || messageId <= 0) {
|
|
return;
|
|
}
|
|
try {
|
|
await trpc.messages.readLatest.mutate({
|
|
generalId: id,
|
|
type,
|
|
messageId,
|
|
});
|
|
if (messages.value) {
|
|
messages.value = {
|
|
...messages.value,
|
|
latestRead: {
|
|
...messages.value.latestRead,
|
|
[type]: Math.max(messages.value.latestRead[type], messageId),
|
|
},
|
|
};
|
|
}
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
}
|
|
};
|
|
|
|
const deleteMessage = async (messageId: number) => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
try {
|
|
await trpc.messages.delete.mutate({ generalId: id, messageId });
|
|
await refreshMessages();
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
}
|
|
};
|
|
|
|
const setGeneralTurn = async (turnIndex: number, action: string, args: Record<string, unknown> = {}) => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
try {
|
|
const result = await trpc.turns.reserved.setGeneral.mutate({
|
|
generalId: id,
|
|
turnIndex,
|
|
action,
|
|
args,
|
|
expectedRevision: reservedGeneralRevision.value,
|
|
});
|
|
reservedGeneralTurns.value = result.turns;
|
|
reservedGeneralRevision.value = result.revision;
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
const snapshot = await trpc.turns.reserved.getGeneral.query({ generalId: id }).catch(() => null);
|
|
if (snapshot) {
|
|
reservedGeneralTurns.value = snapshot.turns;
|
|
reservedGeneralRevision.value = snapshot.revision;
|
|
}
|
|
}
|
|
};
|
|
|
|
const shiftGeneralTurns = async (amount: number) => {
|
|
const id = generalId.value;
|
|
if (!id) {
|
|
return;
|
|
}
|
|
try {
|
|
const result = await trpc.turns.reserved.shiftGeneral.mutate({
|
|
generalId: id,
|
|
amount,
|
|
expectedRevision: reservedGeneralRevision.value,
|
|
});
|
|
reservedGeneralTurns.value = result.turns;
|
|
reservedGeneralRevision.value = result.revision;
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
const snapshot = await trpc.turns.reserved.getGeneral.query({ generalId: id }).catch(() => null);
|
|
if (snapshot) {
|
|
reservedGeneralTurns.value = snapshot.turns;
|
|
reservedGeneralRevision.value = snapshot.revision;
|
|
}
|
|
}
|
|
};
|
|
|
|
const setGeneralTurns = async (
|
|
entries: Array<{ turnList: number[]; action: string; args: Record<string, unknown> }>
|
|
) => {
|
|
const id = generalId.value;
|
|
if (!id || !entries.length) return;
|
|
try {
|
|
const result = await trpc.turns.reserved.setGeneralBulk.mutate({
|
|
generalId: id,
|
|
entries,
|
|
expectedRevision: reservedGeneralRevision.value,
|
|
});
|
|
reservedGeneralTurns.value = result.turns;
|
|
reservedGeneralRevision.value = result.revision;
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
const snapshot = await trpc.turns.reserved.getGeneral.query({ generalId: id }).catch(() => null);
|
|
if (snapshot) {
|
|
reservedGeneralTurns.value = snapshot.turns;
|
|
reservedGeneralRevision.value = snapshot.revision;
|
|
}
|
|
}
|
|
};
|
|
|
|
const repeatGeneralTurns = async (amount: number) => {
|
|
const id = generalId.value;
|
|
if (!id) return;
|
|
try {
|
|
const result = await trpc.turns.reserved.repeatGeneral.mutate({
|
|
generalId: id,
|
|
amount,
|
|
expectedRevision: reservedGeneralRevision.value,
|
|
});
|
|
reservedGeneralTurns.value = result.turns;
|
|
reservedGeneralRevision.value = result.revision;
|
|
} catch (err) {
|
|
error.value = resolveErrorMessage(err);
|
|
const snapshot = await trpc.turns.reserved.getGeneral.query({ generalId: id }).catch(() => null);
|
|
if (snapshot) {
|
|
reservedGeneralTurns.value = snapshot.turns;
|
|
reservedGeneralRevision.value = snapshot.revision;
|
|
}
|
|
}
|
|
};
|
|
|
|
let realtimeSource: EventSource | null = null;
|
|
let realtimeToken: string | null = null;
|
|
let visibilityListenerInstalled = false;
|
|
|
|
const isAccessToken = (token: string | null): boolean => Boolean(token?.startsWith('ga_'));
|
|
|
|
const buildRealtimeUrl = (token: string): string => {
|
|
const base = import.meta.env.VITE_GAME_SSE_URL ?? '/events';
|
|
const url = new URL(base, window.location.origin);
|
|
url.searchParams.set('token', token);
|
|
return url.toString();
|
|
};
|
|
|
|
const parseRealtimePayload = (raw: MessageEvent): RealtimeEvent | null => {
|
|
if (!raw.data || typeof raw.data !== 'string') {
|
|
return null;
|
|
}
|
|
try {
|
|
const parsed = JSON.parse(raw.data) as RealtimeEvent;
|
|
if (!parsed || typeof parsed !== 'object') {
|
|
return null;
|
|
}
|
|
if (typeof parsed.type !== 'string') {
|
|
return null;
|
|
}
|
|
return parsed;
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const isMailboxRelevant = (mailbox: number): boolean => {
|
|
if (mailbox === MESSAGE_MAILBOX_PUBLIC) {
|
|
return true;
|
|
}
|
|
const currentGeneralId = generalId.value;
|
|
if (currentGeneralId && mailbox === currentGeneralId) {
|
|
return true;
|
|
}
|
|
const currentNationId = nationId.value;
|
|
if (currentNationId && mailbox === MESSAGE_MAILBOX_NATIONAL_BASE + currentNationId) {
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
const closeRealtimeSource = () => {
|
|
if (!realtimeSource) {
|
|
return;
|
|
}
|
|
realtimeSource.close();
|
|
realtimeSource = null;
|
|
realtimeToken = null;
|
|
};
|
|
|
|
const ensureAccessToken = async (): Promise<string | null> => {
|
|
if (!session.gameToken) {
|
|
return null;
|
|
}
|
|
if (isAccessToken(session.gameToken)) {
|
|
return session.gameToken;
|
|
}
|
|
const exchanged = await session.exchangeGatewayToken();
|
|
if (!exchanged) {
|
|
return null;
|
|
}
|
|
return session.gameToken && isAccessToken(session.gameToken) ? session.gameToken : null;
|
|
};
|
|
|
|
const connectRealtime = async () => {
|
|
if (typeof window === 'undefined') {
|
|
return;
|
|
}
|
|
if (
|
|
!realtimeActive.value ||
|
|
document.visibilityState === 'hidden' ||
|
|
!realtimeEnabled.value ||
|
|
!session.isReady ||
|
|
!session.hasGeneral
|
|
) {
|
|
return;
|
|
}
|
|
const token = await ensureAccessToken();
|
|
if (!token) {
|
|
realtimeStatus.value = 'idle';
|
|
return;
|
|
}
|
|
if (realtimeSource && realtimeToken === token) {
|
|
return;
|
|
}
|
|
closeRealtimeSource();
|
|
realtimeToken = token;
|
|
realtimeStatus.value = 'idle';
|
|
|
|
const source = new EventSource(buildRealtimeUrl(token));
|
|
realtimeSource = source;
|
|
|
|
source.addEventListener('open', () => {
|
|
realtimeStatus.value = 'connected';
|
|
});
|
|
source.addEventListener('error', () => {
|
|
realtimeStatus.value = realtimeEnabled.value ? 'idle' : 'paused';
|
|
});
|
|
source.addEventListener('turnCompleted', (event) => {
|
|
const payload = parseRealtimePayload(event);
|
|
if (!payload || payload.type !== 'turnCompleted') {
|
|
return;
|
|
}
|
|
if (!payload.changes) {
|
|
// Rolling deployment fallback for an older daemon.
|
|
realtimeRefreshQueue.request();
|
|
return;
|
|
}
|
|
readModelRefreshQueue.request(payload.changes);
|
|
});
|
|
source.addEventListener('readModelChanged', (event) => {
|
|
const payload = parseRealtimePayload(event);
|
|
if (!payload || payload.type !== 'readModelChanged') {
|
|
return;
|
|
}
|
|
readModelRefreshQueue.request(payload.changes);
|
|
});
|
|
source.addEventListener('messageCreated', (event) => {
|
|
const payload = parseRealtimePayload(event);
|
|
if (!payload || payload.type !== 'messageCreated') {
|
|
return;
|
|
}
|
|
if (isMailboxRelevant(payload.mailbox)) {
|
|
void refreshMessages();
|
|
}
|
|
});
|
|
source.addEventListener('ping', () => {
|
|
if (realtimeEnabled.value) {
|
|
realtimeStatus.value = 'connected';
|
|
}
|
|
});
|
|
};
|
|
|
|
const handleVisibilityChange = () => {
|
|
if (!realtimeActive.value) return;
|
|
if (document.visibilityState === 'hidden') {
|
|
realtimeRefreshQueue.cancelPending();
|
|
readModelRefreshQueue.cancelPending();
|
|
closeRealtimeSource();
|
|
realtimeStatus.value = 'idle';
|
|
return;
|
|
}
|
|
realtimeRefreshQueue.beginCooldown();
|
|
void connectRealtime();
|
|
realtimeRefreshQueue.request();
|
|
};
|
|
|
|
const startRealtime = () => {
|
|
if (typeof window === 'undefined' || realtimeActive.value) return;
|
|
realtimeActive.value = true;
|
|
realtimeRefreshQueue.beginCooldown();
|
|
if (!visibilityListenerInstalled) {
|
|
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
visibilityListenerInstalled = true;
|
|
}
|
|
};
|
|
|
|
const stopRealtime = () => {
|
|
realtimeActive.value = false;
|
|
realtimeRefreshQueue.cancelPending();
|
|
readModelRefreshQueue.cancelPending();
|
|
closeRealtimeSource();
|
|
if (visibilityListenerInstalled) {
|
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
visibilityListenerInstalled = false;
|
|
}
|
|
realtimeStatus.value = realtimeEnabled.value ? 'idle' : 'paused';
|
|
};
|
|
|
|
watch(
|
|
() => [realtimeActive.value, realtimeEnabled.value, session.isReady, session.hasGeneral, session.gameToken],
|
|
([active, enabled, ready, hasGeneral]) => {
|
|
if (!active) {
|
|
closeRealtimeSource();
|
|
realtimeStatus.value = enabled ? 'idle' : 'paused';
|
|
return;
|
|
}
|
|
if (!enabled) {
|
|
closeRealtimeSource();
|
|
realtimeStatus.value = 'paused';
|
|
return;
|
|
}
|
|
if (!ready || !hasGeneral) {
|
|
closeRealtimeSource();
|
|
realtimeStatus.value = 'idle';
|
|
return;
|
|
}
|
|
void connectRealtime();
|
|
}
|
|
);
|
|
|
|
return {
|
|
loading,
|
|
refreshing,
|
|
error,
|
|
recordsError,
|
|
frontStatusError,
|
|
realtimeEnabled,
|
|
realtimeStatus,
|
|
general,
|
|
city,
|
|
nation,
|
|
lobbyInfo,
|
|
worldMap,
|
|
mapLayout,
|
|
selectedCity,
|
|
commandTable,
|
|
messages,
|
|
messageContacts,
|
|
boardAccess,
|
|
reservedGeneralTurns,
|
|
globalRecords,
|
|
generalRecords,
|
|
worldHistory,
|
|
frontStatus,
|
|
surveyNotice,
|
|
messageDraftText,
|
|
targetMailbox,
|
|
mailboxGroups,
|
|
statusLine,
|
|
realtimeLabel,
|
|
setRealtimeEnabled,
|
|
startRealtime,
|
|
stopRealtime,
|
|
dismissSurveyNotice,
|
|
loadMainData,
|
|
refreshMessages,
|
|
sendMessage,
|
|
loadOlderMessages,
|
|
respondToMessage,
|
|
readLatestMessage,
|
|
deleteMessage,
|
|
setGeneralTurn,
|
|
setGeneralTurns,
|
|
shiftGeneralTurns,
|
|
repeatGeneralTurns,
|
|
};
|
|
});
|