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