All files utils.dashboard.js

88.77% Statements 87/98
48.07% Branches 25/52
100% Functions 3/3
88.65% Lines 86/97

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 3332x           6x     6x 6x         2x         2x 2x 2x   2x 2x 2x   2x 2x 2x     2x 2x 2x     2x                   2x             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 1x 1x 1x   1x         1x         1x     1x 1x   1x 1x     1x             1x     1x 1x 1x 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  
const adaptiveSystem = require('system.adaptive');
 
/**
 * Formats a number for better readability (e.g., 1000 -> 1.0K, 1000000 -> 1.0M)
 */
function formatNumber(num) {
    Iif (num >= 1000000) {
        return (num / 1000000).toFixed(1) + 'M';
    }
    Eif (num >= 1000) {
        return (num / 1000).toFixed(1) + 'K';
    }
    return num.toString();
}
 
const DashboardRenderer = {
    renderRoomDashboard(room) {
        // ⚡ PERFORMANCE OPTIMIZATION: Per-tick caching of common searches on the Room object.
        // This allows other systems (like defense.manager) to reuse these results.
        // Using unique tick keys prevents accidental cache collisions between modules.
        Eif (room._myCreepsTick !== Game.time) {
            room._myCreeps = room.find(FIND_MY_CREEPS);
            room._myCreepsTick = Game.time;
        }
        Eif (room._myStructuresTick !== Game.time) {
            room._myStructures = room.find(FIND_MY_STRUCTURES);
            room._myStructuresTick = Game.time;
        }
        Eif (room._hostileCreepsTick !== Game.time) {
            room._hostileCreeps = room.find(FIND_HOSTILE_CREEPS);
            room._hostileCreepsTick = Game.time;
        }
 
        const creeps = room._myCreeps;
        const structures = room._myStructures;
        const hostiles = room._hostileCreeps;
 
        // ⚡ PERFORMANCE OPTIMIZATION: Use a single loop to count roles instead of multiple filters.
        const roleCount = {
            harvester: 0,
            upgrader: 0,
            builder: 0,
            repairer: 0,
            transporter: 0,
            scout: 0,
            medic: 0,
            explorer: 0,
        };
        for (let i = 0; i < creeps.length; i++) {
            const role = creeps[i].memory.role;
            if (roleCount[role] !== undefined) {
                roleCount[role]++;
            }
        }
 
        const energyStats = {
            available: room.energyAvailable,
            capacity: room.energyCapacityAvailable,
            storageEnergy: room.storage ? room.storage.store[RESOURCE_ENERGY] : 0,
            storageCapacity: room.storage ? room.storage.store.getCapacity(RESOURCE_ENERGY) : 0,
        };
 
        const info = {
            room: room.name,
            gcl: {
                level: Game.gcl.level,
                percent: Math.floor((Game.gcl.progress / Game.gcl.progressTotal) * 100),
                progress: Game.gcl.progress,
                progressTotal: Game.gcl.progressTotal,
            },
            controller: room.controller
                ? {
                      level: room.controller.level,
                      progress: room.controller.progress || 0,
                      progressTotal: room.controller.progressTotal || 0,
                      percent: room.controller.progressTotal
                          ? Math.floor(
                                (room.controller.progress / room.controller.progressTotal) * 100
                            )
                          : 100,
                  }
                : null,
            hostiles: hostiles.length,
            structures: structures.length,
            energy: `${formatNumber(energyStats.available)}/${formatNumber(energyStats.capacity)}`,
            energyPercent: energyStats.capacity
                ? Math.floor((energyStats.available / energyStats.capacity) * 100)
                : 0,
            energyAvailable: energyStats.available,
            energyCapacity: energyStats.capacity,
            storage: formatNumber(energyStats.storageEnergy),
            storagePercent: energyStats.storageCapacity
                ? Math.floor((energyStats.storageEnergy / energyStats.storageCapacity) * 100)
                : 0,
            creeps: roleCount,
            mode: adaptiveSystem.getModeName(Memory.adaptive?.currentMode ?? 2).toUpperCase(),
            bucket: Game.cpu.bucket,
            cpuUsed: Game.cpu.getUsed().toFixed(2),
            tick: Game.time,
        };
 
        return info;
    },
 
    displayVisuals(room) {
        const info = this.renderRoomDashboard(room);
 
        let y = 2.0;
        const x = 1;
        const width = 8.5;
        const height = 12.2;
 
        // 🎨 Accessibility: Semi-transparent background for readability
        room.visual.rect(x - 0.5, y - 1, width, height, {
            fill: '#000000',
            opacity: 0.5,
            stroke: '#ffffff',
            strokeWidth: 0.05,
        });
 
        // 🏠 Room Name & Mode
        const modeIcons = {
            EMERGENCY: '🚨',
            MINIMAL: '🔋',
            NORMAL: '⚖️',
            FULL: '🚀',
        };
        const modeColors = {
            EMERGENCY: '#ff0000',
            MINIMAL: '#ffaa00',
            NORMAL: '#ffff00',
            FULL: '#00ff00',
        };
        const modeColor = modeColors[info.mode] || '#ffffff';
        const modeIcon = modeIcons[info.mode] || '⚙️';
 
        room.visual.text(`🏠 ${info.room} ${modeIcon} [${info.mode}]`, x, y, {
            font: 0.8,
            color: modeColor,
            align: 'left',
            stroke: '#000000',
            strokeWidth: 0.05,
        });
        y++;
 
        // 🌐 GCL info
        room.visual.text(`🌐 GCL: ${info.gcl.level} (${info.gcl.percent}%)`, x, y, {
            font: 0.7,
            color: '#ffffff',
            align: 'left',
            stroke: '#000000',
            strokeWidth: 0.05,
        });
        y += 0.4;
 
        // GCL Progress Bar
        const gclBarWidth = 6;
        const gclBarHeight = 0.2;
        const gclProgress = info.gcl.progress / info.gcl.progressTotal;
        room.visual.rect(x, y - 0.1, gclBarWidth, gclBarHeight, {
            fill: '#333333',
            stroke: '#ffffff',
            strokeWidth: 0.02,
        });
        room.visual.rect(x, y - 0.1, gclBarWidth * gclProgress, gclBarHeight, {
            fill: '#ffffff',
            opacity: 0.8,
        });
        y += 0.6;
 
        // 🎮 Controller info
        const controllerText = info.controller
            ? `🎮 RCL: ${info.controller.level} (${info.controller.percent}%)`
            : '🎮 RCL: None';
        room.visual.text(controllerText, x, y, {
            font: 0.7,
            color: '#ffff00',
            align: 'left',
            stroke: '#000000',
            strokeWidth: 0.05,
        });
        y += 0.4;
 
        // RCL Progress Bar
        Eif (info.controller && info.controller.level < 8) {
            const barWidth = 6;
            const barHeight = 0.2;
            const progress = info.controller.progress / info.controller.progressTotal;
 
            room.visual.rect(x, y - 0.1, barWidth, barHeight, {
                fill: '#333333',
                stroke: '#ffffff',
                strokeWidth: 0.02,
            });
            room.visual.rect(x, y - 0.1, barWidth * progress, barHeight, {
                fill: '#ffff00',
                opacity: 0.8,
            });
        }
        y += 0.8;
 
        // ⚡ Energy info
        let energyColor = '#00ffff'; // Cyan (Default/Healthy)
        Iif (info.energyPercent < 30) {
            energyColor = '#ff0000'; // Red (Critical)
        } else Eif (info.energyPercent < 70) {
            energyColor = '#ffff00'; // Yellow (Warning)
        }
 
        room.visual.text(`⚡ Energy: ${info.energy} (${info.energyPercent}%)`, x, y, {
            font: 0.7,
            color: energyColor,
            align: 'left',
            stroke: '#000000',
            strokeWidth: 0.05,
        });
        y += 0.4;
 
        // Energy Progress Bar
        const energyBarWidth = 6;
        const energyBarHeight = 0.2;
        const energyProgress = Math.min(info.energyAvailable / info.energyCapacity, 1) || 0;
        room.visual.rect(x, y - 0.1, energyBarWidth, energyBarHeight, {
            fill: '#333333',
            stroke: '#ffffff',
            strokeWidth: 0.02,
        });
        room.visual.rect(x, y - 0.1, energyBarWidth * energyProgress, energyBarHeight, {
            fill: energyColor,
            opacity: 0.8,
        });
        y += 0.6;
 
        // 📦 Storage info
        room.visual.text(`📦 Storage: ${info.storage} (${info.storagePercent}%)`, x, y, {
            font: 0.7,
            color: '#ffffff',
            align: 'left',
            stroke: '#000000',
            strokeWidth: 0.05,
        });
        y += 0.4;
 
        // Storage Progress Bar
        const storageBarWidth = 6;
        const storageBarHeight = 0.2;
        const storageProgress = info.storagePercent / 100;
        room.visual.rect(x, y - 0.1, storageBarWidth, storageBarHeight, {
            fill: '#333333',
            stroke: '#ffffff',
            strokeWidth: 0.02,
        });
        room.visual.rect(x, y - 0.1, storageBarWidth * storageProgress, storageBarHeight, {
            fill: '#ffffff',
            opacity: 0.8,
        });
        y += 0.8;
 
        // 👥 Creeps info
        room.visual.text(
            `👥 ⛏️:${info.creeps.harvester} ⬆️:${info.creeps.upgrader} 🛠️:${info.creeps.builder} 🔧:${info.creeps.repairer}`,
            x,
            y,
            {
                font: 0.7,
                color: '#ffffff',
                align: 'left',
                stroke: '#000000',
                strokeWidth: 0.05,
            }
        );
        y += 0.8;
        room.visual.text(
            `   🚚:${info.creeps.transporter} 📡:${info.creeps.scout} 💊:${info.creeps.medic} 🗺️:${info.creeps.explorer}`,
            x,
            y,
            {
                font: 0.7,
                color: '#ffffff',
                align: 'left',
                stroke: '#000000',
                strokeWidth: 0.05,
            }
        );
 
        Iif (info.hostiles > 0) {
            y++;
            // 🎨 Animation: Pulsing opacity for urgency
            const pulse = 0.7 + 0.3 * Math.sin(Game.time / 3);
            room.visual.text(`⚠️ HOSTILES: ${info.hostiles}`, x, y, {
                font: 0.8,
                color: '#ff0000',
                opacity: pulse,
                align: 'left',
                stroke: '#000000',
                strokeWidth: 0.1,
            });
        }
 
        // 🔋 CPU Bucket
        y += 0.8;
        const bucketProgress = Math.min(info.bucket / 10000, 1);
        room.visual.rect(x, y, 6, 0.2, { fill: '#333333', stroke: '#ffffff', strokeWidth: 0.02 });
 
        let bucketColor = '#ff0000';
        if (info.bucket > 7000) {
            bucketColor = '#00ff00';
        } else Eif (info.bucket > 3000) {
            bucketColor = '#ffff00';
        }
 
        room.visual.rect(x, y, 6 * bucketProgress, 0.2, {
            fill: bucketColor,
            opacity: 0.7,
        });
        const bucketPercent = Math.floor(bucketProgress * 100);
        room.visual.text(
            `CPU: ${info.cpuUsed} | Bucket: ${info.bucket} (${bucketPercent}%) | Tick: ${info.tick}`,
            x,
            y + 0.6,
            {
                font: 0.4,
                color: '#ffffff', // 🎨 Accessibility: White text for consistency and contrast
                align: 'left',
                stroke: '#000000',
                strokeWidth: 0.05,
            }
        );
    },
};
 
module.exports = DashboardRenderer;