mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-02 13:06:08 +02:00
add game session tracking and display in status bar
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import { createPinia, setActivePinia } from 'pinia';
|
||||
|
||||
vi.mock('../../services/config.js', () => ({
|
||||
default: {
|
||||
getBool: vi.fn(),
|
||||
getString: vi.fn()
|
||||
}
|
||||
}));
|
||||
|
||||
import configRepository from '../../services/config.js';
|
||||
import { useGameStore } from '../game';
|
||||
|
||||
function flushPromises() {
|
||||
return new Promise((resolve) => setTimeout(resolve, 0));
|
||||
}
|
||||
|
||||
describe('useGameStore', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
vi.clearAllMocks();
|
||||
configRepository.getBool.mockResolvedValue(true);
|
||||
configRepository.getString.mockImplementation((key, defaultValue) => {
|
||||
if (key === 'VRCX_lastGameSessionMs') {
|
||||
return Promise.resolve('7200000');
|
||||
}
|
||||
if (key === 'VRCX_lastGameOfflineAt') {
|
||||
return Promise.resolve('1700000000000');
|
||||
}
|
||||
return Promise.resolve(defaultValue ?? null);
|
||||
});
|
||||
});
|
||||
|
||||
test('loads persisted last session data during init', async () => {
|
||||
const store = useGameStore();
|
||||
await flushPromises();
|
||||
|
||||
expect(store.isGameNoVR).toBe(true);
|
||||
expect(store.lastSessionDurationMs).toBe(7200000);
|
||||
expect(store.lastOfflineAt).toBe(1700000000000);
|
||||
});
|
||||
|
||||
test('setLastSession updates session values', () => {
|
||||
const store = useGameStore();
|
||||
|
||||
store.setLastSession(15000, 25000);
|
||||
|
||||
expect(store.lastSessionDurationMs).toBe(15000);
|
||||
expect(store.lastOfflineAt).toBe(25000);
|
||||
});
|
||||
});
|
||||
@@ -22,11 +22,21 @@ export const useGameStore = defineStore('Game', () => {
|
||||
|
||||
const isHmdAfk = ref(false);
|
||||
|
||||
const lastSessionDurationMs = ref(0);
|
||||
|
||||
const lastOfflineAt = ref(0);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
async function init() {
|
||||
isGameNoVR.value = await configRepository.getBool('isGameNoVR');
|
||||
const [savedMs, savedAt] = await Promise.all([
|
||||
configRepository.getString('VRCX_lastGameSessionMs', null),
|
||||
configRepository.getString('VRCX_lastGameOfflineAt', null)
|
||||
]);
|
||||
if (savedMs) lastSessionDurationMs.value = Number(savedMs) || 0;
|
||||
if (savedAt) lastOfflineAt.value = Number(savedAt) || 0;
|
||||
}
|
||||
|
||||
init();
|
||||
@@ -59,6 +69,15 @@ export const useGameStore = defineStore('Game', () => {
|
||||
isHmdAfk.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} durationMs Session duration in milliseconds.
|
||||
* @param {number} offlineTimestamp Timestamp when game stopped.
|
||||
*/
|
||||
function setLastSession(durationMs, offlineTimestamp) {
|
||||
lastSessionDurationMs.value = durationMs;
|
||||
lastOfflineAt.value = offlineTimestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Date | null} value Last crashed time.
|
||||
*/
|
||||
@@ -99,11 +118,14 @@ export const useGameStore = defineStore('Game', () => {
|
||||
isGameNoVR,
|
||||
isSteamVRRunning,
|
||||
isHmdAfk,
|
||||
lastSessionDurationMs,
|
||||
lastOfflineAt,
|
||||
|
||||
setIsGameRunning,
|
||||
setIsGameNoVR,
|
||||
setIsSteamVRRunning,
|
||||
setIsHmdAfk,
|
||||
setLastSession,
|
||||
setLastCrashedTime,
|
||||
getVRChatCacheSize,
|
||||
getVRChatRegistryKey
|
||||
|
||||
Reference in New Issue
Block a user