All files Dialog.ts

99.58% Statements 238/239
90.09% Branches 91/101
100% Functions 34/34
100% Lines 214/214

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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419                                                39x 39x 39x       8x   8x 1x 1x     7x   2x 2x 1x 2x     2x   1x   3x 3x 3x 2x 1x       7x 7x 7x 7x   7x 7x         7x                     2x 2x 2x 2x       5x 5x   2x 2x   2x 3x 3x 3x 3x 3x         6x 6x   3x 1x 1x     2x 1x 1x     1x     1x         1x                 4x   1x 1x 1x 1x 1x 1x   1x     1x 1x   1x 1x 1x 1x               1x 1x   1x 1x 1x 1x       1x 1x 1x   1x 4x 1x 1x 3x 1x 1x 2x 1x 1x   1x       1x 1x   1x 1x 1x   1x 1x 1x       1x 1x 1x 1x   1x   1x 4x 2x 2x 2x 1x 1x   1x 1x       1x     1x     1x   1x 1x 1x   1x 1x 1x       1x 1x   1x 1x 1x 1x       1x 1x 1x 1x   1x 2x 1x 1x   1x 1x       1x 1x   1x 1x   1x 1x 1x       1x 1x 1x     1x 1x 1x   1x 1x   1x               15x 15x 15x       18x 18x       7x 7x 7x   7x 17x 17x   17x   10x   3x 3x 3x     4x 4x 4x         18x       6x 6x 4x   4x   4x 3x 3x   2x 2x 2x   2x   2x 3x 3x   3x 4x 4x     3x           7x 7x 5x   5x 3x 3x 3x   2x 4x 4x           13x 13x 11x 11x 11x       7x   7x 7x 7x                   4x   6x 6x 6x 6x 6x 6x 6x 6x          
// @ts-expect-error "Import attributes are only supported when the --module option is set to esnext, nodenext, or preserve"
import dialogTemplate from '../tpl/dialog.hbs' with {type: 'text'}
// @ts-expect-error "Import attributes are only supported when the --module option is set to esnext, nodenext, or preserve"
import itemsImageTemplate from '../tpl/_items-image.hbs' with {type: 'text'}
// @ts-expect-error "Import attributes are only supported when the --module option is set to esnext, nodenext, or preserve"
import itemsLabelTemplate from '../tpl/_items-label.hbs' with {type: 'text'}
// @ts-expect-error "Import attributes are only supported when the --module option is set to esnext, nodenext, or preserve"
import keysTableTemplate from '../tpl/_keys-table.hbs' with {type: 'text'}
// @ts-expect-error "Import attributes are only supported when the --module option is set to esnext, nodenext, or preserve"
import agentsListTemplate from '../tpl/_agents-list.hbs' with {type: 'text'}
 
import {translateKey} from '../../types/key-translations'
import {AgentInventory, HelperHandlebars, ItemWithBreakdown, KeyInfo, Team} from '../../types/Types'
import {InventoryHelper} from './InventoryHelper'
 
export class DialogHelper {
 
    private handlebars!: HelperHandlebars
    private itemsImageTpl!: Handlebars.TemplateDelegate
    private itemsLabelTpl!: Handlebars.TemplateDelegate
    private keysTpl!: Handlebars.TemplateDelegate
    private agentsTpl!: Handlebars.TemplateDelegate
 
    public constructor(
        private readonly pluginName: string,
        private readonly title: string,
        private readonly inventoryHelper: InventoryHelper,
    ) {}
 
    public getDialog(): JQuery {
        this.handlebars = window.plugin.HelperHandlebars
 
        if (!this.handlebars) {
            alert(`${this.pluginName} - Handlebars helper not found`)
            throw new Error(`${this.pluginName} - Handlebars helper not found`)
        }
 
        this.handlebars.registerHelper({
            eachInMap: (map: Map<any, any>, block: Handlebars.HelperOptions) => {
                let out = ''
                if (map instanceof Map) {
                    for (const [key, value] of map) {
                        out += block.fn({key, value})
                    }
                }
                return out
            },
            translateKey: (key: string): string => translateKey(key),
            distanceToCenter: (lat: number, lng: number): string => {
                const center = window.map.getCenter()
                const distance = L.latLng(lat, lng).distanceTo(center)
                if (distance >= 10_000) return `${Math.round(distance / 1000)} km`
                if (distance >= 1000) return `${Math.round(distance / 100) / 10} km`
                return `${Math.round(distance)} m`
            },
        })
 
        this.itemsImageTpl = this.handlebars.compile(itemsImageTemplate)
        this.itemsLabelTpl = this.handlebars.compile(itemsLabelTemplate)
        this.keysTpl = this.handlebars.compile(keysTableTemplate)
        this.agentsTpl = this.handlebars.compile(agentsListTemplate)
 
        const mainTpl = this.handlebars.compile(dialogTemplate)
        const html = mainTpl({
            plugin: `window.plugin.${this.pluginName}`,
            prefix: this.pluginName,
        })
 
        return window.dialog({
            id: this.pluginName,
            title: this.title,
            html,
            width: 900,
            height: 700,
            buttons: [],
        }).parent()
    }
 
