mirror of
https://github.com/motajs/template.git
synced 2026-11-22 04:54:30 +08:00
refactor: 插件系统 & 数据端顶层移植
This commit is contained in:
1 parent
b0229c627f
commit
2f3d3387a0
74 files changed
+1322
-3321
No files matched your search
@@ -0,0 +1 @@
|
||||
export * from './list';
|
||||
@@ -0,0 +1,5 @@
|
||||
import { IDataPluginDefine } from '@user/data-state';
|
||||
import { ShopPlugin } from './shop';
|
||||
|
||||
/** 数据端插件列表 */
|
||||
export const dataPlugins: IDataPluginDefine[] = [new ShopPlugin()];
|
||||
@@ -0,0 +1 @@
|
||||
export * from './shop';
|
||||
@@ -0,0 +1,103 @@
|
||||
import { SaveCompression } from '@user/data-common';
|
||||
import { IShopManager, IShopManagerSave, IShopState } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
import { ShopState } from './state';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { ICoreState } from '@user/data-state';
|
||||
|
||||
export class ShopManager implements IShopManager {
|
||||
/** 当前管理器包含的商店列表 */
|
||||
private readonly shops: Map<string, IShopState> = new Map();
|
||||
|
||||
constructor(readonly state: ICoreState) {}
|
||||
|
||||
/**
|
||||
* 传入商店 id 或实例,输出商店实例
|
||||
* @param shop 商店的 id 或其实例
|
||||
*/
|
||||
private getShopState(shop: string | IShopState): IShopState | null {
|
||||
if (typeof shop === 'string') {
|
||||
return this.shops.get(shop) ?? null;
|
||||
} else {
|
||||
return shop;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入商店 id 或实例,输出商店 id
|
||||
* @param shop 商店的 id 或其实例
|
||||
*/
|
||||
private getShopId(shop: string | IShopState): string | undefined {
|
||||
if (typeof shop === 'string') {
|
||||
return shop;
|
||||
} else {
|
||||
return shop.id;
|
||||
}
|
||||
}
|
||||
|
||||
addShop(shop: IShopState): void {
|
||||
if (this.shops.has(shop.id)) {
|
||||
logger.warn(198, shop.id);
|
||||
return;
|
||||
}
|
||||
this.shops.set(shop.id, shop);
|
||||
}
|
||||
|
||||
removeShop(shop: string | IShopState): void {
|
||||
const id = this.getShopId(shop);
|
||||
if (!isNil(id)) this.shops.delete(id);
|
||||
}
|
||||
|
||||
getShop(id: string): IShopState | null {
|
||||
return this.shops.get(id) ?? null;
|
||||
}
|
||||
|
||||
hasShop(shop: string | IShopState): boolean {
|
||||
const id = this.getShopId(shop);
|
||||
return isNil(id) || this.shops.has(id);
|
||||
}
|
||||
|
||||
iterateShops(): Iterable<[string, IShopState]> {
|
||||
return this.shops;
|
||||
}
|
||||
|
||||
*iterateEnabledShops(): Iterable<[string, IShopState]> {
|
||||
for (const [key, state] of this.shops) {
|
||||
if (state.enabled) yield [key, state];
|
||||
}
|
||||
}
|
||||
|
||||
*iterateDisabledShops(): Iterable<[string, IShopState]> {
|
||||
for (const [key, state] of this.shops) {
|
||||
if (!state.enabled) yield [key, state];
|
||||
}
|
||||
}
|
||||
|
||||
enableShop(shop: string | IShopState): void {
|
||||
const state = this.getShopState(shop);
|
||||
if (state) state.enable();
|
||||
}
|
||||
|
||||
disableShop(shop: string | IShopState): void {
|
||||
const state = this.getShopState(shop);
|
||||
if (state) state.disable();
|
||||
}
|
||||
|
||||
saveState(compression: SaveCompression): IShopManagerSave {
|
||||
const list = [...this.shops.values()].map(v =>
|
||||
v.saveState(compression)
|
||||
);
|
||||
return {
|
||||
list
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IShopManagerSave, compression: SaveCompression): void {
|
||||
this.shops.clear();
|
||||
for (const save of state.list) {
|
||||
const shop: IShopState = new ShopState(save.id, this.state);
|
||||
shop.loadState(save, compression);
|
||||
this.shops.set(save.id, shop);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ICoreState } from '@user/data-state';
|
||||
import { IShopManager, IShopPlugin, IShopState, IShopSystem } from './types';
|
||||
import { ShopManager } from './manager';
|
||||
import { ShopState } from './state';
|
||||
import { ShopSystem } from './system';
|
||||
|
||||
export class ShopPlugin implements IShopPlugin {
|
||||
id: string = 'shop-plugin';
|
||||
dependencies: string[] = [];
|
||||
|
||||
shop!: IShopSystem;
|
||||
manager!: IShopManager;
|
||||
|
||||
/** 数据端主对象 */
|
||||
private state!: ICoreState;
|
||||
|
||||
createShopState(id: string): IShopState {
|
||||
return new ShopState(id, this.state);
|
||||
}
|
||||
|
||||
awake(state: ICoreState): void {
|
||||
this.state = state;
|
||||
this.shop = new ShopSystem(state);
|
||||
this.manager = new ShopManager(state);
|
||||
|
||||
state.addSaveableContent('@plugin/shop', this.manager);
|
||||
}
|
||||
|
||||
async initialize(): Promise<void> {}
|
||||
|
||||
async afterLoad(): Promise<void> {}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import { IShopState, IShopStateSave } from './types';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { logger } from '@motajs/common';
|
||||
import { ICoreState } from '@user/data-state';
|
||||
|
||||
export class ShopState implements IShopState {
|
||||
enabled: boolean = false;
|
||||
|
||||
/** 此商店拥有的所有道具及其库存 */
|
||||
private readonly items: Map<number, number> = new Map();
|
||||
/** 此商店中,每个道具的买入价格 */
|
||||
private readonly buyPrice: Map<number, number> = new Map();
|
||||
/** 此商店中,每个道具的售出价格 */
|
||||
private readonly sellPrice: Map<number, number> = new Map();
|
||||
|
||||
constructor(
|
||||
readonly id: string,
|
||||
readonly state: ICoreState
|
||||
) {}
|
||||
|
||||
addItem(token: number | string, count: number): void {
|
||||
const num = this.state.tileStore.num(token);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, token.toString());
|
||||
return;
|
||||
}
|
||||
// 注意,这里不把库存为 0 的置空是因为置空之后迭代不到,商店将不再显示这个道具
|
||||
// 但是商店理应显示库存为 0 的道具,一是因为玩家可能需要卖出,二是因为玩家可能需要 0 库存这一信息
|
||||
const before = this.items.getOrInsert(num, 0);
|
||||
this.items.set(num, before + count);
|
||||
}
|
||||
|
||||
getCount(token: number | string): number {
|
||||
const num = this.state.tileStore.num(token);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, token.toString());
|
||||
return -1;
|
||||
}
|
||||
return this.items.get(num) ?? -1;
|
||||
}
|
||||
|
||||
iterateItems(): Iterable<[number, number]> {
|
||||
return this.items;
|
||||
}
|
||||
|
||||
setPrice(item: number | string, buy: number, sell: number): void {
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, item.toString());
|
||||
return;
|
||||
}
|
||||
this.buyPrice.set(num, buy);
|
||||
this.sellPrice.set(num, sell);
|
||||
}
|
||||
|
||||
getPrice(item: number | string): [buy: number, sell: number] {
|
||||
const num = this.state.tileStore.num(item);
|
||||
if (isNil(num)) {
|
||||
logger.warn(196, item.toString());
|
||||
return [-1, -1];
|
||||
}
|
||||
const buy = this.buyPrice.get(num);
|
||||
const sell = this.sellPrice.get(num);
|
||||
if (isNil(buy) || isNil(sell)) return [-1, -1];
|
||||
else return [buy, sell];
|
||||
}
|
||||
|
||||
enable(): void {
|
||||
this.enabled = true;
|
||||
}
|
||||
|
||||
disable(): void {
|
||||
this.enabled = false;
|
||||
}
|
||||
|
||||
saveState(): IShopStateSave {
|
||||
return {
|
||||
id: this.id,
|
||||
enabled: this.enabled,
|
||||
items: this.items,
|
||||
buyPrice: this.buyPrice,
|
||||
sellPrice: this.sellPrice
|
||||
};
|
||||
}
|
||||
|
||||
loadState(state: IShopStateSave): void {
|
||||
if (state.id !== this.id) {
|
||||
logger.warn(197, this.id, state.id);
|
||||
return;
|
||||
}
|
||||
this.enabled = state.enabled;
|
||||
this.items.clear();
|
||||
for (const [key, value] of state.items) {
|
||||
this.items.set(key, value);
|
||||
}
|
||||
this.buyPrice.clear();
|
||||
for (const [key, value] of state.buyPrice) {
|
||||
this.buyPrice.set(key, value);
|
||||
}
|
||||
this.sellPrice.clear();
|
||||
for (const [key, value] of state.sellPrice) {
|
||||
this.sellPrice.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import {
|
||||
IShopHeroAttr,
|
||||
IShopManager,
|
||||
IShopState,
|
||||
IShopSystem,
|
||||
ShopTradeResult
|
||||
} from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
import { isNil } from 'lodash-es';
|
||||
import { shouldReplay } from '@user/data-common';
|
||||
import { ICoreState } from '@user/data-state';
|
||||
import { IHeroState } from '@user/data-base';
|
||||
|
||||
export class ShopSystem implements IShopSystem {
|
||||
manager: IShopManager | null = null;
|
||||
hero: IHeroState<IShopHeroAttr> | null = null;
|
||||
|
||||
private current: IShopState | null = null;
|
||||
|
||||
constructor(readonly state: ICoreState) {}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
import { IHeroState } from '@user/data-base';
|
||||
import { ISaveableContent } from '@user/data-common';
|
||||
import { ICoreStateExtended, IDataPluginDefine } from '@user/data-state';
|
||||
|
||||
export interface IShopPlugin extends IDataPluginDefine {
|
||||
/** 商店插件所使用的商店系统对象 */
|
||||
readonly shop: IShopSystem;
|
||||
/** 商店插件所使用的商店管理器对象 */
|
||||
readonly manager: IShopManager;
|
||||
|
||||
/**
|
||||
* 创建商店状态对象
|
||||
* @param id 商店状态对象的标识符
|
||||
*/
|
||||
createShopState(id: string): IShopState;
|
||||
}
|
||||
|
||||
export interface IShopStateSave {
|
||||
/** 商店 id */
|
||||
readonly id: string;
|
||||
/** 当前商店是否已启用 */
|
||||
readonly enabled: boolean;
|
||||
/** 商店拥有的库存 */
|
||||
readonly items: ReadonlyMap<number, number>;
|
||||
/** 商店道具的买入价 */
|
||||
readonly buyPrice: ReadonlyMap<number, number>;
|
||||
/** 商店道具的售出价 */
|
||||
readonly sellPrice: ReadonlyMap<number, number>;
|
||||
}
|
||||
|
||||
export interface IShopState
|
||||
extends ICoreStateExtended, ISaveableContent<IShopStateSave> {
|
||||
/** 当前商店是否已经启用 */
|
||||
readonly enabled: boolean;
|
||||
/** 当前商店的标识符 */
|
||||
readonly id: string;
|
||||
|
||||
/**
|
||||
* 向此商店添加或减少道具库存,会与原本的库存叠加
|
||||
* @param token 道具数字或 id
|
||||
* @param count 添加或减少的道具数量,正数表示添加,负数表示减少
|
||||
*/
|
||||
addItem(token: number | string, count: number): void;
|
||||
|
||||
/**
|
||||
* 获取指定道具的库存数量,不存在道具则返回 -1
|
||||
* @param token 道具数字或 id
|
||||
*/
|
||||
getCount(token: number | string): number;
|
||||
|
||||
/**
|
||||
* 迭代商店拥有的所有道具,键表示道具的图块数字,值表示数量
|
||||
*/
|
||||
iterateItems(): Iterable<[number, number]>;
|
||||
|
||||
/**
|
||||
* 设置道具价格
|
||||
* @param item 道具数字或 id
|
||||
* @param buy 道具买入价格
|
||||
* @param sell 道具卖出价格
|
||||
*/
|
||||
setPrice(item: number | string, buy: number, sell: number): void;
|
||||
|
||||
/**
|
||||
* 获取道具价格,如果道具不存在则返回 -1
|
||||
* @param item 道具数字或 id
|
||||
*/
|
||||
getPrice(item: number | string): [buy: number, sell: number];
|
||||
|
||||
/**
|
||||
* 启用该商店
|
||||
*/
|
||||
enable(): void;
|
||||
|
||||
/**
|
||||
* 禁用该商店
|
||||
*/
|
||||
disable(): void;
|
||||
}
|
||||
|
||||
export interface IShopManagerSave {
|
||||
/** 商店状态列表 */
|
||||
readonly list: IShopStateSave[];
|
||||
}
|
||||
|
||||
export interface IShopManager
|
||||
extends ICoreStateExtended, ISaveableContent<IShopManagerSave> {
|
||||
/**
|
||||
* 添加新的商店
|
||||
* @param shop 商店状态对象
|
||||
*/
|
||||
addShop(shop: IShopState): void;
|
||||
|
||||
/**
|
||||
* 移除已有商店
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
removeShop(shop: string | IShopState): void;
|
||||
|
||||
/**
|
||||
* 根据商店 id 获取其状态对象
|
||||
* @param id 商店 id
|
||||
*/
|
||||
getShop(id: string): IShopState | null;
|
||||
|
||||
/**
|
||||
* 判断指定商店是否属于当前管理器
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
hasShop(shop: string | IShopState): boolean;
|
||||
|
||||
/**
|
||||
* 迭代当前的所有商店,包括未启用的
|
||||
*/
|
||||
iterateShops(): Iterable<[string, IShopState]>;
|
||||
|
||||
/**
|
||||
* 迭代当前所有已启用的商店
|
||||
*/
|
||||
iterateEnabledShops(): Iterable<[string, IShopState]>;
|
||||
|
||||
/**
|
||||
* 迭代当前所有未启用的商店
|
||||
*/
|
||||
iterateDisabledShops(): Iterable<[string, IShopState]>;
|
||||
|
||||
/**
|
||||
* 启用指定商店
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
enableShop(shop: string | IShopState): void;
|
||||
|
||||
/**
|
||||
* 禁用指定商店
|
||||
* @param shop 商店 id 或其状态对象
|
||||
*/
|
||||
disableShop(shop: string | IShopState): void;
|
||||
}
|
||||
|
||||
export interface IShopHeroAttr {
|
||||
/** 勇士拥有的金币 */
|
||||
money: number;
|
||||
}
|
||||
|
||||
export const enum ShopTradeResult {
|
||||
/** 成功购入或售出 */
|
||||
Success,
|
||||
/** 操作不合法,一般是因为道具不存在或未打开商店 */
|
||||
Invalid,
|
||||
/** 商店库存不足,无法购入这么多道具 */
|
||||
ShopLack,
|
||||
/** 玩家库存不足,无法卖出这么多道具 */
|
||||
HeroLack,
|
||||
/** 玩家货币不足,无法购入这么多道具 */
|
||||
MoneyLack
|
||||
}
|
||||
|
||||
export interface IShopSystem extends ICoreStateExtended {
|
||||
/** 当前使用的商店管理器 */
|
||||
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