Files
18xchat/assets/Scripts/chat18x/data/ShopData.ts
T

184 lines
5.0 KiB
TypeScript
Raw Normal View History

2025-08-26 11:10:15 +08:00
/**
2025-09-08 19:44:45 +08:00
* 商城数据
2025-08-26 11:10:15 +08:00
*/
import { BaseData } from "./BaseData";
import proto from 'db://assets/Scripts/proto/proto.pb.js';
2025-09-21 20:25:36 +08:00
import { logger } from "db://assets/Scripts/Main/Common/Logger";
2025-08-26 11:10:15 +08:00
export class ShopData extends BaseData {
// 商品数据
2025-09-08 19:44:45 +08:00
private _goods = new Map<number, proto.cs.IPurchaseConfig>();
2025-08-26 11:10:15 +08:00
// 商品 id
private _goodIds: number[] = [];
public reset(): void {
this._goods.clear();
this._goodIds = [];
}
public clear(): void {
this._goods.clear();
this._goodIds = [];
}
public destroy(): void {
super.destroy();
this._goods = null;
this._goodIds = null;
}
/** 设置商品数据 */
2025-09-08 19:44:45 +08:00
public set goods(res: proto.cs.ICSGetPurchaseRes) {
const list = res.items ?? [];
2025-08-26 11:10:15 +08:00
this._goods.clear();
this._goodIds = [];
for (const g of list) {
2025-09-08 19:44:45 +08:00
const vo: proto.cs.IPurchaseConfig = {
2025-08-26 11:10:15 +08:00
id: g.id, // 商品 id
2025-09-08 19:44:45 +08:00
name: g.name, // 商品名字
count: g.count, // 商品数量
2025-09-22 11:57:21 +08:00
price: g.price, // 商品价格,卢币
usd: g.usd, // 商品价格,usd
2025-08-26 11:10:15 +08:00
};
this._goods.set(vo.id, vo);
this._goodIds.push(vo.id);
}
2025-09-21 20:25:36 +08:00
logger.log("设置商品数据:", this._goods);
2025-08-26 11:10:15 +08:00
}
/** 获取所有商品数据 */
2025-09-08 19:44:45 +08:00
public get goods(): Map<number, proto.cs.IPurchaseConfig> { return this._goods; }
2025-08-26 11:10:15 +08:00
/** 获取所有商品数据的数量 */
public get goodsCount(): number { return this._goods ? this._goods.size : 0; }
/** 获取所有商品 id */
public get goodIds(): number[] { return this._goodIds; }
2025-09-09 18:16:13 +08:00
/** 是否是充值商品,首位数字为 1 */
public isRechargeId(id: number): boolean {
const firstDigit = id.toString()[0]; // 取首位字符
return firstDigit === "1";
}
2025-09-08 19:44:45 +08:00
/** 获取 充值商品的id数组,首位数字为 1 */
2025-09-09 18:16:13 +08:00
public getRechargeIds(): number[] {
2025-09-08 19:44:45 +08:00
let allId = this._goodIds;
if (!allId) return [];
// 获取所有 id
let resultId = [];
for (let one of allId) {
2025-09-09 18:16:13 +08:00
if (this.isRechargeId(one)) {
2025-09-08 19:44:45 +08:00
resultId.push(one);
}
}
return resultId;
2025-08-26 11:10:15 +08:00
}
2025-09-09 18:16:13 +08:00
/** 是否是会员商品,首位数字为 2 */
public isMemberId(id: number): boolean {
const firstDigit = id.toString()[0]; // 取首位字符
return firstDigit === "2";
}
2025-09-08 19:44:45 +08:00
/** 获取 会员商品的id数组,首位数字为 2 */
2025-09-09 18:16:13 +08:00
public getMemberIds(): number[] {
2025-09-08 19:44:45 +08:00
let allId = this._goodIds;
if (!allId) return [];
// 获取所有 id
let resultId = [];
for (let one of allId) {
2025-09-09 18:16:13 +08:00
if (this.isMemberId(one)) {
2025-09-08 19:44:45 +08:00
resultId.push(one);
}
}
return resultId;
2025-08-26 11:10:15 +08:00
}
2025-09-10 15:26:56 +08:00
/** 获取7天会员的 id */
public get7DayMemberId(): number {
const ids = this.getMemberIds();
const length = ids.length;
if (length <= 0) return 0;
return ids[0];
}
/** 获取30天会员的 id */
public get30DayMemberId(): number {
const ids = this.getMemberIds();
const length = ids.length;
if (length <= 1) return 0;
return ids[1];
}
2025-09-09 18:16:13 +08:00
/** 是否是聊天商品,首位数字为 3 */
public isChatId(id: number): boolean {
const firstDigit = id.toString()[0]; // 取首位字符
return firstDigit === "3";
}
2025-09-08 19:44:45 +08:00
/** 获取 聊天商品的id数组,首位数字为 3 */
2025-09-09 18:16:13 +08:00
public getChatIds(): number[] {
2025-09-08 19:44:45 +08:00
let allId = this._goodIds;
if (!allId) return [];
// 获取所有 id
let resultId = [];
for (let one of allId) {
2025-09-09 18:16:13 +08:00
if (this.isChatId(one)) {
2025-09-08 19:44:45 +08:00
resultId.push(one);
}
}
return resultId;
}
2025-08-26 11:10:15 +08:00
/** 根据商品 id 获取商品数据 */
2025-09-08 19:44:45 +08:00
private getGoodDataById(id: number): proto.cs.IPurchaseConfig | null {
2025-08-26 11:10:15 +08:00
if (!this._goods) return null;
return this._goods.get(id) ?? null;
}
/** 根据商品 id 获取商品名字 */
public getNameById(id: number): string | null {
let oneData = this.getGoodDataById(id);
if (!oneData) return null;
return oneData.name;
}
2025-09-22 11:57:21 +08:00
/** 根据商品 id 获取商品的原来价格,除100,卢币 */
2025-09-09 19:16:46 +08:00
public getOriginalPriceById(id: number): number {
2025-08-26 11:10:15 +08:00
let oneData = this.getGoodDataById(id);
2025-09-09 19:16:46 +08:00
if (!oneData) return 0;
let price = oneData.price;
return price/100;
}
2025-09-22 11:57:21 +08:00
/** 根据商品 id 获取商品价格,卢币 */
2025-09-09 19:16:46 +08:00
public getPriceById(id: number): number {
let oneData = this.getGoodDataById(id);
if (!oneData) return 0;
2025-08-26 11:10:15 +08:00
return oneData.price;
}
2025-09-22 11:57:21 +08:00
/** 根据商品 id 获取商品的原来价格,除100,usd */
public getOriginalUsdPriceById(id: number): number {
let oneData = this.getGoodDataById(id);
if (!oneData) return 0;
let price = oneData.usd;
return price/100;
}
/** 根据商品 id 获取商品价格,usd */
public getUsdPriceById(id: number): number {
let oneData = this.getGoodDataById(id);
if (!oneData) return 0;
return oneData.usd;
}
2025-08-26 11:10:15 +08:00
/** 根据商品 id 获取商品数量 */
public getCountById(id: number): number | null {
let oneData = this.getGoodDataById(id);
if (!oneData) return null;
return oneData.count;
}
}