92 lines
3.4 KiB
TypeScript
92 lines
3.4 KiB
TypeScript
import type { ApiResponse } from "../client/types";
|
|
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
|
import type { IChatService } from "./IChatService";
|
|
import { DataManager, DataId } from "../../data/DataManager";
|
|
import { GirlData } from "../../data/GirlData";
|
|
|
|
export class MockChatService implements IChatService {
|
|
private delay(ms = 160) { return new Promise<void>(r => setTimeout(r, ms)); }
|
|
|
|
private ok<T>(data: T): ApiResponse<T> {
|
|
return ({ code: proto.cs.EnmRetCode.SUCCESS, data } as unknown) as ApiResponse<T>;
|
|
}
|
|
|
|
// 上报聊天数据
|
|
async reqChatMsg(req: proto.cs.ICSChatMsgReq): Promise<ApiResponse<proto.cs.ICSChatMsgRes>> {
|
|
// 延时
|
|
await this.delay();
|
|
// 请求数据
|
|
const reqGirlId = req.girlId;
|
|
// 数据层的数据
|
|
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
|
const reqGirlCategory = girlData.getGrilCategoryById(reqGirlId);
|
|
const chatTotalCount = girlData.getChatTotalCount(reqGirlCategory.toString(), reqGirlId);
|
|
const chatRemainCount = girlData.getChatRemainCount(reqGirlCategory.toString(), reqGirlId);
|
|
// 计算
|
|
let resultTotalCount = chatTotalCount + 1;
|
|
let resultRemainCount = chatRemainCount;
|
|
if (chatRemainCount > 0) {
|
|
resultRemainCount = chatRemainCount - 1;
|
|
}
|
|
// 返回数据
|
|
const res: proto.cs.ICSChatMsgRes = {
|
|
chatRemainCount: resultRemainCount,
|
|
chatTotalCount: resultTotalCount,
|
|
};
|
|
return this.ok(res);
|
|
}
|
|
|
|
// 获取聊天数据
|
|
async reqGetChatMsg(req: proto.cs.ICSGetChatMsgReq): Promise<ApiResponse<proto.cs.ICSGetChatMsgRes>> {
|
|
// 延时
|
|
await this.delay();
|
|
// 请求数据
|
|
const reqGirlId = req.GirlId;
|
|
const page = req.page;
|
|
const limit = req.limit;
|
|
// 数据层的数据
|
|
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
|
const reqGirlCategory = girlData.getGrilCategoryById(reqGirlId);
|
|
const chatTotalCount = girlData.getChatTotalCount(reqGirlCategory.toString(), reqGirlId);
|
|
const chatRemainCount = girlData.getChatRemainCount(reqGirlCategory.toString(), reqGirlId);
|
|
// 模拟一个完整的聊天记录池
|
|
const allMsgs: proto.cs.IChatMsg[] = [];
|
|
for (let i = 0; i < 50; i++) {
|
|
allMsgs.push({
|
|
msg: `模拟消息 ${i + 1} 来自技师 ${reqGirlId}`,
|
|
isAi: i % 2 === 0, // 偶数条当成 AI 说的
|
|
});
|
|
}
|
|
// 分页计算
|
|
const start = (page - 1) * limit;
|
|
const end = start + limit;
|
|
const pageMsgs = allMsgs.slice(start, end);
|
|
// 返回数据
|
|
const res: proto.cs.ICSGetChatMsgRes = {
|
|
msgs: pageMsgs,
|
|
chatRemainCount,
|
|
chatTotalCount,
|
|
};
|
|
return this.ok(res);
|
|
}
|
|
|
|
// 获取技师聊天次数
|
|
async reqChatCountData(req: proto.cs.ICSGetChatRemainCountReq): Promise<ApiResponse<proto.cs.ICSGetChatRemainCountRes>> {
|
|
// 延时
|
|
await this.delay();
|
|
// 请求数据
|
|
const reqGirlId = req.id;
|
|
// 数据层的数据
|
|
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
|
const reqGirlCategory = girlData.getGrilCategoryById(reqGirlId);
|
|
const chatTotalCount = girlData.getChatTotalCount(reqGirlCategory.toString(), reqGirlId);
|
|
const chatRemainCount = girlData.getChatRemainCount(reqGirlCategory.toString(), reqGirlId);
|
|
// 返回数据
|
|
const res: proto.cs.ICSGetChatRemainCountRes = {
|
|
chatRemainCount,
|
|
chatTotalCount,
|
|
};
|
|
return this.ok(res);
|
|
}
|
|
}
|