hall apis

This commit is contained in:
k8w 2022-04-20 19:43:00 +08:00
parent e197b2842a
commit f70fff7851
37 changed files with 763 additions and 551 deletions

View File

@ -1,10 +1,9 @@
{ {
"name": "backend-.", "name": "backend",
"version": "0.1.0", "version": "0.1.0",
"main": "index.js", "main": "index.js",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "tsrpc-cli dev",
"dev:hall": "tsrpc-cli dev --entry src/hallServer.ts", "dev:hall": "tsrpc-cli dev --entry src/hallServer.ts",
"dev:room": "tsrpc-cli dev --entry src/roomServer.ts", "dev:room": "tsrpc-cli dev --entry src/roomServer.ts",
"build": "tsrpc-cli build", "build": "tsrpc-cli build",
@ -17,6 +16,7 @@
"devDependencies": { "devDependencies": {
"@types/mocha": "^8.2.3", "@types/mocha": "^8.2.3",
"@types/node": "^15.14.9", "@types/node": "^15.14.9",
"@types/uuid": "^8.3.4",
"mocha": "^9.2.2", "mocha": "^9.2.2",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"ts-node": "^10.7.0", "ts-node": "^10.7.0",
@ -24,6 +24,7 @@
"typescript": "^4.6.3" "typescript": "^4.6.3"
}, },
"dependencies": { "dependencies": {
"tsrpc": "^3.3.0" "tsrpc": "^3.3.0",
"uuid": "^8.3.2"
} }
} }

View File

@ -0,0 +1,59 @@
import path from "path";
import { HttpServer, WsClient } from "tsrpc";
import { UserUtil } from "../models/UserUtil";
import { MsgUpdateRoomState } from "../shared/protocols/roomServer/admin/MsgUpdateRoomState";
import { serviceProto } from "../shared/protocols/serviceProto_hallServer";
import { ServiceType as ServiceType_Room } from "../shared/protocols/serviceProto_roomServer";
import { UserInfo } from "../shared/types/UserInfo";
export class HallServer {
readonly server = new HttpServer(serviceProto, {
port: 3000,
// Remove this to use binary mode (remove from the client too)
json: true
});
/** 已注册的 RoomServer */
readonly roomServers: {
url: string,
conn: WsClient<ServiceType_Room>,
state?: MsgUpdateRoomState
}[] = [];
constructor() {
// Flows
// 前置鉴别登录态
this.server.flows.preApiCallFlow.push(async call => {
call.currentUser = call.req.sso ? await UserUtil.parseSso(call.req.sso) : undefined;
// 需要登录的接口:前置登录态判定
if (!call.service.conf?.allowGuest) {
if (!call.currentUser) {
call.error('你还未登录', { code: 'NEED_LOGIN' });
return undefined;
}
}
if (call.currentUser) {
call.logger.prefixs.push(`[uid=${call.currentUser.id}]`);
}
return call;
});
}
async init() {
await this.server.autoImplementApi(path.resolve(__dirname, './api'));
}
async start() {
await this.server.start();
}
}
declare module 'tsrpc' {
export interface ApiCall {
/** 只要协议配置的 `allowGuest` 不为 `true`,则必定有值 */
currentUser?: UserInfo;
}
}

View File

@ -0,0 +1,34 @@
import { ApiCall, TsrpcError } from "tsrpc";
import { hallServer } from "../../hallServer";
import { BackConfig } from "../../models/BackConfig";
import { ReqCreateRoom, ResCreateRoom } from "../../shared/protocols/hallServer/PtlCreateRoom";
export async function ApiCreateRoom(call: ApiCall<ReqCreateRoom, ResCreateRoom>) {
// 挑选一个人数最少的 RoomServer
let server = hallServer.roomServers.filter(v => v.state).orderBy(v => v.state!.userNum)[0];
if (!server) {
return call.error('没有可用的 RoomServer', { type: TsrpcError.Type.ServerError });
}
if (!call.req.roomName) {
return call.error('请输入房间名称');
}
// RPC
let op = await server.conn.callApi('admin/CreateRoom', {
adminToken: BackConfig.adminToken,
creator: {
uid: call.currentUser!.id,
nickname: call.currentUser!.nickname
},
roomName: call.req.roomName
})
if (!op.isSucc) {
return call.error(op.err);
}
call.succ({
serverUrl: server.url,
roomId: op.res.roomId
})
}

View File

