refactor: store state

This commit is contained in:
pa
2025-10-11 15:30:44 +09:00
committed by Natsumi
parent 1e18d89b61
commit 86f7847c46
31 changed files with 2719 additions and 4029 deletions

View File

@@ -1,6 +1,6 @@
import dayjs from 'dayjs';
import { defineStore } from 'pinia';
import { computed, reactive, watch } from 'vue';
import { reactive, watch, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import * as workerTimers from 'worker-timers';
import { userRequest } from '../api';
@@ -49,98 +49,57 @@ export const useGameLogStore = defineStore('GameLog', () => {
const galleryStore = useGalleryStore();
const photonStore = usePhotonStore();
const sharedFeedStore = useSharedFeedStore();
const state = reactive({
nowPlaying: {
url: '',
name: '',
length: 0,
startTime: 0,
offset: 0,
elapsed: 0,
percentage: 0,
remainingText: '',
playing: false,
thumbnailUrl: ''
},
gameLogTable: {
data: [],
loading: false,
search: '',
filter: [],
tableProps: {
stripe: true,
size: 'small',
defaultSort: {
prop: 'created_at',
order: 'descending'
}
},
pageSize: 15,
paginationProps: {
small: true,
layout: 'sizes,prev,pager,next,total',
pageSizes: [10, 15, 20, 25, 50, 100]
},
vip: false
},
gameLogSessionTable: [],
lastVideoUrl: '',
lastResourceloadUrl: '',
lastLocationAvatarList: new Map()
});
async function init() {
state.gameLogTable.filter = JSON.parse(
await configRepository.getString('VRCX_gameLogTableFilters', '[]')
);
state.gameLogTable.vip = await configRepository.getBool(
'VRCX_gameLogTableVIPFilter',
false
);
}
init();
const gameLogTable = computed({
get: () => state.gameLogTable,
set: (value) => {
state.gameLogTable = value;
}
const gameLogTable = ref({
data: [],
loading: false,
search: '',
filter: [],
tableProps: {
stripe: true,
size: 'small',
defaultSort: {
prop: 'created_at',
order: 'descending'
}
},
pageSize: 15,
paginationProps: {
small: true,
layout: 'sizes,prev,pager,next,total',
pageSizes: [10, 15, 20, 25, 50, 100]
},
vip: false
});
const gameLogSessionTable = computed({
get: () => state.gameLogSessionTable,
set: (value) => {
state.gameLogSessionTable = value;
}
const gameLogSessionTable = ref([]);
const nowPlaying = ref({
url: '',
name: '',
length: 0,
startTime: 0,
offset: 0,
elapsed: 0,
percentage: 0,
remainingText: '',
playing: false,
thumbnailUrl: ''
});
const nowPlaying = computed({
get: () => state.nowPlaying,
set: (value) => {
state.nowPlaying = value;
}
});
const lastVideoUrl = ref('');
const lastVideoUrl = computed({
get: () => state.lastVideoUrl,
set: (value) => {
state.lastVideoUrl = value;
}
});
const lastResourceloadUrl = computed({
get: () => state.lastResourceloadUrl,
set: (value) => {
state.lastResourceloadUrl = value;
}
});
const lastResourceloadUrl = ref('');
watch(
() => watchState.isLoggedIn,
(isLoggedIn) => {
state.gameLogTable.data = [];
state.gameLogSessionTable = [];
gameLogTable.value.data = [];
gameLogSessionTable.value = [];
if (isLoggedIn) {
initGameLogTable();
}
@@ -151,7 +110,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
watch(
() => watchState.isFavoritesLoaded,
(isFavoritesLoaded) => {
if (isFavoritesLoaded && state.gameLogTable.vip) {
if (isFavoritesLoaded && gameLogTable.value.vip) {
gameLogTableLookup(); // re-apply VIP filter after friends are loaded
}
}
@@ -167,8 +126,20 @@ export const useGameLogStore = defineStore('GameLog', () => {
{ flush: 'sync' }
);
async function init() {
gameLogTable.value.filter = JSON.parse(
await configRepository.getString('VRCX_gameLogTableFilters', '[]')
);
gameLogTable.value.vip = await configRepository.getBool(
'VRCX_gameLogTableVIPFilter',
false
);
}
init();
function clearNowPlaying() {
state.nowPlaying = {
nowPlaying.value = {
url: '',
name: '',
length: 0,
@@ -184,7 +155,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
}
function setNowPlaying(ctx) {
if (state.nowPlaying.url !== ctx.videoUrl) {
if (nowPlaying.value.url !== ctx.videoUrl) {
if (!ctx.userId && ctx.displayName) {
for (const ref of userStore.cachedUsers.values()) {
if (ref.displayName === ctx.displayName) {
@@ -202,7 +173,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
displayName = ` (${ctx.displayName})`;
}
const name = `${ctx.videoName}${displayName}`;
state.nowPlaying = {
nowPlaying.value = {
url: ctx.videoUrl,
name,
length: ctx.videoLength,
@@ -215,8 +186,8 @@ export const useGameLogStore = defineStore('GameLog', () => {
thumbnailUrl: ctx.thumbnailUrl
};
} else {
state.nowPlaying = {
...state.nowPlaying,
nowPlaying.value = {
...nowPlaying.value,
length: ctx.videoLength,
offset: ctx.videoPos,
elapsed: 0,
@@ -225,23 +196,23 @@ export const useGameLogStore = defineStore('GameLog', () => {
thumbnailUrl: ctx.thumbnailUrl
};
if (ctx.updatedAt && ctx.videoPos) {
state.nowPlaying.startTime =
nowPlaying.value.startTime =
Date.parse(ctx.updatedAt) / 1000 - ctx.videoPos;
} else {
state.nowPlaying.startTime =
nowPlaying.value.startTime =
Date.parse(ctx.created_at) / 1000 - ctx.videoPos;
}
}
vrStore.updateVrNowPlaying();
if (!state.nowPlaying.playing && ctx.videoLength > 0) {
state.nowPlaying.playing = true;
if (!nowPlaying.value.playing && ctx.videoLength > 0) {
nowPlaying.value.playing = true;
updateNowPlaying();
}
}
function updateNowPlaying() {
const np = state.nowPlaying;
if (!state.nowPlaying.playing) {
const np = nowPlaying.value;
if (!nowPlaying.value.playing) {
return;
}
@@ -264,7 +235,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
console.log('Loading player list from game log...');
let ctx;
let i;
const data = state.gameLogSessionTable;
const data = gameLogSessionTable.value;
if (data.length === 0) {
return;
}
@@ -362,27 +333,27 @@ export const useGameLogStore = defineStore('GameLog', () => {
async function gameLogTableLookup() {
await configRepository.setString(
'VRCX_gameLogTableFilters',
JSON.stringify(state.gameLogTable.filter)
JSON.stringify(gameLogTable.value.filter)
);
await configRepository.setBool(
'VRCX_gameLogTableVIPFilter',
state.gameLogTable.vip
gameLogTable.value.vip
);
state.gameLogTable.loading = true;
gameLogTable.value.loading = true;
let vipList = [];
if (state.gameLogTable.vip) {
if (gameLogTable.value.vip) {
vipList = Array.from(friendStore.localFavoriteFriends.values());
}
state.gameLogTable.data = await database.lookupGameLogDatabase(
state.gameLogTable.search,
state.gameLogTable.filter,
gameLogTable.value.data = await database.lookupGameLogDatabase(
gameLogTable.value.search,
gameLogTable.value.filter,
vipList
);
state.gameLogTable.loading = false;
gameLogTable.value.loading = false;
}
function addGameLog(entry) {
state.gameLogSessionTable.push(entry);
gameLogSessionTable.value.push(entry);
sharedFeedStore.updateSharedFeed(false);
if (entry.type === 'VideoPlay') {
// event time can be before last gameLog entry
@@ -391,7 +362,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
// If the VIP friend filter is enabled, logs from other friends will be ignored.
if (
state.gameLogTable.vip &&
gameLogTable.value.vip &&
!friendStore.localFavoriteFriends.has(entry.userId) &&
(entry.type === 'OnPlayerJoined' ||
entry.type === 'OnPlayerLeft' ||
@@ -412,15 +383,15 @@ export const useGameLogStore = defineStore('GameLog', () => {
return;
}
if (
state.gameLogTable.filter.length > 0 &&
!state.gameLogTable.filter.includes(entry.type)
gameLogTable.value.filter.length > 0 &&
!gameLogTable.value.filter.includes(entry.type)
) {
return;
}
if (!gameLogSearch(entry)) {
return;
}
state.gameLogTable.data.push(entry);
gameLogTable.value.data.push(entry);
sweepGameLog();
uiStore.notifyMenu('gameLog');
}
@@ -435,7 +406,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
}
function gameLogSearch(row) {
const value = state.gameLogTable.search.toUpperCase();
const value = gameLogTable.value.search.toUpperCase();
if (!value) {
return true;
}
@@ -504,7 +475,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
}
function sweepGameLog() {
const { data } = state.gameLogTable;
const { data } = gameLogTable.value;
const j = data.length;
if (j > vrcxStore.maxTableSize) {
data.splice(0, j - vrcxStore.maxTableSize);
@@ -514,14 +485,14 @@ export const useGameLogStore = defineStore('GameLog', () => {
date.setDate(date.getDate() - 1); // 24 hour limit
const limit = date.toJSON();
let i = 0;
const k = state.gameLogSessionTable.length;
while (i < k && state.gameLogSessionTable[i].created_at < limit) {
const k = gameLogSessionTable.value.length;
while (i < k && gameLogSessionTable.value[i].created_at < limit) {
++i;
}
if (i === k) {
state.gameLogSessionTable = [];
gameLogSessionTable.value = [];
} else if (i) {
state.gameLogSessionTable.splice(0, i);
gameLogSessionTable.value.splice(0, i);
}
}
@@ -668,6 +639,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
'Sort by Last Seen'
)
) {
// TODO: remove
friendStore.sortVIPFriends = true;
friendStore.sortOnlineFriends = true;
}
@@ -706,27 +678,27 @@ export const useGameLogStore = defineStore('GameLog', () => {
break;
case 'video-play':
gameLog.videoUrl = decodeURI(gameLog.videoUrl);
if (state.lastVideoUrl === gameLog.videoUrl) {
if (lastVideoUrl.value === gameLog.videoUrl) {
break;
}
state.lastVideoUrl = gameLog.videoUrl;
lastVideoUrl.value = gameLog.videoUrl;
addGameLogVideo(gameLog, location, userId);
break;
case 'video-sync':
const timestamp = gameLog.timestamp.replace(/,/g, '');
if (state.nowPlaying.playing) {
state.nowPlaying.offset = parseInt(timestamp, 10);
if (nowPlaying.value.playing) {
nowPlaying.value.offset = parseInt(timestamp, 10);
}
break;
case 'resource-load-string':
case 'resource-load-image':
if (
!generalSettingsStore.logResourceLoad ||
state.lastResourceloadUrl === gameLog.resourceUrl
lastResourceloadUrl.value === gameLog.resourceUrl
) {
break;
}
state.lastResourceloadUrl = gameLog.resourceUrl;
lastResourceloadUrl.value = gameLog.resourceUrl;
entry = {
created_at: gameLog.dt,
type:
@@ -1073,7 +1045,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
if (displayName === 'Random') {
displayName = '';
}
if (videoUrl === state.nowPlaying.url) {
if (videoUrl === nowPlaying.value.url) {
const entry = {
updatedAt: gameLog.dt,
videoUrl,
@@ -1144,7 +1116,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
// ummm okay
videoPos = 0;
}
if (videoUrl === state.nowPlaying.url) {
if (videoUrl === nowPlaying.value.url) {
const entry = {
updatedAt: gameLog.dt,
videoUrl,
@@ -1210,7 +1182,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
if (videoId === '9999') {
videoId = 'YouTube';
}
if (videoUrl === state.nowPlaying.url) {
if (videoUrl === nowPlaying.value.url) {
const entry = {
updatedAt: gameLog.dt,
videoUrl,
@@ -1270,7 +1242,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
const videoName = replaceBioSymbols(data[4]);
const videoUrl = videoName;
const videoId = 'LSMedia';
if (videoUrl === state.nowPlaying.url) {
if (videoUrl === nowPlaying.value.url) {
const entry = {
updatedAt: gameLog.dt,
videoUrl,
@@ -1329,7 +1301,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
clearNowPlaying();
return;
}
if (videoUrl === state.nowPlaying.url) {
if (videoUrl === nowPlaying.value.url) {
const entry = {
updatedAt: gameLog.dt,
videoUrl,
@@ -1367,7 +1339,7 @@ export const useGameLogStore = defineStore('GameLog', () => {
async function getGameLogTable() {
await database.initTables();
state.gameLogSessionTable = await database.getGamelogDatabase();
gameLogSessionTable.value = await database.getGamelogDatabase();
const dateTill = await database.getLastDateGameLogDatabase();
updateGameLog(dateTill);
}
@@ -1430,9 +1402,9 @@ export const useGameLogStore = defineStore('GameLog', () => {
}
async function initGameLogTable() {
state.gameLogTable.data = await database.lookupGameLogDatabase(
state.gameLogTable.search,
state.gameLogTable.filter
gameLogTable.value.data = await database.lookupGameLogDatabase(
gameLogTable.value.search,
gameLogTable.value.filter
);
}