53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import dayjs from "dayjs";
|
|
import "dayjs/locale/zh-tw";
|
|
import dotenv from "dotenv";
|
|
import * as path from "path";
|
|
import { BaseConnection, WsServer } from "tsrpc";
|
|
import Lobby from "./component/Lobby/Lobby";
|
|
import User from "./component/User/User";
|
|
import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumerator";
|
|
import "./Engine/CatanEngine/CSharp/String";
|
|
import "./Engine/Utils/CCExtensions/ArrayExtension";
|
|
import "./Engine/Utils/CCExtensions/NumberExtension";
|
|
import { serviceProto, ServiceType } from "./shared/protocols/serviceProto";
|
|
|
|
BaseEnumerator.Init();
|
|
dayjs.locale("zh-tw");
|
|
dotenv.config();
|
|
|
|
// Create the Server
|
|
const port: number = +process.env.PORT || 3000;
|
|
export const server: WsServer<ServiceType> = new WsServer(serviceProto, {
|
|
port: port,
|
|
// Remove this to use binary mode (remove from the client too)
|
|
json: true
|
|
});
|
|
|
|
// Initialize before server start
|
|
async function init() {
|
|
await server.autoImplementApi(path.resolve(__dirname, "api"));
|
|
|
|
// Prepare something... (e.g. connect the db)
|
|
server.flows.postConnectFlow.push((conn: BaseConnection<ServiceType>) => {
|
|
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`);
|
|
return flow;
|
|
});
|
|
}
|
|
|
|
// Entry function
|
|
async function main() {
|
|
await init();
|
|
await server.start();
|
|
}
|
|
main();
|
|
|
|
declare module "tsrpc" {
|
|
export interface BaseConnection extends User { }
|
|
} |