@ -0,0 +1,22 @@
import { ApiCall } from "tsrpc";
import { hallServer } from "../../hallServer";
import { ReqListRooms, ResListRooms } from "../../shared/protocols/hallServer/PtlListRooms";
export async function ApiListRooms(call: ApiCall<ReqListRooms, ResListRooms>) {
let rooms = hallServer.roomServers.reduce((prev, next) => {
if (next.state) {
prev = prev.concat(next.state.rooms.map(v => ({
name: v.name,
userNum: v.userNum,
serverUrl: next.url,
roomId: v.id,
updateTime: v.updateTime
})))
}
return prev;
}, [] as (ResListRooms['rooms'][0] & { updateTime: number })[])
call.succ({
rooms: rooms.orderByDesc(v => v.updateTime).slice(0, 100)
})
}

View File

@ -0,0 +1,16 @@
import { ApiCall } from "tsrpc";
import uuid from "uuid";
import { UserUtil } from "../../models/UserUtil";
import { ReqLogin, ResLogin } from "../../shared/protocols/hallServer/PtlLogin";
import { UserInfo } from "../../shared/types/UserInfo";
export async function ApiLogin(call: ApiCall<ReqLogin, ResLogin>) {
let uid = uuid.v4();
let user: UserInfo = {
id: uid,
nickname: call.req.nickname
};
let sso = await UserUtil.createSso(user)
call.succ({ sso, user })
}

View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqStartMatch, ResStartMatch } from "../../shared/protocols/hallServer/PtlStartMatch";
export async function ApiStartMatch(call: ApiCall<ReqStartMatch, ResStartMatch>) {
// TODO
call.error('API Not Implemented');
}

View File

@ -0,0 +1,56 @@
import { ApiCall, TerminalColorLogger, WsClient } from "tsrpc";
import { hallServer } from "../../../hallServer";
import { BackConfig } from "../../../models/BackConfig";
import { ReqRegisterRoomServer, ResRegisterRoomServer } from "../../../shared/protocols/hallServer/admin/PtlRegisterRoomServer";
import { serviceProto } from "../../../shared/protocols/serviceProto_roomServer";
import { HallServer } from "../../HallServer";
let nextRoomIndex = 1;
export async function ApiRegisterRoomServer(call: ApiCall<ReqRegisterRoomServer, ResRegisterRoomServer>) {
// 鉴权
if (call.req.adminToken !== BackConfig.adminToken) {
return call.error('非法操作');
}
// Create
let client = new WsClient(serviceProto, {
server: call.req.serverUrl,
logger: new TerminalColorLogger({
pid: `RoomServer${nextRoomIndex++}`
}),
heartbeat: {
interval: 5000,
timeout: 5000
}
});
// Flows
client.flows.postDisconnectFlow.push(v => {
hallServer.roomServers.remove(v1 => v1.conn === client);
return v;
});
client.listenMsg('admin/UpdateRoomState', msg => {
roomServer.state = msg;
});
// Connect
let op = await client.connect();
if (!op.isSucc) {
return call.error(op.errMsg);
}
// Auth
let op2 = await client.callApi('admin/Auth', { adminToken: call.req.adminToken });
if (!op2.isSucc) {
return call.error(op2.err);
}
// Succ
let roomServer: HallServer['roomServers'][number] = {
url: call.req.serverUrl,
conn: client
}
hallServer.roomServers.push(roomServer);
call.succ({});
}

View File

@ -0,0 +1,44 @@
import path from "path";
import { WsServer } from "tsrpc";
import { Room } from "../models/Room";
import { serviceProto } from "../shared/protocols/serviceProto_roomServer";
import { UserInfo } from "../shared/types/UserInfo";
export class RoomServer {
readonly server = new WsServer(serviceProto, {
port: parseInt(process.env['PORT'] || '3001'),
// Remove this to use binary mode (remove from the client too)
json: true
});
constructor() {
// Flows
// 前置鉴别登录态
this.server.flows.preApiCallFlow.push(async call => {
// 需要登录的接口:前置登录态判定
if (!call.service.conf?.allowGuest) {
if (!call.conn.currentUser) {
call.error('你还未登录', { code: 'NEED_LOGIN' });
return undefined;
}
}
return call;
});
}
async init() {
await this.server.autoImplementApi(path.resolve(__dirname, './api'));
}
async start() {
await this.server.start();
}
}
declare module 'tsrpc' {
export interface BaseConnection {
currentUser?: UserInfo;
currentRoom?: Room;
}
}

View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqExitRoom, ResExitRoom } from "../../shared/protocols/roomServer/PtlExitRoom";
export async function ApiExitRoom(call: ApiCall<ReqExitRoom, ResExitRoom>) {
// TODO
call.error('API Not Implemented');
}

