[mod] lint:fix
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import Client from "../../../../component/Client/Client"
|
||||
import Client from "../../../../component/Client/Client";
|
||||
|
||||
|
||||
export interface INetResponse<TResponse> {
|
||||
|
||||
@@ -3,7 +3,7 @@ declare interface Number {
|
||||
|
||||
/**
|
||||
* 金額每三位數(千)加逗號, 並且補到小數點第2位
|
||||
* 輸出 41,038,560.00
|
||||
* 輸出 41,038,560.00
|
||||
* @param precision 補到小數點第幾位
|
||||
* @param isPadZero 是否要補零
|
||||
* */
|
||||
@@ -11,24 +11,24 @@ declare interface Number {
|
||||
/**
|
||||
* 基本4位數(9,999-999B-T)
|
||||
* */
|
||||
ExTransferToBMK(precision?: number,offset?: number): string;
|
||||
ExTransferToBMK(precision?: number, offset?: number): string;
|
||||
/**
|
||||
* 數字轉字串, 頭補0
|
||||
* @param size
|
||||
* @param size
|
||||
*/
|
||||
Pad(size: number): string;
|
||||
/**
|
||||
* 四捨五入到小數點第X位 (同server計算規則)
|
||||
* @param precision
|
||||
* @param precision
|
||||
*/
|
||||
ExToNumRoundDecimal(precision: number): number;
|
||||
/**
|
||||
* 無條件捨去到小數點第X位
|
||||
* @param precision
|
||||
* @param precision
|
||||
*/
|
||||
ExToNumFloorDecimal(precision: number): number;
|
||||
/**
|
||||
* 無條件捨去強制保留X位小數,如:2,會在2後面補上00.即2.00
|
||||
* 無條件捨去強制保留X位小數,如:2,會在2後面補上00.即2.00
|
||||
* @param precision 補到小數點第幾位
|
||||
* @param isPadZero 是否要補零
|
||||
*/
|
||||
@@ -51,26 +51,26 @@ declare interface Number {
|
||||
|
||||
}
|
||||
|
||||
Number.prototype.ExFormatNumberWithComma || Object.defineProperty(Number.prototype, 'ExFormatNumberWithComma', {
|
||||
Number.prototype.ExFormatNumberWithComma || Object.defineProperty(Number.prototype, "ExFormatNumberWithComma", {
|
||||
enumerable: false,
|
||||
value: function (precision: number = 2, isPadZero: boolean = true) {
|
||||
|
||||
// let arr = String(this).split('.');
|
||||
let arr = this.ExToStringFloorDecimal(precision, isPadZero).split('.');
|
||||
let num = arr[0], result = '';
|
||||
let arr = this.ExToStringFloorDecimal(precision, isPadZero).split(".");
|
||||
let num = arr[0], result = "";
|
||||
while (num.length > 3) {
|
||||
result = ',' + num.slice(-3) + result;
|
||||
result = "," + num.slice(-3) + result;
|
||||
num = num.slice(0, num.length - 3);
|
||||
}
|
||||
if (num.length > 0) result = num + result;
|
||||
return arr[1] ? result + '.' + arr[1] : result;
|
||||
return arr[1] ? result + "." + arr[1] : result;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
Number.prototype.ExTransferToBMK || Object.defineProperty(Number.prototype, 'ExTransferToBMK', {
|
||||
Number.prototype.ExTransferToBMK || Object.defineProperty(Number.prototype, "ExTransferToBMK", {
|
||||
enumerable: false,
|
||||
value: function (precision: number=2,offset: number = 0) {
|
||||
value: function (precision: number = 2, offset: number = 0) {
|
||||
/**千 */
|
||||
let MONEY_1K: number = 1000;
|
||||
/**萬 */
|
||||
@@ -118,36 +118,36 @@ Number.prototype.ExTransferToBMK || Object.defineProperty(Number.prototype, 'ExT
|
||||
return this.ExFormatNumberWithComma(precision);
|
||||
}
|
||||
}
|
||||
})
|
||||
Number.prototype.Pad || Object.defineProperty(Number.prototype, 'Pad', {
|
||||
});
|
||||
Number.prototype.Pad || Object.defineProperty(Number.prototype, "Pad", {
|
||||
enumerable: false,
|
||||
value: function (size: number) {
|
||||
let s = this + "";
|
||||
while (s.length < size) s = "0" + s;
|
||||
return s;
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToNumRoundDecimal || Object.defineProperty(Number.prototype, 'ExToNumRoundDecimal', {
|
||||
});
|
||||
Number.prototype.ExToNumRoundDecimal || Object.defineProperty(Number.prototype, "ExToNumRoundDecimal", {
|
||||
enumerable: false,
|
||||
value: function (precision: number) {
|
||||
return Math.round(Math.round(this * Math.pow(10, (precision || 0) + 1)) / 10) / Math.pow(10, (precision || 0));
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToInt || Object.defineProperty(Number.prototype, 'ExToInt',{
|
||||
});
|
||||
Number.prototype.ExToInt || Object.defineProperty(Number.prototype, "ExToInt", {
|
||||
enumerable: false,
|
||||
value: function (){
|
||||
value: function () {
|
||||
return ~~this;
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToNumFloorDecimal || Object.defineProperty(Number.prototype, 'ExToNumFloorDecimal', {
|
||||
});
|
||||
Number.prototype.ExToNumFloorDecimal || Object.defineProperty(Number.prototype, "ExToNumFloorDecimal", {
|
||||
enumerable: false,
|
||||
value: function (precision: number) {
|
||||
value: function (precision: number) {
|
||||
let str = this.toPrecision(12);
|
||||
let dotPos = str.indexOf('.');
|
||||
let dotPos = str.indexOf(".");
|
||||
return dotPos == -1 ? this : +`${str.substr(0, dotPos + 1 + precision)}`;
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToStringFloorDecimal || Object.defineProperty(Number.prototype, 'ExToStringFloorDecimal', {
|
||||
});
|
||||
Number.prototype.ExToStringFloorDecimal || Object.defineProperty(Number.prototype, "ExToStringFloorDecimal", {
|
||||
enumerable: false,
|
||||
value: function (precision: number, isPadZero: boolean = true) {
|
||||
// 取小數點第X位
|
||||
@@ -155,35 +155,35 @@ Number.prototype.ExToStringFloorDecimal || Object.defineProperty(Number.prototyp
|
||||
let s = f.toString();
|
||||
// 補0
|
||||
if (isPadZero) {
|
||||
let rs = s.indexOf('.');
|
||||
let rs = s.indexOf(".");
|
||||
if (rs < 0) {
|
||||
rs = s.length;
|
||||
s += '.';
|
||||
s += ".";
|
||||
}
|
||||
while (s.length <= rs + precision) {
|
||||
s += '0';
|
||||
s += "0";
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
})
|
||||
Number.prototype.Float2Fixed || Object.defineProperty(Number.prototype, 'Float2Fixed', {
|
||||
});
|
||||
Number.prototype.Float2Fixed || Object.defineProperty(Number.prototype, "Float2Fixed", {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
if (this.toString().indexOf('e') === -1) {
|
||||
return Number(this.toString().replace('.', ''));
|
||||
if (this.toString().indexOf("e") === -1) {
|
||||
return Number(this.toString().replace(".", ""));
|
||||
}
|
||||
const dLen = this.DigitLength();
|
||||
return dLen > 0 ? +parseFloat((this * Math.pow(10, dLen)).toPrecision(12)) : this;
|
||||
}
|
||||
})
|
||||
Number.prototype.DigitLength || Object.defineProperty(Number.prototype, 'DigitLength', {
|
||||
});
|
||||
Number.prototype.DigitLength || Object.defineProperty(Number.prototype, "DigitLength", {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
const eSplit = this.toString().split(/[eE]/);
|
||||
const len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
|
||||
const len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0));
|
||||
return len > 0 ? len : 0;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ export module NumberEx {
|
||||
* 是否进行边界检查
|
||||
* @param flag 标记开关,true 为开启,false 为关闭
|
||||
*/
|
||||
function enableBoundaryChecking(flag = true) {
|
||||
function enableBoundaryChecking(flag: boolean = true) {
|
||||
_boundaryCheckingState = flag;
|
||||
}
|
||||
}
|
||||
@@ -17,8 +17,8 @@ export module RandomEx {
|
||||
}
|
||||
/**
|
||||
* 取得隨機小數
|
||||
* @param min
|
||||
* @param max
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
export function GetFloat(min: number = Number.MIN_VALUE, max: number = Number.MAX_VALUE): number {
|
||||
return Math.random() * (max - min) + min;
|
||||
@@ -26,7 +26,7 @@ export module RandomEx {
|
||||
/**
|
||||
* 隨機取得複數個不重複回傳
|
||||
* @param num 取得數量
|
||||
* @param items 陣列
|
||||
* @param items 陣列
|
||||
*/
|
||||
export function GetMultiNoRepeat(num: number, items: any[]): any[] {
|
||||
let result: any[] = [];
|
||||
@@ -36,7 +36,7 @@ export module RandomEx {
|
||||
if (result.indexOf(item) == -1) {
|
||||
result.push(item);
|
||||
}
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,16 +5,16 @@ import { ServiceType } from "../../shared/protocols/serviceProto";
|
||||
export default async function (call: ApiCall<ReqLogin, ResLogin>) {
|
||||
// Error
|
||||
if (!call.req.name) {
|
||||
call.error('Name is empty')
|
||||
call.error("Name is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
// Success
|
||||
const { sn, req } = call
|
||||
const { name } = req
|
||||
const conn: BaseConnection<ServiceType> = call.conn
|
||||
console.log(`name: ${name} is Login`)
|
||||
conn.UserId = sn
|
||||
conn.NickName = name
|
||||
call.succ(0)
|
||||
const { sn, req } = call;
|
||||
const { name } = req;
|
||||
const conn: BaseConnection<ServiceType> = call.conn;
|
||||
console.log(`name: ${name} is Login`);
|
||||
conn.UserId = sn;
|
||||
conn.NickName = name;
|
||||
call.succ(0);
|
||||
}
|
||||
@@ -4,9 +4,9 @@ import { ReqCreate, ResCreate } from "../../shared/protocols/room/PtlCreate";
|
||||
import { ServiceType } from "../../shared/protocols/serviceProto";
|
||||
|
||||
export default async function (call: ApiCall<ReqCreate, ResCreate>) {
|
||||
const conn: BaseConnection<ServiceType> = call.conn
|
||||
const room = new Room()
|
||||
room.Join(conn)
|
||||
conn.Room = room
|
||||
call.succ(room.RoomId)
|
||||
const conn: BaseConnection<ServiceType> = call.conn;
|
||||
const room = new Room();
|
||||
room.Join(conn);
|
||||
conn.Room = room;
|
||||
call.succ(room.RoomId);
|
||||
}
|
||||
@@ -3,5 +3,5 @@ import { ReqExit, ResExit } from "../../shared/protocols/room/PtlExit";
|
||||
|
||||
export default async function (call: ApiCall<ReqExit, ResExit>) {
|
||||
// TODO
|
||||
call.error('API Not Implemented');
|
||||
call.error("API Not Implemented");
|
||||
}
|
||||
@@ -4,22 +4,22 @@ import { ReqJoin, ResJoin } from "../../shared/protocols/room/PtlJoin";
|
||||
import { ServiceType } from "../../shared/protocols/serviceProto";
|
||||
|
||||
export default async function (call: ApiCall<ReqJoin, ResJoin>) {
|
||||
const { roomId } = call.req
|
||||
const conn: BaseConnection<ServiceType> = call.conn
|
||||
const room = Room.GetRoom(roomId)
|
||||
const { roomId } = call.req;
|
||||
const conn: BaseConnection<ServiceType> = call.conn;
|
||||
const room = Room.GetRoom(roomId);
|
||||
if (room) {
|
||||
if (room.ConnCount() >= 2) {
|
||||
call.error('房間已滿');
|
||||
call.error("房間已滿");
|
||||
return;
|
||||
}
|
||||
room.Join(conn)
|
||||
conn.Room = room
|
||||
call.succ(0)
|
||||
room.Join(conn);
|
||||
conn.Room = room;
|
||||
call.succ(0);
|
||||
|
||||
if (room.ConnCount() >= 2) {
|
||||
room.GotoGame();
|
||||
}
|
||||
} else {
|
||||
call.error('roomId 錯誤');
|
||||
call.error("roomId 錯誤");
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,10 @@ import Lobby from "../../component/Lobby/Lobby";
|
||||
import { ReqList, ResList } from "../../shared/protocols/room/PtlList";
|
||||
|
||||
export default async function (call: ApiCall<ReqList, ResList>) {
|
||||
const data: any[] = []
|
||||
const data: any[] = [];
|
||||
for (let i = 0; i < Lobby.Room.length; i++) {
|
||||
const room = Lobby.Room[i]
|
||||
data.push(room.RoomId)
|
||||
const room = Lobby.Room[i];
|
||||
data.push(room.RoomId);
|
||||
}
|
||||
call.succ(data)
|
||||
call.succ(data);
|
||||
}
|
||||
@@ -14,8 +14,8 @@ export default class Lobby {
|
||||
|
||||
//#region get set
|
||||
|
||||
public static get Room(): Room[] { return this.room }
|
||||
private static room: Room[] = []
|
||||
public static get Room(): Room[] { return this.room; }
|
||||
private static room: Room[] = [];
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -23,15 +23,15 @@ export default class Lobby {
|
||||
|
||||
/** AddConns */
|
||||
public static AddConns(conn: BaseConnection): void {
|
||||
this.conns.push(conn)
|
||||
this.conns.push(conn);
|
||||
}
|
||||
|
||||
/** DelConns */
|
||||
public static DelConns(conn: BaseConnection): void {
|
||||
for (let i = 0; i < this.conns.length; i++) {
|
||||
if (this.conns[i] === conn) {
|
||||
this.conns.splice(i, 1)
|
||||
break
|
||||
this.conns.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export default class Room {
|
||||
|
||||
//#region public
|
||||
|
||||
public RoomId: number = 0
|
||||
public RoomId: number = 0;
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -25,6 +25,8 @@ export default class Room {
|
||||
|
||||
private conns: BaseConnection[] = [];
|
||||
|
||||
private nowPlayer: [index: number, name: string] = undefined;
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Lifecycle
|
||||
@@ -44,7 +46,7 @@ export default class Room {
|
||||
|
||||
/** GetRoom */
|
||||
public static GetRoom(roomId: number): Room {
|
||||
return this.rooms[roomId]
|
||||
return this.rooms[roomId];
|
||||
}
|
||||
|
||||
//#endregion
|
||||
@@ -58,12 +60,12 @@ export default class Room {
|
||||
|
||||
/** Exit */
|
||||
public Exit(conn: BaseConnection): void {
|
||||
server.broadcastMsg('room/Exit', 0, <WsConnection<ServiceType>[]>this.conns)
|
||||
server.broadcastMsg("room/Exit", 0, <WsConnection<ServiceType>[]>this.conns);
|
||||
}
|
||||
|
||||
/** GotoGame */
|
||||
public GotoGame(): void {
|
||||
server.broadcastMsg('room/GoToGame', 0, <WsConnection<ServiceType>[]>this.conns)
|
||||
server.broadcastMsg("room/GoToGame", 0, <WsConnection<ServiceType>[]>this.conns);
|
||||
}
|
||||
|
||||
/** ConnCount */
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import Room from "../Room/Room"
|
||||
import Room from "../Room/Room";
|
||||
|
||||
|
||||
/**
|
||||
@@ -8,14 +8,14 @@ export default class User {
|
||||
|
||||
//#region User
|
||||
|
||||
public UserId: number = undefined
|
||||
public NickName: string = undefined
|
||||
public UserId: number = undefined;
|
||||
public NickName: string = undefined;
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region Room
|
||||
|
||||
public Room: Room = undefined
|
||||
public Room: Room = undefined;
|
||||
|
||||
//#endregion
|
||||
}
|
||||
|
||||
20
src/index.ts
20
src/index.ts
@@ -9,14 +9,14 @@ import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumer
|
||||
import "./Engine/CatanEngine/CSharp/String";
|
||||
import "./Engine/Utils/CCExtensions/ArrayExtension";
|
||||
import "./Engine/Utils/CCExtensions/NumberExtension";
|
||||
import { serviceProto, ServiceType } from './shared/protocols/serviceProto';
|
||||
import { serviceProto, ServiceType } from "./shared/protocols/serviceProto";
|
||||
|
||||
BaseEnumerator.Init();
|
||||
dayjs.locale("zh-tw")
|
||||
dayjs.locale("zh-tw");
|
||||
dotenv.config();
|
||||
|
||||
// Create the Server
|
||||
const port: number = +process.env.PORT || 3000
|
||||
const port: number = +process.env.PORT || 3000;
|
||||
export const server = new WsServer(serviceProto, {
|
||||
port: port,
|
||||
// Remove this to use binary mode (remove from the client too)
|
||||
@@ -25,24 +25,24 @@ export const server = new WsServer(serviceProto, {
|
||||
|
||||
// Initialize before server start
|
||||
async function init() {
|
||||
await server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
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<ServiceType>) => {
|
||||
Lobby.AddConns(conn)
|
||||
console.log(`${conn.ip} is connected`)
|
||||
Lobby.AddConns(conn);
|
||||
console.log(`${conn.ip} is connected`);
|
||||
return conn;
|
||||
});
|
||||
server.flows.postDisconnectFlow.push((flow) => {
|
||||
const { conn, reason } = flow;
|
||||
Lobby.DelConns(conn)
|
||||
console.log(`${conn.ip} is disconnected`)
|
||||
Lobby.DelConns(conn);
|
||||
console.log(`${conn.ip} is disconnected`);
|
||||
return flow;
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Entry function
|
||||
async function main() {
|
||||
@@ -51,6 +51,6 @@ async function main() {
|
||||
}
|
||||
main();
|
||||
|
||||
declare module 'tsrpc' {
|
||||
declare module "tsrpc" {
|
||||
export interface BaseConnection extends User { }
|
||||
}
|
||||
Reference in New Issue
Block a user