diff --git a/src/modulesRoom/shortcut/index.ts b/src/modulesRoom/shortcut/index.ts index 514ad530..a783b2ba 100644 --- a/src/modulesRoom/shortcut/index.ts +++ b/src/modulesRoom/shortcut/index.ts @@ -3,13 +3,13 @@ import { AllRoomShortcut, StructureIdCache } from './types' /** * 挂载房间快捷访问 - * + * * 提供对房间内资源的快捷访问方式,如:W1N1.nuker、W1N1.sources 等 * 包括唯一型建筑(Nuker、Factory ...)复数型建筑(Spawn、extension)和自然资源(Source、Mineral ...) - * + * * 所有可用的访问属性见上方 SINGLE_STRUCTURES 和 MULTIPLE_STRUCTURES */ -const mountShortcut = function () { +const mountShortcut = function() { // 添加基础的快捷访问 SINGLE_STRUCTURES.forEach(setShortcut(true)) MULTIPLE_STRUCTURES.forEach(setShortcut(false)) @@ -23,7 +23,7 @@ export default mountShortcut /** * 所有在房间中具有唯一性的建筑 - * + * * 可以直接通过例如 room[STRUCTURE_OBSERVER] 获取到对应的对象 */ const SINGLE_STRUCTURES: AllRoomShortcut[] = [ @@ -35,10 +35,9 @@ const SINGLE_STRUCTURES: AllRoomShortcut[] = [ 'mineral' ] - /** * 所有在房间中会存在多个的建筑 - * + * * 可以直接通过例如 room[STRUCTURE_SPAWN] 获取到对应的对象**数组** */ const MULTIPLE_STRUCTURES: AllRoomShortcut[] = [ @@ -58,65 +57,77 @@ const MULTIPLE_STRUCTURES: AllRoomShortcut[] = [ /** * 判断某个建筑类型是否为需要挂载的建筑类型 - * + * * @param type 要进行判断的建筑类型 */ -const isShortcutStructure = function (type: string): type is AllRoomShortcut { - return ([ ...SINGLE_STRUCTURES, ...MULTIPLE_STRUCTURES ] as string[]).includes(type) +const isShortcutStructure = function(type: string): type is AllRoomShortcut { + return ([ + ...SINGLE_STRUCTURES, + ...MULTIPLE_STRUCTURES + ] as string[]).includes(type) } /** * 全局建筑 Id 缓存 - * + * * 在全局重置后会运行 Room.find 获取建筑并将其 id 缓存在这里 */ const structureIdCache: StructureIdCache = {} - /** * 获取缓存中的建筑 ID - * + * * @param roomName 要查询的房间名 * @param type 要查询的建筑类型 */ -const getCacheId = function (roomName: string, type: AllRoomShortcut): Id[] { +const getCacheId = function( + roomName: string, + type: AllRoomShortcut +): Id[] { if (!structureIdCache[roomName]) return undefined if (!structureIdCache[roomName][type]) return [] return structureIdCache[roomName][type] } - /** * 设置建筑 ID 缓存 - * + * * 本方法会直接 **替换** 目标位置的旧缓存 - * + * * @param roomName 要设置到的房间 * @param type 要设置到的建筑类型 * @param ids 要设置的 id */ -const setCacheId = function (roomName: string, type: AllRoomShortcut, ids: Id[]) { +const setCacheId = function( + roomName: string, + type: AllRoomShortcut, + ids: Id[] +) { if (!structureIdCache[roomName]) structureIdCache[roomName] = {} if (!structureIdCache[roomName][type]) structureIdCache[roomName][type] = [] - return structureIdCache[roomName][type] = ids + return (structureIdCache[roomName][type] = ids) } /** * 追加新的建筑缓存 - * + * * **新建筑造好后需要调用该方法**, * 该方法会将提供的缓存 id 追加到指定位置的缓存末尾 - * + * * @param roomName 房间名 * @param type 要追加到的建筑类型 * @param id 新的建筑 id */ -export const updateStructure = function (roomName: string, type: string, id: Id) { +export const updateStructure = function( + roomName: string, + type: string, + id: Id +) { // 传入的建筑类型有可能不需要挂载,这里剔除掉 if (!isShortcutStructure(type)) return - + if (!structureIdCache[roomName]) structureIdCache[roomName] = {} if (!structureIdCache[roomName][type]) structureIdCache[roomName][type] = [] @@ -127,13 +138,16 @@ export const updateStructure = function (roomName: string, type: string, id: Id< /** * [核心实现] 获取指定房间的建筑缓存 - * + * * @param room 目标房间 * @param type 要获取的建筑类型 - * + * * @returns 对应的建筑**数组** */ -const getStructureWithCache = function (room: Room, type: AllRoomShortcut): TargetStructure[] { +const getStructureWithCache = function( + room: Room, + type: AllRoomShortcut +): TargetStructure[] { const privateKey = getPrivateKey(type) // 本 tick 有缓存就直接返回 @@ -165,16 +179,15 @@ const getStructureWithCache = function (room return target } - /** * 获取指定房间的建筑缓存(从内存中保存的 id) - * + * * @param room 目标房间 * @param privateKey 建筑缓存在目标房间的键 * @param memoryKey 建筑 id 在房间内存中对应的字段名 * @returns 对应的建筑 */ -const getStructureWithMemory = function ( +const getStructureWithMemory = function( room: Room, privateKey: string, memoryKey: string @@ -183,9 +196,11 @@ const getStructureWithMemory = function ( // 内存中没有 id 就说明没有该建筑 if (!room.memory[memoryKey]) return undefined - + // 从 id 获取建筑并缓存 - const target: TargetStructure = Game.getObjectById(room.memory[memoryKey]) + const target: TargetStructure = Game.getObjectById( + room.memory[memoryKey] + ) as TargetStructure // 如果保存的 id 失效的话,就移除缓存 if (!target) { @@ -202,23 +217,31 @@ const getStructureWithMemory = function ( * 中央 link 访问器 */ const centerLinkGetter = function(): StructureLink { - return getStructureWithMemory(this, '_centerLink', 'centerLinkId') + return getStructureWithMemory( + this, + '_centerLink', + 'centerLinkId' + ) } /** * 中央 link 访问器 */ const upgradeLinkGetter = function(): StructureLink { - return getStructureWithMemory(this, '_upgradeLink', 'upgradeLinkId') + return getStructureWithMemory( + this, + '_upgradeLink', + 'upgradeLinkId' + ) } /** * 设置建筑快捷方式 - * + * * @param isSingle 要设置的是唯一建筑还是复数建筑 * @returns 一个函数,接受要挂载的建筑类型,并挂载至房间上 */ -const setShortcut = function (isSingle: boolean) { +const setShortcut = function(isSingle: boolean) { return (type: AllRoomShortcut) => { Object.defineProperty(Room.prototype, type, { get() { @@ -231,25 +254,26 @@ const setShortcut = function (isSingle: boolean) { } } - /** * 获取指定键的私有键名 - * + * * @param key 要获取私有键名的键 */ -const getPrivateKey = key => `_${key}` - +const getPrivateKey = (key) => `_${key}` /** * 初始化指定房间的建筑缓存 - * + * * @param room 要初始化的房间 */ -const initShortcutCache = function (room: Room): void { +const initShortcutCache = function(room: Room): void { structureIdCache[room.name] = {} // 查找建筑 - const structureGroup = _.groupBy(room.find(FIND_STRUCTURES), s => s.structureType) + const structureGroup = _.groupBy( + room.find(FIND_STRUCTURES), + (s) => s.structureType + ) // 查找静态资源 Object.assign(structureGroup, { mineral: room.find(FIND_MINERALS), @@ -257,9 +281,11 @@ const initShortcutCache = function (room: Room): void { }) // 把需要的建筑 id 存入全局缓存,并直接初始化 room 缓存 - for (const type of [ ...MULTIPLE_STRUCTURES, ...SINGLE_STRUCTURES ]) { + for (const type of [...MULTIPLE_STRUCTURES, ...SINGLE_STRUCTURES]) { // 如果房间内某种建筑还没有的话就填充为空数组 - structureIdCache[room.name][type] = (structureGroup[type] || []).map(s => s.id) + structureIdCache[room.name][type] = (structureGroup[type] || []).map( + (s) => s.id + ) room[getPrivateKey(type)] = structureGroup[type] || [] } -} \ No newline at end of file +} diff --git a/src/utils.ts b/src/utils.ts index 59393848..da592288 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,36 +1,40 @@ /** * 判断一个位置是否在房间入口处(是否骑墙) */ -export const onEnter = function (pos: RoomPosition): boolean { +export const onEnter = function(pos: RoomPosition): boolean { return pos.x === 0 || pos.x === 49 || pos.y === 0 || pos.y === 49 } /** -* 获取指定方向的相反方向 -* -* @param direction 目标方向 -*/ -export const getOppositeDirection = function (direction: DirectionConstant): DirectionConstant { - return ((direction + 3) % 8 + 1) + * 获取指定方向的相反方向 + * + * @param direction 目标方向 + */ +export const getOppositeDirection = function( + direction: DirectionConstant +): DirectionConstant { + return (((direction + 3) % 8) + 1) } /** * 将指定位置序列化为字符串 * 形如: 12/32/E1N2 - * + * * @param pos 要进行压缩的位置 */ -export const serializePos = function (pos: RoomPosition): string { +export const serializePos = function(pos: RoomPosition): string { return `${pos.x}/${pos.y}/${pos.roomName}` } /** * 将位置序列化字符串转换为位置 * 位置序列化字符串形如: 12/32/E1N2 - * + * * @param posStr 要进行转换的字符串 */ -export const unserializePos = function (posStr: string): RoomPosition | undefined { +export const unserializePos = function( + posStr: string +): RoomPosition | undefined { // 形如 ["12", "32", "E1N2"] const [x, y, roomName] = posStr.split('/') @@ -41,11 +45,14 @@ export const unserializePos = function (posStr: string): RoomPosition | undefine /** * 全局喊话 */ -export const globalSay = function (words: string[]) { +export const globalSay = function(words: string[]) { if (!Memory.sayIndex) Memory.sayIndex = 0 - Object.values(Game.creeps).forEach(creep => creep.say(words[Memory.sayIndex], true)) - Memory.sayIndex = Memory.sayIndex + 1 >= words.length ? 0 : Memory.sayIndex + 1 + Object.values(Game.creeps).forEach((creep) => + creep.say(words[Memory.sayIndex], true) + ) + Memory.sayIndex = + Memory.sayIndex + 1 >= words.length ? 0 : Memory.sayIndex + 1 } declare global { @@ -60,8 +67,10 @@ declare global { /** * 获取全局唯一索引 */ -export const getUniqueKey = function (): number { - return Game._uniqueKey = Game._uniqueKey ? Game._uniqueKey + 0.1 : Game.time +export const getUniqueKey = function(): number { + return (Game._uniqueKey = Game._uniqueKey + ? Game._uniqueKey + 0.1 + : Game.time) } declare global { @@ -76,8 +85,8 @@ declare global { /** * 移除过期的 flag 内存 */ -export const clearFlag = function (): string { - let logs = [ '已清理过期旗帜:' ] +export const clearFlag = function(): string { + let logs = ['已清理过期旗帜:'] for (const flagName in Memory.flags) { if (!Game.flags[flagName]) { delete Memory.flags[flagName] @@ -90,11 +99,11 @@ export const clearFlag = function (): string { /** * 判断是否为白名单玩家 - * + * * @param creep 要检查的 creep * @returns 是否为白名单玩家 */ -export const whiteListFilter = function (creep) { +export const whiteListFilter = function(creep) { if (!Memory.whiteList) return true // 加入白名单的玩家单位不会被攻击,但是会被记录 if (creep.owner.username in Memory.whiteList) { @@ -110,7 +119,8 @@ export const whiteListFilter = function (creep) { */ export const generatePixelAppPlugin: AppLifecycleCallbacks = { tickEnd: () => { - if (Game.cpu.bucket >= 10000 && Game.cpu.generatePixel) Game.cpu.generatePixel() + if (Game.cpu.bucket >= 10000 && Game.cpu.generatePixel) + Game.cpu.generatePixel() } } @@ -124,12 +134,16 @@ export const getName = { /** * 给指定对象设置访问器 - * + * * @param target 要设置访问器的对象 * @param name 访问器的名字 * @param getter 访问器方法 */ -export const createGetter = function (target: AnyObject, name: string, getter: () => any) { +export const createGetter = function( + target: AnyObject, + name: string, + getter: () => any +) { Object.defineProperty(target.prototype, name, { get: getter, enumerable: false, @@ -139,35 +153,40 @@ export const createGetter = function (target: AnyObject, name: string, getter: ( /** * 在指定房间显示 cost - * + * * @param cost 要显示的 cost * @param room 要显示到的房间 */ -export const showCost = function (cost: CostMatrix, room: Room): void { - for (let x = 1; x < 49; x ++) for (let y = 1; y < 49; y ++) { - room.visual.text(cost.get(x, y).toString(), x, y, { - color: '#a9b7c6', - font: 0.5, - opacity: 0.7 - }) - } +export const showCost = function(cost: CostMatrix, room: Room): void { + for (let x = 1; x < 49; x++) + for (let y = 1; y < 49; y++) { + room.visual.text(cost.get(x, y).toString(), x, y, { + color: '#a9b7c6', + font: 0.5, + opacity: 0.7 + }) + } } /** * 获取指定对象并缓存 * 会将初始化回调的返回值进行缓存,该返回值 **必须拥有 id 字段** - * + * * @param initValue 初始化该值的回调,在没有找到缓存 id 时将会调用该方法获取要缓存的初始值 * @param cachePlace id 存放的对象,一般位于 xxx.memory 上 * @param cacheKey 要缓存到的键,例如 targetId 之类的字符串 */ -export const useCache = function (initValue: () => T, cachePlace: AnyObject, cacheKey: string): T { +export const useCache = function( + initValue: () => T, + cachePlace: AnyObject, + cacheKey: string +): T { const cacheId = cachePlace[cacheKey] let target: T = undefined // 如果有缓存了,就读取缓存 if (cacheId) { - target = Game.getObjectById(cacheId) + target = Game.getObjectById(cacheId) as T if (target) return target // 缓存失效了,移除缓存 id,下面会重新搜索 @@ -184,8 +203,8 @@ export const useCache = function (initValue: () => T, ca /** * 数组交叉合并 */ -export const crossMerge = function (a: T[], b: T[]): T[] { +export const crossMerge = function(a: T[], b: T[]): T[] { return Array.from({ length: a.length + b.length }, (_, index) => { return (index % 2 ? b.shift() : a.shift()) || a.shift() || b.shift() }) -} \ No newline at end of file +}