2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-08 08:41:09 +00:00
|
|
|
import { ref, computed } from 'vue';
|
2024-01-04 09:32:46 +00:00
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-01-08 08:41:09 +00:00
|
|
|
|
|
|
|
type SaveData = {
|
|
|
|
gameVersion: number;
|
|
|
|
cookies: number;
|
2023-01-08 11:42:45 +00:00
|
|
|
totalCookies: number;
|
|
|
|
totalHandmadeCookies: number;
|
2023-01-08 08:41:09 +00:00
|
|
|
clicked: number;
|
2023-01-08 11:42:45 +00:00
|
|
|
achievements: any[];
|
|
|
|
facilities: any[];
|
2023-01-08 08:41:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const saveData = ref<SaveData>();
|
|
|
|
export const ready = computed(() => saveData.value != null);
|
|
|
|
|
|
|
|
let prev = '';
|
|
|
|
|
|
|
|
export async function load() {
|
|
|
|
try {
|
2024-01-04 09:32:46 +00:00
|
|
|
saveData.value = await misskeyApi('i/registry/get', {
|
2023-01-08 08:41:09 +00:00
|
|
|
scope: ['clickerGame'],
|
|
|
|
key: 'saveData',
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'NO_SUCH_KEY') {
|
|
|
|
saveData.value = {
|
2023-01-08 11:42:45 +00:00
|
|
|
gameVersion: 2,
|
2023-01-08 08:41:09 +00:00
|
|
|
cookies: 0,
|
2023-01-08 11:42:45 +00:00
|
|
|
totalCookies: 0,
|
|
|
|
totalHandmadeCookies: 0,
|
2023-01-08 08:41:09 +00:00
|
|
|
clicked: 0,
|
2023-01-08 11:42:45 +00:00
|
|
|
achievements: [],
|
|
|
|
facilities: [],
|
2023-01-08 08:41:09 +00:00
|
|
|
};
|
|
|
|
save();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
2023-01-08 11:42:45 +00: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 08:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function save() {
|
|
|
|
const current = JSON.stringify(saveData.value);
|
|
|
|
if (current === prev) return;
|
|
|
|
|
2024-01-04 09:32:46 +00:00
|
|
|
await misskeyApi('i/registry/set', {
|
2023-01-08 08:41:09 +00:00
|
|
|
scope: ['clickerGame'],
|
|
|
|
key: 'saveData',
|
|
|
|
value: saveData.value,
|
|
|
|
});
|
|
|
|
|
|
|
|
prev = current;
|
|
|
|
}
|