feat(WIP): 턴 선택기 고급 확장
- 최근 예약 턴 저장
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import type { TurnObj } from '@/defs';
|
||||
import { ref } from 'vue';
|
||||
|
||||
|
||||
export class StoredActionsHelper {
|
||||
public readonly recentActions = ref<TurnObj[]>([]);
|
||||
public readonly storedActions = ref(new Map<string, Record<number, TurnObj>>());
|
||||
public readonly recentActionsKey: string;
|
||||
public readonly storedActionsKey: string;
|
||||
|
||||
constructor(protected serverNick: string, protected type: 'general'|'nation', protected maxRecent = 10) {
|
||||
this.recentActionsKey = `${serverNick}_${type}RecentActions`;
|
||||
this.storedActionsKey = `${serverNick}_${type}StoredActions`;
|
||||
this.loadRecentActions();
|
||||
this.loadStoredActions();
|
||||
}
|
||||
|
||||
loadRecentActions() {
|
||||
this.recentActions.value = JSON.parse(localStorage.getItem(this.recentActionsKey) ?? '[]');
|
||||
}
|
||||
|
||||
pushRecentActions(action: TurnObj) {
|
||||
this.recentActions.value.unshift(action);
|
||||
if(this.recentActions.value.length > this.maxRecent){
|
||||
this.recentActions.value.pop();
|
||||
}
|
||||
this.saveRecentActions();
|
||||
}
|
||||
|
||||
saveRecentActions() {
|
||||
localStorage.setItem(this.recentActionsKey, JSON.stringify(this.recentActions.value));
|
||||
}
|
||||
|
||||
loadStoredActions() {
|
||||
const rawValue: [string, Record<number, TurnObj>][] = JSON.parse(localStorage.getItem(this.storedActionsKey) ?? '[]');
|
||||
this.storedActions.value = new Map(rawValue);
|
||||
}
|
||||
|
||||
setStoredActions(actionKey: string, actions: Record<number, TurnObj>) {
|
||||
this.storedActions.value.set(actionKey, actions);
|
||||
this.saveStoredActions();
|
||||
}
|
||||
|
||||
deleteStoredActions(actionKey: string) {
|
||||
if (this.storedActions.value.delete(actionKey)) {
|
||||
this.saveStoredActions();
|
||||
}
|
||||
}
|
||||
|
||||
saveStoredActions() {
|
||||
localStorage.setItem(this.storedActionsKey, JSON.stringify(Object.entries(this.storedActions.value)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user