[add] Room 完成

This commit is contained in:
建喵 2023-09-02 22:33:40 +08:00
parent 6cc42d3c54
commit 4f28fd3277
11 changed files with 96 additions and 60 deletions

View File

@ -10,7 +10,7 @@
"build": "tsrpc-cli build", "build": "tsrpc-cli build",
"doc": "tsrpc-cli doc", "doc": "tsrpc-cli doc",
"test": "mocha test/**/*.test.ts", "test": "mocha test/**/*.test.ts",
"api": "tsrpc-cli api && tsrpc-cli proto", "api": "tsrpc-cli proto && tsrpc-cli api",
"proto": "tsrpc-cli proto", "proto": "tsrpc-cli proto",
"sync": "tsrpc-cli sync" "sync": "tsrpc-cli sync"
}, },

View File

@ -1,12 +0,0 @@
import { ApiCall } from "tsrpc";
import Lobby from "../component/Lobby/Lobby";
import { ReqLobbyList, ResLobbyList } from "../shared/protocols/PtlLobbyList";
export default async function (call: ApiCall<ReqLobbyList, ResLobbyList>) {
const data: any[] = []
for (let i = 0; i < Lobby.Room.length; i++) {
const room = Lobby.Room[i]
data.push(room.RoomId)
}
call.succ(data)
}

View File

@ -1,26 +0,0 @@
import { ApiCall } from "tsrpc";
import { server } from "..";
import { ReqSend, ResSend } from "../shared/protocols/PtlSend";
// This is a demo code file
// Feel free to delete it
export default async function (call: ApiCall<ReqSend, ResSend>) {
// Error
if (call.req.content.length === 0) {
call.error('Content is empty')
return;
}
// Success
let time = new Date();
call.succ({
time: time
});
// Broadcast
server.broadcastMsg('Chat', {
content: call.req.content,
time: time
})
}

View File

