diff --git a/game-server/app/servers/connector/handler/entryHandler.ts b/game-server/app/servers/connector/handler/entryHandler.ts index 9bbeb7e0e..1f598c182 100644 --- a/game-server/app/servers/connector/handler/entryHandler.ts +++ b/game-server/app/servers/connector/handler/entryHandler.ts @@ -19,7 +19,7 @@ import { GuildModel } from '../../../db/Guild'; import { gameData } from '../../../pubUtils/data'; import { getMails } from '../../../services/mailService'; -import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGuildChannel, leaveSysChannel, leaveWorldChannel, recentPrivateChatInfos } from '../../../services/chatService'; +import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGuildChannel, leaveSysChannel, leaveWorldChannel, recentGuildMsgs, recentPrivateChatInfos, recentSysMsgs, recentWorldMsgs } from '../../../services/chatService'; import { reportOneOnline } from '../../../services/timeTaskService'; export default function (app: Application) { return new EntryHandler(app); @@ -113,6 +113,8 @@ export class EntryHandler { if (recentPrivateChats) { role['recentPrivateChats'] = recentPrivateChats; } + role['worldMsgs'] = await recentWorldMsgs(role.serverId); + role['sysMsgs'] = await recentSysMsgs(role.serverId); if(role.hasGuild) { let userGuild = await UserGuildModel.getMyGuild(role.roleId, USER_GUILD_SELECT.ENTRY ); @@ -120,6 +122,7 @@ export class EntryHandler { let guild = await GuildModel.findGuild(userGuild.guildCode, role.serverId, GUILD_SELECT.ENTRY); if(guild) { addRoleToGuildChannel(role.roleId, self.app.get('serverId'), userGuild.guildCode); + role['guildMsgs'] = await recentGuildMsgs(userGuild.guildCode); role['guildAuth'] = userGuild.auth; role['guildCode'] = userGuild.guildCode; let {lv: guildLv, memberCnt} = guild; diff --git a/game-server/app/services/chatService.ts b/game-server/app/services/chatService.ts index c2d77a4a0..eb1614736 100644 --- a/game-server/app/services/chatService.ts +++ b/game-server/app/services/chatService.ts @@ -10,7 +10,7 @@ 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 } from '../consts'; +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 } from '../consts'; import { addRedisChannel, getRoleOnlineInfo, redisChannelServer } from './redisService'; import { crc32 } from 'crc'; import { HeroType } from '../db/Hero'; @@ -34,7 +34,7 @@ export function privateRoomId(roleId: string, targetRoleId: string) { * @param {string} channelId * @returns */ -export function groupRoomId(channel: string, channelId: string) { +export function groupRoomId(channel: string, channelId: string | number) { return `${channel}_${channelId}`; } @@ -174,12 +174,12 @@ async function addRoleToChannel(roomId: string, roleId: string, sid: string) { } export async function addRoleToSysChannel(roleId: string, sid: string, serverId: number) { - const roomId = groupRoomId(CHANNEL_PREFIX.SYS, `${serverId}`); + 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}`); + const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId); await addRoleToChannel(roomId, roleId, sid); } @@ -194,12 +194,12 @@ async function leaveChannel(roomId: string, roleId: string, sid: string) { } export async function leaveSysChannel(roleId: string, sid: string, serverId: number) { - const roomId = groupRoomId(CHANNEL_PREFIX.SYS, `${serverId}`); + 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}`); + const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId); await leaveChannel(roomId, roleId, sid); } @@ -230,7 +230,7 @@ async function pushNormalHeroInfoBySource(roleId: string, roleName: string, serv 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); - const roomId = groupRoomId(CHANNEL_PREFIX.SYS, `${serverId}`); + const roomId = groupRoomId(CHANNEL_PREFIX.SYS, serverId); await pushGroupMsgToRoom(roomId, msgData); } @@ -325,3 +325,23 @@ 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; +} \ No newline at end of file diff --git a/game-server/test/chat.test.ts b/game-server/test/chat.test.ts index ff09ddf4d..42d905a51 100644 --- a/game-server/test/chat.test.ts +++ b/game-server/test/chat.test.ts @@ -173,6 +173,28 @@ describe('聊天测试', function() { checkChatInfos(roleInfoT); }); + it('最近群聊的消息', function() { + function checkMsgs(msgs, roleName) { + expect(msgs).to.be.an('array'); + if (msgs.length === 0) { + console.warn(`用户${roleName}缺少群消息`); + } + for (let msg of msgs) { + expect(msg.roleId).to.be.a('string'); + expect(msg.roleName).to.be.a('string'); + expect(msg.content).to.be.a('string'); + expect(msg.roomId).to.be.a('string'); + } + } + function checkAllGroupMsgs(roleInfo) { + checkMsgs(roleInfo.sysMsgs, roleInfo.roleName); + checkMsgs(roleInfo.worldMsgs, roleInfo.roleName); + checkMsgs(roleInfo.guildMsgs, roleInfo.roleName); + } + checkAllGroupMsgs(roleInfo); + checkAllGroupMsgs(roleInfoT); + }); + it('测试系统频道橙将合成消息', function(done) { let msgReceiveCnt = 0; pinusClientT.on(ON_GROUP_MSG_ROUTE, (res) => { diff --git a/shared/consts/constModules/chatConst.ts b/shared/consts/constModules/chatConst.ts index 1fb8897a9..7dbf5df9a 100644 --- a/shared/consts/constModules/chatConst.ts +++ b/shared/consts/constModules/chatConst.ts @@ -1,5 +1,6 @@ export const MSG_CODE_LEN = 8; export const RECENT_PRIVATE_CHATS_CNT = 20; +export const RECENT_GROUP_MSGS_CNT = 10; export const MAX_PRIVATE_MSGS = 99; export const MSG_STATUS = {