Skip to content

Commit

Permalink
feat: 实现 common 库中的 Terrain 相关方法
Browse files Browse the repository at this point in the history
  • Loading branch information
LokiSharp committed Oct 24, 2023
1 parent 07b7109 commit 1912461
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 18 deletions.
14 changes: 3 additions & 11 deletions common/src/Rpc/RpcClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Socket } from "net";
import { EventEmitter } from "events";
import { JSONFrameStream } from "@/Rpc/JSONFrameStream";
import { Defer, Reject, Resolve, RpcClientFrameObj } from "@/types";
import { Defer, RpcClientFrameObj } from "@/types";
import { makeDefer } from "@/utils";

export class RpcClient {
public socket: Socket;
Expand Down Expand Up @@ -39,15 +40,6 @@ export class RpcClient {
this.defers.delete(String(obj.id));
}

public static defer(): Defer {
let resolve = ((): void => {}) as Resolve,
reject = ((): void => {}) as Reject;
const defer = new Promise((res, rej) => {
[resolve, reject] = [res, rej];
});
return { defer, reject, resolve };
}

public request(method: string, ...args: unknown[]): Defer {
this.requestId++;
const request = {
Expand All @@ -56,7 +48,7 @@ export class RpcClient {
args,
};
this.socket.write(JSONFrameStream.makeFrame(request));
const defered = RpcClient.defer();
const defered = makeDefer();
this.defers.set(String(this.requestId), defered);
return defered;
}
Expand Down
6 changes: 4 additions & 2 deletions common/src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import net from "net";
import { RpcClient } from "@/Rpc/RpcClient";
import { config } from "@/ConfigManager";
import { DataBaseStorage, Defer } from "@/types";
import { makeDefer } from "@/utils";

export let connected = false;
export let resetAllData: () => Defer;
export const db: DataBaseStorage = {} as DataBaseStorage;
Expand Down Expand Up @@ -33,8 +35,8 @@ export function storageConnect(): Promise<unknown> {
);
const rpcClient = new RpcClient(socket);

const defer = RpcClient.defer();
const resetDefer = RpcClient.defer();
const defer = makeDefer();
const resetDefer = makeDefer();

function resetInterceptor(
fn: (...args: unknown[]) => Defer,

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 16.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 16.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 16.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, latest)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20.x)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest, latest)

'args' is defined but never used

Check warning on line 42 in common/src/Storage.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest, latest)

'args' is defined but never used
Expand Down
78 changes: 75 additions & 3 deletions common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,79 @@
import { ConfigManager } from "@/ConfigManager";
export { RpcServer } from "@/Rpc/RpcServer";
export { RpcClient } from "@/Rpc/RpcClient";
import * as storage from "@/Storage";
import { JSONFrameStream } from "@/Rpc/JSONFrameStream";
import { RpcServer } from "@/Rpc/RpcServer";
import { RpcClient } from "@/Rpc/RpcClient";
import { Defer, Terrain } from "@/types";
import { makeDefer } from "@/utils";
import { createServer } from "net";
import * as _ from "lodash";

export const common = {
configManager: new ConfigManager(),
};
export class Common {
public configManager = new ConfigManager();
public storage = storage;
public rpc = {
JSONFrameStream,
RpcServer,
RpcClient,
};
public findPort(port: number): Defer {
const defer = makeDefer();
const server = createServer((socket) => socket.end());
server.listen(port, (): void => {
server.once("close", (): void => {
defer.resolve(String(port));
});
server.close();
});
server.on("error", (): void => {
defer.resolve(String(this.findPort.bind(this)(port + 1)));
});
return defer;
}

public encodeTerrain(terrain: Terrain[]): string {
let result = "";
for (let y = 0; y < 50; y++) {
for (let x = 0; x < 50; x++) {
const objects = _.filter(terrain, { x, y });
let code = 0;
if (_.some(objects, { type: "wall" })) {
code = code | 1;
}
if (_.some(objects, { type: "swamp" })) {
code = code | 2;
}
result = result + code;
}
}
return result;
}

public decodeTerrain(str: string, room: string): Terrain[] {
const result = [];

for (let y = 0; y < 50; y++) {
for (let x = 0; x < 50; x++) {
const code = str.charAt(y * 50 + x);
if (code && 1) {
result.push({ room, x, y, type: "wall" });
}
if (code && 2) {
result.push({ room, x, y, type: "swamp" });
}
}
}
return result;
}

public checkTerrain(
terrainStr: string,
x: number,
y: number,
mask: number,
): boolean {
return (parseInt(terrainStr.charAt(y * 50 + x)) & mask) > 0;
}
}
2 changes: 2 additions & 0 deletions common/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ export type DataBaseMethod =
| "update"
| "bulk"
| "findEx";

export type Terrain = { type: string; x: number; y: number; room?: string };
10 changes: 10 additions & 0 deletions common/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Defer, Reject, Resolve } from "@/types";

export function makeDefer(): Defer {
let resolve = ((): void => {}) as Resolve,
reject = ((): void => {}) as Reject;
const defer = new Promise((res, rej) => {
[resolve, reject] = [res, rej];
});
return { defer, reject, resolve };
}
30 changes: 30 additions & 0 deletions common/tests/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Common } from "@/index";

test("findPort", async () => {
const common = new Common();
const defer = common.findPort(8080);
await defer.defer;
});

test("decodeTerrain", async () => {
const common = new Common();
const terrain = [{ type: "wall", x: 0, y: 0 }];
const terrainString = common.encodeTerrain(terrain);
console.log(terrainString);
});

test("encodeTerrain", async () => {
const common = new Common();
const terrain = [{ type: "wall", x: 0, y: 0 }];
const terrainString = common.encodeTerrain(terrain);
const result = common.decodeTerrain(terrainString, "testRoom");
expect(result[0].type).toBe(terrain[0].type);
});

test("checkTerrain", async () => {
const common = new Common();
const terrain = [{ type: "wall", x: 0, y: 0 }];
const terrainString = common.encodeTerrain(terrain);
const result = common.checkTerrain(terrainString, 0, 0, 1);
expect(result).toBe(true);
});
4 changes: 3 additions & 1 deletion driver/src/runtime/UserVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import * as v8 from "v8";
import fs from "fs";
import { Metric, NodeJS, StaticTerrainData, VM } from "@/runtime/types";
import RuntimeGlobal = NodeJS.RuntimeGlobal;
import { common } from "@neo-screeps/common";
import { Common } from "@neo-screeps/common";

const common = new Common();

export class UserVM {
public vms: { [userId: string]: VM } = {};
Expand Down
3 changes: 2 additions & 1 deletion storage/src/Database.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { common } from "@neo-screeps/common";
import { Common } from "@neo-screeps/common";
import fs from "fs";
import _ from "lodash";
import Loki, { Collection } from "lokijs";

const common = new Common();
const config = common.configManager.config;

export class DataBase {
Expand Down

0 comments on commit 1912461

Please sign in to comment.