const Code = require("../../../lib/code"); module.exports = function (app) { return new ChatHandler(app); }; class ChatHandler { constructor(app) { this.app = app; this.channelService = this.app.get("channelService"); } /** * @api {post} chat.chatHandler.joinWorldChannel joinWorldChannel 加入世界频道 * @apiGroup Chat * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to join world channel." * } * * */ async joinWorldChannel(msg, session, next) { const uid = session.uid; const channelId = "worldChannel"; const channel = this.channelService.getChannel(channelId, true); if (channel.getMember(uid)) { next(null, { code: Code.CHAT.FA_USER_ALREAD_JOINED, msg: "User already joined the world channel", }); return; } channel.add(session.uid, session.get("sid")); next(null, { code: Code.OK }); } /** * @api {post} chat.chatHandler.leaveWorldChannel leaveWorldChannel 离开世界频道 * @apiGroup Chat * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to leave world channel." * } * */ async leaveWorldChannel(msg, session, next) { const username = session.get("username"); const channelId = "worldChannel"; const channel = this.channelService.getChannel(channelId, false); if (channel) { channel.leave(session.uid, session.get("sid")); } next(null, { code: Code.OK }); } /** * @api {post} chat.chatHandler.joinTeamChannel joinTeamChannel 加入队伍频道 * @apiGroup Chat * * @apiParam {String} teamId 队伍ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to join team channel." * } */ async joinTeamChannel(msg, session, next) { const uid = session.uid; const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; const channel = this.channelService.getChannel(channelId, true); if (channel.getMember(uid)) { next(null, { code: Code.CHAT.FA_USER_ALREAD_JOINED, msg: "User already joined the team channel", }); return; } channel.add(session.uid, session.get("sid")); next(null, { code: 200 }); channel.pushMessage({ code: Code.OK, route: "onAdd", user: uid, }); } /** * @api {post} chat.chatHandler.leaveTeamChannel leaveTeamChannel 离开队伍频道 * @apiGroup Chat * * @apiParam {String} teamId 队伍ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to leave team channel." * } */ async leaveTeamChannel(msg, session, next) { const uid = session.uid; const username = session.get("username"); const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; // 把this.app.get('channelService') 替换成 this.channelService const channel = this.app.get("channelService").getChannel(channelId, false); if (channel) { channel.leave(uid, session.get("sid")); } next(null, { code: 200 }); channel.pushMessage({ code: Code.OK, route: "onLeave", user: uid, }); } /** * @api {post} chat.chatHandler.worldChat worldChat 世界聊天 * @apiGroup Chat * * @apiParam {String} content 聊天内容 * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to world chat." * } */ async worldChat(msg, session, next) { const username = session.get("username"); const content = msg.content; const channelId = "worldChannel"; const channel = this.app.get("channelService").getChannel(channelId, true); channel.pushMessage({ code: Code.OK, route: "onChat", from: username, content: content, type: "world", }); next(null, { code: 200 }); } /** * @api {post} chat.chatHandler.teamChat teamChat 队伍聊天 * @apiGroup Chat * * @apiParam {String} content 聊天内容 * @apiParam {String} teamId 队伍ID * * @apiSuccess {Number} code 状态码 * * @apiSuccessExample {json} Success-Response: * { * "code": 200 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to team chat." * } */ async teamChat(msg, session, next) { const uid = session.uid; const username = session.get("username"); const content = msg.content; const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; const channel = this.app.get("channelService").getChannel(channelId, true); if (channel.getMember(uid)) { channel.pushMessage({ code: Code.OK, route: "onChat", from: username, content: content, type: "team", teamId: teamId, }); next(null, { code: 200 }); } next(null, { code: Code.CHAT.FA_USER_NOT_TEAM_MEMBER, msg: "User not team member", }); } }