Files
ZYZ/game-server/app/servers/chat/handler/chatHandler.ts
2021-03-11 17:27:37 +08:00

167 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CHANNEL_PREFIX, MSG_SOURCE } from './../../../consts/constModules/chatConst';
import {Application, BackendSession} from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts';
import { createAccuseData, createGroupMsg, createPrivateMsg, getPrivateMessages, pushGroupMsgToRoom, pushMsgToRole, updatePrivateMsgReadInfo } from '../../../services/chatService';
export default function(app: Application) {
return new ChatHandler(app);
}
export class ChatHandler {
constructor(private app: Application) {
}
/**
* Send messages to users
*
* @param {Object} msg message from client
* @param {Object} session
*
*/
async send(msg: {content: string , target: string}, session: BackendSession) {
let rid = session.get('rid');
let username = session.uid.split('*')[0];
let channelService = this.app.get('channelService');
let param = {
msg: msg.content,
from: username,
target: msg.target
};
let channel = channelService.getChannel(rid, false);
// the target is all users
if (msg.target === '*') {
channel.pushMessage('onChat', resResult(STATUS.SUCCESS, param));
}
// the target is specific user
else {
let tuid = msg.target + '*' + rid;
let tsid = channel.getMember(tuid)['sid'];
channelService.pushMessageByUids('onChat', resResult(STATUS.SUCCESS, param), [{
uid: tuid,
sid: tsid
}]);
}
}
async send2(msg: {content: string , target: string}, session: BackendSession) {
let rid = session.get('rid');
let username = session.uid.split('*')[0];
let channelService = this.app.get('channelService');
let param = {
msg: msg.content,
from: username,
target: msg.target
};
let channel = channelService.getChannel(rid, false);
// the target is all users
if (msg.target === '*') {
channel.pushMessage('onChat', resResult(STATUS.SUCCESS, param));
}
// the target is specific user
else {
let tuid = msg.target + '*' + rid;
let tsid = channel.getMember(tuid)['sid'];
channelService.pushMessageByUids('onChat', resResult(STATUS.SUCCESS, param), [{
uid: tuid,
sid: tsid
}]);
}
}
/**
* @description 群聊发送接口
* @param {{ channel: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }} msg
* @param {BackendSession} session
* @memberof ChatHandler
*/
async sendGroupMessage(msg: { channel: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
const { channel, type, content, targetRoleId, targetMsgCode } = msg;
if (channel === CHANNEL_PREFIX.SYS) return resResult(STATUS.SYS_CHANNEL_AUTH_NOT_ENOUGH);
const roleId = session.get('roleId');
const roleName = session.get('roleName');
const serverId = session.get('serverId');
const guildCode = session.get('guildCode');
let channelId = '';
if (channel === CHANNEL_PREFIX.WORLD) channelId = `${serverId}`;
if (channel === CHANNEL_PREFIX.GUILD) channelId = guildCode;
const msgData = await createGroupMsg(roleId, roleName, channel, channelId, type, MSG_SOURCE.ROLE_SEND_TEXT, content, targetRoleId, targetMsgCode);
if (!msgData) return resResult(STATUS.WRONG_PARMS);
await pushGroupMsgToRoom(msgData);
return resResult(STATUS.SUCCESS);
}
/**
* @description 私聊发送接口
* @param {{ type: number, content: string, targetRoleId: string, targetMsgCode: string }} msg
* @param {BackendSession} session
* @memberof ChatHandler
*/
async sendPrivateMessage(msg: { type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
const { type, content, targetRoleId, targetMsgCode } = msg;
const roleId = session.get('roleId');
const roleName = session.get('roleName');
const msgData = await createPrivateMsg(roleId, roleName, type, MSG_SOURCE.ROLE_SEND_TEXT, content, targetRoleId, targetMsgCode);
await pushMsgToRole(msgData);
if (!msgData) return resResult(STATUS.WRONG_PARMS);
return resResult(STATUS.SUCCESS, msgData);
}
/**
* @description 获取初始聊天消息,数据和登录时返回的一致
* @param {{}} msg
* @memberof ChatHandler
*/
async getInitMessage(msg: {}, session: BackendSession) {
}
/**
* @description 获取私聊历史消息
* @param {{targetRoleId: string, fromSeqId: number, count: number}} msg 聊天对象;聊天消息的顺序 id不传为从最新开始count 为消息条数
* @memberof ChatHandler
*/
async getPrivateMessage(msg: {targetRoleId: string, fromSeqId: number, count: number}, session: BackendSession) {
const roleId = session.get('roleId');
const { targetRoleId, fromSeqId = Infinity, count = DEFAULT_MSG_PER_PAGE } = msg;
const msgs = await getPrivateMessages(roleId, targetRoleId, fromSeqId, count);
return resResult(STATUS.SUCCESS, { targetRoleId, msgs });
}
/**
* @description 查看私聊,主要用于更新最后查看时间和未读消息数
* @param {{ targetRoleId: string }} msg
* @param {BackendSession} session
* @returns
* @memberof ChatHandler
*/
async readPrivateMessage(msg: { targetRoleId: string }, session: BackendSession) {
const roleId = session.get('roleId');
const { targetRoleId } = msg;
const result = await updatePrivateMsgReadInfo(roleId, targetRoleId);
if (!result) {
return resResult(STATUS.UPDATE_PRIVATE_MSG_READ_TIME_ERR);
}
return resResult(STATUS.SUCCESS, result);
}
/**
* @description 举报玩家的消息
* @param {{targetRoleId: string, targetMsgCode: string; reason: number}} msg 被举报玩家的 Id被举报的消息编号举报原因
* @param {BackendSession} session
* @returns
* @memberof ChatHandler
*/
async accuse(msg: {targetRoleId: string, targetMsgCode: string; reason: number}, session: BackendSession) {
const roleId = session.get('roleId');
const { targetRoleId, targetMsgCode, reason } = msg;
const accuseRec = await createAccuseData(roleId, targetRoleId, targetMsgCode, reason);
if (!accuseRec) return resResult(STATUS.WRONG_PARMS);
return resResult(STATUS.SUCCESS, accuseRec);
}
}