108 lines
3.6 KiB
TypeScript
108 lines
3.6 KiB
TypeScript
import { CounterModel } from './../db/Counter';
|
|
import { STATUS } from './../consts/statusCode';
|
|
import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './../db/PrivateMessage';
|
|
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
|
|
import { genCode, resResult } from '../pubUtils/util';
|
|
import { pinus } from 'pinus';
|
|
import { MSG_CODE_LEN, MSG_STATUS, ON_MSG_ROUTE } from '../consts';
|
|
import { getRoleOnlineInfo } from './redisService';
|
|
|
|
/**
|
|
* @description 生成私聊房间号
|
|
* @export
|
|
* @param {string[]} roleIds
|
|
* @returns
|
|
*/
|
|
export function privateRoomId(roleId: string, targetRoleId: string) {
|
|
return [roleId, targetRoleId].sort().join('_');
|
|
}
|
|
|
|
/**
|
|
* @description 生成群聊房间号
|
|
* @export
|
|
* @param {string} channel
|
|
* @param {string} channelId
|
|
* @returns
|
|
*/
|
|
export function groupRoomId(channel: string, channelId: string) {
|
|
return `${channel}_${channelId}`;
|
|
}
|
|
|
|
function msgCounterName(roomId: string) {
|
|
return `chat_${roomId}`;
|
|
}
|
|
|
|
/**
|
|
* @description 设置群消息和私聊消息的公共字段
|
|
* @param {(PrivateMessageParam | GroupMessageParam)} msg
|
|
*/
|
|
async function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
|
msg.msgCode = genCode(MSG_CODE_LEN);
|
|
msg.status = MSG_STATUS.NORMAL;
|
|
if (!msg.roomId) {
|
|
console.error('roomId needed while setup msg parameter');
|
|
return;
|
|
}
|
|
msg.seqId = await CounterModel.getNewCounter({name: msgCounterName(msg.roomId), def: 1});
|
|
}
|
|
|
|
/**
|
|
* @description 生成私聊消息数据
|
|
* @param {string} roleId
|
|
* @param {string} roleName
|
|
* @param {number} type
|
|
* @param {string} content
|
|
* @param {string} targetRoleId
|
|
* @param {string} targetMsgCode
|
|
* @returns
|
|
*/
|
|
async function createPrivateMsgData(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
|
const result: PrivateMessageParam = {roleId, roleName, type, content, targetRoleId, targetMsgCode};
|
|
result.roomId = privateRoomId(roleId, targetRoleId);
|
|
await setupBaseMsgParm(result);
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @description 数据库中创建私聊数据
|
|
* @export
|
|
* @param {string} roleId
|
|
* @param {string} roleName
|
|
* @param {number} type
|
|
* @param {string} content
|
|
* @param {string} targetRoleId
|
|
* @param {string} targetMsgCode
|
|
* @returns
|
|
*/
|
|
export async function createPrivateMsg(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
|
const msgData: PrivateMessageParam = await createPrivateMsgData(roleId, roleName, type, content, targetRoleId, targetMsgCode);
|
|
const result: PrivateMessageType = await PrivateMessageModel.createMsg(msgData);
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @description 给某个玩家发送消息
|
|
* @export
|
|
* @param {string} targetRoleId
|
|
* @param {(PrivateMessageType | GroupMessageType)} msg
|
|
*/
|
|
export async function pushMsgToRole(targetRoleId: string, msg: PrivateMessageType | GroupMessageType) {
|
|
const { sid } = await getRoleOnlineInfo(targetRoleId);
|
|
if (sid) {
|
|
pinus.app.get('channelService').pushMessageByUids(ON_MSG_ROUTE, resResult(STATUS.SUCCESS, msg), [{uid: targetRoleId, sid}]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @description 获取私聊历史消息
|
|
* @export
|
|
* @param {string} roleId
|
|
* @param {string} targetRoleId
|
|
* @param {number} fromSeqId
|
|
* @param {number} count
|
|
* @returns
|
|
*/
|
|
export async function getPrivateMessages(roleId: string, targetRoleId: string, fromSeqId: number, count: number) {
|
|
const result = await PrivateMessageModel.getMsgs(privateRoomId(roleId, targetRoleId), fromSeqId, count);
|
|
return result;
|
|
} |