[+] Export maimai userdata

This commit is contained in:
Clansty
2024-08-01 06:56:31 +08:00
parent b32b0e970c
commit 7c4f887ef4
7 changed files with 76 additions and 51 deletions

View File

@@ -21,8 +21,7 @@
} }
</script> </script>
<main> <div class="fields">
<div class="fields">
{#each gameFields as field} {#each gameFields as field}
<div class="field"> <div class="field">
{#if field.type === "Boolean"} {#if field.type === "Boolean"}
@@ -37,10 +36,9 @@
{/if} {/if}
</div> </div>
{/each} {/each}
</div> </div>
<StatusOverlays {error} loading={!gameFields.length && !!submitting}/> <StatusOverlays {error} loading={!gameFields.length && !!submitting}/>
</main>
<style lang="sass"> <style lang="sass">
.fields .fields

View File

@@ -34,10 +34,26 @@
break break
} }
} }
function exportData() {
submitting = "export"
GAME.export('mai2')
.then(data => download(JSON.stringify(data), `AquaDX_maimai2_export_${values[0]}.json`))
.catch(e => error = e.message)
.finally(() => submitting = "")
}
function download(data: string, filename: string) {
const blob = new Blob([data]);
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
link.click();
}
</script> </script>
<main> <div class="fields" out:fade={FADE_OUT} in:fade={FADE_IN}>
<div class="fields" out:fade={FADE_OUT} in:fade={FADE_IN}>
{#each profileFields as [field, name], i (field)} {#each profileFields as [field, name], i (field)}
<div class="field"> <div class="field">
<label for={field}>{name}</label> <label for={field}>{name}</label>
@@ -58,10 +74,13 @@
</div> </div>
{/each} {/each}
<GameSettingFields game="mai2"/> <GameSettingFields game="mai2"/>
</div> <button class="exportButton" on:click={exportData}>
<Icon icon="bxs:file-export"/>
{t('settings.export')}
</button>
</div>
<StatusOverlays {error} loading={!values[0] || !!submitting}/> <StatusOverlays {error} loading={!values[0] || !!submitting}/>
</main>
<style lang="sass"> <style lang="sass">
.fields .fields
@@ -84,4 +103,10 @@
> input > input
flex: 1 flex: 1
.exportButton
display: flex
justify-content: center
align-items: center
gap: 5px
</style> </style>

View File

@@ -4,8 +4,6 @@
import GameSettingFields from "./GameSettingFields.svelte"; import GameSettingFields from "./GameSettingFields.svelte";
</script> </script>
<main> <div out:fade={FADE_OUT} in:fade={FADE_IN}>
<div out:fade={FADE_OUT} in:fade={FADE_IN}>
<GameSettingFields game="wacca"/> <GameSettingFields game="wacca"/>
</div> </div>
</main>

View File

@@ -141,6 +141,7 @@ export const EN_REF_SETTINGS = {
'settings.profile.bio': 'Bio', 'settings.profile.bio': 'Bio',
'settings.profile.unset': 'Unset', 'settings.profile.unset': 'Unset',
'settings.profile.unchanged': 'Unchanged', 'settings.profile.unchanged': 'Unchanged',
'settings.export': 'Export Player Data',
} }
export const EN_REF_USERBOX = { export const EN_REF_USERBOX = {

View File

@@ -150,6 +150,7 @@ const zhSettings: typeof EN_REF_SETTINGS = {
'settings.profile.bio': '简介', 'settings.profile.bio': '简介',
'settings.profile.unset': '未设置', 'settings.profile.unset': '未设置',
'settings.profile.unchanged': '未更改', 'settings.profile.unchanged': '未更改',
'settings.export': '导出玩家数据',
} }
export const ZH = { ...zhUser, ...zhWelcome, ...zhGeneral, export const ZH = { ...zhUser, ...zhWelcome, ...zhGeneral,

View File

@@ -46,7 +46,7 @@ export function fetchWithParams(input: URL | RequestInfo, init?: RequestInitWith
const cache: { [index: string]: any } = {} const cache: { [index: string]: any } = {}
export async function post(endpoint: string, params: any, init?: RequestInitWithParams): Promise<any> { export async function post(endpoint: string, params: Record<string, any> = {}, init?: RequestInitWithParams): Promise<any> {
// Add token if exists // Add token if exists
const token = localStorage.getItem('token') const token = localStorage.getItem('token')
if (token && !('token' in params)) params = { ...(params ?? {}), token } if (token && !('token' in params)) params = { ...(params ?? {}), token }
@@ -301,6 +301,8 @@ export const GAME = {
post(`/api/v2/game/${game}/ranking`, { }), post(`/api/v2/game/${game}/ranking`, { }),
changeName: (game: GameName, newName: string): Promise<{ newName: string }> => changeName: (game: GameName, newName: string): Promise<{ newName: string }> =>
post(`/api/v2/game/${game}/change-name`, { newName }), post(`/api/v2/game/${game}/change-name`, { newName }),
export: (game: GameName): Promise<Record<string, any>> =>
post(`/api/v2/game/${game}/export`),
} }
export const DATA = { export const DATA = {

View File

@@ -108,11 +108,11 @@ abstract class ImportController<ExportModel: IExportClass<UserModel>, UserModel:
// Check existing data // Check existing data
userDataRepo.findByCard(u.ghostCard)?.also { gu -> userDataRepo.findByCard(u.ghostCard)?.also { gu ->
// Store a backup of the old data // Store a backup of the old data
val fl = "mai2-backup-${u.auId}-${LocalDateTime.now().urlSafeStr()}.json" val fl = "${game}-backup-${u.auId}-${LocalDateTime.now().urlSafeStr()}.json"
(Path(netProps.importBackupPath) / fl).writeText(export(u).toJson()) (Path(netProps.importBackupPath) / fl).writeText(export(u).toJson())
// Delete the old data (After migration v1000.7, all user-linked entities have ON DELETE CASCADE) // Delete the old data (After migration v1000.7, all user-linked entities have ON DELETE CASCADE)
log.info("Mai2 Import: Deleting old data for user ${u.auId}") log.info("$game Import: Deleting old data for user ${u.auId}")
userDataRepo.delete(gu) userDataRepo.delete(gu)
userDataRepo.flush() userDataRepo.flush()
} }