@ -1,5 +1,6 @@
import { ApiCall, BaseConnection } from "tsrpc"; import { ApiCall, BaseConnection } from "tsrpc";
import { ReqLogin, ResLogin } from "../../shared/protocols/account/PtlLogin"; import { ReqLogin, ResLogin } from "../../shared/protocols/account/PtlLogin";
import { ServiceType } from "../../shared/protocols/serviceProto";
export default async function (call: ApiCall<ReqLogin, ResLogin>) { export default async function (call: ApiCall<ReqLogin, ResLogin>) {
// Error // Error
@ -11,7 +12,7 @@ export default async function (call: ApiCall<ReqLogin, ResLogin>) {
// Success // Success
const { sn, req } = call const { sn, req } = call
const { name } = req const { name } = req
const conn: BaseConnection<any> = call.conn const conn: BaseConnection<ServiceType> = call.conn
console.log(`name: ${name} is Login`) console.log(`name: ${name} is Login`)
conn.UserId = sn conn.UserId = sn
conn.NickName = name conn.NickName = name

12
src/api/room/ApiCreate.ts Normal file
View File

@ -0,0 +1,12 @@
import { ApiCall, BaseConnection } from "tsrpc";
import Room from "../../component/Room/Room";
import { ReqCreate, ResCreate } from "../../shared/protocols/room/PtlCreate";
import { ServiceType } from "../../shared/protocols/serviceProto";
export default async function (call: ApiCall<ReqCreate, ResCreate>) {
const conn: BaseConnection<ServiceType> = call.conn
const room = new Room()
room.Join(conn)
conn.Room = room
call.succ(room.RoomId)
}

7
src/api/room/ApiExit.ts Normal file
View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqExit, ResExit } from "../../shared/protocols/room/PtlExit";
export default async function (call: ApiCall<ReqExit, ResExit>) {
// TODO
call.error('API Not Implemented');
}

25
src/api/room/ApiJoin.ts Normal file
View File

@ -0,0 +1,25 @@
import { ApiCall, BaseConnection } from "tsrpc";
import Room from "../../component/Room/Room";
import { ReqJoin, ResJoin } from "../../shared/protocols/room/PtlJoin";
import { ServiceType } from "../../shared/protocols/serviceProto";
export default async function (call: ApiCall<ReqJoin, ResJoin>) {
const { roomId } = call.req
const conn: BaseConnection<ServiceType> = call.conn
const room = Room.GetRoom(roomId)
if (room) {
if (room.ConnCount() >= 2) {
call.error('房間已滿');
return;
}
room.Join(conn)
conn.Room = room
call.succ(0)
if (room.ConnCount() >= 2) {
room.GotoGame();
}
} else {
call.error('roomId 錯誤');
}
}

12
src/api/room/ApiList.ts Normal file
View File

@ -0,0 +1,12 @@
import { ApiCall } from "tsrpc";
import Lobby from "../../component/Lobby/Lobby";
import { ReqList, ResList } from "../../shared/protocols/room/PtlList";
export default async function (call: ApiCall<ReqList, ResList>) {
const data: any[] = []
for (let i = 0; i < Lobby.Room.length; i++) {
const room = Lobby.Room[i]
data.push(room.RoomId)
}
call.succ(data)
}

View File

@ -8,7 +8,7 @@
// //#region private // //#region private
// private conn: BaseConnection<any> = undefined // private conn: BaseConnection<ServiceType> = undefined
// private ws: any = undefined // private ws: any = undefined
// private sn: number = undefined // private sn: number = undefined
@ -30,7 +30,7 @@
// /** // /**
// * // *
// */ // */
// constructor(conn: BaseConnection<any>, sn: number) { // constructor(conn: BaseConnection<ServiceType>, sn: number) {
// this.conn = conn // this.conn = conn
// this.ws = conn["ws"] // this.ws = conn["ws"]
// this.sn = sn // this.sn = sn

View File

@ -1,4 +1,6 @@
import { BaseConnection } from "tsrpc"; import { BaseConnection, WsConnection } from "tsrpc";
import { server } from "../..";
import { ServiceType } from "../../shared/protocols/serviceProto";
/** /**
@ -6,10 +8,10 @@ import { BaseConnection } from "tsrpc";
*/ */
export default class Room { export default class Room {
//#region public //#region static
static maxRoomId: number = 0; private static maxRoomId: number = 0;
static rooms: { [roomId: number]: Room } = {}; private static rooms: { [roomId: number]: Room } = {};
//#endregion //#endregion
@ -33,6 +35,16 @@ export default class Room {
constructor() { constructor() {
// 给每个新房间生成一个唯一的 ID // 给每个新房间生成一个唯一的 ID
this.RoomId = ++Room.maxRoomId; this.RoomId = ++Room.maxRoomId;
Room.rooms[this.RoomId] = this;
}
//#endregion
//#region Custom
/** GetRoom */
public static GetRoom(roomId: number): Room {
return this.rooms[roomId]
} }
//#endregion //#endregion
@ -44,5 +56,20 @@ export default class Room {
this.conns.push(conn); this.conns.push(conn);
} }
/** Exit */
public Exit(conn: BaseConnection): void {
server.broadcastMsg('room/Exit', 0, <WsConnection<ServiceType>[]>this.conns)
}
/** GotoGame */
public GotoGame(): void {
server.broadcastMsg('room/GoToGame', 0, <WsConnection<ServiceType>[]>this.conns)
}
/** ConnCount */
public ConnCount(): number {
return this.conns.length;
}
//#endregion //#endregion
} }

View File

@ -1,3 +1,4 @@
import Room from "../Room/Room"
/** /**
@ -5,27 +6,16 @@
*/ */
export default class User { export default class User {
//#region get set //#region User
// public get NickName(): string {
// return this.nickName
// }
// private nickName: string = undefined
public UserId: number = undefined public UserId: number = undefined
public NickName: string = undefined public NickName: string = undefined
//#endregion //#endregion
//#region Lifecycle //#region Room
// constructor(nickName: string) { public Room: Room = undefined
// this.nickName = nickName
// }
//#endregion
//#region Custom
//#endregion //#endregion
} }