[mod] user

This commit is contained in:
建喵 2023-09-01 18:25:41 +08:00
parent f9f27ec7c5
commit 2568a95cc4
6 changed files with 143 additions and 140 deletions

View File

@ -1,6 +1,4 @@
import { ApiCall, BaseConnection } from "tsrpc";
import Client from "../component/Client/Client";
import User from "../component/Client/User";
import Lobby from "../component/Lobby/Lobby";
import { ReqAccountLogin, ResAccountLogin } from "../shared/protocols/PtlAccountLogin";
@ -16,9 +14,8 @@ export default async function (call: ApiCall<ReqAccountLogin, ResAccountLogin>)
const { name } = req
const conn: BaseConnection<any> = call.conn
console.log(`name: ${name} is Login`)
const user = new User(name)
const client = new Client(conn, sn)
client.setUser(user)
Lobby.AddClient(client)
conn.UserId = sn
conn.NickName = name
Lobby.AddConns(conn)
call.succ(0)
}

View File

@ -1,128 +1,127 @@
import { BaseConnection } from "tsrpc"
import User from "./User"
// import { BaseConnection } from "tsrpc"
/**
* Client
*/
export default class Client {
// /**
// * Client
// */
// export default class Client {
//#region private
// //#region private
private conn: BaseConnection<any> = undefined
private ws: any = undefined
private sn: number = undefined
// private conn: BaseConnection<any> = undefined
// private ws: any = undefined
// private sn: number = undefined
//#endregion
// //#endregion
//#region get set
// //#region get set
public get User(): User {
return this.user
}
private user: User = undefined
//#endregion
//#region Lifecycle
/**
*
*/
constructor(conn: BaseConnection<any>, sn: number) {
this.conn = conn
this.ws = conn["ws"]
this.sn = sn
// // 當收到client消息時
// ws.on('message', this.onMessage.bind(this))
// // 當連線關閉
// ws.on('close', this.onClose.bind(this))
}
//#endregion
//#region Custom
/**
* setUser
*/
public setUser(user: User) {
this.user = user
}
//#endregion
//#region Server
// private onMessage(buffer: _ws.RawData): void {
// // 收回來是 Buffer 格式、需轉成字串
// const dataStr: string = "[" + buffer.toString().split("[").slice(1).join("[")
// const json = JSON.parse(dataStr)
// const method = <string>json[0]
// let status = 0
// const data = json[1]
// const resp = {
// Method: method,
// Status: status,
// Data: data,
// IsValid: method && status === 0,
// WS: this
// public get User(): User {
// return this.user
// }
// if (true) {
// if (data) {
// console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}(${JSON.stringify(resp.Data)})`)
// } else {
// console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}()`)
// }
// private user: User = undefined
// //#endregion
// //#region Lifecycle
// /**
// *
// */
// constructor(conn: BaseConnection<any>, sn: number) {
// this.conn = conn
// this.ws = conn["ws"]
// this.sn = sn
// // // 當收到client消息時
// // ws.on('message', this.onMessage.bind(this))
// // // 當連線關閉
// // ws.on('close', this.onClose.bind(this))
// }
// WebSocketServerClass.Instance.OnDataReceived.DispatchCallback(resp)
// //#endregion
// // /// 發送消息給client
// // this.SendClient(data)
// // WebSocketServerClass.Instance.SendAllClient(data)
// //#region Custom
// /**
// * setUser
// */
// public setUser(user: User) {
// this.user = user
// }
// private onClose(): void {
// console.log(`Client_${this.clientCount} Close connected`)
// }
// //#endregion
// /** 發送給client */
// public SendClient(req: INetResponse<any>): void {
// const status = 0
// const json: any[] = [req.Method]
// //@ts-ignore
// if (req.Data != null && req.Data != undefined && req.Data != NaN) {
// json[1] = [status, req.Data]
// }
// //#region Server
// if (true) {
// //@ts-ignore
// if (req.Data != null && req.Data != undefined && req.Data != NaN) {
// console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`)
// } else {
// console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}()`)
// }
// }
// // private onMessage(buffer: _ws.RawData): void {
// // // 收回來是 Buffer 格式、需轉成字串
// // const dataStr: string = "[" + buffer.toString().split("[").slice(1).join("[")
// // const json = JSON.parse(dataStr)
// // const method = <string>json[0]
// // let status = 0
// // const data = json[1]
// // const resp = {
// // Method: method,
// // Status: status,
// // Data: data,
// // IsValid: method && status === 0,
// // WS: this
// // }
// const str = JSON.stringify(json)
// if (str.length > 65535) {
// throw new Error('要傳的資料太大囉')
// // if (true) {
// // if (data) {
// // console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}(${JSON.stringify(resp.Data)})`)
// // } else {
// // console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}()`)
// // }
// // }
// // WebSocketServerClass.Instance.OnDataReceived.DispatchCallback(resp)
// // // /// 發送消息給client
// // // this.SendClient(data)
// // // WebSocketServerClass.Instance.SendAllClient(data)
// // }
// // private onClose(): void {
// // console.log(`Client_${this.clientCount} Close connected`)
// // }
// // /** 發送給client */
// // public SendClient(req: INetResponse<any>): void {
// // const status = 0
// // const json: any[] = [req.Method]
// // //@ts-ignore
// // if (req.Data != null && req.Data != undefined && req.Data != NaN) {
// // json[1] = [status, req.Data]
// // }
// // if (true) {
// // //@ts-ignore
// // if (req.Data != null && req.Data != undefined && req.Data != NaN) {
// // console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`)
// // } else {
// // console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}()`)
// // }
// // }
// // const str = JSON.stringify(json)
// // if (str.length > 65535) {
// // throw new Error('要傳的資料太大囉')
// // }
// // const strary = Encoding.UTF8.GetBytes(str)
// // const buffer = new Uint8Array(4 + strary.byteLength)
// // const u16ary = new Uint16Array(buffer.buffer, 0, 3)
// // u16ary[0] = strary.byteLength
// // buffer[3] = 0x01
// // buffer.set(strary, 4)
// // this.ws.send(buffer)
// // }
// //#endregion
// }
// const strary = Encoding.UTF8.GetBytes(str)
// const buffer = new Uint8Array(4 + strary.byteLength)
// const u16ary = new Uint16Array(buffer.buffer, 0, 3)
// u16ary[0] = strary.byteLength
// buffer[3] = 0x01
// buffer.set(strary, 4)
// this.ws.send(buffer)
// }
//#endregion
}