View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqJoinRoom, ResJoinRoom } from "../../shared/protocols/roomServer/PtlJoinRoom";
export async function ApiJoinRoom(call: ApiCall<ReqJoinRoom, ResJoinRoom>) {
// TODO
call.error('API Not Implemented');
}

View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqUpdateRoom, ResUpdateRoom } from "../../shared/protocols/roomServer/PtlUpdateRoom";
export async function ApiUpdateRoom(call: ApiCall<ReqUpdateRoom, ResUpdateRoom>) {
// TODO
call.error('API Not Implemented');
}

View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqAuth, ResAuth } from "../../../shared/protocols/roomServer/admin/PtlAuth";
export async function ApiAuth(call: ApiCall<ReqAuth, ResAuth>) {
// TODO
call.error('API Not Implemented');
}

View File

@ -0,0 +1,7 @@
import { ApiCall } from "tsrpc";
import { ReqCreateRoom, ResCreateRoom } from "../../../shared/protocols/roomServer/admin/PtlCreateRoom";
export async function ApiCreateRoom(call: ApiCall<ReqCreateRoom, ResCreateRoom>) {
// TODO
call.error('API Not Implemented');
}

View File

@ -0,0 +1,11 @@
import { HallServer } from "./HallServer/HallServer";
export const hallServer = new HallServer();
// Entry function
async function main() {
await hallServer.init();
await hallServer.start();
}
main();

View File

@ -1,25 +0,0 @@
import * as path from "path";
import { WsServer } from "tsrpc";
import { serviceProto } from './shared/protocols/serviceProto';
// Create the Server
export const server = new WsServer(serviceProto, {
port: 3000,
// Remove this to use binary mode (remove from the client too)
json: true
});
// Initialize before server start
async function init() {
await server.autoImplementApi(path.resolve(__dirname, 'api'));
// TODO
// Prepare something... (e.g. connect the db)
};
// Entry function
async function main() {
await init();
await server.start();
}
main();

View File

@ -0,0 +1,5 @@
export const BackConfig = {
adminToken: 'AAABBBCCC'
}

View File

@ -0,0 +1,3 @@
export class Room {
}

View File

@ -0,0 +1,21 @@
import { UserInfo } from "../shared/types/UserInfo";
// 登录态 SSO 的编解码
// 这里简单起见,使用未加密的 JSON 字符串
// 你可以根据自己的需要,改为加密字符串,或者服务端 Session Key 等
export class UserUtil {
static async createSso(user: UserInfo): Promise<string> {
return JSON.stringify(user);
}
static async parseSso(sso: string): Promise<UserInfo | undefined> {
try {
return JSON.parse(sso);
}
catch {
return undefined;
}
}
}

View File

@ -0,0 +1,10 @@
import { RoomServer } from "./RoomServer/RoomServer";
export const roomServer = new RoomServer();
// Entry function
async function main() {
await roomServer.init();
await roomServer.start();
}
main();

View File

@ -1,188 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { ReqRegisterRoomServer, ResRegisterRoomServer } from '../shared/protocols/hallServer/admin/PtlRegisterRoomServer';
import { ReqCreateRoom, ResCreateRoom } from '../shared/protocols/hallServer/PtlCreateRoom';
import { ReqListRooms, ResListRooms } from '../shared/protocols/hallServer/PtlListRooms';
import { ReqLogin, ResLogin } from '../shared/protocols/hallServer/PtlLogin';
import { ReqStartMatch, ResStartMatch } from '../shared/protocols/hallServer/PtlStartMatch';
export interface ServiceType {
api: {
"admin/RegisterRoomServer": {
req: ReqRegisterRoomServer,
res: ResRegisterRoomServer
},
"CreateRoom": {
req: ReqCreateRoom,
res: ResCreateRoom
},
"ListRooms": {
req: ReqListRooms,
res: ResListRooms
},
"Login": {
req: ReqLogin,
res: ResLogin
},
"StartMatch": {
req: ReqStartMatch,
res: ResStartMatch
}
},
msg: {
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"services": [
{
"id": 0,
"name": "admin/RegisterRoomServer",
"type": "api",
"conf": {}
},
{
"id": 1,
"name": "CreateRoom",
"type": "api",
"conf": {}
},
{
"id": 2,
"name": "ListRooms",
"type": "api",
"conf": {}
},
{
"id": 3,
"name": "Login",
"type": "api"
},
{
"id": 4,
"name": "StartMatch",
"type": "api",
"conf": {}
}
],
"types": {
"admin/PtlRegisterRoomServer/ReqRegisterRoomServer": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"../base/BaseRequest": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "sso",
"type": {
"type": "String"
},
"optional": true
}
]
},
"admin/PtlRegisterRoomServer/ResRegisterRoomServer": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"../base/BaseResponse": {
"type": "Interface"
},
"PtlCreateRoom/ReqCreateRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"PtlCreateRoom/ResCreateRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"PtlListRooms/ReqListRooms": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"PtlListRooms/ResListRooms": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"PtlLogin/ReqLogin": {
"type": "Interface"
},
"PtlLogin/ResLogin": {
"type": "Interface"
},
"PtlStartMatch/ReqStartMatch": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"PtlStartMatch/ResStartMatch": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
}
}
};

