协议修改

This commit is contained in:
chen wei bo
2025-09-08 19:44:58 +08:00
parent e7cb527ca0
commit d4d3e986b3
17 changed files with 878 additions and 66 deletions
@@ -1,6 +1,8 @@
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)); }
@@ -11,29 +13,44 @@ export class MockShopService implements IShopService {
}
private ok<T>(data: T): ApiResponse<T> {
return ({ ok: true, data } as unknown) as ApiResponse<T>;
return ({ code: proto.cs.EnmRetCode.SUCCESS, data } as unknown) as ApiResponse<T>;
}
private genGood(id: number, vip = false): proto.cs.IGood {
private genGood(id: number, name: string, count: number, price: number): proto.cs.IPurchaseConfig {
return {
id,
name: vip ? `VIP-${id}` : `Diamond Pack ${id}`,
desc: vip ? `VIP access for ${this.randInt(24, 168)} hours` : `Diamonds x${this.randInt(60, 2000)}`,
price: this.priceUSD(vip ? 4.99 : 0.99, vip ? 49.99 : 29.99),
count: vip ? this.randInt(24, 168) : this.randInt(60, 2000), // VIP=小时,普通=数量
name,
count,
price,
};
}
// 获取商品列表
async reqShopList(_req: proto.cs.ICSGetShopReq): Promise<ApiResponse<proto.cs.ICSGetShopRes>> {
async reqShopList(_req: proto.cs.ICSGetPurchaseReq): Promise<ApiResponse<proto.cs.ICSGetPurchaseRes>> {
// 延时
await this.delay();
// 普通商品(<=2000+ VIP 商品(>2000
const normals: proto.cs.IGood[] = Array.from({ length: 5 }, (_, i) => this.genGood(1000 + i, false));
const vips: proto.cs.IGood[] = Array.from({ length: 3 }, (_, i) => this.genGood(2001 + i, true));
// 注意:你的 proto 定义中字段名为 "Goods"(大写 G
const res: proto.cs.ICSGetShopRes = { Goods: [...normals, ...vips] };
// 配置数据
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);
}
}