All files StorageHelper.ts

100% Statements 49/49
100% Branches 10/10
100% Functions 17/17
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95    1x 1x                 41x 41x 25x 25x   1x 1x         25x       6x       14x 14x         14x 14x 14x       4x 3x       6x 6x 6x 1x 1x     5x 5x 4x   1x   5x       3x 3x 3x 2x 2x       6x 6x 3x 3x   1x         2x       2x       1x      
import {AgentInventory, Team} from '../../types/Types'
 
const STORAGE_KEY = 'plugin-kuku-team-inventory-teams'
const SETTINGS_KEY = 'plugin-kuku-team-inventory-settings'
 
interface Settings {
    mapDisplayMode: 'count' | 'icon'
}
 
export class StorageHelper {
 
    public loadTeams(): Team[] {
        const raw = localStorage.getItem(STORAGE_KEY)
        if (!raw) return []
        try {
            return JSON.parse(raw) as Team[]
        } catch {
            console.warn('[KuKuTeamInventory] Failed to parse teams from localStorage, resetting')
            return []
        }
    }
 
    public saveTeams(teams: Team[]): void {
        localStorage.setItem(STORAGE_KEY, JSON.stringify(teams))
    }
 
    public getTeam(teamId: string): Team | undefined {
        return this.loadTeams().find(t => t.id === teamId)
    }
 
    public createTeam(name: string): Team {
        const teams = this.loadTeams()
        const team: Team = {
            id: `team-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`,
            name,
            agents: [],
        }
        teams.push(team)
        this.saveTeams(teams)
        return team
    }
 
    public deleteTeam(teamId: string): void {
        const teams = this.loadTeams().filter(t => t.id !== teamId)
        this.saveTeams(teams)
    }
 
    public addAgentToTeam(teamId: string, agent: AgentInventory): void {
        const teams = this.loadTeams()
        const team = teams.find(t => t.id === teamId)
        if (!team) {
            console.warn(`[KuKuTeamInventory] Team not found: ${teamId}`)
            return
        }
        // Replace existing agent data if same name
        const existingIndex = team.agents.findIndex(a => a.name === agent.name)
        if (existingIndex === -1) {
            team.agents.push(agent)
        } else {
            team.agents[existingIndex] = agent
        }
        this.saveTeams(teams)
    }
 
    public removeAgent(teamId: string, agentName: string): void {
        const teams = this.loadTeams()
        const team = teams.find(t => t.id === teamId)
        if (!team) return
        team.agents = team.agents.filter(a => a.name !== agentName)
        this.saveTeams(teams)
    }
 
    public loadSettings(): Settings {
        const raw = localStorage.getItem(SETTINGS_KEY)
        if (!raw) return {mapDisplayMode: 'count'}
        try {
            return {mapDisplayMode: 'count', ...JSON.parse(raw) as Partial<Settings>}
        } catch {
            return {mapDisplayMode: 'count'}
        }
    }
 
    public saveSettings(settings: Settings): void {
        localStorage.setItem(SETTINGS_KEY, JSON.stringify(settings))
    }
 
    public loadMapDisplayMode(): 'count' | 'icon' {
        return this.loadSettings().mapDisplayMode
    }
 
    public saveMapDisplayMode(mode: 'count' | 'icon'): void {
        this.saveSettings({...this.loadSettings(), mapDisplayMode: mode})
    }
}