206 lines
6.0 KiB
TypeScript
206 lines
6.0 KiB
TypeScript
/**
|
|
* 技师的聊天数据
|
|
*/
|
|
import { BaseData } from "./BaseData";
|
|
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
|
import Utils from "../../Main/Common/Utils";
|
|
import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
|
|
import { logger } from "db://assets/Scripts/Main/Common/Logger";
|
|
|
|
export class GirlChatData extends BaseData {
|
|
// id
|
|
private id: number;
|
|
// 总聊天次数
|
|
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 {
|
|
super.destroy();
|
|
this.id = null;
|
|
this.chatTotalCount = null;
|
|
this.chatRemainCount = null;
|
|
this.chatRecord = null;
|
|
this.chatRecordIds = null;
|
|
}
|
|
|
|
/** 获取 id */
|
|
public getId(): number {
|
|
return this.id;
|
|
}
|
|
|
|
/** 保存 id */
|
|
public setId(value: number): void {
|
|
this.id = value;
|
|
}
|
|
|
|
/** 获取总聊天次数 */
|
|
public getChatTotalCount(): number {
|
|
return this.chatTotalCount;
|
|
}
|
|
|
|
/** 保存总聊天次数 */
|
|
public setChatTotalCount(value: number): void {
|
|
this.chatTotalCount = value;
|
|
// 发出事件
|
|
let msgData = {
|
|
girlId: this.id,
|
|
chatTotalCount: this.chatTotalCount,
|
|
};
|
|
Utils.sendInnerMsg(InnerMsgCode.ChatTotalCountChange, msgData);
|
|
logger.log("当前技师 ", this.id, " ,总聊天次数:", this.chatTotalCount);
|
|
}
|
|
|
|
/** 获取剩余聊天次数 */
|
|
public getChatRemainCount(): number {
|
|
return this.chatRemainCount;
|
|
}
|
|
|
|
/** 保存剩余聊天次数 */
|
|
public setChatRemainCount(value: number): void {
|
|
this.chatRemainCount = value;
|
|
logger.log("当前技师 ", this.id, " ,剩余聊天次数:", this.chatRemainCount);
|
|
}
|
|
|
|
/** 更新剩余聊天次数 */
|
|
public useChat(times: number = 1) {
|
|
if (this.chatRemainCount > 0) {
|
|
this.setChatRemainCount(Math.max(0, this.chatRemainCount - times));
|
|
}
|
|
}
|
|
|
|
/** 是否可聊天 */
|
|
public isCanChat(): boolean {
|
|
// 剩余聊天次数
|
|
let remainCount = this.chatRemainCount;
|
|
if (remainCount < 0) {
|
|
// 会员
|
|
return true;
|
|
} else if (remainCount === 0) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 解析聊天记录数据,一个
|
|
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(Number(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);
|
|
logger.log("当前技师 ", this.id, " ,聊天记录:", this.chatRecord);
|
|
}
|
|
|
|
/** 获取聊天记录的所有 id */
|
|
public getChatRecordIds(): number[] {
|
|
return this.chatRecordIds;
|
|
}
|
|
|
|
/** 是否已经有过聊天记录 */
|
|
public isAlreadyChat(): boolean {
|
|
const hasIds = this.chatRecordIds && this.chatRecordIds.length > 0;
|
|
const hasRecords = this.chatRecord && this.chatRecord.size > 0;
|
|
return hasIds || hasRecords;
|
|
}
|
|
|
|
/** 获取最新的聊天记录的id */
|
|
public getLatestChatRecordId(): number | null {
|
|
if (this.chatRecordIds.length === 0) return null;
|
|
return this.chatRecordIds[0];
|
|
}
|
|
|
|
/** 获取最新的聊天记录的时间戳(毫秒) */
|
|
public getLatestChatRecordTimestamp(): number | null {
|
|
if (this.chatRecordIds.length === 0) return null;
|
|
// 最新一条的 msgId 本身就是时间戳(毫秒)
|
|
return this.chatRecordIds[0];
|
|
}
|
|
|
|
/** 获取聊天记录,根据 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 : "";
|
|
}
|
|
}
|