1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-23 11:08:22 +09:00
MisskeyIO/packages/frontend/src/scripts/clicker-game.ts

69 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-01-08 17:41:09 +09:00
import { ref, computed } from 'vue';
import * as os from '@/os';
type SaveData = {
gameVersion: number;
cookies: number;
2023-01-08 20:42:45 +09:00
totalCookies: number;
totalHandmadeCookies: number;
2023-01-08 17:41:09 +09:00
clicked: number;
2023-01-08 20:42:45 +09:00
achievements: any[];
facilities: any[];
2023-01-08 17:41:09 +09:00
};
export const saveData = ref<SaveData>();
export const ready = computed(() => saveData.value != null);
let prev = '';
export async function load() {
try {
saveData.value = await os.api('i/registry/get', {
scope: ['clickerGame'],
key: 'saveData',
});
} catch (err) {
if (err.code === 'NO_SUCH_KEY') {
saveData.value = {
2023-01-08 20:42:45 +09:00
gameVersion: 2,
2023-01-08 17:41:09 +09:00
cookies: 0,
2023-01-08 20:42:45 +09:00
totalCookies: 0,
totalHandmadeCookies: 0,
2023-01-08 17:41:09 +09:00
clicked: 0,
2023-01-08 20:42:45 +09:00
achievements: [],
facilities: [],
2023-01-08 17:41:09 +09:00
};
save();
return;
}
throw err;
}
2023-01-08 20:42:45 +09:00
// migration
if (saveData.value.gameVersion === 1) {
saveData.value = {
gameVersion: 2,
cookies: saveData.value.cookies,
totalCookies: saveData.value.cookies,
totalHandmadeCookies: saveData.value.cookies,
clicked: saveData.value.clicked,
achievements: [],
facilities: [],
};
save();
}
2023-01-08 17:41:09 +09:00
}
export async function save() {
const current = JSON.stringify(saveData.value);
if (current === prev) return;
await os.api('i/registry/set', {
scope: ['clickerGame'],
key: 'saveData',
value: saveData.value,
});
prev = current;
}