# 商城系统

# 一、介绍

该模块提供了配置表中的所有商品,并提供购买商品的接口

# 二、使用

// 先在头部引入商城模块
import { shopSystem } from "@timi/mogs-sdk";
1
2

1. 获取全部商品列表

const goods = await shopSystem.goods;
for(const item of goods) {
    console.log('商品名称:',item.name)
}
1
2
3
4

2. 商品关键属性使用

const item = (await shopSystem.goods)[0];
console.log('购买该商品的币种为:', item.currency.name)
console.log('商品关联的奖励池:', item.rewardPool);
console.log('商品的原价:', item.price, '折扣后的价格:', item.discountPrice);
1
2
3
4

1、币种直接关联物品表,理论上可配置为任何物品,但目前只有金币、钻石、体力这三种物品是在属性系统中的

2、商品本身不直接关联物品表,而是关联奖励池,奖励池具体内容请参考奖励池

3、价格、折扣后价格所对应的数值,是以币种作为单位的,即currency字段

3. 购买指定商品

await shopSystem.buy({ id: item.id });
1

4. 监听商品价格变动

只有配置为浮动价格的商品,购买后才会触发其价格的变动,商品是否为浮动价格,可查看isDynamicPrice字段

shopSystem.onGoodsPriceChange((e) => {
    console.log('之前价格:', e.previousValue.price);
    console.log('当前价格:', e.currentValue.price);
})
1
2
3
4

5. 监听商品剩余可购买次数变化

如果商品配置了限购ID,意味着每次购买之后,剩余可购买次数都会减一,直至为0不可购买。

购买上限对应buyLimitation字段中的limitCount

商品剩余购买次数对应buyLimitation字段中的restCount

游戏侧可通过监听对应事件来实时获取剩余可购买次数

shopSystem.onRestBuyCountChange((e) => {
    console.log('触发此次事件的商品id为:', e.id);
    console.log('之前剩余可购买次数:', e.previousValue);
    console.log('当前剩余可购买次数:', e.currentValue);
    if (e.currentValue === 0) console.log('该商品无法继续购买了!');
})
1
2
3
4
5
6