node-ts-screeps-api 0.0.4 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -1
- package/README.md +3 -0
- package/dist/authInfo.d.ts +2 -4
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/src/apiType.d.ts +2 -2
- package/dist/src/rawApi.d.ts +7 -6
- package/dist/src/rawApiType/roomObjects.d.ts +74 -0
- package/dist/src/type.d.ts +1 -1
- package/package.json +1 -1
- package/src/api.ts +0 -1
- package/src/apiType.ts +2 -2
- package/src/rawApi.ts +8 -7
- package/src/rawApiType/roomObjects.ts +76 -0
- package/src/socket.ts +0 -1
- package/src/type.ts +1 -1
- package/test/unit/api.test.ts +29 -21
- package/tsconfig.json +0 -6
package/dist/src/apiType.d.ts
CHANGED
|
@@ -5,10 +5,10 @@ export declare type RawApiPostData<T extends RawApiType> = T extends "signinByPa
|
|
|
5
5
|
} : T extends "signinByToken" ? {
|
|
6
6
|
token: string;
|
|
7
7
|
} : T extends "getSegment" ? {
|
|
8
|
-
shard
|
|
8
|
+
shard?: string;
|
|
9
9
|
segment: number;
|
|
10
10
|
} : T extends "postSegment" ? {
|
|
11
|
-
shard
|
|
11
|
+
shard?: string;
|
|
12
12
|
segment: number;
|
|
13
13
|
data: string;
|
|
14
14
|
} : never;
|
package/dist/src/rawApi.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { RawApiPostData, RawApiReturnData } from "./apiType";
|
|
2
|
-
import { ApiConfig, AuthType, Badge, MyUserInfo, RequestOpts } from "./type";
|
|
2
|
+
import { ApiConfig, AuthType, Badge, BasicRequestMethod, MyUserInfo, RequestOpts } from "./type";
|
|
3
|
+
import { RoomObjectReturn } from "./rawApiType/roomObjects";
|
|
3
4
|
export declare class RawApi<T extends AuthType> {
|
|
4
5
|
apiConfig: ApiConfig<T>;
|
|
5
6
|
xToken?: string;
|
|
@@ -9,7 +10,7 @@ export declare class RawApi<T extends AuthType> {
|
|
|
9
10
|
constructor(apiConfig: ApiConfig<T>);
|
|
10
11
|
private setServer;
|
|
11
12
|
private gz;
|
|
12
|
-
|
|
13
|
+
req<T>(method: BasicRequestMethod, path: string, body?: {}, headers?: {}): Promise<T>;
|
|
13
14
|
inflate(data: string): Promise<string>;
|
|
14
15
|
auth(): Promise<RawApiReturnData<"signinByPassword">>;
|
|
15
16
|
getSegment(args: RawApiPostData<"getSegment">): Promise<RawApiReturnData<"getSegment">>;
|
|
@@ -38,7 +39,7 @@ export declare class RawApi<T extends AuthType> {
|
|
|
38
39
|
y: number;
|
|
39
40
|
structureType: string;
|
|
40
41
|
name: string;
|
|
41
|
-
shard
|
|
42
|
+
shard?: string;
|
|
42
43
|
}): Promise<{
|
|
43
44
|
ok: number;
|
|
44
45
|
result: {
|
|
@@ -63,11 +64,11 @@ export declare class RawApi<T extends AuthType> {
|
|
|
63
64
|
}>;
|
|
64
65
|
getRoomObjects(args: {
|
|
65
66
|
room: string;
|
|
66
|
-
shard
|
|
67
|
-
}): Promise<
|
|
67
|
+
shard?: string;
|
|
68
|
+
}): Promise<RoomObjectReturn>;
|
|
68
69
|
getEncodedRoomTerrain(args: {
|
|
69
70
|
room: string;
|
|
70
|
-
shard
|
|
71
|
+
shard?: string;
|
|
71
72
|
}): Promise<{
|
|
72
73
|
ok: number;
|
|
73
74
|
terrain: [{
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Badge } from "../type";
|
|
2
|
+
export interface RoomObjectReturn {
|
|
3
|
+
ok: 1;
|
|
4
|
+
objects: AnyRoomObjects[];
|
|
5
|
+
user: {
|
|
6
|
+
[name: string]: {
|
|
7
|
+
_id: string;
|
|
8
|
+
username: string;
|
|
9
|
+
badge: Badge;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export declare type AnyRoomObjects = Source | Mineral | Controller;
|
|
14
|
+
export declare type RoomObjectType = "source" | "mineral" | "controller";
|
|
15
|
+
export declare type SpecifiedRoomObject<T extends RoomObjectType> = T extends "source" ? Source : T extends "mineral" ? Mineral : T extends "controller" ? Controller : never;
|
|
16
|
+
export interface BasicRoomObject {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
type: RoomObjectType;
|
|
20
|
+
_id: string;
|
|
21
|
+
room: string;
|
|
22
|
+
}
|
|
23
|
+
export interface Source extends BasicRoomObject {
|
|
24
|
+
type: "source";
|
|
25
|
+
energy: number;
|
|
26
|
+
energyCapacity: number;
|
|
27
|
+
ticksToRegeneration: number;
|
|
28
|
+
invaderHarvested: number;
|
|
29
|
+
nextRegenerationTime: number;
|
|
30
|
+
}
|
|
31
|
+
export interface Mineral extends BasicRoomObject {
|
|
32
|
+
type: "mineral";
|
|
33
|
+
mineralType: string;
|
|
34
|
+
mineralAmount: number;
|
|
35
|
+
}
|
|
36
|
+
export interface Controller extends BasicRoomObject {
|
|
37
|
+
type: "controller";
|
|
38
|
+
level?: number;
|
|
39
|
+
/**
|
|
40
|
+
* 占领了该controller的玩家id
|
|
41
|
+
*
|
|
42
|
+
* @type {string}
|
|
43
|
+
* @memberof Controller
|
|
44
|
+
*/
|
|
45
|
+
user?: string;
|
|
46
|
+
/**
|
|
47
|
+
* 升级进度
|
|
48
|
+
*
|
|
49
|
+
* @type {number}
|
|
50
|
+
* @memberof Controller
|
|
51
|
+
*/
|
|
52
|
+
progress: number;
|
|
53
|
+
/**
|
|
54
|
+
* 将要在什么时候降级
|
|
55
|
+
*
|
|
56
|
+
* @type {number}
|
|
57
|
+
* @memberof Controller
|
|
58
|
+
*/
|
|
59
|
+
downgradeTime: number;
|
|
60
|
+
/**
|
|
61
|
+
* 上次启用的时间
|
|
62
|
+
*
|
|
63
|
+
* @type {number}
|
|
64
|
+
* @memberof Controller
|
|
65
|
+
*/
|
|
66
|
+
safeMode: number;
|
|
67
|
+
/**
|
|
68
|
+
* 可用次数
|
|
69
|
+
*
|
|
70
|
+
* @type {number}
|
|
71
|
+
* @memberof Controller
|
|
72
|
+
*/
|
|
73
|
+
safeModeAvailable: number;
|
|
74
|
+
}
|
package/dist/src/type.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export declare type AuthInfo<T extends AuthType> = T extends "signinByToken" ? {
|
|
|
11
11
|
password: string;
|
|
12
12
|
};
|
|
13
13
|
export interface HostInfo {
|
|
14
|
-
protocol: "http" | "https";
|
|
14
|
+
protocol: "http" | "https" | "localhost";
|
|
15
15
|
hostname: string;
|
|
16
16
|
port: number;
|
|
17
17
|
path: string;
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
package/src/apiType.ts
CHANGED
|
@@ -4,9 +4,9 @@ export type RawApiPostData<T extends RawApiType> = T extends "signinByPassword"
|
|
|
4
4
|
: T extends "signinByToken"
|
|
5
5
|
? { token: string }
|
|
6
6
|
: T extends "getSegment"
|
|
7
|
-
? { shard
|
|
7
|
+
? { shard?: string; segment: number }
|
|
8
8
|
: T extends "postSegment"
|
|
9
|
-
? { shard
|
|
9
|
+
? { shard?: string; segment: number; data: string }
|
|
10
10
|
: never;
|
|
11
11
|
|
|
12
12
|
export type RawApiReturnData<T extends RawApiType> = T extends "signinByPassword"
|
package/src/rawApi.ts
CHANGED
|
@@ -4,6 +4,7 @@ import axios, { AxiosInstance } from "axios";
|
|
|
4
4
|
import { promisify } from "util";
|
|
5
5
|
import { gunzip, inflate } from "zlib";
|
|
6
6
|
import { ApiConfig, AuthType, Badge, BasicRequestMethod, MyUserInfo, RequestOpts } from "./type";
|
|
7
|
+
import { RoomObjectReturn } from "./rawApiType/roomObjects";
|
|
7
8
|
|
|
8
9
|
const gunzipAsync = promisify(gunzip);
|
|
9
10
|
const inflateAsync = promisify(inflate);
|
|
@@ -15,10 +16,10 @@ export class RawApi<T extends AuthType> {
|
|
|
15
16
|
private baseUrl: string;
|
|
16
17
|
public constructor(apiConfig: ApiConfig<T>) {
|
|
17
18
|
this.apiConfig = apiConfig;
|
|
18
|
-
const { protocol, hostname, path } = this.apiConfig.hostInfo;
|
|
19
|
-
this.baseUrl =
|
|
19
|
+
const { protocol, hostname, path, port } = this.apiConfig.hostInfo;
|
|
20
|
+
this.baseUrl =
|
|
21
|
+
protocol !== "localhost" ? `${protocol}://${hostname}${path}` : `http://${hostname}:${port}${path}`;
|
|
20
22
|
}
|
|
21
|
-
|
|
22
23
|
private setServer(opts: Partial<RequestOpts>): void {
|
|
23
24
|
if (!this.opts) {
|
|
24
25
|
this.opts = {};
|
|
@@ -37,7 +38,7 @@ export class RawApi<T extends AuthType> {
|
|
|
37
38
|
const ret = (await gunzipAsync(buf)) as { toString: () => string };
|
|
38
39
|
return JSON.parse(ret.toString()) as T;
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
+
public async req<T>(method: BasicRequestMethod, path: string, body = {}, headers = {}): Promise<T> {
|
|
41
42
|
this.setServer({ method, url: this.baseUrl, headers });
|
|
42
43
|
const opts: RequestOpts = {
|
|
43
44
|
method,
|
|
@@ -123,7 +124,7 @@ export class RawApi<T extends AuthType> {
|
|
|
123
124
|
y: number;
|
|
124
125
|
structureType: string;
|
|
125
126
|
name: string;
|
|
126
|
-
shard
|
|
127
|
+
shard?: string;
|
|
127
128
|
}): Promise<{
|
|
128
129
|
ok: number;
|
|
129
130
|
result: {
|
|
@@ -149,13 +150,13 @@ export class RawApi<T extends AuthType> {
|
|
|
149
150
|
return this.req("POST", "/api/game/create-construction", args);
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
public async getRoomObjects(args: { room: string; shard
|
|
153
|
+
public async getRoomObjects(args: { room: string; shard?: string }): Promise<RoomObjectReturn> {
|
|
153
154
|
return this.req("GET", "/api/game/room-objects", args);
|
|
154
155
|
}
|
|
155
156
|
|
|
156
157
|
public async getEncodedRoomTerrain(args: {
|
|
157
158
|
room: string;
|
|
158
|
-
shard
|
|
159
|
+
shard?: string;
|
|
159
160
|
}): Promise<{ ok: number; terrain: [{ _id: string; room: string; terrain: string; type: string }] }> {
|
|
160
161
|
return this.req("GET", "/api/game/room-terrain", { ...args, encoded: 1 });
|
|
161
162
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Badge } from "../type";
|
|
2
|
+
|
|
3
|
+
export interface RoomObjectReturn {
|
|
4
|
+
ok: 1;
|
|
5
|
+
objects: AnyRoomObjects[];
|
|
6
|
+
user: { [name: string]: { _id: string; username: string; badge: Badge } };
|
|
7
|
+
}
|
|
8
|
+
export type AnyRoomObjects = Source | Mineral | Controller;
|
|
9
|
+
export type RoomObjectType = "source" | "mineral" | "controller";
|
|
10
|
+
export type SpecifiedRoomObject<T extends RoomObjectType> = T extends "source"
|
|
11
|
+
? Source
|
|
12
|
+
: T extends "mineral"
|
|
13
|
+
? Mineral
|
|
14
|
+
: T extends "controller"
|
|
15
|
+
? Controller
|
|
16
|
+
: never;
|
|
17
|
+
export interface BasicRoomObject {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
type: RoomObjectType;
|
|
21
|
+
_id: string;
|
|
22
|
+
room: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Source extends BasicRoomObject {
|
|
26
|
+
type: "source";
|
|
27
|
+
energy: number;
|
|
28
|
+
energyCapacity: number;
|
|
29
|
+
ticksToRegeneration: number;
|
|
30
|
+
invaderHarvested: number;
|
|
31
|
+
nextRegenerationTime: number;
|
|
32
|
+
}
|
|
33
|
+
export interface Mineral extends BasicRoomObject {
|
|
34
|
+
type: "mineral";
|
|
35
|
+
mineralType: string;
|
|
36
|
+
mineralAmount: number;
|
|
37
|
+
}
|
|
38
|
+
export interface Controller extends BasicRoomObject {
|
|
39
|
+
type: "controller";
|
|
40
|
+
level?: number;
|
|
41
|
+
/**
|
|
42
|
+
* 占领了该controller的玩家id
|
|
43
|
+
*
|
|
44
|
+
* @type {string}
|
|
45
|
+
* @memberof Controller
|
|
46
|
+
*/
|
|
47
|
+
user?: string;
|
|
48
|
+
/**
|
|
49
|
+
* 升级进度
|
|
50
|
+
*
|
|
51
|
+
* @type {number}
|
|
52
|
+
* @memberof Controller
|
|
53
|
+
*/
|
|
54
|
+
progress: number;
|
|
55
|
+
/**
|
|
56
|
+
* 将要在什么时候降级
|
|
57
|
+
*
|
|
58
|
+
* @type {number}
|
|
59
|
+
* @memberof Controller
|
|
60
|
+
*/
|
|
61
|
+
downgradeTime: number;
|
|
62
|
+
/**
|
|
63
|
+
* 上次启用的时间
|
|
64
|
+
*
|
|
65
|
+
* @type {number}
|
|
66
|
+
* @memberof Controller
|
|
67
|
+
*/
|
|
68
|
+
safeMode: number;
|
|
69
|
+
/**
|
|
70
|
+
* 可用次数
|
|
71
|
+
*
|
|
72
|
+
* @type {number}
|
|
73
|
+
* @memberof Controller
|
|
74
|
+
*/
|
|
75
|
+
safeModeAvailable: number;
|
|
76
|
+
}
|
package/src/socket.ts
CHANGED
package/src/type.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type AuthInfo<T extends AuthType> = T extends "signinByToken"
|
|
|
12
12
|
: { type: T; email: string; password: string };
|
|
13
13
|
|
|
14
14
|
export interface HostInfo {
|
|
15
|
-
protocol: "http" | "https";
|
|
15
|
+
protocol: "http" | "https" | "localhost";
|
|
16
16
|
hostname: string;
|
|
17
17
|
port: number;
|
|
18
18
|
path: string;
|
package/test/unit/api.test.ts
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
import { ScreepsApi } from "index";
|
|
2
1
|
import * as assert from "assert";
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
|
|
3
|
+
import { apiConfig } from "../../authInfo";
|
|
4
|
+
import { writeFileSync } from "fs";
|
|
5
|
+
import { ScreepsApi } from "../../src";
|
|
6
|
+
import { ApiConfig } from "../../src/type";
|
|
5
7
|
// 上面的userData需要自己在根目录创建,示例参照根目录的authInfoSample.ts
|
|
6
8
|
describe("api", () => {
|
|
7
9
|
describe("create api", () => {
|
|
8
|
-
const apiConfig: ApiConfig<"signinByPassword"> = {
|
|
9
|
-
authInfo: {
|
|
10
|
-
type: "signinByPassword",
|
|
11
|
-
email: userData.email,
|
|
12
|
-
password: userData.password
|
|
13
|
-
},
|
|
14
|
-
hostInfo: {
|
|
15
|
-
protocol: "https",
|
|
16
|
-
port: 443,
|
|
17
|
-
path: "/",
|
|
18
|
-
hostname: "screeps.com"
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
10
|
const api = new ScreepsApi(apiConfig);
|
|
22
11
|
const rawApi = api.rawApi;
|
|
23
12
|
const socket = api.socket;
|
|
@@ -39,7 +28,10 @@ describe("api", () => {
|
|
|
39
28
|
|
|
40
29
|
it("test socket", async () => {
|
|
41
30
|
let hasCpu = false;
|
|
42
|
-
|
|
31
|
+
if (api.apiConfig.hostInfo.protocol === "localhost") {
|
|
32
|
+
console.log("socket is not usable in private server");
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
43
35
|
await api.auth();
|
|
44
36
|
await socket.connect();
|
|
45
37
|
await socket.subscribe("cpu");
|
|
@@ -63,7 +55,7 @@ describe("api", () => {
|
|
|
63
55
|
});
|
|
64
56
|
|
|
65
57
|
it("sign in", async () => {
|
|
66
|
-
const data = await
|
|
58
|
+
const data = await api.auth();
|
|
67
59
|
console.log(data);
|
|
68
60
|
});
|
|
69
61
|
|
|
@@ -81,13 +73,29 @@ describe("api", () => {
|
|
|
81
73
|
});
|
|
82
74
|
|
|
83
75
|
it("get roomObjects", async () => {
|
|
84
|
-
const data = await rawApi.getRoomObjects({
|
|
85
|
-
console.log(
|
|
76
|
+
const data = await rawApi.getRoomObjects({ room: "E4N1" });
|
|
77
|
+
console.log(data.objects?.[0]?._id);
|
|
78
|
+
// const name = "test-E34S21";
|
|
79
|
+
// writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
|
|
86
80
|
});
|
|
87
81
|
|
|
88
82
|
it("get encoded roomTerrain", async () => {
|
|
89
|
-
const data = await rawApi.getEncodedRoomTerrain({
|
|
83
|
+
const data = await rawApi.getEncodedRoomTerrain({ room: "E4N1" });
|
|
84
|
+
console.log(data);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("get me", async () => {
|
|
88
|
+
const data = await rawApi.me();
|
|
89
|
+
console.log(data);
|
|
90
|
+
const name = "test-me";
|
|
91
|
+
writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("test", async () => {
|
|
95
|
+
const data = await rawApi.req("GET", "/api/user/overview", { interval: 8, statName: "energy" });
|
|
90
96
|
console.log(data);
|
|
97
|
+
const name = "test-test";
|
|
98
|
+
writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
|
|
91
99
|
});
|
|
92
100
|
});
|
|
93
101
|
});
|
package/tsconfig.json
CHANGED
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
"target": "es2017",
|
|
8
8
|
"moduleResolution": "Node",
|
|
9
9
|
"outDir": "dist",
|
|
10
|
-
"baseUrl": "src/",
|
|
11
10
|
"sourceMap": true,
|
|
12
11
|
"strict": true,
|
|
13
12
|
"experimentalDecorators": true,
|
|
@@ -17,11 +16,6 @@
|
|
|
17
16
|
"esModuleInterop": true,
|
|
18
17
|
"declaration": true,
|
|
19
18
|
"resolveJsonModule": true,
|
|
20
|
-
"paths": {
|
|
21
|
-
"@/*": [
|
|
22
|
-
"src/*"
|
|
23
|
-
]
|
|
24
|
-
}
|
|
25
19
|
},
|
|
26
20
|
"exclude": [
|
|
27
21
|
"node_modules",
|