View File

@ -1,252 +0,0 @@
import { ServiceProto } from 'tsrpc-proto';
import { MsgUpdateRoomState } from '../shared/protocols/roomServer/admin/MsgUpdateRoomState';
import { ReqAuth, ResAuth } from '../shared/protocols/roomServer/admin/PtlAuth';
import { ReqCreateRoom, ResCreateRoom } from '../shared/protocols/roomServer/admin/PtlCreateRoom';
import { ReqPreJoinRoom, ResPreJoinRoom } from '../shared/protocols/roomServer/admin/PtlPreJoinRoom';
import { ReqExitRoom, ResExitRoom } from '../shared/protocols/roomServer/PtlExitRoom';
import { ReqJoinRoom, ResJoinRoom } from '../shared/protocols/roomServer/PtlJoinRoom';
import { ReqUpdateRoom, ResUpdateRoom } from '../shared/protocols/roomServer/PtlUpdateRoom';
import { MsgChat } from '../shared/protocols/roomServer/roomMsg/MsgChat';
import { MsgUpdateRoomInfo } from '../shared/protocols/roomServer/roomMsg/MsgUpdateRoomInfo';
export interface ServiceType {
api: {
"admin/Auth": {
req: ReqAuth,
res: ResAuth
},
"admin/CreateRoom": {
req: ReqCreateRoom,
res: ResCreateRoom
},
"admin/PreJoinRoom": {
req: ReqPreJoinRoom,
res: ResPreJoinRoom
},
"ExitRoom": {
req: ReqExitRoom,
res: ResExitRoom
},
"JoinRoom": {
req: ReqJoinRoom,
res: ResJoinRoom
},
"UpdateRoom": {
req: ReqUpdateRoom,
res: ResUpdateRoom
}
},
msg: {
"admin/UpdateRoomState": MsgUpdateRoomState,
"roomMsg/Chat": MsgChat,
"roomMsg/UpdateRoomInfo": MsgUpdateRoomInfo
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"services": [
{
"id": 0,
"name": "admin/UpdateRoomState",
"type": "msg"
},
{
"id": 1,
"name": "admin/Auth",
"type": "api"
},
{
"id": 2,
"name": "admin/CreateRoom",
"type": "api",
"conf": {}
},
{
"id": 3,
"name": "admin/PreJoinRoom",
"type": "api",
"conf": {}
},
{
"id": 4,
"name": "ExitRoom",
"type": "api",
"conf": {}
},
{
"id": 5,
"name": "JoinRoom",
"type": "api",
"conf": {}
},
{
"id": 6,
"name": "UpdateRoom",
"type": "api",
"conf": {}
},
{
"id": 7,
"name": "roomMsg/Chat",
"type": "msg"
},
{
"id": 8,
"name": "roomMsg/UpdateRoomInfo",
"type": "msg"
}
],
"types": {
"admin/MsgUpdateRoomState/MsgUpdateRoomState": {
"type": "Interface"
},
"admin/PtlAuth/ReqAuth": {
"type": "Interface"
},
"admin/PtlAuth/ResAuth": {
"type": "Interface"
},
"admin/PtlCreateRoom/ReqCreateRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"../base/BaseRequest": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "sso",
"type": {
"type": "String"
},
"optional": true
}
]
},
"admin/PtlCreateRoom/ResCreateRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"../base/BaseResponse": {
"type": "Interface"
},
"admin/PtlPreJoinRoom/ReqPreJoinRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"admin/PtlPreJoinRoom/ResPreJoinRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"PtlExitRoom/ReqExitRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"PtlExitRoom/ResExitRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"PtlJoinRoom/ReqJoinRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"PtlJoinRoom/ResJoinRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"PtlUpdateRoom/ReqUpdateRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseRequest"
}
}
]
},
"PtlUpdateRoom/ResUpdateRoom": {
"type": "Interface",
"extends": [
{
"id": 0,
"type": {
"type": "Reference",
"target": "../base/BaseResponse"
}
}
]
},
"roomMsg/MsgChat/MsgChat": {
"type": "Interface"
},
"roomMsg/MsgUpdateRoomInfo/MsgUpdateRoomInfo": {
"type": "Interface"
}
}
};

