Files
cut-abstractions/samples/moduleManager/module_tools.ts
2025-07-22 18:38:25 +08:00

93 lines
3.0 KiB
TypeScript

import { Processor, ProcessorModule } from "../../src/device";
import { confItem, Knife, PlaceBlock, PlaceBlockDetail, PlaceMaterial, _knifeType } from "../confClass";
import { Big_bang, ComposingType, LineType, WorkerItemType, xbang } from "../handleAbility/RectOptimizeWorker/bang";
/** 模块 刀库
*
* input 入参
*/
/** 优化前的 刀库 */
export const ToolsModule: ProcessorModule<any, any> = {
moduleName: "RectOptimizeMachine",
moduleVersion: '20250714',
config: {
knifeList: [],
},
setConfig(config) {
this.config = { ...this.config, ...config };
},
// 会在处理器自动执行
/**
*
* @param input 输入数据
* @param next 下一个流程的函数
* @param context 上下文
* @returns
*/
process(input, next, context) {
// 将刀库添加到上下文
Reflect.set(context, 'knifeList', this.config.knifeList)
// 将刀具查询方法加到上下文
Reflect.set(context, 'getKnifeByParams', getKnifeByParams)
/** 通用 找刀具 根据查询条件 */
function getKnifeByParams(params: _knifeType, knifeList: Knife[]) {
let knife: Knife | null = null
if (params) {
let tempKnifeList: Knife[] = [...knifeList] // []
let keys = Object.keys(params)
if (keys.length > 0) {
keys.forEach(key => {
if (Array.isArray(params[key]) && key == 'ability') {
// 进来的应该是ability 是数组 判断刀的能力
for (const arrItem of params[key]) {
let _knifeList = knifeList.filter(e => e.ability.includes(arrItem))
_knifeList.forEach(k => {
if (!this.KnifeIsInKnifeList(k, tempKnifeList)) {
tempKnifeList.push(k)
}
})
}
} else if (['string', 'number'].includes(typeof (params[key]))) {
if (params && params[key] && typeof (params[key]) == 'number') {
if (key == 'length') {
tempKnifeList = tempKnifeList.filter(e => e[key] >= params[key])
} else {
tempKnifeList = tempKnifeList.filter(e => e[key] == params[key])
}
}
}
});
if (tempKnifeList.length > 0) {
knife = tempKnifeList[0]
}
} else {
console.log('传入的查询条件 没有参数')
}
}
return knife
}
return next ? next(input) : input;
},
onError(error) {
console.log('出错了哦', error);
}
};