2023-08-31 19:28:35 +08:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import "dayjs/locale/zh-tw";
|
2023-09-01 18:02:06 +08:00
|
|
|
import dotenv from "dotenv";
|
2023-08-31 19:28:35 +08:00
|
|
|
import * as path from "path";
|
2023-09-02 00:33:25 +08:00
|
|
|
import { BaseConnection, WsServer } from "tsrpc";
|
|
|
|
import Lobby from "./component/Lobby/Lobby";
|
2023-09-01 18:25:41 +08:00
|
|
|
import User from "./component/User/User";
|
2023-08-31 19:28:35 +08:00
|
|
|
import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumerator";
|
|
|
|
import "./Engine/CatanEngine/CSharp/String";
|
|
|
|
import "./Engine/Utils/CCExtensions/ArrayExtension";
|
|
|
|
import "./Engine/Utils/CCExtensions/NumberExtension";
|
2023-09-02 00:33:25 +08:00
|
|
|
import { serviceProto, ServiceType } from './shared/protocols/serviceProto';
|
2023-08-31 19:28:35 +08:00
|
|
|
|
|
|
|
BaseEnumerator.Init();
|
|
|
|
dayjs.locale("zh-tw")
|
2023-09-01 18:02:06 +08:00
|
|
|
dotenv.config();
|
2023-08-31 19:28:35 +08:00
|
|
|
|
|
|
|
// Create the Server
|
2023-09-01 18:02:06 +08:00
|
|
|
const port: number = +process.env.PORT || 3000
|
2023-08-31 19:28:35 +08:00
|
|
|
export const server = new WsServer(serviceProto, {
|
2023-09-01 18:02:06 +08:00
|
|
|
port: port,
|
2023-08-31 19:28:35 +08:00
|
|
|
// 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)
|
2023-09-02 00:33:25 +08:00
|
|
|
// server.flows.postConnectFlow.push((conn) => {
|
|
|
|
// Lobby.AddConns(conn);
|
|
|
|
// });
|
|
|
|
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;
|
|
|
|
});
|
2023-08-31 19:28:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Entry function
|
|
|
|
async function main() {
|
|
|
|
await init();
|
|
|
|
await server.start();
|
|
|
|
}
|
2023-09-01 18:25:41 +08:00
|
|
|
main();
|
|
|
|
|
|
|
|
declare module 'tsrpc' {
|
|
|
|
export interface BaseConnection extends User { }
|
|
|
|
}
|