-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
129 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters