diff --git a/package.json b/package.json index fbae5c0..4dd9c80 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build": "tsrpc-cli build", "doc": "tsrpc-cli doc", "test": "mocha test/**/*.test.ts", - "api": "tsrpc-cli api && tsrpc-cli proto", + "api": "tsrpc-cli proto && tsrpc-cli api", "proto": "tsrpc-cli proto", "sync": "tsrpc-cli sync" }, diff --git a/src/api/ApiLobbyList.ts b/src/api/ApiLobbyList.ts deleted file mode 100644 index 33ce61c..0000000 --- a/src/api/ApiLobbyList.ts +++ /dev/null @@ -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) { - const data: any[] = [] - for (let i = 0; i < Lobby.Room.length; i++) { - const room = Lobby.Room[i] - data.push(room.RoomId) - } - call.succ(data) -} \ No newline at end of file diff --git a/src/api/ApiSend.ts b/src/api/ApiSend.ts deleted file mode 100644 index 105a430..0000000 --- a/src/api/ApiSend.ts +++ /dev/null @@ -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) { - // 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 - }) -} \ No newline at end of file diff --git a/src/api/account/ApiLogin.ts b/src/api/account/ApiLogin.ts index 322dcac..8ce4171 100644 --- a/src/api/account/ApiLogin.ts +++ b/src/api/account/ApiLogin.ts @@ -1,5 +1,6 @@ import { ApiCall, BaseConnection } from "tsrpc"; import { ReqLogin, ResLogin } from "../../shared/protocols/account/PtlLogin"; +import { ServiceType } from "../../shared/protocols/serviceProto"; export default async function (call: ApiCall) { // Error @@ -11,7 +12,7 @@ export default async function (call: ApiCall) { // Success const { sn, req } = call const { name } = req - const conn: BaseConnection = call.conn + const conn: BaseConnection = call.conn console.log(`name: ${name} is Login`) conn.UserId = sn conn.NickName = name diff --git a/src/api/room/ApiCreate.ts b/src/api/room/ApiCreate.ts new file mode 100644 index 0000000..2346259 --- /dev/null +++ b/src/api/room/ApiCreate.ts @@ -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) { + const conn: BaseConnection = call.conn + const room = new Room() + room.Join(conn) + conn.Room = room + call.succ(room.RoomId) +} \ No newline at end of file diff --git a/src/api/room/ApiExit.ts b/src/api/room/ApiExit.ts new file mode 100644 index 0000000..e0ea2ce --- /dev/null +++ b/src/api/room/ApiExit.ts @@ -0,0 +1,7 @@ +import { ApiCall } from "tsrpc"; +import { ReqExit, ResExit } from "../../shared/protocols/room/PtlExit"; + +export default async function (call: ApiCall) { + // TODO + call.error('API Not Implemented'); +} \ No newline at end of file diff --git a/src/api/room/ApiJoin.ts b/src/api/room/ApiJoin.ts new file mode 100644 index 0000000..7c94591 --- /dev/null +++ b/src/api/room/ApiJoin.ts @@ -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) { + const { roomId } = call.req + const conn: BaseConnection = 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 錯誤'); + } +} \ No newline at end of file diff --git a/src/api/room/ApiList.ts b/src/api/room/ApiList.ts new file mode 100644 index 0000000..4d1ffe8 --- /dev/null +++ b/src/api/room/ApiList.ts @@ -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) { + const data: any[] = [] + for (let i = 0; i < Lobby.Room.length; i++) { + const room = Lobby.Room[i] + data.push(room.RoomId) + } + call.succ(data) +} \ No newline at end of file diff --git a/src/component/Client/Client.ts b/src/component/Client/Client.ts index f592a53..23f942b 100644 --- a/src/component/Client/Client.ts +++ b/src/component/Client/Client.ts @@ -8,7 +8,7 @@ // //#region private -// private conn: BaseConnection = undefined +// private conn: BaseConnection = undefined // private ws: any = undefined // private sn: number = undefined @@ -30,7 +30,7 @@ // /** // * // */ -// constructor(conn: BaseConnection, sn: number) { +// constructor(conn: BaseConnection, sn: number) { // this.conn = conn // this.ws = conn["ws"] // this.sn = sn diff --git a/src/component/Room/Room.ts b/src/component/Room/Room.ts index 07049a3..407adeb 100644 --- a/src/component/Room/Room.ts +++ b/src/component/Room/Room.ts @@ -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 { - //#region public + //#region static - static maxRoomId: number = 0; - static rooms: { [roomId: number]: Room } = {}; + private static maxRoomId: number = 0; + private static rooms: { [roomId: number]: Room } = {}; //#endregion @@ -33,6 +35,16 @@ export default class Room { constructor() { // 给每个新房间生成一个唯一的 ID this.RoomId = ++Room.maxRoomId; + Room.rooms[this.RoomId] = this; + } + + //#endregion + + //#region Custom + + /** GetRoom */ + public static GetRoom(roomId: number): Room { + return this.rooms[roomId] } //#endregion @@ -44,5 +56,20 @@ export default class Room { this.conns.push(conn); } + /** Exit */ + public Exit(conn: BaseConnection): void { + server.broadcastMsg('room/Exit', 0, []>this.conns) + } + + /** GotoGame */ + public GotoGame(): void { + server.broadcastMsg('room/GoToGame', 0, []>this.conns) + } + + /** ConnCount */ + public ConnCount(): number { + return this.conns.length; + } + //#endregion } diff --git a/src/component/User/User.ts b/src/component/User/User.ts index 0a12f1b..dbc184a 100644 --- a/src/component/User/User.ts +++ b/src/component/User/User.ts @@ -1,3 +1,4 @@ +import Room from "../Room/Room" /** @@ -5,27 +6,16 @@ */ 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 NickName: string = undefined //#endregion - //#region Lifecycle + //#region Room - // constructor(nickName: string) { - // this.nickName = nickName - // } - - //#endregion - - //#region Custom + public Room: Room = undefined //#endregion }