    public updateAll(teams: Team[], selectedTeamId: string | undefined, agents: AgentInventory[], focusedAgent?: string): void {
        this.updateTeamSelector(teams, selectedTeamId)
        const team = selectedTeamId ? (teams.find(t => t.id === selectedTeamId) ?? undefined) : undefined
        this.updateAgentsList(team, focusedAgent)
        this.updateInventoryPanels(agents)
    }
 
    public updateTeamSelector(teams: Team[], selectedTeamId: string | undefined): void {
        const select = document.getElementById(`${this.pluginName}-team-select`) as HTMLSelectElement | undefined
        if (!select) return
 
        while (select.options.length > 1) select.remove(1)
        select.options[0].selected = !selectedTeamId
 
        for (const team of teams) {
            const opt = document.createElement('option')
            opt.value = team.id
            opt.text = `${team.name} (${team.agents.length} agent${team.agents.length === 1 ? '' : 's'})`
            opt.selected = team.id === selectedTeamId
            select.add(opt)
        }
    }
 
    public updateAgentsList(team: Team | undefined, focusedAgent?: string): void {
        const container = this.getContainer('AgentsList')
        if (!container) return
 
        if (!team) {
            container.innerHTML = '<p>Select a team above to see agents.</p>'
            return
        }
 
        if (team.agents.length === 0) {
            container.innerHTML = '<p>No agents yet. Import a JSON file to add data.</p>'
            return
        }
 
        const agentData = team.agents.map(agent => ({
            name: agent.name,
            importedAt: new Date(agent.importedAt).toISOString().slice(0, 16).replace('T', ' '),
            keyCount: agent.keys.reduce((sum, k) => sum + k.total, 0),
            keyPortals: agent.keys.length,
            focused: agent.name === focusedAgent,
        }))
 
        container.innerHTML = this.agentsTpl({
            agents: agentData,
            teamId: team.id,
            plugin: `window.plugin.${this.pluginName}`,
            focusedAgent,
        })
    }
 
    public updateInventoryPanels(agents: AgentInventory[]): void {
        if (!this.itemsImageTpl) return
 
        const resonators = this.inventoryHelper.aggregateItems(agents, 'resonators')
        const weapons = this.inventoryHelper.aggregateItems(agents, 'weapons')
        const mods = this.inventoryHelper.aggregateItems(agents, 'mods')
        const cubes = this.inventoryHelper.aggregateItems(agents, 'cubes')
        const boosts = this.inventoryHelper.aggregateItems(agents, 'boosts')
        const keys = this.inventoryHelper.aggregateKeys(agents)
 
        const cntEquipment = this.processResonators(resonators)
            + this.processWeapons(weapons)
            + this.processMods(mods)
        const cntKeys = this.processKeys(keys)
        const cntOther = this.processCubes(cubes) + this.processBoosts(boosts)
 
        this.setCount('cntEquipment', cntEquipment)
        this.setCount('cntKeys', cntKeys)
        this.setCount('cntOther', cntOther)
        this.setCount('cntTotal', cntEquipment + cntKeys + cntOther)
    }
 
    // ------------------------------------------------------------------
    // Private section processors
    // ------------------------------------------------------------------
 
    private processResonators(resonators: Map<string, ItemWithBreakdown>): number {
        const sorted = this.sortByNumericSuffix(resonators)
        this.getContainer('Resonators').innerHTML = this.itemsImageTpl({items: sorted})
 
        let total = 0
        for (const item of resonators.values()) total += item.total
        this.setCount('cntResonators', total)
        return total
    }
 
