diff --git a/.vscode/settings.json b/.vscode/settings.json index c122d94..0103b2d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,7 @@ "editor.formatOnSave": false }, "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, "editor.formatOnSave": true, "editor.renderWhitespace": "boundary", diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 85171d2..c9d5e49 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -3,8 +3,9 @@ import { build } from 'esbuild'; build({ entryPoints: ['src/main.ts'], bundle: true, - target: 'es2018', // Screeps supports ES2018 well + target: 'es6', // Screeps supports ES2018 well platform: 'node', + tsconfig: 'tsconfig.json', format: 'cjs', outdir: 'dist', sourcemap: true, diff --git a/src/main.ts b/src/main.ts index e47707c..da523ad 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,5 @@ import SpawnHandler from "spawnHandler"; +import SpawnStorage from "spawnStorage"; declare global { /* @@ -13,7 +14,7 @@ declare global { interface Memory { uuid: number; log: any; - spawnHandlers: { [name: string]: SpawnHandler }; + spawnHandlers: { [name: string]: string }; } interface CreepMemory { @@ -30,41 +31,24 @@ declare global { } } -// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change -// This utility uses source maps to get the line numbers and file names of the original, TS source code -// export const loop = ErrorMapper.wrapLoop(() => { -// console.log(`Current game tick is ${Game.time}`); -// // Automatically delete memory of missing creeps -// for (const name in Memory.creeps) { -// if (!(name in Game.creeps)) { -// delete Memory.creeps[name]; -// } -// } -// }); export const loop = () => { + const spawnStorage = new SpawnStorage(); Memory.spawnHandlers = Memory.spawnHandlers || {}; - console.log(`Current game tick is ${Game.time}`); - // Check if spawn still exists const activeSpawns = Object.keys(Game.spawns); - for (const spawnName in Object.keys(Memory.spawnHandlers)) { - if (spawnName in Game.spawns) { - console.log(`Spawn ${spawnName} exists, continuing to run its handler.`); - continue; - } - console.log(`Spawn ${spawnName} does not exist, deleting its handler.`); - delete Memory.spawnHandlers[spawnName]; - } + spawnStorage.clearDeadHandlers(activeSpawns); for (const spawnName of activeSpawns) { // Create a handler for each spawn - if (spawnName in Memory.spawnHandlers) { - Memory.spawnHandlers[spawnName] = new SpawnHandler(Game.spawns[spawnName]); + var currentHandler = spawnStorage.getHandler(spawnName); + if (!currentHandler) { + currentHandler = spawnStorage.addHandler(spawnName, new SpawnHandler(Game.spawns[spawnName])); } + // Run the handler - Memory.spawnHandlers[spawnName].run(); + currentHandler.run(); } }; diff --git a/src/spawnStorage.ts b/src/spawnStorage.ts new file mode 100644 index 0000000..73dea39 --- /dev/null +++ b/src/spawnStorage.ts @@ -0,0 +1,60 @@ +import SpawnHandler from "spawnHandler"; + +class SpawnStorage { + constructor() { + // Initialize the spawn storage + if (!Memory.spawnHandlers) { + Memory.spawnHandlers = {}; + } + } + + get allHandlers(): { [name: string]: SpawnHandler } { + const handlers: { [name: string]: SpawnHandler } = {}; + for (const spawnName in Memory.spawnHandlers) { + const instance = this.getHandler(spawnName); + if (!instance) { + console.log(`No handler found for spawn: ${spawnName}`); + continue; + } + handlers[spawnName] = instance; + } + return handlers; + } + + // Method to get a spawn handler by name + getHandler(spawnName: string): SpawnHandler | null { + if (!Memory.spawnHandlers[spawnName]) { + console.log(`No handler found for spawn: ${spawnName}`); + return null; + } + const data = JSON.parse(Memory.spawnHandlers[spawnName]) as SpawnHandler; + return Object.assign(new SpawnHandler(Game.spawns[spawnName]), data); + } + + // Method to add or update a spawn handler + addHandler(spawnName: string, handler: SpawnHandler): SpawnHandler { + Memory.spawnHandlers[spawnName] = JSON.stringify(handler); + return handler; + } + + // Method to remove a spawn handler + removeHandler(spawnName: string) { + delete Memory.spawnHandlers[spawnName]; + } + + checkHandlerExists(spawnName: string): boolean { + return !!Memory.spawnHandlers[spawnName]; + } + + clearDeadHandlers(activeSpawns: string[]) { + for (const spawnName of Object.keys(this.allHandlers)) { + if (!(spawnName in activeSpawns)) { + continue; + } + this.removeHandler(spawnName); + } + } +} + + +export default SpawnStorage; diff --git a/tsconfig.json b/tsconfig.json index 62457f4..660a83d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "target": "es2018", - "lib": ["es2018", "dom"], + "target": "ES6", + "lib": ["ES2015", "DOM"], "module": "commonjs", "moduleResolution": "node", "outDir": "dist",