添加查询和创建角色功能;建立长连接后返回部分初始数据

This commit is contained in:
liangtongchuan
2020-09-01 22:15:31 +08:00
parent 2617efa6ad
commit dbdb11692c
23 changed files with 466 additions and 145 deletions
@@ -1,6 +1,9 @@
import { RoleModel } from './../../../db/Role';
import { UserModel } from './../../../db/User';
import {Application} from 'pinus';
import { Application, Session } from 'pinus';
import {FrontendSession} from 'pinus';
import Hero from '../../../db/Hero';
import Equip from '../../../db/Equip';
export default function (app: Application) {
return new EntryHandler(app);
@@ -10,16 +13,16 @@ export class EntryHandler {
constructor(private app: Application) {
}
/**
* New client entry chat server.
*
* @param {Object} msg request message
* @param {Object} session current session object
*/
async enter(msg: { rid: string, username: string, token: string }, session: FrontendSession) {
async enter(msg: { rid: string, username: string, token: string, serverId: number }, session: FrontendSession) {
let self = this;
let rid = msg.rid;
let serverId = msg.serverId;
let uid = msg.username + '*' + rid;
let sessionService = self.app.get('sessionService');
@@ -32,6 +35,14 @@ export class EntryHandler {
};
}
let role = await RoleModel.findByUid(user.uid, serverId);
if (!role) {
return {
code: 201,
error: true,
data: '用户还未创建角色',
}
}
// duplicate log in
if (!!sessionService.getByUid(uid)) {
console.log('uid not found');
@@ -43,6 +54,8 @@ export class EntryHandler {
await session.abind(uid);
session.set('rid', rid);
session.set('uid', user.uid);
session.set('roleId', role.roleId);
session.push('rid', function (err) {
if (err) {
console.error('set rid for session service failed! error is : %j', err.stack);
@@ -52,9 +65,14 @@ export class EntryHandler {
// put user into channel
let users = await self.app.rpc.chat.chatRemote.add.route(session)(uid, self.app.get('serverId'), rid, true);
let heros = await Hero.findByRole(role.roleId);
let equips = await Equip.findbyRole(role.roleId);
role['heros'] = heros;
role['equips'] = equips;
return {
users: users
status: 200,
users: users,
role
};
}