    private processWeapons(weapons: Map<string, ItemWithBreakdown>): number {
        const bursters = new Map<string, ItemWithBreakdown>()
        const strikes = new Map<string, ItemWithBreakdown>()
        let cntBursters = 0, cntStrikes = 0, cntFlips = 0
 
        for (const [key, value] of weapons) {
            if (key.startsWith('EMP_BURSTER')) {
                bursters.set(key, value)
                cntBursters += value.total
            } else if (key.startsWith('ULTRA_STRIKE')) {
                strikes.set(key, value)
                cntStrikes += value.total
            } else if (key === 'ADA-0' || key === 'JARVIS-0') {
                strikes.set(key, value)
                cntFlips += value.total
            } else {
                console.warn('[KuKuTeamInventory] Unknown weapon:', key)
            }
        }
 
        this.getContainer('Bursters').innerHTML = this.itemsImageTpl({items: this.sortByNumericSuffix(bursters)})
        this.getContainer('Strikes').innerHTML = this.itemsImageTpl({items: this.sortByNumericSuffix(strikes)})
 
        this.setCount('cntBursters', cntBursters)
        this.setCount('cntStrikes', cntStrikes)
        this.setCount('cntFlips', cntFlips)
 
        const total = cntBursters + cntStrikes + cntFlips
        this.setCount('cntWeapons', total)
        return total
    }
 
    private processMods(mods: Map<string, ItemWithBreakdown>): number {
        const shields = new Map<string, ItemWithBreakdown>()
        const hackMods = new Map<string, ItemWithBreakdown>()
        const otherMods = new Map<string, ItemWithBreakdown>()
        let cntShields = 0, cntHack = 0, cntOther = 0
 
        const rarities = ['COMMON', 'RARE', 'VERY_RARE']
 
        for (const [key, value] of mods) {
            if (key.startsWith('RES_SHIELD') || key.startsWith('EXTRA_SHIELD')) {
                shields.set(key, value)
                cntShields += value.total
            } else if (key.startsWith('HEATSINK') || key.startsWith('MULTIHACK')) {
                hackMods.set(key, value)
                cntHack += value.total
            } else {
                otherMods.set(key, value)
                cntOther += value.total
            }
        }
 
        this.getContainer('Shields').innerHTML = this.itemsImageTpl({
            items: this.sortByCompoundKey(shields, ['RES_SHIELD', 'EXTRA_SHIELD'], rarities),
        })
        this.getContainer('HackMods').innerHTML = this.itemsImageTpl({
            items: this.sortByCompoundKey(hackMods, ['HEATSINK', 'MULTIHACK'], rarities),
        })
        this.getContainer('OtherMods').innerHTML = this.itemsImageTpl({items: otherMods})
 
        this.setCount('cntModShields', cntShields)
        this.setCount('cntModHack', cntHack)
        this.setCount('cntModOther', cntOther)
 
        const total = cntShields + cntHack + cntOther
        this.setCount('cntMods', total)
        return total
    }
 
    private processCubes(cubes: Map<string, ItemWithBreakdown>): number {
        const sorted = this.sortByNumericSuffix(cubes)
        this.getContainer('Cubes').innerHTML = this.itemsLabelTpl({items: sorted})
 
        let total = 0
        for (const item of cubes.values()) total += item.total
        this.setCount('cntCubes', total)
        return total
    }
 
    private processBoosts(boosts: Map<string, ItemWithBreakdown>): number {
        const play = new Map<string, ItemWithBreakdown>()
        const beacons = new Map<string, ItemWithBreakdown>()
        const playTypes = new Set(['FRACK', 'APEX', 'BB_BATTLE', 'FW_ENL', 'FW_RES'])
        let cntPlay = 0, cntBeacons = 0
 
        for (const [key, value] of boosts) {
            if (playTypes.has(key)) {
                play.set(key, value)
                cntPlay += value.total
            } else {
                beacons.set(key, value)
                cntBeacons += value.total
            }
        }
 
        this.getContainer('Boosts-Play').innerHTML = this.itemsLabelTpl({items: play})
        this.getContainer('Boosts-Beacons').innerHTML = this.itemsLabelTpl({items: beacons})
 
        this.setCount('cntBoostsPlay', cntPlay)
        this.setCount('cntBoostsBeacons', cntBeacons)
 
        const total = cntPlay + cntBeacons
        this.setCount('cntBoosts', total)
        return total
    }
 
    private processKeys(keys: Map<string, KeyInfo>): number {
        const container = document.getElementById(`${this.pluginName}-Keys-Container`)
        Eif (container) {
            container.innerHTML = this.keysTpl({items: keys})
        }
 
        let total = 0
        for (const info of keys.values()) total += info.total
        this.setCount('cntKeysTotal', total)
 
        this.enableTableSorting(`${this.pluginName}-keysTable`)
        this.enableKeysSearch(`${this.pluginName}-keysTable`, `${this.pluginName}-keys-search`)
 
        return total
    }
 
    // ------------------------------------------------------------------
    // Helpers
    // ------------------------------------------------------------------
 
