From 428f2706c39b26fc2b8421027c814fb98edbbf4e Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 26 Mar 2022 23:44:01 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B5=9C=EA=B7=BC=20=EC=8B=A4=ED=96=89?= =?UTF-8?q?=ED=84=B4=EC=9D=98=20=EC=A4=91=EB=B3=B5=20=EA=B4=80=EB=A6=AC?= =?UTF-8?q?=EB=A5=BC=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/PartialReservedCommand.vue | 2 +- hwe/ts/components/ChiefReservedCommand.vue | 2 +- hwe/ts/util/StoredActionsHelper.ts | 36 +++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/hwe/ts/PartialReservedCommand.vue b/hwe/ts/PartialReservedCommand.vue index eebad327..cc4b5dcc 100644 --- a/hwe/ts/PartialReservedCommand.vue +++ b/hwe/ts/PartialReservedCommand.vue @@ -70,7 +70,7 @@
{{ action.brief }} diff --git a/hwe/ts/components/ChiefReservedCommand.vue b/hwe/ts/components/ChiefReservedCommand.vue index 92353e8e..b9d99c11 100644 --- a/hwe/ts/components/ChiefReservedCommand.vue +++ b/hwe/ts/components/ChiefReservedCommand.vue @@ -86,7 +86,7 @@
diff --git a/hwe/ts/util/StoredActionsHelper.ts b/hwe/ts/util/StoredActionsHelper.ts index 6a74f7d0..834bc79f 100644 --- a/hwe/ts/util/StoredActionsHelper.ts +++ b/hwe/ts/util/StoredActionsHelper.ts @@ -1,9 +1,9 @@ import type { TurnObj } from '@/defs'; -import { ref, watch } from 'vue'; +import { ref, watch, type Ref } from 'vue'; export class StoredActionsHelper { - public readonly recentActions = ref([]); + public readonly recentActions: Ref>; public readonly storedActions = ref(new Map()); public readonly clipboard = ref<[number[], TurnObj][] | undefined>(undefined); public readonly activatedCategory = ref(""); @@ -16,6 +16,7 @@ export class StoredActionsHelper { public readonly editModeKey: string; constructor(protected serverNick: string, protected type: 'general' | 'nation', protected mapName: string, protected unitSet: string, protected maxRecent = 10) { + this.recentActions = ref(new Map()); const typeKey = `${serverNick}_${mapName}_${unitSet}_${type}`; this.recentActionsKey = `${typeKey}RecentActions`; this.storedActionsKey = `${typeKey}StoredActions`; @@ -49,26 +50,47 @@ export class StoredActionsHelper { } loadRecentActions() { - this.recentActions.value = JSON.parse(localStorage.getItem(this.recentActionsKey) ?? '[]'); + try { + const rawRecentActions = JSON.parse(localStorage.getItem(this.recentActionsKey) ?? '[]') as TurnObj[]; + const recentActions = new Map(); + for (const action of rawRecentActions) { + const actionKey = JSON.stringify([action.action, action.arg]); + recentActions.set(actionKey, action); + } + this.recentActions.value = recentActions; + } + catch(e){ + console.log(`loadRecentActions error ${e}`); + } } pushRecentActions(action: TurnObj) { - this.recentActions.value.unshift(action); - if (this.recentActions.value.length > this.maxRecent) { - this.recentActions.value.pop(); + const actionKey = JSON.stringify([action.action, action.arg]); + if (this.recentActions.value.has(actionKey)) { + this.recentActions.value.delete(actionKey); } + else if (this.recentActions.value.size > this.maxRecent) { + const firstKey = this.recentActions.value.keys().next().value; + this.recentActions.value.delete(firstKey); + } + this.recentActions.value.set(actionKey, action); this.saveRecentActions(); } saveRecentActions() { - localStorage.setItem(this.recentActionsKey, JSON.stringify(this.recentActions.value)); + localStorage.setItem(this.recentActionsKey, JSON.stringify(Array.from(this.recentActions.value.values()))); } loadStoredActions() { + try { const rawValue: [string, [number[], TurnObj][]][] = JSON.parse( localStorage.getItem(this.storedActionsKey) ?? '[]' ); this.storedActions.value = new Map(rawValue); + } + catch(e){ + console.log(`loadStoredActions error ${e}`); + } } setStoredActions(actionKey: string, actions: [number[], TurnObj][]) {