From 3dfff7ea545b48550ada8788f0f12fc12f824036 Mon Sep 17 00:00:00 2001 From: JianMiau Date: Sun, 10 Sep 2023 15:29:58 +0800 Subject: [PATCH] [add] set ESlint --- .eslintignore | 2 ++ .eslintrc.json | 2 +- src/Engine/Utils/Number/NumberEx.ts | 20 ++++++++++---------- src/api/room/ApiCreate.ts | 2 +- src/api/room/ApiJoin.ts | 2 +- src/api/room/ApiList.ts | 5 +++-- src/component/Lobby/Lobby.ts | 2 +- src/component/Room/Room.ts | 3 +++ src/index.ts | 5 +---- 9 files changed, 23 insertions(+), 20 deletions(-) create mode 100644 .eslintignore diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..d813ca5 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +src/utils +src/Engine \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index 85b16b5..df1f122 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -342,7 +342,7 @@ "memberVariableDeclaration": true, "parameter": true, "propertyDeclaration": true, - "variableDeclaration": false, + "variableDeclaration": true, "variableDeclarationIgnoreFunction": true } ] diff --git a/src/Engine/Utils/Number/NumberEx.ts b/src/Engine/Utils/Number/NumberEx.ts index a1fbfa1..18d503d 100644 --- a/src/Engine/Utils/Number/NumberEx.ts +++ b/src/Engine/Utils/Number/NumberEx.ts @@ -19,10 +19,10 @@ export module NumberEx { if (others.length > 0) { return times(times(num1, num2), others[0], ...others.slice(1)); } - const num1Changed = num1.Float2Fixed(); - const num2Changed = num2.Float2Fixed(); - const baseNum = num1.DigitLength() + num2.DigitLength(); - const leftValue = num1Changed * num2Changed; + const num1Changed: number = num1.Float2Fixed(); + const num2Changed: number = num2.Float2Fixed(); + const baseNum: number = num1.DigitLength() + num2.DigitLength(); + const leftValue: number = num1Changed * num2Changed; checkBoundary(leftValue); @@ -36,7 +36,7 @@ export module NumberEx { if (others.length > 0) { return plus(plus(num1, num2), others[0], ...others.slice(1)); } - const baseNum = Math.pow(10, Math.max(num1.DigitLength(), num2.DigitLength())); + const baseNum: number = Math.pow(10, Math.max(num1.DigitLength(), num2.DigitLength())); return (times(num1, baseNum) + times(num2, baseNum)) / baseNum; } @@ -47,7 +47,7 @@ export module NumberEx { if (others.length > 0) { return minus(minus(num1, num2), others[0], ...others.slice(1)); } - const baseNum = Math.pow(10, Math.max(num1.DigitLength(), num2.DigitLength())); + const baseNum: number = Math.pow(10, Math.max(num1.DigitLength(), num2.DigitLength())); return (times(num1, baseNum) - times(num2, baseNum)) / baseNum; } @@ -58,8 +58,8 @@ export module NumberEx { if (others.length > 0) { return divide(divide(num1, num2), others[0], ...others.slice(1)); } - const num1Changed = num1.Float2Fixed(); - const num2Changed = num2.Float2Fixed(); + const num1Changed: number = num1.Float2Fixed(); + const num2Changed: number = num2.Float2Fixed(); checkBoundary(num1Changed); checkBoundary(num2Changed); return times((num1Changed / num2Changed), Math.pow(10, num2.DigitLength() - num1.DigitLength())); @@ -69,11 +69,11 @@ export module NumberEx { * 四舍五入 */ export function round(num: number, ratio: number): number { - const base = Math.pow(10, ratio); + const base: number = Math.pow(10, ratio); return divide(Math.round(times(num, base)), base); } - let _boundaryCheckingState = false; + let _boundaryCheckingState: boolean = false; /** * 是否进行边界检查 * @param flag 标记开关,true 为开启,false 为关闭 diff --git a/src/api/room/ApiCreate.ts b/src/api/room/ApiCreate.ts index 0a32981..202d7f5 100644 --- a/src/api/room/ApiCreate.ts +++ b/src/api/room/ApiCreate.ts @@ -5,7 +5,7 @@ import { ServiceType } from "../../shared/protocols/serviceProto"; export default async function (call: ApiCall) { const conn: BaseConnection = call.conn; - const room = new Room(); + const room: Room = new Room(); room.Join(conn); conn.Room = room; call.succ(room.RoomId); diff --git a/src/api/room/ApiJoin.ts b/src/api/room/ApiJoin.ts index de13ba6..2f0d52a 100644 --- a/src/api/room/ApiJoin.ts +++ b/src/api/room/ApiJoin.ts @@ -6,7 +6,7 @@ import { ServiceType } from "../../shared/protocols/serviceProto"; export default async function (call: ApiCall) { const { roomId } = call.req; const conn: BaseConnection = call.conn; - const room = Room.GetRoom(roomId); + const room: Room = Room.GetRoom(roomId); if (room) { if (room.ConnCount() >= 2) { call.error("房間已滿"); diff --git a/src/api/room/ApiList.ts b/src/api/room/ApiList.ts index 9fbfc8a..d4271a2 100644 --- a/src/api/room/ApiList.ts +++ b/src/api/room/ApiList.ts @@ -1,11 +1,12 @@ import { ApiCall } from "tsrpc"; import Lobby from "../../component/Lobby/Lobby"; +import Room from "../../component/Room/Room"; import { ReqList, ResList } from "../../shared/protocols/room/PtlList"; export default async function (call: ApiCall) { const data: any[] = []; - for (let i = 0; i < Lobby.Room.length; i++) { - const room = Lobby.Room[i]; + for (let i: number = 0; i < Lobby.Room.length; i++) { + const room: Room = Lobby.Room[i]; data.push(room.RoomId); } call.succ(data); diff --git a/src/component/Lobby/Lobby.ts b/src/component/Lobby/Lobby.ts index 8221adc..e90aa0d 100644 --- a/src/component/Lobby/Lobby.ts +++ b/src/component/Lobby/Lobby.ts @@ -28,7 +28,7 @@ export default class Lobby { /** DelConns */ public static DelConns(conn: BaseConnection): void { - for (let i = 0; i < this.conns.length; i++) { + for (let i: number = 0; i < this.conns.length; i++) { if (this.conns[i] === conn) { this.conns.splice(i, 1); break; diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index dbaaf03..b0511c2 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -1,5 +1,6 @@ import { BaseConnection, WsConnection } from "tsrpc"; import { server } from "../.."; +import { RandomEx } from "../../Engine/Utils/Number/RandomEx"; import { ServiceType } from "../../shared/protocols/serviceProto"; @@ -38,6 +39,8 @@ export default class Room { // 给每个新房间生成一个唯一的 ID this.RoomId = ++Room.maxRoomId; Room.rooms[this.RoomId] = this; + + const now: number = RandomEx.GetInt(0, 2); } //#endregion diff --git a/src/index.ts b/src/index.ts index 36d6c0a..9dc13f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ dotenv.config(); // Create the Server const port: number = +process.env.PORT || 3000; -export const server = new WsServer(serviceProto, { +export const server: WsServer = new WsServer(serviceProto, { port: port, // Remove this to use binary mode (remove from the client too) json: true @@ -28,9 +28,6 @@ async function init() { await server.autoImplementApi(path.resolve(__dirname, "api")); // Prepare something... (e.g. connect the db) - // server.flows.postConnectFlow.push((conn) => { - // Lobby.AddConns(conn); - // }); server.flows.postConnectFlow.push((conn: BaseConnection) => { Lobby.AddConns(conn); console.log(`${conn.ip} is connected`);