View File

@ -1,11 +1,12 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../base"; import { BaseConf, BaseRequest, BaseResponse } from "./../base";
export interface ReqCreateRoom extends BaseRequest { export interface ReqCreateRoom extends BaseRequest {
roomName: string
} }
export interface ResCreateRoom extends BaseResponse { export interface ResCreateRoom extends BaseResponse {
serverUrl: string,
roomId: string
} }
export const conf: BaseConf = { export const conf: BaseConf = {

View File

@ -1,11 +1,17 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../base"; import { uint } from "tsrpc";
import { BaseConf, BaseRequest, BaseResponse } from "./../base";
export interface ReqListRooms extends BaseRequest { export interface ReqListRooms extends BaseRequest {
} }
export interface ResListRooms extends BaseResponse { export interface ResListRooms extends BaseResponse {
rooms: {
name: string,
userNum: uint,
serverUrl: string,
roomId: string
}[]
} }
export const conf: BaseConf = { export const conf: BaseConf = {

View File

@ -1,9 +1,15 @@
export interface ReqLogin { import { UserInfo } from "../../types/UserInfo";
import { BaseConf } from "../base";
export interface ReqLogin {
nickname: string
} }
export interface ResLogin { export interface ResLogin {
sso: string,
user: UserInfo
} }
// export const conf = {} export const conf: BaseConf = {
allowGuest: true
}

View File

@ -1,11 +1,13 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../base"; import { uint } from "tsrpc-proto";
import { BaseConf, BaseRequest, BaseResponse } from "./../base";
export interface ReqStartMatch extends BaseRequest { export interface ReqStartMatch extends BaseRequest {
} }
export interface ResStartMatch extends BaseResponse { export interface ResStartMatch extends BaseResponse {
serverUrl: string,
roomId: uint
} }
export const conf: BaseConf = { export const conf: BaseConf = {

View File

@ -1,13 +1,12 @@
import { BaseConf, BaseRequest, BaseResponse } from "../../base"; import { BaseConf } from "../../base";
export interface ReqRegisterRoomServer extends BaseRequest {
export interface ReqRegisterRoomServer {
/** RoomServer 的连接地址 */
serverUrl: string,
/** Token 用于鉴权 */
adminToken: string
} }
export interface ResRegisterRoomServer extends BaseResponse { export interface ResRegisterRoomServer {
}
export const conf: BaseConf = {
} }

View File

@ -1,4 +1,4 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../base"; import { BaseConf, BaseRequest, BaseResponse } from "./../base";
export interface ReqExitRoom extends BaseRequest { export interface ReqExitRoom extends BaseRequest {

View File

@ -1,7 +1,7 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../base"; import { BaseRequest, BaseResponse, BaseConf } from "./../base";
export interface ReqJoinRoom extends BaseRequest { export interface ReqJoinRoom extends BaseRequest {
roomId: string
} }
export interface ResJoinRoom extends BaseResponse { export interface ResJoinRoom extends BaseResponse {

View File

@ -1,7 +1,8 @@
import { BaseConf, BaseRequest, BaseResponse } from "../base"; import { BaseConf, BaseRequest, BaseResponse } from "../base";
export interface ReqUpdateRoom extends BaseRequest { export interface ReqUpdateRoom extends BaseRequest {
roomId: string,
roomName: string
} }
export interface ResUpdateRoom extends BaseResponse { export interface ResUpdateRoom extends BaseResponse {

View File

@ -1,5 +1,16 @@
export interface MsgUpdateRoomState { import { uint } from "tsrpc-proto";
export interface MsgUpdateRoomState {
userNum: uint,
rooms: {
id: string,
name: string,
userNum: uint,
/** 为 undefined 代表不在匹配中 */
startMatchTime?: uint,
// 房间信息的最后更新时间
updateTime: uint
}[]
} }
// export const conf = {} // export const conf = {}

View File

@ -1,9 +1,8 @@
export interface ReqAuth {
export interface ReqAuth {
adminToken: string
} }
export interface ResAuth { export interface ResAuth {
} }
// export const conf = {}

View File

@ -1,13 +1,13 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../../base";
export interface ReqCreateRoom extends BaseRequest {
export interface ReqCreateRoom {
adminToken: string,
creator: {
uid: string,
nickname: string
}
roomName: string
} }
export interface ResCreateRoom extends BaseResponse { export interface ResCreateRoom {
roomId: string
}
export const conf: BaseConf = {
} }

View File

@ -1,13 +0,0 @@
import { BaseRequest, BaseResponse, BaseConf } from "./../../base";
export interface ReqPreJoinRoom extends BaseRequest {
}
export interface ResPreJoinRoom extends BaseResponse {
}
export const conf: BaseConf = {
}

View File

@ -1,4 +1,5 @@
import { ServiceProto } from 'tsrpc-proto'; import { ServiceProto } from 'tsrpc-proto';
import { ReqRegisterRoomServer, ResRegisterRoomServer } from './hallServer/admin/PtlRegisterRoomServer';
import { ReqCreateRoom, ResCreateRoom } from './hallServer/PtlCreateRoom'; import { ReqCreateRoom, ResCreateRoom } from './hallServer/PtlCreateRoom';
import { ReqListRooms, ResListRooms } from './hallServer/PtlListRooms'; import { ReqListRooms, ResListRooms } from './hallServer/PtlListRooms';
import { ReqLogin, ResLogin } from './hallServer/PtlLogin'; import { ReqLogin, ResLogin } from './hallServer/PtlLogin';
@ -6,6 +7,10 @@ import { ReqStartMatch, ResStartMatch } from './hallServer/PtlStartMatch';
export interface ServiceType { export interface ServiceType {
api: { api: {
"admin/RegisterRoomServer": {
req: ReqRegisterRoomServer,
res: ResRegisterRoomServer
},
"CreateRoom": { "CreateRoom": {
req: ReqCreateRoom, req: ReqCreateRoom,
res: ResCreateRoom res: ResCreateRoom
@ -29,8 +34,13 @@ export interface ServiceType {
} }
export const serviceProto: ServiceProto<ServiceType> = { export const serviceProto: ServiceProto<ServiceType> = {
"version": 1, "version": 2,
"services": [ "services": [
{
"id": 0,
"name": "admin/RegisterRoomServer",
"type": "api"
},
{ {
"id": 1, "id": 1,
"name": "CreateRoom", "name": "CreateRoom",
@ -46,7 +56,10 @@ export const serviceProto: ServiceProto<ServiceType> = {
{ {
"id": 3, "id": 3,
"name": "Login", "name": "Login",
"type": "api" "type": "api",
"conf": {
"allowGuest": true
}
}, },
{ {
"id": 4, "id": 4,
@ -56,6 +69,28 @@ export const serviceProto: ServiceProto<ServiceType> = {
} }
], ],
"types": { "types": {
"admin/PtlRegisterRoomServer/ReqRegisterRoomServer": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "serverUrl",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "adminToken",
"type": {
"type": "String"
}
}
]
},
"admin/PtlRegisterRoomServer/ResRegisterRoomServer": {
"type": "Interface"
},
"PtlCreateRoom/ReqCreateRoom": { "PtlCreateRoom/ReqCreateRoom": {
"type": "Interface", "type": "Interface",
"extends": [ "extends": [
@ -66,6 +101,15 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "../base/BaseRequest" "target": "../base/BaseRequest"
} }
} }
],
"properties": [
{
"id": 0,
"name": "roomName",
"type": {
"type": "String"
}
}
] ]
}, },
"../base/BaseRequest": { "../base/BaseRequest": {
@ -91,6 +135,22 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "../base/BaseResponse" "target": "../base/BaseResponse"
} }
} }
],
"properties": [
{
"id": 0,
"name": "serverUrl",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "roomId",
"type": {
"type": "String"
}
}
] ]
}, },
"../base/BaseResponse": { "../base/BaseResponse": {
@ -118,13 +178,101 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "../base/BaseResponse" "target": "../base/BaseResponse"
} }
} }
],
"properties": [
{
"id": 0,
"name": "rooms",
"type": {
"type": "Array",
"elementType": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "name",
"type": {
"type": "String"
}
},
{
"id": 4,
"name": "userNum",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 2,
"name": "serverUrl",
"type": {
"type": "String"
}
},
{
"id": 3,
"name": "roomId",
"type": {
"type": "String"
}
}
]
}
}
}
] ]
}, },
"PtlLogin/ReqLogin": { "PtlLogin/ReqLogin": {
"type": "Interface" "type": "Interface",
"properties": [
{
"id": 0,
"name": "nickname",
"type": {
"type": "String"
}
}
]
}, },
"PtlLogin/ResLogin": { "PtlLogin/ResLogin": {
"type": "Interface" "type": "Interface",
"properties": [
{
"id": 0,
"name": "sso",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "user",
"type": {
"type": "Reference",
"target": "../../types/UserInfo/UserInfo"
}
}
]
},
"../../types/UserInfo/UserInfo": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "id",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "nickname",
"type": {
"type": "String"
}
}
]
}, },
"PtlStartMatch/ReqStartMatch": { "PtlStartMatch/ReqStartMatch": {
"type": "Interface", "type": "Interface",
@ -148,6 +296,23 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "../base/BaseResponse" "target": "../base/BaseResponse"
} }
} }
],
"properties": [
{
"id": 0,
"name": "serverUrl",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "roomId",
"type": {
"type": "Number",
"scalarType": "uint"
}
}
] ]
} }
} }

