增加聊天记录的存储和读取
This commit is contained in:
@@ -11,24 +11,33 @@ export class GirlChatData extends BaseData {
|
||||
private chatTotalCount: number;
|
||||
// 剩余聊天次数, < 0: 不限制; = 0 聊天次数已经用完; > 0 还剩余多少次聊天次数
|
||||
private chatRemainCount: number;
|
||||
// 聊天记录
|
||||
private chatRecord: Map<string, proto.cs.IChatMsg>;
|
||||
private chatRecordIds: number[];
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.id = 0;
|
||||
this.chatTotalCount = 0;
|
||||
this.chatRemainCount = 0;
|
||||
this.chatRecord = new Map<string, proto.cs.IChatMsg>(),
|
||||
this.chatRecordIds = [];
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.id = 0;
|
||||
this.chatTotalCount = 0;
|
||||
this.chatRemainCount = 0;
|
||||
this.chatRecord.clear();
|
||||
this.chatRecordIds = [];
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
this.id = 0;
|
||||
this.chatTotalCount = 0;
|
||||
this.chatRemainCount = 0;
|
||||
this.chatRecord.clear();
|
||||
this.chatRecordIds = [];
|
||||
}
|
||||
|
||||
public destroy(): void {
|
||||
@@ -36,6 +45,8 @@ export class GirlChatData extends BaseData {
|
||||
this.id = null;
|
||||
this.chatTotalCount = null;
|
||||
this.chatRemainCount = null;
|
||||
this.chatRecord = null;
|
||||
this.chatRecordIds = null;
|
||||
}
|
||||
|
||||
/** 获取 id */
|
||||
@@ -75,5 +86,77 @@ export class GirlChatData extends BaseData {
|
||||
if (this.chatRemainCount > 0) {
|
||||
this.setChatRemainCount(Math.max(0, this.chatRemainCount - times));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 解析聊天记录数据,一个
|
||||
private parseOneChatRecord(data: proto.cs.IChatMsg): proto.cs.IChatMsg | null {
|
||||
if (!data) return null;
|
||||
const oneData: proto.cs.IChatMsg = {
|
||||
msgId: data.msgId, // id,使用时间戳(精确到毫秒)
|
||||
msg: data.msg, // 聊天内容
|
||||
isAi: data.isAi, // 是否是 ai 聊天数据
|
||||
};
|
||||
return oneData;
|
||||
}
|
||||
|
||||
// 解析聊天记录数据,多个
|
||||
private parseAllChatRecord(data: proto.cs.IChatMsg[]): proto.cs.IChatMsg[] {
|
||||
if (!data) return [];
|
||||
let allData = [];
|
||||
for (const value of data) {
|
||||
const oneData: proto.cs.IChatMsg = this.parseOneChatRecord(value);
|
||||
if (oneData) {
|
||||
allData.push(oneData);
|
||||
}
|
||||
}
|
||||
return allData;
|
||||
}
|
||||
|
||||
/** 保存聊天记录 */
|
||||
public setChatRecord(data: proto.cs.IChatMsg[]): void {
|
||||
// 如果传入的记录为空,直接返回
|
||||
if (!data || data.length === 0) return;
|
||||
// 解析数据
|
||||
let allData = this.parseAllChatRecord(data);
|
||||
if (!allData) return;
|
||||
|
||||
// 使用 Map 来进行去重,key 为 msgId
|
||||
const existingMsgIds = new Set(this.chatRecordIds);
|
||||
|
||||
// 过滤掉已经存在的消息(通过 msgId)
|
||||
const newChatRecords = allData.filter(msg => !existingMsgIds.has(msg.msgId));
|
||||
|
||||
// 将新的聊天记录添加到 Map 中
|
||||
newChatRecords.forEach(msg => {
|
||||
this.chatRecord.set(msg.msgId.toString(), msg);
|
||||
this.chatRecordIds.push(Number(msg.msgId));
|
||||
});
|
||||
|
||||
// 排序:根据 msgId 从大到小排序
|
||||
this.chatRecordIds.sort((a, b) => b - a);
|
||||
|
||||
console.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord);
|
||||
}
|
||||
|
||||
/** 获取聊天记录的所有 id */
|
||||
public getChatRecordIds(): number[] {
|
||||
return this.chatRecordIds;
|
||||
}
|
||||
|
||||
/** 获取聊天记录,根据 id */
|
||||
private getChatRecordById(id: number): proto.cs.IChatMsg | null {
|
||||
return this.chatRecord.get(id.toString()) ?? null;
|
||||
}
|
||||
|
||||
/** 获取聊天记录,根据 id,是否是 ai 聊天数据 */
|
||||
public getChatRecordIsAi(id: number): boolean {
|
||||
const chatRecord = this.getChatRecordById(id);
|
||||
return chatRecord ? chatRecord.isAi : false;
|
||||
}
|
||||
|
||||
/** 获取聊天记录,根据 id,聊天内容 */
|
||||
public getChatRecordMsg(id: number): string {
|
||||
const chatRecord = this.getChatRecordById(id);
|
||||
return chatRecord ? chatRecord.msg : "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user