Skip to content

Commit 08af7f1

Browse files
committed
Fixed style and consistency
Added tslint and @types/screeps Added tslint to build process Removed old typings Separated several interfaces into their own files
1 parent 5954f1c commit 08af7f1

27 files changed

+859
-307
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tslint.enable": true,
3+
"tslint.configFile": "tslint.json",
4+
"tslint.autoFixOnSave": true,
5+
"tslint.ignoreDefinitionFiles": false
6+
}

dist/index.d.ts

Lines changed: 138 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,108 @@
1-
// Bundle for programs that are logically grouped
2-
interface IPosisBundle<IDefaultRootMemory> {
3-
// host will call that once, possibly outside of main loop, registers all bundle processes here
4-
install(registry: IPosisProcessRegistry): void;
5-
// image name of root process in the bundle, if any
6-
rootImageName?: string;
7-
// function returning default starting memory for root process, doubles as public parameter documentation
8-
makeDefaultRootMemory?: (override?: IDefaultRootMemory) => IDefaultRootMemory;
1+
/** Bundle for programs that are logically grouped */
2+
declare interface IPosisBundle<IDefaultRootMemory> {
3+
/** host will call that once, possibly outside of main loop, registers all bundle processes here */
4+
install(registry: IPosisProcessRegistry): void;
5+
/** image name of root process in the bundle, if any */
6+
rootImageName?: string;
7+
/** function returning default starting memory for root process, doubles as public parameter documentation */
8+
makeDefaultRootMemory?: (override?: IDefaultRootMemory) => IDefaultRootMemory;
99
}
10-
interface IPosisExtension {}
11-
interface IPosisKernel extends IPosisExtension {
10+
declare interface IPosisExtension { }
11+
declare interface IPosisInterfaces {
12+
baseKernel: IPosisKernel;
13+
spawn: IPosisSpawnExtension;
14+
sleep: IPosisSleepExtension;
15+
coop: IPosisCooperativeScheduling;
16+
}
17+
declare interface IPosisKernel extends IPosisExtension {
18+
19+
/**
20+
* beings running a process
21+
* @param imageName registered image for the process constructor
22+
* @param startContext context for a process
23+
*/
1224
startProcess(imageName: string, startContext: any): { pid: PosisPID; process: IPosisProcess } | undefined;
13-
// killProcess also kills all children of this process
14-
// note to the wise: probably absorb any calls to this that would wipe out your entire process tree.
25+
26+
/**
27+
* killProcess also kills all children of this process
28+
* note to the wise: probably absorb any calls to this that would wipe out your entire process tree.
29+
* @param pid
30+
*/
1531
killProcess(pid: PosisPID): void;
32+
33+
/**
34+
* gets the instance of a running process
35+
* @param pid
36+
* @returns process instance or undefined if the pid is invalid
37+
*/
1638
getProcessById(pid: PosisPID): IPosisProcess | undefined;
1739

18-
// passing undefined as parentId means "make me a root process"
19-
// i.e. one that will not be killed if another process is killed
40+
/**
41+
* passing undefined as parentId means "make me a root process"
42+
* i.e. one that will not be killed if another process is killed
43+
* @param pid
44+
* @param parentId
45+
* @returns `true` if process was successfully reparented
46+
*/
2047
setParent(pid: PosisPID, parentId?: PosisPID): boolean;
2148
}
22-
interface IPosisLogger {
49+
declare interface IPosisLogger {
2350
// because sometimes you don't want to eval arguments to ignore them
2451
debug(message: (() => string) | string): void;
2552
info(message: (() => string) | string): void;
2653
warn(message: (() => string) | string): void;
2754
error(message: (() => string) | string): void;
2855
}
29-
interface IPosisProcessContext {
30-
readonly memory: any; // private memory
31-
readonly imageName: string; // image name (maps to constructor)
32-
readonly id: PosisPID; // ID
33-
readonly parentId: PosisPID; // Parent ID
34-
readonly log: IPosisLogger; // Logger
35-
queryPosisInterface<T extends keyof PosisInterfaces>(interfaceId: T): PosisInterfaces[T] | undefined;
56+
declare interface IPosisProcess {
57+
/**
58+
* Main function, implement all process logic here.
59+
*/
60+
run(): void;
3661
}
37-
38-
// Bundle: Don't write to context object (including setting new props on it), host will likely freeze it anyway.
39-
// Host: freeze the thing!
40-
interface PosisProcessConstructor {
41-
new (context: IPosisProcessContext): IPosisProcess;
62+
/**
63+
* Bundle: Don't write to context object (including setting new props on it), host will likely freeze it anyway.
64+
* Host: freeze the thing!
65+
*/
66+
declare interface IPosisProcessConstructor {
67+
new(context: IPosisProcessContext): IPosisProcess;
4268
}
43-
44-
interface IPosisProcess {
45-
// Main function, implement all process logic here.
46-
run(): void;
69+
declare interface IPosisProcessContext {
70+
/** private memory */
71+
readonly memory: any;
72+
/** image name (maps to constructor) */
73+
readonly imageName: string;
74+
/** ID */
75+
readonly id: PosisPID;
76+
/** Parent ID */
77+
readonly parentId: PosisPID;
78+
/** Logger */
79+
readonly log: IPosisLogger;
80+
queryPosisExtension<T extends keyof IPosisInterfaces>(extensionId: T): IPosisInterfaces[T] | undefined;
4781
}
48-
interface IPosisProcessRegistry {
49-
// name your processes' image names with initials preceding, like ANI/MyCoolPosisProgram (but the actual class name can be whatever you want)
50-
// if your bundle consists of several programs you can pretend that we have a VFS: "ANI/MyBundle/BundledProgram1"
51-
register(imageName: string, constructor: new (context: IPosisProcessContext) => IPosisProcess): boolean;
52-
}
53-
type PosisPID = string | number;
54-
55-
interface PosisInterfaces {
56-
baseKernel: IPosisKernel;
57-
spawn: IPosisSpawnExtension;
58-
sleep: IPosisSleepExtension;
59-
coop: IPosisCooperativeScheduling;
82+
declare interface IPosisProcessRegistry {
83+
/**
84+
* name your processes' image names with initials preceding, like ANI/MyCoolPosisProgram (but the actual class name can be whatever you want)
85+
* if your bundle consists of several programs you can pretend that we have a VFS: "ANI/MyBundle/BundledProgram1"
86+
*/
87+
register(imageName: string, constructor: IPosisProcessConstructor): boolean;
6088
}
61-
interface IPosisCooperativeScheduling {
62-
// CPU used by process so far. Might include setup time kernel chooses to charge to the process.
89+
declare type PosisPID = string | number;
90+
declare interface IPosisCooperativeScheduling {
91+
/** CPU used by process so far. Might include setup time kernel chooses to charge to the process. */
6392
readonly used: number;
64-
// CPU budget scheduler allocated to this process.
93+
/** CPU budget scheduler allocated to this process. */
6594
readonly budget: number;
66-
// Process can wrap function and yield when it is ready to give up for the tick or can continue if CPU is available.
67-
// optionally yield a shutdown function to perform shutdown tasks like saving current state
95+
/**
96+
* Process can wrap function and yield when it is ready to give up for the tick or can continue if CPU is available.
97+
* optionally yield a shutdown function to perform shutdown tasks like saving current state
98+
*/
6899
wrap?(makeIterator: () => IterableIterator<void | (() => void)>): void;
69100
}
70-
interface IPosisSleepExtension {
71-
// puts currently running process to sleep for a given number of ticks
101+
declare interface IPosisSleepExtension {
102+
/**
103+
* puts currently running process to sleep for a given number of ticks
104+
* @param ticks number of ticks to sleep for
105+
*/
72106
sleep(ticks: number): void;
73107
}
74108
declare const enum EPosisSpawnStatus {
@@ -77,41 +111,64 @@ declare const enum EPosisSpawnStatus {
77111
SPAWNING,
78112
SPAWNED
79113
}
80-
81-
// NOT FINAL, discussions still underway in slack #posis
82-
83-
// process calls spawnCreep, checks status, if not ERROR, poll
84-
// getStatus until status is SPAWNED (Handling ERROR if it happens),
85-
// then call getCreep to get the creep itself
86-
87-
interface IPosisSpawnExtension {
88-
// Queues/Spawns the creep and returns an ID
89-
spawnCreep(opts: {
90-
// - 'rooms' are names of rooms associated with the creep being spawned.
91-
// Must contain at least one room. Host should select spawner based on its own logic
92-
// May contain additional rooms as a hints to host. Host may ignore hints
93-
rooms: string[],
94-
// - 'body' are body variants of the creep being spawned, at least one must be provided
95-
// Host must guarantee that the spawning creep will have one of provided bodies
96-
// Which body to spawn is up to host to select based on its own logic
97-
// Body templates should be sorted in the order of diminishing desirability
98-
body: string[][],
99-
// - 'priority' is spawn priority in range -1000 (the highest) to 1000 (the lowest)
100-
// Used as a hint for host's logic. Host may (but not guarantee) spawn creeps in priority order
101-
priority?: number,
102-
// - 'pid' is id of the process which is spawned creep associated to
103-
// Used as a hint for host's logic. Host may (but not guarantee) consider currently spawned creeps
104-
// detached, may (but not guarantee) remove scheduled creeps from queue on process termination
105-
pid?: PosisPID }): string;
106-
// Used to see if its been dropped from queue
114+
/*
115+
* NOT FINAL, discussions still underway in slack #posis
116+
*
117+
* process calls spawnCreep, checks status, if not ERROR, poll
118+
* getStatus until status is SPAWNED (Handling ERROR if it happens),
119+
* then call getCreep to get the creep itself
120+
*/
121+
declare interface IPosisSpawnExtension {
122+
/**
123+
* Queues/Spawns the requested creep
124+
* @param opts options for how and where to spawn the requested creep
125+
* @returns a unique id related to this specific creep request
126+
*/
127+
spawnCreep(opts: IPosisSpawnOptions): string;
128+
/**
129+
* Used to see if its been dropped from queue
130+
* @param id an id returned by spawnCreep()
131+
*/
107132
getStatus(id: string): {
108133
status: EPosisSpawnStatus
109134
message?: string
110-
}
135+
};
136+
/**
137+
* Get the currently spawning creep
138+
* @param id an id returned by spawnCreep()
139+
* @returns Creep object or undefined if the creep does not exist yet
140+
*/
111141
getCreep(id: string): Creep | undefined;
112142
/**
113-
* Cancel a previously ordered Creep (`spawnCreep`).
114-
* Returns `true` if it was cancelled successfully.
115-
*/
143+
* Cancel a previously ordered Creep (`spawnCreep`).
144+
* @param id an id returned by spawnCreep()
145+
* @returns `true` if it was cancelled successfully.
146+
*/
116147
cancelCreep(id: string): boolean;
117148
}
149+
declare interface IPosisSpawnOptions {
150+
/**
151+
* names of rooms associated with the creep being spawned.
152+
* Must contain at least one room. Host should select spawner based on its own logic
153+
* May contain additional rooms as a hints to host. Host may ignore hints
154+
*/
155+
rooms: string[];
156+
/**
157+
* body variants of the creep being spawned, at least one must be provided
158+
* host must guarantee that the spawning creep will have one of provided bodies
159+
* which body to spawn is up to host to select based on its own logic
160+
* body templates should be sorted in the order of diminishing desirability
161+
*/
162+
body: string[][];
163+
/**
164+
* spawn priority in range -1000 (the highest) to 1000 (the lowest)
165+
* used as a hint for host's logic. Host may (but not guarantee) spawn creeps in priority order
166+
*/
167+
priority?: number;
168+
/**
169+
* id of the process which is spawned creep associated to
170+
* Used as a hint for host's logic. Host may (but not guarantee) consider currently spawned creeps
171+
* detached, may (but not guarantee) remove scheduled creeps from queue on process termination
172+
*/
173+
pid?: PosisPID;
174+
}

examples/bundles/coop.bundle.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import shimCooperativeScheduling from '../shims/extensions/coop';
22

3+
// tslint:disable-next-line:class-name
34
class PosisTest_CooperativeSchedulingProcess implements IPosisProcess {
45

56
public static ImageName = "PosisTest/CooperativeSchedulingProcess";
67

78
protected coop: IPosisCooperativeScheduling;
89

910
public constructor(context: IPosisProcessContext) {
10-
this.coop = shimCooperativeScheduling(context.queryPosisInterface('coop'));
11+
this.coop = shimCooperativeScheduling(context.queryPosisExtension('coop'));
1112
}
1213

1314
public run(): void {
@@ -19,7 +20,7 @@ class PosisTest_CooperativeSchedulingProcess implements IPosisProcess {
1920
// save results
2021
};
2122
// and some pretty logging if you want to
22-
})
23+
});
2324
}
2425
}
2526

@@ -29,6 +30,6 @@ export default {
2930
registry.register(PosisTest_CooperativeSchedulingProcess.ImageName, PosisTest_CooperativeSchedulingProcess);
3031
},
3132

32-
rootImageName: PosisTest_CooperativeSchedulingProcess.ImageName,
33+
rootImageName: PosisTest_CooperativeSchedulingProcess.ImageName
3334

3435
} as IPosisBundle<undefined>;

0 commit comments

Comments
 (0)