View File

@ -1,4 +1,7 @@
import { ServiceProto } from 'tsrpc-proto'; import { ServiceProto } from 'tsrpc-proto';
import { MsgUpdateRoomState } from './roomServer/admin/MsgUpdateRoomState';
import { ReqAuth, ResAuth } from './roomServer/admin/PtlAuth';
import { ReqCreateRoom, ResCreateRoom } from './roomServer/admin/PtlCreateRoom';
import { ReqExitRoom, ResExitRoom } from './roomServer/PtlExitRoom'; import { ReqExitRoom, ResExitRoom } from './roomServer/PtlExitRoom';
import { ReqJoinRoom, ResJoinRoom } from './roomServer/PtlJoinRoom'; import { ReqJoinRoom, ResJoinRoom } from './roomServer/PtlJoinRoom';
import { ReqUpdateRoom, ResUpdateRoom } from './roomServer/PtlUpdateRoom'; import { ReqUpdateRoom, ResUpdateRoom } from './roomServer/PtlUpdateRoom';
@ -7,6 +10,14 @@ import { MsgUpdateRoomInfo } from './roomServer/roomMsg/MsgUpdateRoomInfo';
export interface ServiceType { export interface ServiceType {
api: { api: {
"admin/Auth": {
req: ReqAuth,
res: ResAuth
},
"admin/CreateRoom": {
req: ReqCreateRoom,
res: ResCreateRoom
},
"ExitRoom": { "ExitRoom": {
req: ReqExitRoom, req: ReqExitRoom,
res: ResExitRoom res: ResExitRoom
@ -21,44 +32,193 @@ export interface ServiceType {
} }
}, },
msg: { msg: {
"admin/UpdateRoomState": MsgUpdateRoomState,
"roomMsg/Chat": MsgChat, "roomMsg/Chat": MsgChat,
"roomMsg/UpdateRoomInfo": MsgUpdateRoomInfo "roomMsg/UpdateRoomInfo": MsgUpdateRoomInfo
} }
} }
export const serviceProto: ServiceProto<ServiceType> = { export const serviceProto: ServiceProto<ServiceType> = {
"version": 1, "version": 2,
"services": [ "services": [
{ {
"id": 4, "id": 0,
"name": "admin/UpdateRoomState",
"type": "msg"
},
{
"id": 1,
"name": "admin/Auth",
"type": "api"
},
{
"id": 2,
"name": "admin/CreateRoom",
"type": "api"
},
{
"id": 3,
"name": "ExitRoom", "name": "ExitRoom",
"type": "api", "type": "api",
"conf": {} "conf": {}
}, },
{ {
"id": 5, "id": 4,
"name": "JoinRoom", "name": "JoinRoom",
"type": "api", "type": "api",
"conf": {} "conf": {}
}, },
{ {
"id": 6, "id": 5,
"name": "UpdateRoom", "name": "UpdateRoom",
"type": "api", "type": "api",
"conf": {} "conf": {}
}, },
{ {
"id": 7, "id": 6,
"name": "roomMsg/Chat", "name": "roomMsg/Chat",
"type": "msg" "type": "msg"
}, },
{ {
"id": 8, "id": 7,
"name": "roomMsg/UpdateRoomInfo", "name": "roomMsg/UpdateRoomInfo",
"type": "msg" "type": "msg"
} }
], ],
"types": { "types": {
"admin/MsgUpdateRoomState/MsgUpdateRoomState": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "userNum",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 1,
"name": "rooms",
"type": {
"type": "Array",
"elementType": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "id",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "name",
"type": {
"type": "String"
}
},
{
"id": 2,
"name": "userNum",
"type": {
"type": "Number",
"scalarType": "uint"
}
},
{
"id": 3,
"name": "startMatchTime",
"type": {
"type": "Number",
"scalarType": "uint"
},
"optional": true
},
{
"id": 4,
"name": "updateTime",
"type": {
"type": "Number",
"scalarType": "uint"
}
}
]
}
}
}
]
},
"admin/PtlAuth/ReqAuth": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "adminToken",
"type": {
"type": "String"
}
}
]
},
"admin/PtlAuth/ResAuth": {
"type": "Interface"
},
"admin/PtlCreateRoom/ReqCreateRoom": {
"type": "Interface",
"properties": [
{
"id": 2,
"name": "adminToken",
"type": {
"type": "String"
}
},
{
"id": 3,
"name": "creator",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "uid",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "nickname",
"type": {
"type": "String"
}
}
]
}
},
{
"id": 1,
"name": "roomName",
"type": {
"type": "String"
}
}
]
},
"admin/PtlCreateRoom/ResCreateRoom": {
"type": "Interface",
"properties": [
{
"id": 1,
"name": "roomId",
"type": {
"type": "String"
}
}
]
},
"PtlExitRoom/ReqExitRoom": { "PtlExitRoom/ReqExitRoom": {
"type": "Interface", "type": "Interface",
"extends": [ "extends": [
@ -109,6 +269,15 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "../base/BaseRequest" "target": "../base/BaseRequest"
} }
} }
],
"properties": [
{
"id": 0,
"name": "roomId",
"type": {
"type": "String"
}
}
] ]
}, },
"PtlJoinRoom/ResJoinRoom": { "PtlJoinRoom/ResJoinRoom": {
@ -133,6 +302,22 @@ export const serviceProto: ServiceProto<ServiceType> = {
"target": "../base/BaseRequest" "target": "../base/BaseRequest"
} }
} }
],
"properties": [
{
"id": 0,
"name": "roomId",
"type": {
"type": "String"
}
},
{
"id": 1,
"name": "roomName",
"type": {
"type": "String"
}
}
] ]
}, },
"PtlUpdateRoom/ResUpdateRoom": { "PtlUpdateRoom/ResUpdateRoom": {

View File

@ -0,0 +1,4 @@
export interface UserInfo {
id: string,
nickname: string
}

View File

@ -3,31 +3,18 @@ import { CodeTemplate, TsrpcConfig } from 'tsrpc-cli';
const tsrpcConf: TsrpcConfig = { const tsrpcConf: TsrpcConfig = {
// Generate ServiceProto // Generate ServiceProto
proto: [ proto: [
// Proto for user
{ {
ptlDir: 'src/shared/protocols/hallServer', // Protocol dir ptlDir: 'src/shared/protocols/hallServer', // Protocol dir
output: 'src/shared/protocols/serviceProto_hallServer.ts', // Path for generated ServiceProto output: 'src/shared/protocols/serviceProto_hallServer.ts', // Path for generated ServiceProto
ignore: 'src/shared/protocols/hallServer/admin/**' // User proto ignore admin apiDir: 'src/HallServer/api', // API dir
},
{
ptlDir: 'src/shared/protocols/roomServer', // Protocol dir
output: 'src/shared/protocols/serviceProto_roomServer.ts', // Path for generated ServiceProto
ignore: 'src/shared/protocols/roomServer/admin/**' // User proto ignore admin
},
// Proto for server RPC
{
ptlDir: 'src/shared/protocols/hallServer', // Protocol dir
output: 'src/server_rpc/serviceProto_hallServer.ts', // Path for generated ServiceProto
apiDir: 'src/api/hallServer', // API dir
docDir: 'docs/hallServer', // API documents dir docDir: 'docs/hallServer', // API documents dir
ptlTemplate: CodeTemplate.getExtendedPtl(), ptlTemplate: CodeTemplate.getExtendedPtl(),
// msgTemplate: CodeTemplate.getExtendedMsg(), // msgTemplate: CodeTemplate.getExtendedMsg(),
}, },
{ {
ptlDir: 'src/shared/protocols/roomServer', // Protocol dir ptlDir: 'src/shared/protocols/roomServer', // Protocol dir
output: 'src/server_rpc/serviceProto_roomServer.ts', // Path for generated ServiceProto output: 'src/shared/protocols/serviceProto_roomServer.ts', // Path for generated ServiceProto
apiDir: 'src/api/roomServer', // API dir apiDir: 'src/RoomServer/api', // API dir
docDir: 'docs/roomServer', // API documents dir docDir: 'docs/roomServer', // API documents dir
ptlTemplate: CodeTemplate.getExtendedPtl(), ptlTemplate: CodeTemplate.getExtendedPtl(),
// msgTemplate: CodeTemplate.getExtendedMsg(), // msgTemplate: CodeTemplate.getExtendedMsg(),