View File

@ -1,29 +0,0 @@
/**
* User
*/
export default class User {
//#region get set
public get Name(): string {
return this.name
}
private name: string = undefined
//#endregion
//#region Lifecycle
constructor(name: string) {
this.name = name
}
//#endregion
//#region Custom
//#endregion
}

View File

@ -1,4 +1,4 @@
import Client from "../Client/Client";
import { BaseConnection } from "tsrpc";
import Room from "../Room/Room";
/**
@ -8,7 +8,7 @@ export default class Lobby {
//#region private
private static clients: Client[] = []
private static conns: BaseConnection[] = [];
private static serialNumber: number = 0
//#endregion
@ -22,9 +22,9 @@ export default class Lobby {
//#region Custom
/** AddClient */
public static AddClient(client: Client): void {
this.clients.push(client)
/** AddConns */
public static AddConns(conn: BaseConnection): void {
this.conns.push(conn)
}
// /** List */

View File

@ -0,0 +1,31 @@
/**
* User
*/
export default class User {
//#region get set
// public get NickName(): string {
// return this.nickName
// }
// private nickName: string = undefined
public UserId: number = undefined
public NickName: string = undefined
//#endregion
//#region Lifecycle
// constructor(nickName: string) {
// this.nickName = nickName
// }
//#endregion
//#region Custom
//#endregion
}

View File

@ -3,6 +3,7 @@ import "dayjs/locale/zh-tw";
import dotenv from "dotenv";
import * as path from "path";
import { WsServer } from "tsrpc";
import User from "./component/User/User";
import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumerator";
import "./Engine/CatanEngine/CSharp/String";
import "./Engine/Utils/CCExtensions/ArrayExtension";
@ -35,3 +36,7 @@ async function main() {
await server.start();
}
main();
declare module 'tsrpc' {
export interface BaseConnection extends User { }
}