GuessWhoIAmS/src/api/room/ApiJoin.ts

25 lines
667 B
TypeScript
Raw Normal View History

2023-09-02 22:33:40 +08:00
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>) {
2023-09-10 15:17:34 +08:00
const { roomId } = call.req;
const conn: BaseConnection<ServiceType> = call.conn;
const room = Room.GetRoom(roomId);
2023-09-02 22:33:40 +08:00
if (room) {
if (room.ConnCount() >= 2) {
2023-09-10 15:17:34 +08:00
call.error("房間已滿");
2023-09-02 22:33:40 +08:00
return;
}
2023-09-10 15:17:34 +08:00
room.Join(conn);
conn.Room = room;
call.succ(0);
2023-09-02 22:33:40 +08:00
if (room.ConnCount() >= 2) {
room.GotoGame();
}
} else {
2023-09-10 15:17:34 +08:00
call.error("roomId 錯誤");
2023-09-02 22:33:40 +08:00
}
}