diff --git a/game-server/app/servers/chat/handler/chatHandler.ts b/game-server/app/servers/chat/handler/chatHandler.ts index 120ee103f..95128d1a6 100644 --- a/game-server/app/servers/chat/handler/chatHandler.ts +++ b/game-server/app/servers/chat/handler/chatHandler.ts @@ -100,7 +100,7 @@ export class ChatHandler { 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(targetRoleId, msgData); + await pushMsgToRole(msgData); if (!msgData) return resResult(STATUS.WRONG_PARMS); return resResult(STATUS.SUCCESS, msgData); } diff --git a/game-server/app/servers/role/handler/heroHandler.ts b/game-server/app/servers/role/handler/heroHandler.ts index 3493434c4..a3f19f617 100644 --- a/game-server/app/servers/role/handler/heroHandler.ts +++ b/game-server/app/servers/role/handler/heroHandler.ts @@ -4,13 +4,12 @@ import { calPlayerCeAndSave, calAllHeroCe } from '../../../services/playerCeServ import { resResult, returnHeroCeRatio, deepCopy } from '../../../pubUtils/util'; import { STATUS } from '../../../consts/statusCode'; import { HeroModel, Connect } from '../../../db/Hero'; -import {CURRENCY_BY_TYPE, CURRENCY_TYPE, CONSUME_TYPE, HERO_GROW_MAX, HERO_SYSTEM_TYPE, ITID, ABI_STAGE, HERO_CE_RATIO, DEBUG_MAGIC_WORD, HERO_INITIAL_QUALITY} from '../../../consts'; +import {CURRENCY_BY_TYPE, CURRENCY_TYPE, CONSUME_TYPE, HERO_GROW_MAX, HERO_SYSTEM_TYPE, ABI_STAGE, DEBUG_MAGIC_WORD, HERO_INITIAL_QUALITY} from '../../../consts'; import { RoleModel } from '../../../db/Role'; import { ItemModel } from '../../../db/Item'; import { gameData, getHeroExpByLv, getHeroStarByQuality, getHeroWakeByQuality, getHeroLvByExp, getMaxGradeByjobClass, getJobByGradeAndClass, getFriendShipById, getFavourLvByExp } from '../../../pubUtils/data'; import { RewardInter } from '../../../pubUtils/interface'; import { getDropItems } from '../../../consts/constModules/itemConst' -import { getRoleOnlineInfo, getAllOnlineRoles } from '../../../services/redisService'; import { pushComposeOrangeHero, pushHeroQualityUpMsg, pushHeroStarMax } from '../../../services/chatService'; import { calculatetopLineup } from '../../../pubUtils/playerCe'; import { PvpDefenseModel } from '../../../db/PvpDefense'; diff --git a/game-server/app/services/chatChannelService.ts b/game-server/app/services/chatChannelService.ts new file mode 100644 index 000000000..adc6db961 --- /dev/null +++ b/game-server/app/services/chatChannelService.ts @@ -0,0 +1,64 @@ + +import { addRedisChannel, redisChannelServer } from './redisService'; +import { groupRoomId } from './chatService'; +import { pinus } from 'pinus'; +import { crc32 } from 'crc'; +import { CHANNEL_PREFIX } from '../consts'; +import { RoleModel } from './../db/Role'; + +export async function channelServer(roomId: string) { + const existSid = await redisChannelServer(roomId); + if (existSid) { + return existSid; + } + const servers = pinus.app.getServersByType('chat'); + if (!servers || !servers.length) return null; + + let index = Math.abs(crc32(roomId)) % servers.length; + const newSid = servers[index].id; + const addResult = await addRedisChannel(roomId, newSid); + if (!addResult) return null; + return newSid; +} + +async function addRoleToChannel(roomId: string, roleId: string, sid: string) { + const channelSid = await channelServer(roomId); + await pinus.app.rpc.chat.chatRemote.addChannel.toServer(channelSid, roomId, roleId, sid); +} + +export async function addRoleToSysChannel(roleId: string, sid: string, serverId: number) { + const roomId = groupRoomId(CHANNEL_PREFIX.SYS, serverId); + await addRoleToChannel(roomId, roleId, sid); +} + +export async function addRoleToWorldChannel(roleId: string, sid: string, serverId: number) { + const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId); + await addRoleToChannel(roomId, roleId, sid); +} + +export async function addRoleToGuildChannel(roleId: string, sid: string, guildCode: string) { + const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode); + await addRoleToChannel(roomId, roleId, sid); +} + +async function leaveChannel(roomId: string, roleId: string, sid: string) { + const channelSid = await channelServer(roomId); + await pinus.app.rpc.chat.chatRemote.leaveChannel.toServer(channelSid, roomId, roleId, sid); +} + +export async function leaveSysChannel(roleId: string, sid: string, serverId: number) { + const roomId = groupRoomId(CHANNEL_PREFIX.SYS, serverId); + await leaveChannel(roomId, roleId, sid); +} + +export async function leaveWorldChannel(roleId: string, sid: string, serverId: number) { + const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId); + await leaveChannel(roomId, roleId, sid); +} + +export async function leaveGuildChannel(roleId: string, sid: string) { + const { guildCode } = await RoleModel.findByRoleId(roleId, 'guildCode'); + if (!guildCode) return; + const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode); + await leaveChannel(roomId, roleId, sid); +} diff --git a/game-server/app/services/chatService.ts b/game-server/app/services/chatService.ts index 6df6a7de8..0e61d8790 100644 --- a/game-server/app/services/chatService.ts +++ b/game-server/app/services/chatService.ts @@ -1,7 +1,4 @@ import { PrivateChatRec } from './../db/ChatInfo'; -import { BossInstanceType } from './../db/BossInstance'; -import { isString } from 'underscore'; -import { cloneDeep, isArray, pick } from 'lodash'; import { RoleModel } from './../db/Role'; import { GroupMessageModel } from './../db/GroupMessage'; import { CounterModel } from './../db/Counter'; @@ -10,12 +7,13 @@ import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './ import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage'; import { genCode, resResult } from '../pubUtils/util'; import { pinus } from 'pinus'; -import { CHANNEL_PREFIX, MSG_CODE_LEN, MSG_SOURCE, MSG_STATUS, MSG_TYPE, ON_GROUP_MSG_ROUTE, ON_MSG_ROUTE, RICH_TEXT_TABLE, RECENT_PRIVATE_CHATS_CNT, MAX_PRIVATE_MSGS, RECENT_GROUP_MSGS_CNT, WAR_TYPE } from '../consts'; -import { addRedisChannel, getRoleOnlineInfo, redisChannelServer } from './redisService'; -import { crc32 } from 'crc'; -import { EPlace, HeroType } from '../db/Hero'; -import { GuildType } from '../db/Guild'; +import { CHANNEL_PREFIX, MSG_CODE_LEN, MSG_STATUS, ON_MSG_ROUTE, RECENT_PRIVATE_CHATS_CNT, RECENT_GROUP_MSGS_CNT } from '../consts'; +import { getRoleOnlineInfo } from './redisService'; import { ChatInfoModel } from '../db/ChatInfo'; +import { channelServer } from './chatChannelService'; + +export * from './chatChannelService'; +export * from './sysChatService'; /** * @description 生成私聊房间号 @@ -58,13 +56,6 @@ async function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) { /** * @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, source: number, content: string, targetRoleId: string, targetMsgCode: string) { const result: PrivateMessageParam = {roleId, roleName, type, source, content, targetRoleId, targetMsgCode}; @@ -73,6 +64,9 @@ async function createPrivateMsgData(roleId: string, roleName: string, type: numb return result; } +/** + * @description 生成群聊消息数据 + */ async function createGroupMsgData(roleId: string, roleName: string, channel: string, channelId: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { const result: GroupMessageParam = {roleId, roleName, channel, channelId, type, source, content, targetRoleId, targetMsgCode}; result.roomId = groupRoomId(channel, channelId); @@ -80,6 +74,143 @@ async function createGroupMsgData(roleId: string, roleName: string, channel: str return result; } +/** + * @description 数据库中创建私聊数据,需要更新聊天双方的聊天时间和未读消息数 + * @export + * @param {string} roleId + * @param {string} roleName + * @param {number} type + * @param {number} source + * @param {string} content + * @param {string} targetRoleId 消息接收者 + * @param {string} targetMsgCode 回复某条消息的唯一标识 + * @returns + */ +export async function createPrivateMsg(roleId: string, roleName: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { + const msgData: PrivateMessageParam = await createPrivateMsgData(roleId, roleName, type, source, content, targetRoleId, targetMsgCode); + const result: PrivateMessageType = await PrivateMessageModel.createMsg(msgData); + + const curTime = new Date(); + await updateRecentChats(true, roleId, targetRoleId, curTime); + await updateRecentChats(false, targetRoleId, roleId, curTime); + return result; +} + +/** + * @description 数据库中创建群聊数据 + * @export + * @param {string} roleId + * @param {string} roleName + * @param {string} channel + * @param {string} channelId + * @param {number} type 消息类型 + * @param {number} source 消息来源,根据配表 + * @param {string} content 富文本的 content 为 JSON.stringfy 的对象 + * @param {string} targetRoleId + * @param {string} targetMsgCode + * @returns + */ +export async function createGroupMsg(roleId: string, roleName: string, channel: string, channelId: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { + const msgData: GroupMessageParam = await createGroupMsgData(roleId, roleName, channel, channelId, type, source, content, targetRoleId, targetMsgCode); + const result: GroupMessageType = await GroupMessageModel.createMsg(msgData); + return result; +} + +/** + * @description 给某个玩家发送消息 + * @export + * @param {string} targetRoleId + * @param {(PrivateMessageType | GroupMessageType)} msg + */ +export async function pushMsgToRole(msg: PrivateMessageType | GroupMessageType) { + const targetRoleId = msg.targetRoleId!; + 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 {GroupMessageType} msg 群消息体 + * @returns + */ +export async function pushGroupMsgToRoom(msg: GroupMessageType) { + if (!msg) return; + const roomId = msg.roomId!; + const channelSid = await channelServer(roomId); + await pinus.app.rpc.chat.chatRemote.sendGroupMsg.toServer(channelSid, roomId, msg); +} + +/** + * @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; +} + +/** + * @description 最近群聊消息 + * @param {string} roomId 群聊房间标识 + * @param {number} [count] 期望获取的消息数 + * @returns + */ +async function recentGroupMsgs(roomId: string, count?: number) { + const result = await GroupMessageModel.getMsgs(roomId, Infinity, count || RECENT_GROUP_MSGS_CNT); + return result || []; +} + +/** + * @description 获取最近的世界聊天消息 + * @export + * @param {number} serverId 用区服编号做房间标识 + * @param {number} [count] + * @returns + */ +export async function recentWorldMsgs(serverId: number, count?: number) { + const result = await recentGroupMsgs(groupRoomId(CHANNEL_PREFIX.WORLD, serverId), count); + return result; +} + +/** + * @description 获取最近的系统聊天消息 + * @export + * @param {number} serverId 用区服编号做房间标识 + * @param {number} [count] + * @returns + */ +export async function recentSysMsgs(serverId: number, count?: number) { + const result = await recentGroupMsgs(groupRoomId(CHANNEL_PREFIX.SYS, serverId), count); + return result; +} + +/** + * @description 获取最近的军团聊天消息 + * @export + * @param {string} guildCode 用军团编号作为房间标识 + * @param {number} [count] + * @returns + */ +export async function recentGuildMsgs(guildCode: string, count?: number) { + const result = await recentGroupMsgs(groupRoomId(CHANNEL_PREFIX.GUILD, guildCode), count); + return result; +} + +/** + * @description 创建新的私聊记录 + * @param {boolean} sender 给发送者还是接收者创建,发送者发消息的时候也阅读了消息 + * @param {Date} curTime 当前时间 + * @param {*} targetRoleId 聊天对象 + * @returns + */ function createChatRec(sender: boolean, curTime: Date, targetRoleId) { const result: PrivateChatRec = { // 接收者没有阅读时间,发送者未读数为 0 @@ -88,6 +219,13 @@ function createChatRec(sender: boolean, curTime: Date, targetRoleId) { return result; } +/** + * @description 更新最近聊天记录 + * @param {boolean} sender 同 createChatRec 接口参数 + * @param {string} roleId 更新此玩家的聊天记录 + * @param {string} targetRoleId roleId 的聊天对象 + * @param {Date} curTime + */ async function updateRecentChats(sender: boolean, roleId: string, targetRoleId: string, curTime: Date) { const { recentPrivateChats } = await ChatInfoModel.findInfo(roleId); const recIdx = recentPrivateChats.findIndex(chat => { return chat.targetRoleId === targetRoleId }); @@ -104,242 +242,8 @@ async function updateRecentChats(sender: boolean, roleId: string, targetRoleId: } /** - * @description 数据库中创建私聊数据 - * @export - * @param {string} roleId - * @param {string} roleName - * @param {number} type - * @param {string} content - * @param {string} targetRoleId - * @param {string} targetMsgCode - * @returns + * @description 获取聊天信息 */ -export async function createPrivateMsg(roleId: string, roleName: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { - const msgData: PrivateMessageParam = await createPrivateMsgData(roleId, roleName, type, source, content, targetRoleId, targetMsgCode); - const result: PrivateMessageType = await PrivateMessageModel.createMsg(msgData); - - const curTime = new Date(); - await updateRecentChats(true, roleId, targetRoleId, curTime); - await updateRecentChats(false, targetRoleId, roleId, curTime); - 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; -} - -export async function createGroupMsg(roleId: string, roleName: string, channel: string, channelId: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { - const msgData: GroupMessageParam = await createGroupMsgData(roleId, roleName, channel, channelId, type, source, content, targetRoleId, targetMsgCode); - const result: GroupMessageType = await GroupMessageModel.createMsg(msgData); - return result; -} - -export async function pushGroupMsgToRoom(msg: GroupMessageType) { - if (!msg) return; - const roomId = msg.roomId!; - const channelSid = await channelServer(roomId); - await pinus.app.rpc.chat.chatRemote.sendGroupMsg.toServer(channelSid, roomId, msg); -} - -export async function channelServer(roomId: string) { - const existSid = await redisChannelServer(roomId); - if (existSid) { - return existSid; - } - const servers = pinus.app.getServersByType('chat'); - if (!servers || !servers.length) return null; - - let index = Math.abs(crc32(roomId)) % servers.length; - const newSid = servers[index].id; - const addResult = await addRedisChannel(roomId, newSid); - if (!addResult) return null; - return newSid; -} - -async function addRoleToChannel(roomId: string, roleId: string, sid: string) { - const channelSid = await channelServer(roomId); - await pinus.app.rpc.chat.chatRemote.addChannel.toServer(channelSid, roomId, roleId, sid); -} - -export async function addRoleToSysChannel(roleId: string, sid: string, serverId: number) { - const roomId = groupRoomId(CHANNEL_PREFIX.SYS, serverId); - await addRoleToChannel(roomId, roleId, sid); -} - -export async function addRoleToWorldChannel(roleId: string, sid: string, serverId: number) { - const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId); - await addRoleToChannel(roomId, roleId, sid); -} - -export async function addRoleToGuildChannel(roleId: string, sid: string, guildCode: string) { - const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode); - await addRoleToChannel(roomId, roleId, sid); -} - -async function leaveChannel(roomId: string, roleId: string, sid: string) { - const channelSid = await channelServer(roomId); - await pinus.app.rpc.chat.chatRemote.leaveChannel.toServer(channelSid, roomId, roleId, sid); -} - -export async function leaveSysChannel(roleId: string, sid: string, serverId: number) { - const roomId = groupRoomId(CHANNEL_PREFIX.SYS, serverId); - await leaveChannel(roomId, roleId, sid); -} - -export async function leaveWorldChannel(roleId: string, sid: string, serverId: number) { - const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId); - await leaveChannel(roomId, roleId, sid); -} - -export async function leaveGuildChannel(roleId: string, sid: string) { - const { guildCode } = await RoleModel.findByRoleId(roleId, 'guildCode'); - if (!guildCode) return; - const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode); - await leaveChannel(roomId, roleId, sid); -} - -function richTextSetting(type, jumpTo) { - const settings = cloneDeep(RICH_TEXT_TABLE); - for (let setting of settings) { - if (setting.type === type && setting.jumpTo === jumpTo) { - return setting; - } - } -} - -export function wrapRichText(type: string, jumpTo: string, content: string, parm: string) { - const setting = richTextSetting(type, jumpTo); - return JSON.stringify({ - id: setting.id, content, parm - }); -} - -async function pushNormalHeroInfoBySource(roleId: string, roleName: string, serverId: number | string, source: number, heroInfo: Partial) { - const hero = pick(heroInfo, ['hName', 'hid', 'seqId', 'quality', 'star', 'starStage', 'colorStar', 'colorStarStage']); - const content = JSON.stringify({ roleId, roleName, hero }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); - await pushGroupMsgToRoom(msgData); -} - -export async function pushHeroQualityUpMsg(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial) { - await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_QUALITY_UP, heroInfo); -} - -export async function pushComposeOrangeHero(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial) { - await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.COMPOSE_ORANGE_HERO, heroInfo); -} - -export async function pushHeroStarMax(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial) { - await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_STAR_MAX, heroInfo); -} - -export async function pushComBtlTeamMsg(teamCode: string, roleId: string, roleName: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.TEAM, teamCode, type, source, content, targetRoleId, targetMsgCode); - if (!msgData) return null; - const channel = pinus.app.get('channelService').getChannel(teamCode); - channel.pushMessage(ON_GROUP_MSG_ROUTE, resResult(STATUS.SUCCESS, msgData)); - return msgData; -} - -export async function pushGuildNoticeUpdateMsg(roleId: string, roleName: string, guildInfo: Partial) { - const { code, notice } = guildInfo; - if (!code || !isString(notice)) return null; - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.TEXT, MSG_SOURCE.GUILD_NOTICE, notice, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushGuildUpStructureMsg(roleId: string, roleName: string, guildInfo: Partial) { - const { code, structure } = guildInfo; - if (!code || !structure || !isArray(structure)) return null; - const guild = pick(guildInfo, ['code', 'structure']); - const content = JSON.stringify({ roleId, roleName, guild }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_STRUCTURE_LV_UP, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushGuildTrainSucMsg(roleId: string, roleName: string, guildCode: string, hid: number) { - if (!guildCode) return null; - const content = JSON.stringify({ hid }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, guildCode, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_TRAIN_SUC, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushGuildBossSucMsg(roleId: string, roleName: string, guildCode: string, bossInstance: BossInstanceType) { - const boss = pick(bossInstance, ['warId', 'bossLv']); - const content = JSON.stringify({ boss }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, guildCode, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_BOSS_SUC, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushEquipRefineSucMsg(roleId: string, roleName: string, serverId: number, eplace: Partial) { - const data = pick(eplace, ['id', 'lv', 'refineLv']); - const content = JSON.stringify({ eplace: data }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.EQUIP_REFINE_SUC, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushNormalEquipMsg(roleId: string, roleName: string, serverId: number, source: number, id: number, name: string) { - const content = JSON.stringify({ id, name }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushTowerMsg(roleId: string, roleName: string, serverId: number, source: number, lv: number) { - if (!shouldPushTowerMsg(lv)) return null; - const content = JSON.stringify({ lv }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -async function pushGKFirstMsg(roleId: string, roleName: string, serverId: number, source: number, warType: number, warId: number) { - const content = JSON.stringify({ warType, warId }); - const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); - await pushGroupMsgToRoom(msgData); - return msgData; -} - -export async function pushMysteryFirstMsg(roleId: string, roleName: string, serverId: number, warType: number, warId: number) { - if (warType !== WAR_TYPE.MYSTERY) return null; - const result = await pushGKFirstMsg(roleId, roleName, serverId, MSG_SOURCE.MYSTERY_FIRST_SUC, warType, warId); - return result; -} - -export async function pushVestigeFirstMsg(roleId: string, roleName: string, serverId: number, warType: number, warId: number) { - if (warType !== WAR_TYPE.VESTIGE) return null; - const result = await pushGKFirstMsg(roleId, roleName, serverId, MSG_SOURCE.VESTIGE_FIRST_SUC, warType, warId); - return result; -} - export async function roleChatInfos(roleId: string, roleName: string) { let result = await ChatInfoModel.findInfo(roleId); if (!result) { @@ -348,6 +252,9 @@ export async function roleChatInfos(roleId: string, roleName: string) { return result; } +/** + * @description 获取最近聊天记录,需要组织最近聊天玩家的信息 + */ export async function recentPrivateChatInfos(roleId: string, roleName: string) { const { recentPrivateChats } = await roleChatInfos(roleId, roleName); recentPrivateChats @@ -366,31 +273,9 @@ export async function recentPrivateChatInfos(roleId: string, roleName: string) { return chatInfos; } -async function recentGroupMsgs(roomId: string, count?: number) { - const result = await GroupMessageModel.getMsgs(roomId, Infinity, count || RECENT_GROUP_MSGS_CNT); - return result || []; -} - -export async function recentWorldMsgs(serverId: number, count?: number) { - const result = await recentGroupMsgs(groupRoomId(CHANNEL_PREFIX.WORLD, serverId), count); - return result; -} - -export async function recentSysMsgs(serverId: number, count?: number) { - const result = await recentGroupMsgs(groupRoomId(CHANNEL_PREFIX.SYS, serverId), count); - return result; -} - -export async function recentGuildMsgs(guildCode: string, count?: number) { - const result = await recentGroupMsgs(groupRoomId(CHANNEL_PREFIX.GUILD, guildCode), count); - return result; -} - -function shouldPushTowerMsg(lv: number) { - // 100 层之后每 50 层触发 - return lv >= 100 && lv % 50 === 0; -} - +/** + * @description 查看消息时更新查看时间和未读消息数 + */ export async function updatePrivateMsgReadInfo(roleId: string, targetRoleId: string) { const time = new Date(); const chatInfo = await ChatInfoModel.updateReadInfo(roleId, targetRoleId, time); diff --git a/game-server/app/services/sysChatService.ts b/game-server/app/services/sysChatService.ts new file mode 100644 index 000000000..760116c0e --- /dev/null +++ b/game-server/app/services/sysChatService.ts @@ -0,0 +1,118 @@ + +import { EPlace, HeroType } from '../db/Hero'; +import { GuildType } from '../db/Guild'; +import { CHANNEL_PREFIX, MSG_SOURCE, MSG_TYPE, ON_GROUP_MSG_ROUTE, STATUS, WAR_TYPE } from '../consts'; +import { createGroupMsg, pushGroupMsgToRoom } from './chatService'; +import { pick } from 'lodash'; +import { isArray, isString } from 'underscore'; +import { pinus } from 'pinus'; +import { resResult } from '../pubUtils/util'; +import { BossInstanceType } from '../db/BossInstance'; + +async function pushNormalHeroInfoBySource(roleId: string, roleName: string, serverId: number | string, source: number, heroInfo: Partial) { + const hero = pick(heroInfo, ['hName', 'hid', 'seqId', 'quality', 'star', 'starStage', 'colorStar', 'colorStarStage']); + const content = JSON.stringify({ roleId, roleName, hero }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); + await pushGroupMsgToRoom(msgData); +} + +export async function pushHeroQualityUpMsg(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial) { + await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_QUALITY_UP, heroInfo); +} + +export async function pushComposeOrangeHero(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial) { + await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.COMPOSE_ORANGE_HERO, heroInfo); +} + +export async function pushHeroStarMax(roleId: string, roleName: string, serverId: number | string, heroInfo: Partial) { + await pushNormalHeroInfoBySource(roleId, roleName, serverId, MSG_SOURCE.HERO_STAR_MAX, heroInfo); +} + +export async function pushComBtlTeamMsg(teamCode: string, roleId: string, roleName: string, type: number, source: number, content: string, targetRoleId: string, targetMsgCode: string) { + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.TEAM, teamCode, type, source, content, targetRoleId, targetMsgCode); + if (!msgData) return null; + const channel = pinus.app.get('channelService').getChannel(teamCode); + channel.pushMessage(ON_GROUP_MSG_ROUTE, resResult(STATUS.SUCCESS, msgData)); + return msgData; +} + +export async function pushGuildNoticeUpdateMsg(roleId: string, roleName: string, guildInfo: Partial) { + const { code, notice } = guildInfo; + if (!code || !isString(notice)) return null; + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.TEXT, MSG_SOURCE.GUILD_NOTICE, notice, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushGuildUpStructureMsg(roleId: string, roleName: string, guildInfo: Partial) { + const { code, structure } = guildInfo; + if (!code || !structure || !isArray(structure)) return null; + const guild = pick(guildInfo, ['code', 'structure']); + const content = JSON.stringify({ roleId, roleName, guild }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_STRUCTURE_LV_UP, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushGuildTrainSucMsg(roleId: string, roleName: string, guildCode: string, hid: number) { + if (!guildCode) return null; + const content = JSON.stringify({ hid }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, guildCode, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_TRAIN_SUC, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushGuildBossSucMsg(roleId: string, roleName: string, guildCode: string, bossInstance: BossInstanceType) { + const boss = pick(bossInstance, ['warId', 'bossLv']); + const content = JSON.stringify({ boss }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, guildCode, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_BOSS_SUC, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushEquipRefineSucMsg(roleId: string, roleName: string, serverId: number, eplace: Partial) { + const data = pick(eplace, ['id', 'lv', 'refineLv']); + const content = JSON.stringify({ eplace: data }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, MSG_SOURCE.EQUIP_REFINE_SUC, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushNormalEquipMsg(roleId: string, roleName: string, serverId: number, source: number, id: number, name: string) { + const content = JSON.stringify({ id, name }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushTowerMsg(roleId: string, roleName: string, serverId: number, source: number, lv: number) { + if (!shouldPushTowerMsg(lv)) return null; + const content = JSON.stringify({ lv }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +async function pushGKFirstMsg(roleId: string, roleName: string, serverId: number, source: number, warType: number, warId: number) { + const content = JSON.stringify({ warType, warId }); + const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.SYS, `${serverId}`, MSG_TYPE.RICH_TEXT, source, content, null, null); + await pushGroupMsgToRoom(msgData); + return msgData; +} + +export async function pushMysteryFirstMsg(roleId: string, roleName: string, serverId: number, warType: number, warId: number) { + if (warType !== WAR_TYPE.MYSTERY) return null; + const result = await pushGKFirstMsg(roleId, roleName, serverId, MSG_SOURCE.MYSTERY_FIRST_SUC, warType, warId); + return result; +} + +export async function pushVestigeFirstMsg(roleId: string, roleName: string, serverId: number, warType: number, warId: number) { + if (warType !== WAR_TYPE.VESTIGE) return null; + const result = await pushGKFirstMsg(roleId, roleName, serverId, MSG_SOURCE.VESTIGE_FIRST_SUC, warType, warId); + return result; +} + +function shouldPushTowerMsg(lv: number) { + // 100 层之后每 50 层触发 + return lv >= 100 && lv % 50 === 0; +}