fix transfer bug

This commit is contained in:
Olivier Gagnon 2024-06-18 14:46:59 -04:00
parent c459586df7
commit 300a93f27f
No known key found for this signature in database
GPG Key ID: 0018772EA86FA03F

@ -183,16 +183,37 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {
helpers.log(ctx, () => "not enough space in one of the containers"); helpers.log(ctx, () => "not enough space in one of the containers");
return Promise.resolve(false); return Promise.resolve(false);
} }
if (fromDevice.isBusy || toDevice.isBusy) {
const fromHas = input.every((item) => fromDevice.content.includes(item)); helpers.log(ctx, () => "one of the entities is busy");
const toHas = output.every((item) => toDevice.content.includes(item));
if (!fromHas || !toHas) {
helpers.log(ctx, () => "one of the entities does not have the items");
return Promise.resolve(false); return Promise.resolve(false);
} }
if (fromDevice.isBusy || toDevice.isBusy) { const fromContentMap = fromDevice.content.reduce(
helpers.log(ctx, () => "one of the entities is busy"); (acc, c) => ({ ...acc, [c]: (acc[c] ?? 0) + 1 }),
{} as Record<Component, number>,
);
const toContentMap = toDevice.content.reduce(
(acc, c) => ({ ...acc, [c]: (acc[c] ?? 0) + 1 }),
{} as Record<Component, number>,
);
const inputContentMap = input.reduce(
(acc, c) => ({ ...acc, [c]: (acc[c] ?? 0) + 1 }),
{} as Record<Component, number>,
);
const outputContentMap = output.reduce(
(acc, c) => ({ ...acc, [c]: (acc[c] ?? 0) + 1 }),
{} as Record<Component, number>,
);
const fromHas = (Object.keys(inputContentMap) as Component[]).every(
(k) => fromContentMap[k] >= inputContentMap[k],
);
const toHas = (Object.keys(outputContentMap) as Component[]).every(
(k) => toContentMap[k] >= outputContentMap[k],
);
if (!fromHas || !toHas) {
helpers.log(ctx, () => "one of the entities does not have the items");
return Promise.resolve(false); return Promise.resolve(false);
} }
@ -205,11 +226,22 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {
.netscriptDelay(ctx, transferSpeed(bus.transferLvl) * isolationMult(myrian.glitches[Glitch.Isolation]), true) .netscriptDelay(ctx, transferSpeed(bus.transferLvl) * isolationMult(myrian.glitches[Glitch.Isolation]), true)
.then(() => { .then(() => {
const previousSize = container.content.length; const previousSize = container.content.length;
toDevice.content = toDevice.content.filter((item) => !output.includes(item));
toDevice.content.push(...input);
fromDevice.content = fromDevice.content.filter((item) => !input.includes(item)); (Object.keys(inputContentMap) as Component[]).forEach((k) => {
fromDevice.content.push(...output); fromContentMap[k] = (fromContentMap[k] ?? 0) - inputContentMap[k];
toContentMap[k] = (toContentMap[k] ?? 0) + inputContentMap[k];
});
(Object.keys(outputContentMap) as Component[]).forEach((k) => {
toContentMap[k] = (toContentMap[k] ?? 0) - outputContentMap[k];
fromContentMap[k] = (fromContentMap[k] ?? 0) + outputContentMap[k];
});
toDevice.content = (Object.keys(toContentMap) as Component[])
.map((k) => new Array(toContentMap[k]).fill(k))
.flat();
fromDevice.content = (Object.keys(fromContentMap) as Component[])
.map((k) => new Array(fromContentMap[k]).fill(k))
.flat();
if (isDeviceISocket(container) && previousSize > container.content.length) { if (isDeviceISocket(container) && previousSize > container.content.length) {
const cooldown = emissionSpeed(container.emissionLvl); const cooldown = emissionSpeed(container.emissionLvl);