Merge branch 'main' of 47.107.44.202:xionglijia/18xchat
This commit is contained in:
@@ -80,11 +80,23 @@ export class ChatAIService {
|
||||
* 创建或获取指定角色的聊天实例
|
||||
* @param roleId 角色ID
|
||||
*/
|
||||
private createOrGetChat(roleId: number): any {
|
||||
async createOrGetChat(roleId: number): Promise<any> {
|
||||
if (!this.chatInstances.has(roleId)) {
|
||||
const systemInstruction = RoleConfigLoader.getRoleInstruction(roleId);
|
||||
const config = ApiConfig.Instance.getAIConfig();
|
||||
|
||||
// // 获取聊天数据
|
||||
// await this.reqGetChatMsg(roleId, 1, 20);
|
||||
// const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
// const category = girlData.getGrilCategoryById(roleId);
|
||||
// const ids = girlData.getChatRecordIds(category.toString(), roleId);
|
||||
// for (let id of ids) {
|
||||
// let isAi = girlData.getChatRecordIsAi(category.toString(), roleId, id);
|
||||
// let mgs = girlData.getChatRecordMsg(category.toString(), roleId, id);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// 从本地加载历史记录
|
||||
const savedHistory = ChatHistoryManager.Instance.loadHistory(roleId);
|
||||
let chat;
|
||||
@@ -311,10 +323,11 @@ export class ChatAIService {
|
||||
* 上报聊天数据到服务器
|
||||
*/
|
||||
async reqChatMsg(girlId: number, msgList: proto.cs.IChatMsg[]) {
|
||||
// 请求购买商品
|
||||
// 请求数据
|
||||
const reqData = {
|
||||
girlId,
|
||||
ChatMsg: msgList,
|
||||
msgId: new Date().getTime(),
|
||||
};
|
||||
console.log("请求上报聊天数据的请求数据:", reqData);
|
||||
let res = await ChatService.I.reqChatMsg(reqData);
|
||||
@@ -328,6 +341,30 @@ export class ChatAIService {
|
||||
girlData.setChatRemainCount(category.toString(), girlId, resData.chatRemainCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取聊天数据
|
||||
*/
|
||||
async reqGetChatMsg(girlId: number, page: number, limit: number) {
|
||||
// 请求数据
|
||||
const reqData = {
|
||||
GirlId: girlId,
|
||||
page,
|
||||
limit,
|
||||
};
|
||||
console.log("获取聊天数据的请求数据:", reqData);
|
||||
let res = await ChatService.I.reqGetChatMsg(reqData);
|
||||
console.log("获取聊天数据的响应数据:", res);
|
||||
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
|
||||
const resData = res.data;
|
||||
// 保存数据
|
||||
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
const category = girlData.getGrilCategoryById(girlId);
|
||||
girlData.setChatRecord(category.toString(), girlId, resData.msgs);
|
||||
girlData.setChatTotalCount(category.toString(), girlId, resData.chatTotalCount);
|
||||
girlData.setChatRemainCount(category.toString(), girlId, resData.chatRemainCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 请求数据类型定义
|
||||
|
||||
@@ -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 : "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,7 +809,35 @@ export class GirlData extends BaseData {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 保存聊天记录 */
|
||||
public setChatRecord(categoryId: string, girlId: number, data: proto.cs.IChatMsg[]): void {
|
||||
let oneData = this.getGrilChat(categoryId, girlId);
|
||||
if (!oneData) return;
|
||||
oneData.setChatRecord(data);
|
||||
}
|
||||
|
||||
/** 获取聊天记录的所有 id */
|
||||
public getChatRecordIds(categoryId: string, girlId: number): number[] {
|
||||
let oneData = this.getGrilChat(categoryId, girlId);
|
||||
if (!oneData) return [];
|
||||
return oneData.getChatRecordIds();
|
||||
}
|
||||
|
||||
/** 获取聊天记录,根据 id,是否是 ai 聊天数据 */
|
||||
public getChatRecordIsAi(categoryId: string, girlId: number, id: number): boolean {
|
||||
let oneData = this.getGrilChat(categoryId, girlId);
|
||||
if (!oneData) return false;
|
||||
return oneData.getChatRecordIsAi(id);
|
||||
}
|
||||
|
||||
/** 获取聊天记录,根据 id,聊天内容 */
|
||||
public getChatRecordMsg(categoryId: string, girlId: number, id: number): string {
|
||||
let oneData = this.getGrilChat(categoryId, girlId);
|
||||
if (!oneData) return "";
|
||||
return oneData.getChatRecordMsg(id);
|
||||
}
|
||||
|
||||
/** --------------------------------------------------------- 技师的好感度数据 --------------------------------------------------------- */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user