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

View File

@ -1,128 +1,127 @@
import { BaseConnection } from "tsrpc" // import { BaseConnection } from "tsrpc"
import User from "./User"
/** // /**
* Client // * Client
*/ // */
export default class Client { // export default class Client {
//#region private // //#region private
private conn: BaseConnection<any> = undefined // private conn: BaseConnection<any> = undefined
private ws: any = undefined // private ws: any = undefined
private sn: number = undefined // private sn: number = undefined
//#endregion // //#endregion
//#region get set // //#region get set
public get User(): User { // public get User(): User {
return this.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
// } // }
// if (true) { // private user: User = undefined
// if (data) {
// console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}(${JSON.stringify(resp.Data)})`)
// } else { // //#endregion
// console.debug(`[RPC] 收到server呼叫:(${resp.WS.clientCount}): ${resp.Method}()`)
// } // //#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 // //#region Custom
// // this.SendClient(data)
// // WebSocketServerClass.Instance.SendAllClient(data) // /**
// * setUser
// */
// public setUser(user: User) {
// this.user = user
// } // }
// private onClose(): void { // //#endregion
// console.log(`Client_${this.clientCount} Close connected`)
// }
// /** 發送給client */ // //#region Server
// 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) { // // private onMessage(buffer: _ws.RawData): void {
// //@ts-ignore // // // 收回來是 Buffer 格式、需轉成字串
// if (req.Data != null && req.Data != undefined && req.Data != NaN) { // // const dataStr: string = "[" + buffer.toString().split("[").slice(1).join("[")
// console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}(${JSON.stringify(req.Data)})`) // // const json = JSON.parse(dataStr)
// } else { // // const method = <string>json[0]
// console.log(`[RPC] 傳送client資料:(${this.clientCount}): ${req.Method}()`) // // 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 (true) {
// if (str.length > 65535) { // // if (data) {
// throw new Error('要傳的資料太大囉') // // 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"; import Room from "../Room/Room";
/** /**
@ -8,7 +8,7 @@ export default class Lobby {
//#region private //#region private
private static clients: Client[] = [] private static conns: BaseConnection[] = [];
private static serialNumber: number = 0 private static serialNumber: number = 0
//#endregion //#endregion
@ -22,9 +22,9 @@ export default class Lobby {
//#region Custom //#region Custom
/** AddClient */ /** AddConns */
public static AddClient(client: Client): void { public static AddConns(conn: BaseConnection): void {
this.clients.push(client) this.conns.push(conn)
} }
// /** List */ // /** 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 dotenv from "dotenv";
import * as path from "path"; import * as path from "path";
import { WsServer } from "tsrpc"; import { WsServer } from "tsrpc";
import User from "./component/User/User";
import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumerator"; import { BaseEnumerator } from "./Engine/CatanEngine/CoroutineV2/Core/BaseEnumerator";
import "./Engine/CatanEngine/CSharp/String"; import "./Engine/CatanEngine/CSharp/String";
import "./Engine/Utils/CCExtensions/ArrayExtension"; import "./Engine/Utils/CCExtensions/ArrayExtension";
@ -35,3 +36,7 @@ async function main() {
await server.start(); await server.start();
} }
main(); main();
declare module 'tsrpc' {
export interface BaseConnection extends User { }
}