node-ts-screeps-api 0.0.12 → 0.0.14

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/authInfoSample.ts CHANGED
@@ -5,7 +5,8 @@ const userData = {
5
5
  email: "notMyEmail@abc.com",
6
6
  password: "notMyPassword"
7
7
  };
8
- export const apiConfig: ApiConfig<"signinByPassword"> = {
8
+
9
+ export const localServerApiConfig: ApiConfig<"signinByPassword"> = {
9
10
  authInfo: {
10
11
  type: "signinByPassword",
11
12
  email: userData.email,
@@ -18,3 +19,17 @@ export const apiConfig: ApiConfig<"signinByPassword"> = {
18
19
  hostname: "127.0.0.1"
19
20
  }
20
21
  };
22
+
23
+ export const officialServerApiConfig: ApiConfig<"signinByPassword"> = {
24
+ authInfo: {
25
+ type: "signinByPassword",
26
+ email: userData.email,
27
+ password: userData.password
28
+ },
29
+ hostInfo: {
30
+ protocol: "https",
31
+ port: 443,
32
+ path: "/",
33
+ hostname: "screeps.com"
34
+ }
35
+ };
@@ -75,7 +75,33 @@ export interface Controller extends BasicRoomObject {
75
75
  export interface Portal extends BasicRoomObject {
76
76
  type: "portal";
77
77
  destination: {
78
+ /**
79
+ * 表示目的地room。
80
+ *
81
+ * @type {string}
82
+ */
78
83
  room: string;
79
- shard: string;
84
+ /**
85
+ * 表示目的地shard,如果没有则目的地为同shard位置。
86
+ *
87
+ * @type {string}
88
+ */
89
+ shard?: string;
80
90
  };
91
+ /**
92
+ * portal的过期时间。
93
+ *
94
+ * 当到达该时间时,该属性消失,portal会增加一个decayTime属性,表示过期的Game.time。
95
+ *
96
+ * @type {number}
97
+ * @memberof Portal
98
+ */
99
+ unstableDate?: number;
100
+ /**
101
+ * portal过期的Game.time。
102
+ *
103
+ * @type {number}
104
+ * @memberof Portal
105
+ */
106
+ decayTime?: number;
81
107
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-ts-screeps-api",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "node-ts-screeps-api",
5
5
  "main": "dist/index.js",
6
6
  "typings": "./dist/src/index.d.ts",
package/src/rawApi.ts CHANGED
@@ -94,6 +94,18 @@ export class RawApi<T extends AuthType> {
94
94
  return this.req("POST", "/api/user/memory-segment", args);
95
95
  }
96
96
 
97
+ public async getMemory(args: { path?: string; shard?: string }): Promise<{ ok: number; data: string }> {
98
+ return this.req("GET", "/api/user/memory", args);
99
+ }
100
+
101
+ public async postMemory(args: {
102
+ path?: string;
103
+ value: string;
104
+ shard?: string;
105
+ }): Promise<{ ok: number; data: string }> {
106
+ return this.req("POST", "/api/user/memory", args);
107
+ }
108
+
97
109
  public async findUser(username: string): Promise<{
98
110
  ok: number;
99
111
  user: {
@@ -81,7 +81,33 @@ export interface Controller extends BasicRoomObject {
81
81
  export interface Portal extends BasicRoomObject {
82
82
  type: "portal";
83
83
  destination: {
84
+ /**
85
+ * 表示目的地room。
86
+ *
87
+ * @type {string}
88
+ */
84
89
  room: string;
85
- shard: string;
90
+ /**
91
+ * 表示目的地shard,如果没有则目的地为同shard位置。
92
+ *
93
+ * @type {string}
94
+ */
95
+ shard?: string;
86
96
  };
97
+ /**
98
+ * portal的过期时间。
99
+ *
100
+ * 当到达该时间时,该属性消失,portal会增加一个decayTime属性,表示过期的Game.time。
101
+ *
102
+ * @type {number}
103
+ * @memberof Portal
104
+ */
105
+ unstableDate?: number;
106
+ /**
107
+ * portal过期的Game.time。
108
+ *
109
+ * @type {number}
110
+ * @memberof Portal
111
+ */
112
+ decayTime?: number;
87
113
  }
@@ -1,29 +1,28 @@
1
1
  import * as assert from "assert";
2
2
 
3
- import { apiConfig } from "../../authInfo";
3
+ import { localServerApiConfig } from "../../authInfo";
4
4
  import { writeFileSync } from "fs";
5
5
  import { ScreepsApi } from "../../src";
6
- import { ApiConfig } from "../../src/type";
7
6
  // 上面的userData需要自己在根目录创建,示例参照根目录的authInfoSample.ts
8
7
  describe("api", () => {
9
8
  describe("create api", () => {
10
- const api = new ScreepsApi(apiConfig);
9
+ const api = new ScreepsApi(localServerApiConfig);
11
10
  const rawApi = api.rawApi;
12
11
  const socket = api.socket;
13
12
  it("is apiConfig stored properly", () => {
14
- assert.deepStrictEqual(api.apiConfig, apiConfig);
13
+ assert.deepStrictEqual(api.apiConfig, localServerApiConfig);
15
14
  });
16
15
 
17
16
  it("throw Error when unauthorized", async () => {
18
- // let anErr;
19
- // try {
20
- // await rawApi.postSegment({ shard: "shard3", segment: 30, data: "test" });
21
- // } catch (err) {
22
- // anErr = err;
23
- // const message = (err as Error).message;
24
- // assert.strictEqual(message, `{"error":"unauthorized"}`);
25
- // }
26
- // if (!anErr) assert.fail("no error was thrown");
17
+ let anErr;
18
+ try {
19
+ await rawApi.postSegment({ segment: 30, data: "test" });
20
+ } catch (err) {
21
+ anErr = err;
22
+ const message = (err as Error).message;
23
+ assert.strictEqual(message, `{"error":"unauthorized"}`);
24
+ }
25
+ if (!anErr) assert.fail("no error was thrown");
27
26
  });
28
27
 
29
28
  it("test socket", async () => {
@@ -61,16 +60,16 @@ describe("api", () => {
61
60
 
62
61
  const testStr = `test${Date.now()}`;
63
62
  it("post segment", async () => {
64
- // const data = await rawApi.postSegment({ shard: "shard3", segment: 30, data: testStr });
65
- // console.log(data);
66
- // assert.strictEqual(data.ok, 1);
63
+ const data = await rawApi.postSegment({ shard: "shard3", segment: 30, data: testStr });
64
+ console.log(data);
65
+ assert.strictEqual(data.ok, 1);
67
66
  });
68
67
 
69
- // it("get segment", async () => {
70
- // const data = await rawApi.getSegment({ shard: "shard3", segment: 30 });
71
- // console.log(data);
72
- // assert.strictEqual(data.data, testStr);
73
- // });
68
+ it("get segment", async () => {
69
+ const data = await rawApi.getSegment({ shard: "shard3", segment: 30 });
70
+ console.log(data);
71
+ assert.strictEqual(data.data, testStr);
72
+ });
74
73
 
75
74
  it("get world size", async () => {
76
75
  const data = await rawApi.getWorldSize({ shard: "shard3" });
@@ -0,0 +1,33 @@
1
+ import * as assert from "assert";
2
+
3
+ import { officialServerApiConfig } from "../../authInfo";
4
+ import { ScreepsApi } from "../../src";
5
+ import { writeFileSync } from "fs";
6
+ // 上面的userData需要自己在根目录创建,示例参照根目录的authInfoSample.ts
7
+ describe("api", () => {
8
+ describe("create api", () => {
9
+ const api = new ScreepsApi(officialServerApiConfig);
10
+ const rawApi = api.rawApi;
11
+ const socket = api.socket;
12
+ it("sign in", async () => {
13
+ const data = await api.auth();
14
+ console.log(data);
15
+ });
16
+
17
+ it("is apiConfig stored properly", async () => {
18
+ assert.deepStrictEqual(api.apiConfig, officialServerApiConfig);
19
+ });
20
+
21
+ it("test getMemory", async () => {
22
+ const data = await rawApi.getMemory({ shard: "shard3" });
23
+ const name = "memory";
24
+ writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
25
+ });
26
+
27
+ it("test getMemory by path", async () => {
28
+ const data = await rawApi.getMemory({ path: "creeps", shard: "shard3" });
29
+ const name = "memory-creeps";
30
+ writeFileSync(`test/data/roomObjects/${name}.json`, JSON.stringify(data, null, 4));
31
+ });
32
+ });
33
+ });