78 lines
3.1 KiB
TypeScript
78 lines
3.1 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 base = (req?.category ?? 0) * 1000 + (req?.page ?? 1) * 100;
|
|
const limit = Math.max(1, Math.min(req?.limit ?? 10, 50));
|
|
const girls = Array.from({ length: limit }, (_, i) => this.genBrief(base + i, "", 1, "", 2, 3, "", 4));
|
|
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) });
|
|
}
|
|
}
|