57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import type { ApiResponse } from "../client/types";
|
|
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
|
import type { IShopService } from "./IShopService";
|
|
import { PurchaseConfig } from "../../config/PurchaseConfig";
|
|
import { ConfigManager, ConfigId } from "../../manager/ConfigManager";
|
|
|
|
export class MockShopService implements IShopService {
|
|
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 priceUSD(min = 0.99, max = 49.99) {
|
|
const v = Math.random() * (max - min) + min;
|
|
return v.toFixed(2); // 字符串,保留两位小数
|
|
}
|
|
|
|
private ok<T>(data: T): ApiResponse<T> {
|
|
return ({ code: proto.cs.EnmRetCode.SUCCESS, data } as unknown) as ApiResponse<T>;
|
|
}
|
|
|
|
private genGood(id: number, name: string, count: number, price: number): proto.cs.IPurchaseConfig {
|
|
return {
|
|
id,
|
|
name,
|
|
count,
|
|
price,
|
|
};
|
|
}
|
|
|
|
// 获取商品列表
|
|
async reqShopList(_req: proto.cs.ICSGetPurchaseReq): Promise<ApiResponse<proto.cs.ICSGetPurchaseRes>> {
|
|
// 延时
|
|
await this.delay();
|
|
// 配置数据
|
|
const configData = ConfigManager.I.getDataById<PurchaseConfig>(ConfigId.Purchase);
|
|
const allId = configData.getAllId();
|
|
let goodData: proto.cs.IPurchaseConfig[] = [];
|
|
for (let id of allId) {
|
|
let name = configData.getNameById(id);
|
|
let count = configData.getCountById(id);
|
|
let price = configData.getPriceById(id);
|
|
let oneData = this.genGood(id, name, count, price);
|
|
goodData.push(oneData);
|
|
}
|
|
// 返回数据
|
|
const res: proto.cs.ICSGetPurchaseRes = { items: goodData };
|
|
return this.ok(res);
|
|
}
|
|
|
|
// 使用余额购买商品
|
|
async reqBuyGood(_req: proto.cs.ICSBuyGoodReq): Promise<ApiResponse<proto.cs.ICSBuyGoodRes>> {
|
|
// 延时
|
|
await this.delay();
|
|
// 返回数据
|
|
const res: proto.cs.ICSBuyGoodRes = {};
|
|
return this.ok(res);
|
|
}
|
|
}
|