Created
June 28, 2020 09:54
-
-
Save lethern/1216d430c43bb8458d03b8136894ecf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cacheEnum | |
CACHE.E_ROOM = 1; | |
CACHE.coreInit = function () { | |
if (!global.my_caches) { | |
CACHE[CACHE.E_ROOM] = Memory.rooms; | |
global.my_caches = true; | |
} | |
API_CACHE[CACHE.E_ROOM] = {}; | |
}; | |
CACHE.get = function (cacheEnum, key) { | |
let res = CACHE[cacheEnum][key]; | |
if (!res) res = CACHE[cacheEnum][key] = {}; | |
return res; | |
}; | |
CACHE.get_singleTick = function (cacheEnum, key) { | |
let res = API_CACHE[cacheEnum][key]; | |
if (!res) res = API_CACHE[cacheEnum][key] = {}; | |
return res; | |
}; | |
UTILS_ROOM.getSpawn = function (room) { | |
return getCached(room.name, "spawn", () => | |
room.find(FIND_MY_STRUCTURES, { | |
filter: { structureType: STRUCTURE_SPAWN } | |
}) | |
); | |
} | |
UTILS_ROOM.getStorage = function (room) { | |
return getCachedSingle(room.name, "storage", "storageId", () => | |
room.find(FIND_MY_STRUCTURES, { | |
filter: { structureType: STRUCTURE_STORAGE } | |
}) | |
); | |
}; | |
function getCached(roomName, objName, findFunc) { | |
let room_cacheAPI = CACHE.get_singleTick(CACHE.E_ROOM, roomName); | |
if (!room_cacheAPI[objName]) { | |
room_cacheAPI[objName] = findFunc(); | |
} | |
return room_cacheAPI[objName]; | |
}; | |
function getCachedSingle(roomName, objName, idName, findFunc) { | |
let room_cacheAPI = CACHE.get_singleTick(CACHE.E_ROOM, roomName); | |
if (room_cacheAPI[objName] === undefined) { | |
let cache = CACHE.get(CACHE.E_ROOM, roomName); | |
if (!cache[idName]) { | |
let found = findFunc(); | |
if (!found.length) { | |
room_cacheAPI[objName] = null; | |
} else { | |
room_cacheAPI[objName] = found[0]; | |
cache[idName] = found[0].id; | |
} | |
} else { | |
room_cacheAPI[objName] = Game.getObjectById(cache[idName]); | |
} | |
} | |
return room_cacheAPI[objName]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment