92 lines
3.3 KiB
TypeScript
92 lines
3.3 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";
|
|
import { DataManager, DataId } from "../../data/DataManager";
|
|
import { GirlData } from "../../data/GirlData";
|
|
import { WalletData } from "../../data/WalletData";
|
|
import { ShopData } from "../../data/ShopData";
|
|
|
|
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 reqGirlId = req.girlId;
|
|
const goodId = req.goodId;
|
|
// 数据层的数据
|
|
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
|
const walletData = DataManager.I.getDataById<WalletData>(DataId.Wallet);
|
|
const shopData = DataManager.I.getDataById<ShopData>(DataId.Shop);
|
|
const category = girlData.getGrilCategoryById(reqGirlId);
|
|
|
|
let chatRemainCount = 0;
|
|
if (shopData.isRechargeId(goodId)) {
|
|
// 充值
|
|
} else if (shopData.isMemberId(goodId)) {
|
|
// 会员
|
|
chatRemainCount = -1;
|
|
} else if (shopData.isChatId(goodId)) {
|
|
// 聊天
|
|
const curCount = girlData.getChatRemainCount(category.toString(), reqGirlId);
|
|
chatRemainCount = curCount + shopData.getCountById(goodId);
|
|
}
|
|
// 余额
|
|
const price = shopData.getPriceById(goodId);
|
|
let balanceNumber = Number(walletData.balance) - Number(price);
|
|
let balance = balanceNumber.toString();
|
|
// vip 30天后过期
|
|
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
let vipExpire = nowSeconds + 30 * 24 * 3600;
|
|
// 返回数据
|
|
const res: proto.cs.ICSBuyGoodRes = {
|
|
balance,
|
|
vipExpire,
|
|
chatRemainCount,
|
|
};
|
|
return this.ok(res);
|
|
}
|
|
}
|