Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed EconRequest name to EconInfo and moved it out of requests object #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions src/js/simpleAllies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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]);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.<MineralConstant, number>} [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
Expand All @@ -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.<MineralConstant, number>} [args.mineralNodes] - The number of mineral nodes the bot has access to, probably used to inform expansion
*/
setEconInfo(args) {
this.myEconInfo = args;
}
}

module.exports = {
Expand Down
117 changes: 72 additions & 45 deletions src/ts/simpleAllies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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'
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<Record<MineralConstant, number>>
}

export interface AllyRequests {
Expand All @@ -132,37 +160,31 @@ export interface AllyRequests {
attack: AttackRequest[]
player: PlayerRequest[]
work: WorkRequest[]
funnel: FunnelRequest[];
econ?: EconRequest
funnel: FunnelRequest[]
room: RoomRequest[]
}

/**
* 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<SimpleAlliesSegment> = {}
private myRequests: AllyRequests = {
resource: [],
defense: [],
attack: [],
player: [],
work: [],
funnel: [],
room: []
}
private myEconInfo?: EconInfo
allySegmentData: SimpleAlliesSegment = {}
currentAlly?: string

/**
Expand All @@ -179,14 +201,16 @@ class SimpleAllies {
funnel: [],
room: [],
}
// reset econ info
this.myEconInfo = undefined

this.readAllySegment()
}

/**
* 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 :(")
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()