    private getContainer(name: string): Element {
        const element = document.getElementById(`${this.pluginName}-${name}-Container`)
        if (!element) console.warn(`[KuKuTeamInventory] Container not found: ${name}`)
        return element as Element
    }
 
    private setCount(name: string, count: number): void {
        const element = document.getElementById(`${this.pluginName}-${name}`)
        Eif (element) element.textContent = count.toString()
    }
 
    public sortTable(tableId: string, columnIndex: number, type: 'string' | 'number' | 'distance', ascending: boolean): void {
        const table = document.getElementById(tableId) as HTMLTableElement
        const tbody = table.tBodies[0]
        const rows = [...tbody.rows]
 
        rows.sort((a, b) => {
            const aText = a.cells[columnIndex].textContent?.trim() ?? ''
            const bText = b.cells[columnIndex].textContent?.trim() ?? ''
 
            switch (type) {
                case 'string':
                    return ascending ? aText.localeCompare(bText) : bText.localeCompare(aText)
                case 'number': {
                    const aNum = parseFloat(aText)
                    const bNum = parseFloat(bText)
                    return ascending ? aNum - bNum : bNum - aNum
                }
                case 'distance': {
                    const aNum = DialogHelper.parseDistance(aText)
                    const bNum = DialogHelper.parseDistance(bText)
                    return ascending ? aNum - bNum : bNum - aNum
                }
            }
        })
 
        rows.forEach(row => tbody.appendChild(row))
    }
 
    public enableTableSorting(tableId: string): void {
        const table = document.getElementById(tableId) as HTMLTableElement
        if (!table || table.dataset.sortEnabled) return
        table.dataset.sortEnabled = 'true'
 
        const headers = table.querySelectorAll<HTMLTableCellElement>('th')
 
        headers.forEach((header, i) => {
            const type = header.dataset.type as 'string' | 'number' | 'distance' | undefined
            if (!type) return
 
            const indicator = document.createElement('span')
            indicator.style.marginLeft = '8px'
            header.appendChild(indicator)
 
            let ascending = true
 
            header.addEventListener('click', () => {
                this.sortTable(tableId, i, type, ascending)
                ascending = !ascending
 
                headers.forEach(hdr => {
                    const span = hdr.querySelector('span:not(.cnt)')
                    if (span) span.textContent = ''
                })
 
                indicator.textContent = ascending ? '▲' : '▼'
            })
        })
    }
 
    public enableKeysSearch(tableId: string, inputId: string): void {
        const input = document.getElementById(inputId) as HTMLInputElement | null
        if (!input || input.dataset.searchEnabled) return
        input.dataset.searchEnabled = 'true'
 
        input.addEventListener('input', () => {
            const query = input.value.toLowerCase().trim()
            const table = document.getElementById(tableId) as HTMLTableElement | null
            if (!table) return
 
            for (const row of table.tBodies[0].rows) {
                const text = row.cells[0]?.textContent?.toLowerCase() ?? ''
                row.style.display = !query || text.includes(query) ? '' : 'none'
            }
        })
    }
 
    private static parseDistance(distanceStr: string): number {
        const match = /^([\d.]+)\s*(\w+)$/.exec(distanceStr.trim())
        if (!match) return 0
        const value = parseFloat(match[1])
        const unit = match[2].toLowerCase()
        return unit === 'km' ? value * 1000 : value
    }
 
    private sortByNumericSuffix<V>(map: Map<string, V>): Map<string, V> {
        return new Map(
            [...map.entries()].toSorted(([a], [b]) => {
                const numA = parseInt(/(\d+)$/.exec(a)?.[1] ?? '0', 10)
                const numB = parseInt(/(\d+)$/.exec(b)?.[1] ?? '0', 10)
                return numA - numB
            })
        )
    }
 
    private sortByCompoundKey<V>(
        map: Map<string, V>,
        typeOrder: string[],
        rarityOrder: string[]
    ): Map<string, V> {
        return new Map(
            [...map.entries()].toSorted(([a], [b]) => {
                const lastA = a.lastIndexOf('-')
                const lastB = b.lastIndexOf('-')
                const typeA = lastA === -1 ? a : a.slice(0, lastA)
                const rarityA = lastA === -1 ? '' : a.slice(lastA + 1)
                const typeB = lastB === -1 ? b : b.slice(0, lastB)
                const rarityB = lastB === -1 ? '' : b.slice(lastB + 1)
                const typeDiff = typeOrder.indexOf(typeA) - typeOrder.indexOf(typeB)
                return typeDiff === 0 ? rarityOrder.indexOf(rarityA) - rarityOrder.indexOf(rarityB) : typeDiff
            })
        )
    }
}