mirror of
https://github.com/motajs/template.git
synced 2026-11-02 06:34:31 +08:00
feat: 商店系统实现
This commit is contained in:
1 parent
acf20f548d
commit
a15b79b29f
7 files changed
+362
-13
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import {
|
||||
IShopManager,
|
||||
IHeroState,
|
||||
IShopState,
|
||||
IStateBase
|
||||
} from '@user/data-base';
|
||||
import { IShopHeroAttr, IShopSystem, ShopTradeResult } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { shouldReplay } from '@user/data-common';
|
||||
|
||||
export class ShopSystem implements IShopSystem {
|
||||
manager: IShopManager | null = null;
|
||||
hero: IHeroState<IShopHeroAttr> | null = null;
|
||||
|
||||
private current: IShopState | null = null;
|
||||
|
||||
constructor(readonly state: IStateBase) {}
|
||||
|
||||
bindManager(manager: IShopManager | null): void {
|
||||
this.closeShop();
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
bindHero(hero: IHeroState<IShopHeroAttr> | null): void {
|
||||
this.closeShop();
|
||||
this.hero = hero;
|
||||
}
|
||||
|
||||
@shouldReplay('Openning shop should be replayed.')
|
||||
openShop(shop: IShopState): void {
|
||||
this.current = shop;
|
||||
}
|
||||
|
||||
getCurrentShop(): IShopState | null {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
@shouldReplay('Closing shop should be replayed.')
|
||||
closeShop(): void {
|
||||
this.current = null;
|
||||
}
|
||||
|
||||
@shouldReplay('Buying items should be replayed.')
|
||||
buyItem(item: number | string, count: number): ShopTradeResult {
|
||||
if (!this.current || !this.hero) {
|
||||
logger.warn(199);
|
||||
return ShopTradeResult.Invalid;
|
||||
}
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) {
|
||||
logger.warn(200, item.toString());
|
||||
return ShopTradeResult.Invalid;
|
||||
}
|
||||
|
||||
const shop = this.current;
|
||||
const hero = this.hero.getModifiableAttribute();
|
||||
|
||||
const currCount = shop.getCount(count);
|
||||
if (currCount < count) return ShopTradeResult.ShopLack;
|
||||
|
||||
const [buyPrice] = shop.getPrice(num);
|
||||
if (buyPrice < 0) {
|
||||
return ShopTradeResult.Invalid;
|
||||
}
|
||||
const need = buyPrice * count;
|
||||
const owned = hero.getBaseAttribute('money');
|
||||
if (owned < need) {
|
||||
return ShopTradeResult.MoneyLack;
|
||||
}
|
||||
|
||||
hero.add('money', -need);
|
||||
shop.addItem(num, -count);
|
||||
return ShopTradeResult.Success;
|
||||
}
|
||||
|
||||
sellItem(item: number | string, count: number): ShopTradeResult {
|
||||
if (!this.current || !this.hero) {
|
||||
logger.warn(199);
|
||||
return ShopTradeResult.Invalid;
|
||||
}
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) {
|
||||
logger.warn(200, item.toString());
|
||||
return ShopTradeResult.Invalid;
|
||||
}
|
||||
|
||||
const shop = this.current;
|
||||
const hero = this.hero.getModifiableAttribute();
|
||||
const pack = this.hero.items;
|
||||
|
||||
const currCount = pack.itemCount(num);
|
||||
if (currCount < count) return ShopTradeResult.HeroLack;
|
||||
|
||||
const [, sellPrice] = shop.getPrice(num);
|
||||
if (sellPrice < 0) {
|
||||
return ShopTradeResult.Invalid;
|
||||
}
|
||||
const need = sellPrice * count;
|
||||
hero.add('money', need);
|
||||
shop.addItem(num, count);
|
||||
return ShopTradeResult.Success;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,39 @@
|
||||
import { IHeroState, IShopManager, IShopState } from '@user/data-base';
|
||||
import {
|
||||
IDataBaseExtended,
|
||||
IHeroState,
|
||||
IShopManager,
|
||||
IShopState
|
||||
} from '@user/data-base';
|
||||
|
||||
export interface IShopHeroAttr {
|
||||
/** 勇士拥有的金币 */
|
||||
money: number;
|
||||
}
|
||||
|
||||
export interface IShopSystem {
|
||||
export const enum ShopTradeResult {
|
||||
/** 成功购入或售出 */
|
||||
Success,
|
||||
/** 操作不合法,一般是因为道具不存在或未打开商店 */
|
||||
Invalid,
|
||||
/** 商店库存不足,无法购入这么多道具 */
|
||||
ShopLack,
|
||||
/** 玩家库存不足,无法卖出这么多道具 */
|
||||
HeroLack,
|
||||
/** 玩家货币不足,无法购入这么多道具 */
|
||||
MoneyLack
|
||||
}
|
||||
|
||||
export interface IShopSystem extends IDataBaseExtended {
|
||||
/** 当前使用的商店管理器 */
|
||||
readonly manager: IShopManager | null;
|
||||
/** 当前绑定的勇士状态对象 */
|
||||
readonly hero: IHeroState<IShopHeroAttr> | null;
|
||||
|
||||
/**
|
||||
* 绑定商店管理器
|
||||
* @param manager 商店管理器
|
||||
*/
|
||||
bindManager(manager: IShopManager): void;
|
||||
bindManager(manager: IShopManager | null): void;
|
||||
|
||||
/**
|
||||
* 绑定系统使用的勇士状态对象
|
||||
@@ -24,6 +47,11 @@ export interface IShopSystem {
|
||||
*/
|
||||
openShop(shop: IShopState): void;
|
||||
|
||||
/**
|
||||
* 获取当前已打开的商店状态对象
|
||||
*/
|
||||
getCurrentShop(): IShopState | null;
|
||||
|
||||
/**
|
||||
* 关闭当前打开的商店
|
||||
*/
|
||||
@@ -34,12 +62,12 @@ export interface IShopSystem {
|
||||
* @param item 道具数字或 id
|
||||
* @param count 购买的道具数量
|
||||
*/
|
||||
buyItem(item: number | string, count: number): void;
|
||||
buyItem(item: number | string, count: number): ShopTradeResult;
|
||||
|
||||
/**
|
||||
* 在当前打开的商店里面售出指定道具
|
||||
* @param item 道具数字或 id
|
||||
* @param count 卖出的道具数量
|
||||
*/
|
||||
sellItem(item: number | string, count: number): void;
|
||||
sellItem(item: number | string, count: number): ShopTradeResult;
|
||||
}
|
||||
Reference in new issue
Block a user