mirror of
https://github.com/motajs/template.git
synced 2026-11-19 02:34:30 +08:00
refactor: 插件系统 & 数据端顶层移植
This commit is contained in:
1 parent
b0229c627f
commit
2f3d3387a0
74 files changed
+1322
-3321
No files matched your search
@@ -89,7 +89,7 @@ beforeAll(async () => {
|
||||
vi.stubGlobal('location', { origin: 'http://localhost' });
|
||||
const executorModule = await import('../executor');
|
||||
const systemModule = await import('../system');
|
||||
const stateModule = await import('../../../../data-state/src/hero');
|
||||
const stateModule = await import('../../../../../src/data/hero');
|
||||
const baseModule = await import('@user/data-base');
|
||||
const commonModule = await import('@user/data-common');
|
||||
modules = {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from './combat';
|
||||
export * from './event';
|
||||
export * from './path';
|
||||
export * from './shop';
|
||||
|
||||
export * from './types';
|
||||
@@ -1 +0,0 @@
|
||||
export * from './types';
|
||||
@@ -1,104 +0,0 @@
|
||||
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,73 +0,0 @@
|
||||
import {
|
||||
IDataBaseExtended,
|
||||
IHeroState,
|
||||
IShopManager,
|
||||
IShopState
|
||||
} from '@user/data-base';
|
||||
|
||||
export interface IShopHeroAttr {
|
||||
/** 勇士拥有的金币 */
|
||||
money: number;
|
||||
}
|
||||
|
||||
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 | null): void;
|
||||
|
||||
/**
|
||||
* 绑定系统使用的勇士状态对象
|
||||
* @param hero 勇士状态对象
|
||||
*/
|
||||
bindHero(hero: IHeroState<IShopHeroAttr> | null): void;
|
||||
|
||||
/**
|
||||
* 打开指定商店,仅数据端操作,不会打开商店 UI
|
||||
* @param shop 商店状态对象
|
||||
*/
|
||||
openShop(shop: IShopState): void;
|
||||
|
||||
/**
|
||||
* 获取当前已打开的商店状态对象
|
||||
*/
|
||||
getCurrentShop(): IShopState | null;
|
||||
|
||||
/**
|
||||
* 关闭当前打开的商店
|
||||
*/
|
||||
closeShop(): void;
|
||||
|
||||
/**
|
||||
* 在当前打开的商店里面购买指定道具
|
||||
* @param item 道具数字或 id
|
||||
* @param count 购买的道具数量
|
||||
*/
|
||||
buyItem(item: number | string, count: number): ShopTradeResult;
|
||||
|
||||
/**
|
||||
* 在当前打开的商店里面售出指定道具
|
||||
* @param item 道具数字或 id
|
||||
* @param count 卖出的道具数量
|
||||
*/
|
||||
sellItem(item: number | string, count: number): ShopTradeResult;
|
||||
}
|
||||
Reference in new issue
Block a user