diff --git a/game-server/app.ts b/game-server/app.ts index a837ba81d..a83205545 100644 --- a/game-server/app.ts +++ b/game-server/app.ts @@ -149,6 +149,7 @@ app.configure('production|development|local', function () { // route configures app.route('chat', routeUtil.chat); app.route('battle', routeUtil.battle); + app.route('gm', routeUtil.gm); // filter configures app.filter(new pinus.filters.timeout()); diff --git a/game-server/app/servers/battle/handler/normalBattleHandler.ts b/game-server/app/servers/battle/handler/normalBattleHandler.ts index 09a2536ad..031f167b3 100644 --- a/game-server/app/servers/battle/handler/normalBattleHandler.ts +++ b/game-server/app/servers/battle/handler/normalBattleHandler.ts @@ -207,6 +207,7 @@ export class NormalBattleHandler { if(!warInfo) { return resResult(STATUS.BATTLE_MISS_INFO); } + console.log(warInfo) // 校验是否三星通关过 let condition1 = await BattleRecordModel.getBattleRecordByIdAndStar(roleId, battleId, 3); if(!condition1) { diff --git a/game-server/app/servers/connector/handler/entryHandler.ts b/game-server/app/servers/connector/handler/entryHandler.ts index cd7c1a2c6..85737da5c 100644 --- a/game-server/app/servers/connector/handler/entryHandler.ts +++ b/game-server/app/servers/connector/handler/entryHandler.ts @@ -2,6 +2,7 @@ import { STATUS } from './../../../consts/statusCode'; import { EquipModel } from './../../../db/Equip'; import { RoleModel } from './../../../db/Role'; import { UserModel } from '../../../db/User'; +import { GMUserModel } from '../../../db/GMUser'; import { Application, Session } from 'pinus'; import {FrontendSession} from 'pinus'; import { HeroModel } from './../../../db/Hero'; @@ -22,6 +23,7 @@ export class EntryHandler { * @param {Object} session current session object */ async enter(msg: { token: string, serverId: number }, session: FrontendSession) { + console.log('******enter') let self = this; let serverId = msg.serverId; let sessionService = self.app.get('sessionService'); @@ -97,4 +99,43 @@ export class EntryHandler { this.app.rpc.battle.eventBattleRemote.kick.route(session)(roleId, this.app.get('serverId')); // this.app.rpc.chat.chatRemote.kick.route(session, true)(session.uid, this.app.get('serverId'), session.get('rid')); } + + + /** + * New client entry chat server. + * + * @param {Object} msg request message + * @param {Object} session current session object + */ + async gmEnter(msg: { token: string, serverId: number }, session: FrontendSession) { + + let self = this; + + let user = await GMUserModel.getGmAccountByToken(msg.token); + if (!user) { + console.log('user token not found'); + return resResult(STATUS.TOKEN_ERR); + } + + console.log(self.app.get('serverId')); + await session.abind(user.username); + session.set('uid', user.uid); + session.set('roleId', user.username); + session.set('roleName', user.name); + session.set('eventStatus', 0); + session.set('sid', self.app.get('serverId')); + session.push('sid', () => {}); + session.push('roleId', () => {}); + session.push('roleName', () => {}); + session.push('eventStatus', () => {}); + // session.push('rid', function (err) { + // if (err) { + // console.error('set rid for session service failed! error is : %j', err.stack); + // } + // }); + session.on('closed', this.onUserLeave.bind(this)); + + // put user into channel + return resResult(STATUS.SUCCESS); + } } \ No newline at end of file diff --git a/game-server/app/servers/gate/handler/gateHandler.ts b/game-server/app/servers/gate/handler/gateHandler.ts index ed4b3be3b..af6b0df26 100644 --- a/game-server/app/servers/gate/handler/gateHandler.ts +++ b/game-server/app/servers/gate/handler/gateHandler.ts @@ -25,6 +25,7 @@ export class GateHandler { } // get all connectors let connectors = this.app.getServersByType('connector'); + console.log(connectors); if (!connectors || connectors.length === 0) { return resResult(STATUS.CONNECTOR_ERR); } diff --git a/game-server/app/servers/gm/handler/gmHandler.ts b/game-server/app/servers/gm/handler/gmHandler.ts new file mode 100644 index 000000000..7a48a1416 --- /dev/null +++ b/game-server/app/servers/gm/handler/gmHandler.ts @@ -0,0 +1,61 @@ +import {Application, BackendSession} from 'pinus'; +import { RoleModel } from '../../../db/Role'; +import { getEventTime, refreshEvent } from '../../../services/eventSercive'; +import EventRecord, { EventRecordModel } from '../../../db/EventRecord'; +import { EVENT_STATUS, EVENT_RECORD_STATUS } from '../../../consts/consts'; +import { resResult } from '../../../pubUtils/util'; +import { STATUS } from '../../../consts/statusCode'; + + +export default function(app: Application) { + return new GmHandler(app); +} + +export class GmHandler { + constructor(private app: Application) { + } + + async pushEvent(msg: { uid: number, serverId: number }, session: BackendSession) { + let sid = session.get('sid'); + + let { uid, serverId } = msg; + console.log(uid, serverId) + let role = await RoleModel.findByUid(uid, serverId); + if(!role) return resResult(STATUS.GM_ROLE_NOT_FOUND); + let {roleId, roleName, eventStatus } = role; + + let channelService = this.app.get('channelService'); + let channel = channelService.getChannel(roleId, true); + let users = channel.getMembers(); + if (users.indexOf(roleId) === -1) { + channel.add(roleId, sid); + } + + + + let now = new Date(); + let t = getEventTime(now); + + let event: Array; + if (eventStatus == EVENT_STATUS.STARTING) { + event = await EventRecordModel.getEventRecordByTime(roleId, 0); + } else if( eventStatus == EVENT_STATUS.OPEN ) { + + event = await EventRecordModel.getEventRecordByTime(roleId, t); + if(event.length == 0) { // 刷新 + const num = 3; // 每次刷3个 + event = await refreshEvent(num, roleId, roleName, t); + } + } + + + let tsid = channel.getMember(roleId)['sid']; + channelService.pushMessageByUids('onSpecialEvent', { + msg: { event } + }, [{ + uid: roleId, + sid: tsid + }]); + return resResult(STATUS.SUCCESS, { list: event }); + } +} \ No newline at end of file diff --git a/game-server/app/servers/gm/remote/gmRemote.ts b/game-server/app/servers/gm/remote/gmRemote.ts new file mode 100644 index 000000000..ab4f48203 --- /dev/null +++ b/game-server/app/servers/gm/remote/gmRemote.ts @@ -0,0 +1,92 @@ +import { Application, ChannelService, FrontendSession, RemoterClass } from 'pinus'; + +export default function (app: Application) { + return new ChatRemote(app); +} + +// rpc 定义挪到单独的定义文件(user.rpc.define.ts)。解决ts-node 有可能找不到定义的问题。 +// 你也可以用其它方法解决,或者没有遇到过这个问题的话,定义还是可以放在这里。 + +// UserRpc的命名空间自动合并 +// declare global { +// interface UserRpc { +// chat: { +// chatRemote: RemoterClass; +// }; +// } +// } +export class ChatRemote { + + constructor(private app: Application) { + this.app = app; + this.channelService = app.get('channelService'); + } + + private channelService: ChannelService; + + /** + * Add user into chat channel. + * + * @param {String} uid unique id for user + * @param {String} sid server id + * @param {String} name channel name + * @param {boolean} flag channel parameter + * + */ + public async add(uid: string, sid: string, name: string, flag: boolean) { + let channel = this.channelService.getChannel(name, flag); + let username = uid.split('*')[0]; + let param = { + user: username + }; + channel.pushMessage('onAdd', param); + + if (!!channel) { + channel.add(uid, sid); + } + + return this.get(name, flag); + } + + /** + * Get user from chat channel. + * + * @param {Object} opts parameters for request + * @param {String} name channel name + * @param {boolean} flag channel parameter + * @return {Array} users uids in channel + * + */ + private get(name: string, flag: boolean) { + let users: string[] = []; + let channel = this.channelService.getChannel(name, flag); + if (!!channel) { + users = channel.getMembers(); + } + for (let i = 0; i < users.length; i++) { + users[i] = users[i].split('*')[0]; + } + return users; + } + + /** + * Kick user out chat channel. + * + * @param {String} uid unique id for user + * @param {String} sid server id + * @param {String} name channel name + * + */ + public async kick(uid: string, sid: string, name: string) { + let channel = this.channelService.getChannel(name, false); + // leave channel + if (!!channel) { + channel.leave(uid, sid); + } + let username = uid.split('*')[0]; + let param = { + user: username + }; + channel.pushMessage('onLeave', param); + } +} \ No newline at end of file diff --git a/game-server/app/util/routeUtil.ts b/game-server/app/util/routeUtil.ts index e596d60f3..8aaff6868 100644 --- a/game-server/app/util/routeUtil.ts +++ b/game-server/app/util/routeUtil.ts @@ -24,4 +24,16 @@ export function battle(session: Session, msg: any, app: Application, cb: (err: E let res = dispatch(session.get('roleId'), battleServers); cb(null, res.id); +} + +export function gm(session: Session, msg: any, app: Application, cb: (err: Error , serverId ?: string) => void) { + let gmServers = app.getServersByType('gm'); + + if(!gmServers || gmServers.length === 0) { + cb(new Error('can not find gm servers.')); + return; + } + + let res = dispatch(session.get('roleId'), gmServers); + cb(null, res.id); } \ No newline at end of file diff --git a/game-server/config/adminServer.ts b/game-server/config/adminServer.ts index 6a9042dcc..8553152d7 100644 --- a/game-server/config/adminServer.ts +++ b/game-server/config/adminServer.ts @@ -10,5 +10,8 @@ module.exports = [{ }, { 'type': 'battle', 'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn' +}, { + 'type': 'gm', + 'token': 'agarxhqb98rpajloaxn34ga8xrunpagkjwlaw3ruxnpaagl29w4rxn' } ]; \ No newline at end of file diff --git a/game-server/config/servers.ts b/game-server/config/servers.ts index 14824f22b..6fccefaf7 100644 --- a/game-server/config/servers.ts +++ b/game-server/config/servers.ts @@ -22,6 +22,9 @@ module.exports = { 'battle': [ {'id': 'battle-server-1', 'host': '127.0.0.1', 'port': 6054, 'args': '--inspect=10007'} ], + 'gm': [ + {'id': 'gm-server-1', 'host': '127.0.0.1', 'port': 6055, 'args': '--inspect=10009'} + ], 'gate': [ { 'id': 'gate-server-1', @@ -67,6 +70,9 @@ module.exports = { 'clientPort': 3015, 'frontend': true } + ], + 'gm': [ + {'id': 'gm-server-1', 'host': '127.0.0.1', 'port': 6055} ] }, 'local': { @@ -82,15 +88,16 @@ module.exports = { } ], 'chat': [ - {'id': 'chat-server-1', 'host': '127.0.0.1', 'port': 6050, 'args': '--inspect=10002'}, - {'id': 'chat-server-2', 'host': '127.0.0.1', 'port': 6051, 'args': '--inspect=10004'}, - {'id': 'chat-server-3', 'host': '127.0.0.1', 'port': 6052, 'args': '--inspect=10005'} + {'id': 'chat-server-1', 'host': '127.0.0.1', 'port': 6050} ], 'role': [ - {'id': 'role-server-1', 'host': '127.0.0.1', 'port': 6053, 'args': '--inspect=10006'} + {'id': 'role-server-1', 'host': '127.0.0.1', 'port': 6053} ], 'battle': [ - {'id': 'battle-server-1', 'host': '127.0.0.1', 'port': 6054, 'args': '--inspect=10007'} + {'id': 'battle-server-1', 'host': '127.0.0.1', 'port': 6054} + ], + 'gm': [ + {'id': 'gm-server-1', 'host': '127.0.0.1', 'port': 6055} ], 'gate': [ { @@ -98,16 +105,14 @@ module.exports = { 'host': '127.0.0.1', 'clientHost': '127.0.0.1', 'clientPort': 3014, - 'frontend': true, - 'args': '--inspect=10003' + 'frontend': true }, { 'id': 'gate-server-2', 'host': '127.0.0.1', 'clientHost': '127.0.0.1', 'clientPort': 3015, - 'frontend': true, - 'args': '--inspect=10008' + 'frontend': true } ] } diff --git a/shared/db/GMUser.ts b/shared/db/GMUser.ts index 469676653..2e12d4cb8 100644 --- a/shared/db/GMUser.ts +++ b/shared/db/GMUser.ts @@ -44,6 +44,7 @@ export default class GMUser extends BaseModel { } public static async getGmAccountByToken(token: string, lean = true) { + console.log(token) const user = await GMUserModel.findOne({token}).select('uid username name token').lean(lean); return user; }