Files
18xchat/assets/Scripts/chat18x/network/services/MockGirlService.ts
T

114 lines
4.3 KiB
TypeScript

import type { ApiResponse } from "../client/types";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import type { IGirlService } from "./IGirlService";
import { ConfigManager, ConfigId } from "../../manager/ConfigManager";
import { GirlConfig } from "../../config/GirlConfig";
export class MockGirlService implements IGirlService {
private delay(ms = 180) { return new Promise<void>(r => setTimeout(r, ms)); }
private randInt(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min; }
private pick<T>(arr: T[]) { return arr[this.randInt(0, arr.length - 1)]; }
private ok<T>(data: T): ApiResponse<T> {
return ({ code: proto.cs.EnmRetCode.SUCCESS, data } as unknown) as ApiResponse<T>;
}
private err<T = unknown>(message = "mock error"): ApiResponse<T> {
return ({ code: 100, error: { message } } as unknown) as ApiResponse<T>;
}
private genBrief(id: number, name: string, age: number, tagKey: string, priceType: number, price: number, avatar: string, star: number): proto.cs.IGirlBrief {
return {
id,
name,
age,
tagKey,
priceType,
price,
avatar,
star,
};
}
private genPhoto(idx: number): proto.cs.IGirlPhoto {
return { pic: `https://dummy.local/photo/${idx}.jpg`, isRelease: Math.random() < 0.5, price: 20 + (idx % 5) * 5 };
}
private genDetail(id: number): proto.cs.IGirlDetail {
const photos = Array.from({ length: 4 }, (_, i) => this.genPhoto(id * 10 + i));
return { brief: this.genBrief(id, "", 1, "", 2, 3, "", 4), desc: `Mock detail for ${id}`, photos, isRelease: Math.random() < 0.4, chatCount: this.randInt(0, 5) };
}
// 每日推荐
async reqDailyRecommend(_req: proto.cs.ICSDailyRecommendReq): Promise<ApiResponse<proto.cs.ICSDailyRecommendRes>> {
// 延时
await this.delay(180);
const configData = ConfigManager.I.getDataById<GirlConfig>(ConfigId.Girl);
const allId = configData.getAllId();
const recId = allId[0];
let name = configData.getNameById(recId);
let age = Number(configData.getAgeById(recId));
let tagKey = configData.getTagKeyById(recId);
let priceType = configData.getPriceTypeById(recId);
let price = 9.9;
let avatar = configData.getAvatarPathById(recId);
let star = 3;
let oneData = this.genBrief(recId, name, age, tagKey, priceType, price, avatar, star);
// 返回数据
const girls = [oneData];
return this.ok({ girls });
}
// 获取技师列表
async reqGetGirlList(req: proto.cs.ICSGetGirlListReq): Promise<ApiResponse<proto.cs.ICSGetGirlListRes>> {
// 延时
await this.delay(200);
const configData = ConfigManager.I.getDataById<GirlConfig>(ConfigId.Girl);
const allId = configData.getAllId();
// 按 category 精确匹配
const category = Number(req?.category ?? -1);
const filteredIds = category >= 0
? allId.filter((id: number) => {
const cat = Number(configData.getCategoryById(id) ?? -1);
return cat === category;
})
: allId;
// 分页
const page = Math.max(1, Number(req?.page ?? 1));
const limit = Math.max(1, Math.min(Number(req?.limit ?? 10), 50));
const start = (page - 1) * limit;
const pageIds = filteredIds.slice(start, start + limit);
// 映射为 IGirlBrief
const girls: proto.cs.IGirlBrief[] = pageIds.map((id: number) => {
const name = configData.getNameById(id);
const age = Number(configData.getAgeById(id));
const tagKey = configData.getTagKeyById(id);
const priceType = Number(configData.getPriceTypeById(id));
const avatar = configData.getAvatarPathById(id);
// 如果有价格配置,就用配置里的,否则给个兜底值
const price = typeof (configData as any).getPriceById === "function"
? Number((configData as any).getPriceById(id) ?? 9.9)
: 9.9;
// 星级兜底:1~5
const star = (id % 5) + 1;
return this.genBrief(id, name, age, tagKey, priceType, price, avatar, star);
});
return this.ok({ girls });
}
// 获取技师详细信息
async reqGetGirlDetail(req: proto.cs.ICSGetGirlDetailReq): Promise<ApiResponse<proto.cs.ICSGetGirlDetailRes>> {
await this.delay(160);
const id = req?.id ?? this.randInt(1, 9999);
return this.ok({ detail: this.genDetail(id) });
}
}