Skip to content

Commit

Permalink
bugfix: ambiguos link saving/loading
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleaxe committed Mar 26, 2023
1 parent 8af1f49 commit 5a298cc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
25 changes: 16 additions & 9 deletions site/src/scripts/model/blueprint-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
RecipeModel,
} from './store';
import {BlueprintItemState, type BlueprintItemStateValues} from '../types';
import type {SavedItem} from './saved-blueprint';
import type {SavedItem, SavedLink} from './saved-blueprint';
import {Rect, type PublicRect, type ReadonlyPointType} from '../geometry';
import type {ErrorCollector} from '../error-collector';

Expand Down Expand Up @@ -63,7 +63,7 @@ export class BlueprintItemModelImpl extends ItemModelImpl {
if(!sourceIo) {
return BlueprintItemState.None;
}
const maybeTarget = this._selectedRecipe?.findSimilarIo(sourceIo, true);
const maybeTarget = this._selectedRecipe?._$findSimilarIo(sourceIo, true);
if(maybeTarget) {
if(maybeTarget.isAlreadyLinked(sourceIo)) {
return BlueprintItemState.LinkAlreadyExists;
Expand All @@ -79,7 +79,7 @@ export class BlueprintItemModelImpl extends ItemModelImpl {
this._state = this.calculateLinkState(sourceIo);
}
createLink(sourceIo: RecipeIOModel) {
const maybeTarget = this._selectedRecipe?.findSimilarIo(sourceIo, true);
const maybeTarget = this._selectedRecipe?._$findSimilarIo(sourceIo, true);
if(!maybeTarget)
return;
this.owner?._$addLink(sourceIo, maybeTarget);
Expand Down Expand Up @@ -171,15 +171,22 @@ export class BlueprintItemModelImpl extends ItemModelImpl {
this.isFlipped = i.f ? true : false;
this._isLocked = i.l ? true : false;
}
_$loadLink(outputItem: BlueprintItemModel, errorCollector: ErrorCollector) {
const outputIoArray = outputItem.selectedRecipe?.output;
if(!outputIoArray)
_$loadLink(l: SavedLink, outputItem: BlueprintItemModel, errorCollector: ErrorCollector) {
let inputIoArray = [...this.selectedRecipe?.input || []];
let outputIoArray = [...outputItem.selectedRecipe?.output || []];
if(!inputIoArray.length || !outputIoArray.length)
return undefined;
//if we can link by multiple paths for same item - then link is ambiguous
//we should restore saved name
if(l.n) {
inputIoArray = inputIoArray.filter((io) => ((io.name == l.n) || io.isAbstractClassItem));
outputIoArray = outputIoArray.filter((io) => ((io.name == l.n) || io.isAbstractClassItem));
}
let link: LinkModel | undefined;
for(const outputIo of outputIoArray) {
const maybeTarget = this._selectedRecipe?.findSimilarIo(outputIo, true);
for(const inputIo of inputIoArray) {
const maybeTarget = outputIoArray.find((io) => RecipeModelImpl._$isSimilarIo(inputIo, io));
if(maybeTarget) {
link = this.owner?._$addLink(outputIo, maybeTarget);
link = this.owner?._$addLink(inputIo, maybeTarget);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion site/src/scripts/model/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class BlueprintModelImpl {
if(!input || !output) {
return;
}
const loadedLink = input._$loadLink(output, errorCollector);
const loadedLink = input._$loadLink(link, output, errorCollector);
if(loadedLink) {
loadedLink._$load(link, errorCollector);
}
Expand Down
17 changes: 17 additions & 0 deletions site/src/scripts/model/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,28 @@ export class LinkModelImpl {
return undefined;
}
_$save(input?: number, output?: number): SavedLink {
//if we can link by multiple paths for same item - then link is ambiguous
//we should store name as well in this case
let isAmbiguous = false;
if(this.input?.isAbstractClassItem || this.output?.isAbstractClassItem) {
isAmbiguous = true;
} else {
const allInputs = this.input?.ownerItem?.selectedRecipe?.input;
const allOutputs = this.output?.ownerItem?.selectedRecipe?.output;
const allInputsNames = new Set([...allInputs || []].map((i) => i.name || ''));
const similarIo = [...allOutputs || []].reduce((sum, i) => {
if(allInputsNames.has(i.name || ''))
sum++;
return sum;
}, 0);
isAmbiguous = similarIo > 1;
}
return {
l: [
input || 0,
output || 0,
],
n: isAmbiguous ? this.input?.name : undefined,
};
}
_$load(l: SavedLink, errorCollector: ErrorCollector) {
Expand Down
27 changes: 14 additions & 13 deletions site/src/scripts/model/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ export class RecipeModelImpl {
this._itemsByKey = new Map(items.map((io) => [io.key, io]));
}

findSimilarIo(sourceIo: RecipeIOModel, reverce: boolean) {
_$findSimilarIo(sourceIo: RecipeIOModel, reverce: boolean) {
const isInput = reverce ? !sourceIo.isInput : sourceIo.isInput;
const targetArray = isInput ? this._input : this._output;
return targetArray.find((io) => {
if(io.isAbstractClassItem && sourceIo.isAbstractClassItem)
return false;
return (io.name === sourceIo.name)
|| (
(io.isAbstractClassItem !== sourceIo.isAbstractClassItem)
&& !io.isMatherialized
&& !sourceIo.isMatherialized
&& (io.type === sourceIo.type)
);
});
return targetArray.find((io) => RecipeModelImpl._$isSimilarIo(sourceIo, io));
}
static _$isSimilarIo(sourceIo: RecipeIOModel, targetIo: RecipeIOModel) {
if(targetIo.isAbstractClassItem && sourceIo.isAbstractClassItem)
return false;
return (targetIo.name === sourceIo.name)
|| (
(targetIo.isAbstractClassItem !== sourceIo.isAbstractClassItem)
&& !targetIo.isMatherialized
&& !sourceIo.isMatherialized
&& (targetIo.type === sourceIo.type)
);
}
_$copySimilarLinksTo(targetRecipe: RecipeModel) {
for(const targetItem of targetRecipe.items) {
const similarItem = this.findSimilarIo(targetItem, false);
const similarItem = this._$findSimilarIo(targetItem, false);
if(!similarItem)
continue;
similarItem._$copySimilarLinksTo(targetItem);
Expand Down
3 changes: 2 additions & 1 deletion site/src/scripts/model/saved-blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Author: Alexey Usov ([email protected], https://t.me/doubleaxe, https://github.com/doubleaxe)
Author: Alexey Usov ([email protected], https://github.com/doubleaxe)
Please don't remove this comment if you use unmodified file
*/
export type SavedBlueprint = {
Expand All @@ -24,4 +24,5 @@ export type SavedItem = {

export type SavedLink = {
l: number[];
n?: string;
};

0 comments on commit 5a298cc

Please sign in to comment.