diff --git a/src/js/simpleAllies.js b/src/js/simpleAllies.js index 9057478..29a9bc9 100644 --- a/src/js/simpleAllies.js +++ b/src/js/simpleAllies.js @@ -20,24 +20,30 @@ const EFunnelGoalType = { // class SimpleAllies { - myRequests = {}; - allySegmentData; + // Request object definitions are documented in Request mothod definitions + myRequests = { + resource: [], + defense: [], + attack: [], + player: [], + work: [], + funnel: [], + room: [] + } + myEconInfo; + allySegmentData = {}; currentAlly; /** * To call before any requests are made or responded to. Configures some required values and gets ally requests */ initRun() { - // Reset the data of myRequests - this.myRequests = { - resource: [], - defense: [], - attack: [], - player: [], - work: [], - funnel: [], - room: [], - }; + // reset my requests + for (let requestType in this.myRequests) { + this.myRequests[requestType].length = 0 + } + // reset econ info + this.myEconInfo = undefined this.readAllySegment(); } @@ -75,7 +81,8 @@ class SimpleAllies { throw Error('Too many segments open: simpleAllies'); } const newSegmentData = { - requests: this.myRequests + requests: this.myRequests, + econ: this.myEconInfo }; RawMemory.segments[exports.allySegmentID] = JSON.stringify(newSegmentData); RawMemory.setPublicSegments([exports.allySegmentID]); @@ -90,6 +97,7 @@ class SimpleAllies { * @param {ResourceConstant} args.resourceType * @param {number} args.amount - How much they want of the resource. If the responder sends only a portion of what you ask for, that's fine * @param {boolean} [args.terminal] - If the bot has no terminal, allies should instead haul the resources to us + * @param {number} [args.timeout] - Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ requestResource(args) { this.myRequests.resource.push(args); @@ -100,6 +108,7 @@ class SimpleAllies { * @param {Object} args - a request object * @param {number} args.priority - 0-1 where 1 is highest consideration * @param {string} args.roomName + * @param {number} [args.timeout] - Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ requestDefense(args) { this.myRequests.defense.push(args); @@ -110,6 +119,7 @@ class SimpleAllies { * @param {Object} args - a request object * @param {number} args.priority - 0-1 where 1 is highest consideration * @param {string} args.roomName + * @param {number} [args.timeout] - Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ requestAttack(args) { this.myRequests.attack.push(args); @@ -118,8 +128,10 @@ class SimpleAllies { /** * Influence allies aggresion score towards a player * @param {Object} args - a request object + * @param {string} args.playerName * @param {number} args.hate - 0-1 where 1 is highest consideration. How much you think your team should hate the player. Should probably affect combat aggression and targetting * @param {number} args.lastAttackedBy - The last time this player has attacked you + * @param {number} [args.timeout] - Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ requestPlayer(args) { this.myRequests.player.push(args); @@ -131,6 +143,7 @@ class SimpleAllies { * @param {string} args.roomName * @param {number} args.priority - 0-1 where 1 is highest consideration * @param {'build' | 'repair'} args.workType + * @param {number} [args.timeout] - Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ requestWork(args) { this.myRequests.work.push(args); @@ -142,23 +155,12 @@ class SimpleAllies { * @param {number} args.maxAmount - Amount of energy needed. Should be equal to energy that needs to be put into controller for achieving goal. * @param {EFunnelGoalType.GCL | EFunnelGoalType.RCL7 | EFunnelGoalType.RCL8} args.goalType - What energy will be spent on. Room receiving energy should focus solely on achieving the goal. * @param {string} [args.roomName] - Room to which energy should be sent. If undefined resources can be sent to any of requesting player's rooms. + * @param {number} [args.timeout] - Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ requestFunnel(args) { this.myRequests.funnel.push(args); } - /** - * Share how your bot is doing economically - * @param {Object} args - a request object - * @param {number} args.credits - total credits the bot has. Should be 0 if there is no market on the server - * @param {number} args.sharableEnergy - the maximum amount of energy the bot is willing to share with allies. Should never be more than the amount of energy the bot has in storing structures - * @param {number} [args.energyIncome] - The average energy income the bot has calculated over the last 100 ticks. Optional, as some bots might not be able to calculate this easily. - * @param {Object.} [args.mineralNodes] - The number of mineral nodes the bot has access to, probably used to inform expansion - */ - requestEcon(args) { - this.myRequests.econ = args; - } - /** * Share scouting data about hostile owned rooms * @param {Object} args - a request object @@ -174,6 +176,20 @@ class SimpleAllies { requestRoom(args) { this.myRequests.room.push(args); } + + // + + /** + * Share how your bot is doing economically + * @param {Object} args - a request object + * @param {number} args.credits - total credits the bot has. Should be 0 if there is no market on the server + * @param {number} args.sharableEnergy - the maximum amount of energy the bot is willing to share with allies. Should never be more than the amount of energy the bot has in storing structures + * @param {number} [args.energyIncome] - The average energy income the bot has calculated over the last 100 ticks. Optional, as some bots might not be able to calculate this easily. + * @param {Object.} [args.mineralNodes] - The number of mineral nodes the bot has access to, probably used to inform expansion + */ + setEconInfo(args) { + this.myEconInfo = args; + } } module.exports = { diff --git a/src/ts/simpleAllies.ts b/src/ts/simpleAllies.ts index 5466adc..b619cc4 100644 --- a/src/ts/simpleAllies.ts +++ b/src/ts/simpleAllies.ts @@ -24,6 +24,10 @@ export interface ResourceRequest { * If the bot has no terminal, allies should instead haul the resources to us */ terminal?: boolean + /** + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. + */ + timeout?: number; } export interface DefenseRequest { @@ -32,6 +36,10 @@ export interface DefenseRequest { * 0-1 where 1 is highest consideration */ priority: number + /** + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. + */ + timeout?: number; } export interface AttackRequest { @@ -40,6 +48,10 @@ export interface AttackRequest { * 0-1 where 1 is highest consideration */ priority: number + /** + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. + */ + timeout?: number; } export interface PlayerRequest { @@ -52,6 +64,10 @@ export interface PlayerRequest { * The last time this player has attacked you */ lastAttackedBy?: number + /** + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. + */ + timeout?: number; } export type WorkRequestType = 'build' | 'repair' @@ -63,6 +79,10 @@ export interface WorkRequest { */ priority: number workType: WorkRequestType + /** + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. + */ + timeout?: number; } export const enum FunnelGoal { @@ -84,26 +104,10 @@ export interface FunnelRequest { * Room to which energy should be sent. If undefined resources can be sent to any of requesting player's rooms. */ roomName?: string; -} - -export interface EconRequest { - /** - * total credits the bot has. Should be 0 if there is no market on the server - */ - credits: number - /** - * the maximum amount of energy the bot is willing to share with allies. Should never be more than the amount of energy the bot has in storing structures - */ - sharableEnergy: number - /** - * The average energy income the bot has calculated over the last 100 ticks - * Optional, as some bots might not be able to calculate this easily. - */ - energyIncome?: number /** - * The number of mineral nodes the bot has access to, probably used to inform expansion + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. */ - mineralNodes?: { [key in MineralConstant]: number } + timeout?: number; } export interface RoomRequest { @@ -124,6 +128,30 @@ export interface RoomRequest { towers: number avgRamprtHits: number terminal: boolean + /** + * Tick after which the request should be ignored. If your bot crashes, or stops updating requests for some other reason, this is a safety mechanism. + */ + timeout?: number; +} + +export interface EconInfo { + /** + * total credits the bot has. Should be 0 if there is no market on the server + */ + credits: number + /** + * the maximum amount of energy the bot is willing to share with allies. Should never be more than the amount of energy the bot has in storing structures + */ + sharableEnergy: number + /** + * The average energy income the bot has calculated over the last 100 ticks + * Optional, as some bots might not be able to calculate this easily. + */ + energyIncome?: number + /** + * The number of mineral nodes the bot has access to, probably used to inform expansion + */ + mineralNodes?: Partial> } export interface AllyRequests { @@ -132,8 +160,7 @@ export interface AllyRequests { attack: AttackRequest[] player: PlayerRequest[] work: WorkRequest[] - funnel: FunnelRequest[]; - econ?: EconRequest + funnel: FunnelRequest[] room: RoomRequest[] } @@ -141,28 +168,23 @@ export interface AllyRequests { * Having data we pass into the segment being an object allows us to send additional information outside of requests */ export interface SimpleAlliesSegment { - /** - * Requests of the new system - */ - requests: AllyRequests -} - -const requestsSekelton: AllyRequests = { - resource: [], - defense: [], - attack: [], - player: [], - work: [], - funnel: [], - room: [], + requests?: AllyRequests + econ?: EconInfo } class SimpleAllies { - myRequests: AllyRequests = {...requestsSekelton} - // Partial since we can't trust others to not omit fields - // we want to make sure we check their existence - allySegmentData: Partial = {} + private myRequests: AllyRequests = { + resource: [], + defense: [], + attack: [], + player: [], + work: [], + funnel: [], + room: [] + } + private myEconInfo?: EconInfo + allySegmentData: SimpleAlliesSegment = {} currentAlly?: string /** @@ -179,6 +201,8 @@ class SimpleAllies { funnel: [], room: [], } + // reset econ info + this.myEconInfo = undefined this.readAllySegment() } @@ -186,7 +210,7 @@ class SimpleAllies { /** * Try to get segment data from our current ally. If successful, assign to the instane */ - readAllySegment() { + private readAllySegment() { if (!allies.length) { throw Error("Failed to find an ally for simpleAllies, you probably have none :(") } @@ -220,7 +244,8 @@ class SimpleAllies { } const newSegmentData: SimpleAlliesSegment = { - requests: this.myRequests as AllyRequests + requests: this.myRequests, + econ: this.myEconInfo } RawMemory.segments[allySegmentID] = JSON.stringify(newSegmentData) @@ -253,13 +278,15 @@ class SimpleAllies { this.myRequests.funnel.push(args) } - requestEcon(args: EconRequest) { - this.myRequests.econ = args - } - requestRoom(args: RoomRequest) { this.myRequests.room.push(args) } + + // + + setEconInfo(args: EconInfo) { + this.myEconInfo = args + } } -export const simpleAllies = new SimpleAllies() +export const simpleAllies = new SimpleAllies() \ No newline at end of file