Refactor conveyor mechanics and action handlers: update spawn count, interval, and delay handling; enhance material management in useMaterialStore; implement spawn action handler; unify action handling across various components.
This commit is contained in:
@@ -4,16 +4,39 @@ import { immer } from 'zustand/middleware/immer';
|
||||
type MaterialsStore = {
|
||||
materials: MaterialsSchema;
|
||||
|
||||
addMaterial: (material: MaterialSchema) => void;
|
||||
removeMaterial: (materialId: string) => void;
|
||||
updateMaterial: (materialId: string, updates: Partial<MaterialSchema>) => void;
|
||||
addMaterial: (material: MaterialSchema) => MaterialSchema | undefined;
|
||||
removeMaterial: (materialId: string) => MaterialSchema | undefined;
|
||||
updateMaterial: (materialId: string, updates: Partial<MaterialSchema>) => MaterialSchema | undefined;
|
||||
|
||||
setStartTime: (materialId: string, startTime: string) => void;
|
||||
setEndTime: (materialId: string, endTime: string) => void;
|
||||
setCost: (materialId: string, cost: number) => void;
|
||||
setWeight: (materialId: string, weight: number) => void;
|
||||
setCurrentLocation: (
|
||||
materialId: string,
|
||||
location: {
|
||||
modelUuid: string;
|
||||
pointUuid: string;
|
||||
actionUuid: string;
|
||||
}
|
||||
) => MaterialSchema | undefined;
|
||||
|
||||
setNextLocation: (
|
||||
materialId: string,
|
||||
location?: {
|
||||
modelUuid: string;
|
||||
pointUuid: string;
|
||||
actionUuid: string;
|
||||
} | null
|
||||
) => MaterialSchema | undefined;
|
||||
|
||||
setStartTime: (materialId: string, startTime: string) => MaterialSchema | undefined;
|
||||
setEndTime: (materialId: string, endTime: string) => MaterialSchema | undefined;
|
||||
setCost: (materialId: string, cost: number) => MaterialSchema | undefined;
|
||||
setWeight: (materialId: string, weight: number) => MaterialSchema | undefined;
|
||||
setIsActive: (materialId: string, isActive: boolean) => MaterialSchema | undefined;
|
||||
setIsVisible: (materialId: string, isVisible: boolean) => MaterialSchema | undefined;
|
||||
setIsRendered: (materialId: string, isRendered: boolean) => MaterialSchema | undefined;
|
||||
|
||||
getMaterialById: (materialId: string) => MaterialSchema | undefined;
|
||||
getMaterialsByPoint: (pointUuid: string) => MaterialSchema[];
|
||||
getMaterialsByModel: (modelUuid: string) => MaterialSchema[];
|
||||
};
|
||||
|
||||
export const useMaterialStore = create<MaterialsStore>()(
|
||||
@@ -21,56 +44,161 @@ export const useMaterialStore = create<MaterialsStore>()(
|
||||
materials: [],
|
||||
|
||||
addMaterial: (material) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
state.materials.push(material);
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
removeMaterial: (materialId) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
state.materials = state.materials.filter(m => m.materialId !== materialId);
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
state.materials.filter(m => m.materialId !== material.materialId);
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
}
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
updateMaterial: (materialId, updates) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
Object.assign(material, updates);
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
}
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setCurrentLocation: (materialId, location) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
material.current = location;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
}
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setNextLocation: (materialId, location) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
material.next = location || undefined;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
}
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setStartTime: (materialId, startTime) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) material.startTime = startTime;
|
||||
if (material) {
|
||||
material.startTime = startTime
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setEndTime: (materialId, endTime) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) material.endTime = endTime;
|
||||
if (material) {
|
||||
material.endTime = endTime;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setCost: (materialId, cost) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) material.cost = cost;
|
||||
if (material) {
|
||||
material.cost = cost;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setWeight: (materialId, weight) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) material.weight = weight;
|
||||
if (material) {
|
||||
material.weight = weight;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setIsActive: (materialId, isActive) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
material.isActive = isActive;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setIsVisible: (materialId, isVisible) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
material.isVisible = isVisible;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
setIsRendered: (materialId, isRendered) => {
|
||||
let updatedMaterial: MaterialSchema | undefined;
|
||||
set((state) => {
|
||||
const material = state.materials.find(m => m.materialId === materialId);
|
||||
if (material) {
|
||||
material.isRendered = isRendered;
|
||||
updatedMaterial = JSON.parse(JSON.stringify(material));
|
||||
};
|
||||
});
|
||||
return updatedMaterial;
|
||||
},
|
||||
|
||||
getMaterialById: (materialId) => {
|
||||
return get().materials.find(m => m.materialId === materialId);
|
||||
},
|
||||
|
||||
getMaterialsByPoint: (pointUuid) => {
|
||||
return get().materials.filter(m =>
|
||||
m.current?.pointUuid === pointUuid ||
|
||||
m.next?.pointUuid === pointUuid
|
||||
);
|
||||
},
|
||||
|
||||
getMaterialsByModel: (modelUuid) => {
|
||||
return get().materials.filter(m =>
|
||||
m.current?.modelUuid === modelUuid ||
|
||||
m.next?.modelUuid === modelUuid
|
||||
);
|
||||
},
|
||||
}))
|
||||
);
|
||||
);
|
||||
@@ -63,6 +63,7 @@ type ProductsStore = {
|
||||
getEventByModelUuid: (productId: string, modelUuid: string) => EventsSchema | undefined;
|
||||
getPointByUuid: (productId: string, modelUuid: string, pointUuid: string) => ConveyorPointSchema | VehiclePointSchema | RoboticArmPointSchema | MachinePointSchema | StoragePointSchema | undefined;
|
||||
getActionByUuid: (productId: string, actionUuid: string) => (ConveyorPointSchema['action'] | VehiclePointSchema['action'] | RoboticArmPointSchema['actions'][0] | MachinePointSchema['action'] | StoragePointSchema['action']) | undefined;
|
||||
getModelUuidByActionUuid: (productId: string, actionUuid: string) => (string) | undefined;
|
||||
getTriggerByUuid: (productId: string, triggerUuid: string) => TriggerSchema | undefined;
|
||||
getIsEventInProduct: (productId: string, modelUuid: string) => boolean;
|
||||
};
|
||||
@@ -573,6 +574,30 @@ export const useProductStore = create<ProductsStore>()(
|
||||
return undefined;
|
||||
},
|
||||
|
||||
getModelUuidByActionUuid: (productId, actionUuid) => {
|
||||
const product = get().products.find(p => p.productId === productId);
|
||||
if (!product) return undefined;
|
||||
|
||||
for (const event of product.eventDatas) {
|
||||
if ('points' in event) {
|
||||
for (const point of (event as ConveyorEventSchema).points) {
|
||||
if (point.action?.actionUuid === actionUuid) {
|
||||
return event.modelUuid;
|
||||
}
|
||||
}
|
||||
} else if ('point' in event) {
|
||||
const point = (event as any).point;
|
||||
if ('action' in point && point.action?.actionUuid === actionUuid) {
|
||||
return event.modelUuid;
|
||||
} else if ('actions' in point) {
|
||||
const action = point.actions.find((a: any) => a.actionUuid === actionUuid);
|
||||
if (action) return event.modelUuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
|
||||
getTriggerByUuid: (productId, triggerUuid) => {
|
||||
const product = get().products.find(p => p.productId === productId);
|
||||
if (!product) return undefined;
|
||||
|
||||
Reference in New Issue
Block a user