54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
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)));
|
|
}
|
|
|
|
} |