[add] login
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
import { ApiCall, BaseConnection } from "tsrpc";
|
||||
import Lobby from "../component/Lobby/Lobby";
|
||||
import { ReqAccountLogin, ResAccountLogin } from "../shared/protocols/PtlAccountLogin";
|
||||
import { ReqLogin, ResLogin } from "../../shared/protocols/account/PtlLogin";
|
||||
|
||||
export default async function (call: ApiCall<ReqAccountLogin, ResAccountLogin>) {
|
||||
export default async function (call: ApiCall<ReqLogin, ResLogin>) {
|
||||
// Error
|
||||
if (!call.req.name) {
|
||||
call.error('Name is empty')
|
||||
@@ -16,6 +15,5 @@ export default async function (call: ApiCall<ReqAccountLogin, ResAccountLogin>)
|
||||
console.log(`name: ${name} is Login`)
|
||||
conn.UserId = sn
|
||||
conn.NickName = name
|
||||
Lobby.AddConns(conn)
|
||||
call.succ(0)
|
||||
}
|
||||
@@ -9,7 +9,6 @@ export default class Lobby {
|
||||
//#region private
|
||||
|
||||
private static conns: BaseConnection[] = [];
|
||||
private static serialNumber: number = 0
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -27,6 +26,16 @@ export default class Lobby {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /** List */
|
||||
// public static List(req: INetResponse<RpcLobbyListRequest>): void {
|
||||
// const data = []
|
||||
|
||||
@@ -15,11 +15,12 @@ export default class Room {
|
||||
|
||||
//#region public
|
||||
|
||||
public RoomId: number = 0
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region private
|
||||
|
||||
private roomId: number = 0
|
||||
private conns: BaseConnection[] = [];
|
||||
|
||||
//#endregion
|
||||
@@ -31,7 +32,7 @@ export default class Room {
|
||||
*/
|
||||
constructor() {
|
||||
// 给每个新房间生成一个唯一的 ID
|
||||
this.roomId = ++Room.maxRoomId;
|
||||
this.RoomId = ++Room.maxRoomId;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
20
src/index.ts
20
src/index.ts
@@ -2,13 +2,14 @@ import dayjs from "dayjs";
|
||||
import "dayjs/locale/zh-tw";
|
||||
import dotenv from "dotenv";
|
||||
import * as path from "path";
|
||||
import { WsServer } from "tsrpc";
|
||||
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 } from './shared/protocols/serviceProto';
|
||||
import { serviceProto, ServiceType } from './shared/protocols/serviceProto';
|
||||
|
||||
BaseEnumerator.Init();
|
||||
dayjs.locale("zh-tw")
|
||||
@@ -26,8 +27,21 @@ export const server = new WsServer(serviceProto, {
|
||||
async function init() {
|
||||
await server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
|
||||
// TODO
|
||||
// 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`)
|
||||
return conn;
|
||||
});
|
||||
server.flows.postDisconnectFlow.push((flow) => {
|
||||
const { conn, reason } = flow;
|
||||
Lobby.DelConns(conn)
|
||||
console.log(`${conn.ip} is disconnected`)
|
||||
return flow;
|
||||
});
|
||||
};
|
||||
|
||||
// Entry function
|
||||
|
||||
Reference in New Issue
Block a user