Merge branch 'main' of 47.107.44.202:xionglijia/18xchat

This commit is contained in:
2025-09-10 19:37:17 +08:00
7 changed files with 196 additions and 5 deletions
+39 -2
View File
@@ -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);
}
}
}
// 请求数据类型定义
+84 -1
View File
@@ -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 : "";
}
}
+28
View File
@@ -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);
}
/** --------------------------------------------------------- 技师的好感度数据 --------------------------------------------------------- */
+6
View File
@@ -2080,6 +2080,9 @@ static getTypeUrl(typeUrlPrefix?: string): string;
/** ChatMsg isAi */
isAi?: (boolean|null);
/** ChatMsg msgId */
msgId?: (number|Long|null);
}
/** Represents a ChatMsg. */
@@ -2097,6 +2100,9 @@ msg: string;
/** ChatMsg isAi. */
isAi: boolean;
/** ChatMsg msgId. */
msgId: (number|Long);
/**
* Creates a new ChatMsg instance using the specified properties.
* @param [properties] Properties to set
+37
View File
@@ -4818,6 +4818,7 @@ $root.cs = (function() {
* @interface IChatMsg
* @property {string|null} [msg] ChatMsg msg
* @property {boolean|null} [isAi] ChatMsg isAi
* @property {number|Long|null} [msgId] ChatMsg msgId
*/
/**
@@ -4851,6 +4852,14 @@ $root.cs = (function() {
*/
ChatMsg.prototype.isAi = false;
/**
* ChatMsg msgId.
* @member {number|Long} msgId
* @memberof cs.ChatMsg
* @instance
*/
ChatMsg.prototype.msgId = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
/**
* Creates a new ChatMsg instance using the specified properties.
* @function create
@@ -4879,6 +4888,8 @@ $root.cs = (function() {
writer.uint32(/* id 1, wireType 2 =*/10).string(message.msg);
if (message.isAi != null && Object.hasOwnProperty.call(message, "isAi"))
writer.uint32(/* id 2, wireType 0 =*/16).bool(message.isAi);
if (message.msgId != null && Object.hasOwnProperty.call(message, "msgId"))
writer.uint32(/* id 3, wireType 0 =*/24).int64(message.msgId);
return writer;
};
@@ -4923,6 +4934,10 @@ $root.cs = (function() {
message.isAi = reader.bool();
break;
}
case 3: {
message.msgId = reader.int64();
break;
}
default:
reader.skipType(tag & 7);
break;
@@ -4964,6 +4979,9 @@ $root.cs = (function() {
if (message.isAi != null && message.hasOwnProperty("isAi"))
if (typeof message.isAi !== "boolean")
return "isAi: boolean expected";
if (message.msgId != null && message.hasOwnProperty("msgId"))
if (!$util.isInteger(message.msgId) && !(message.msgId && $util.isInteger(message.msgId.low) && $util.isInteger(message.msgId.high)))
return "msgId: integer|Long expected";
return null;
};
@@ -4983,6 +5001,15 @@ $root.cs = (function() {
message.msg = String(object.msg);
if (object.isAi != null)
message.isAi = Boolean(object.isAi);
if (object.msgId != null)
if ($util.Long)
(message.msgId = $util.Long.fromValue(object.msgId)).unsigned = false;
else if (typeof object.msgId === "string")
message.msgId = parseInt(object.msgId, 10);
else if (typeof object.msgId === "number")
message.msgId = object.msgId;
else if (typeof object.msgId === "object")
message.msgId = new $util.LongBits(object.msgId.low >>> 0, object.msgId.high >>> 0).toNumber();
return message;
};
@@ -5002,11 +5029,21 @@ $root.cs = (function() {
if (options.defaults) {
object.msg = "";
object.isAi = false;
if ($util.Long) {
var long = new $util.Long(0, 0, false);
object.msgId = options.longs === String ? long.toString() : options.longs === Number ? long.toNumber() : long;
} else
object.msgId = options.longs === String ? "0" : 0;
}
if (message.msg != null && message.hasOwnProperty("msg"))
object.msg = message.msg;
if (message.isAi != null && message.hasOwnProperty("isAi"))
object.isAi = message.isAi;
if (message.msgId != null && message.hasOwnProperty("msgId"))
if (typeof message.msgId === "number")
object.msgId = options.longs === String ? String(message.msgId) : message.msgId;
else
object.msgId = options.longs === String ? $util.Long.prototype.toString.call(message.msgId) : options.longs === Number ? new $util.LongBits(message.msgId.low >>> 0, message.msgId.high >>> 0).toNumber() : message.msgId;
return object;
};