Skip to content

Commit

Permalink
some fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Gagnon committed Jun 9, 2024
1 parent e11fa02 commit 99edf49
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/Myrian/Myrian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const vulnsMap: Record<Component, number> = {
[Component.W7]: 16384,
};

export const isDeviceContainer = (device: BaseDevice): device is ContainerDevice => "inventory" in device;
export const isDeviceContainer = (device: BaseDevice): device is ContainerDevice => "content" in device;

export const isDeviceBus = (d: Device): d is Bus => d.type === DeviceType.Bus;
export const isDeviceISocket = (d: Device): d is ISocket => d.type === DeviceType.ISocket;
Expand Down
180 changes: 103 additions & 77 deletions src/NetscriptFunctions/Myrian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,21 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {
}

bus.isBusy = true;
return helpers.netscriptDelay(ctx, moveSpeed(bus.moveLvl), true).then(() => {
bus.isBusy = false;
if (findDevice([x, y])) {
helpers.log(ctx, () => `[${x}, ${y}] is occupied`);
return Promise.resolve(false);
}
bus.x = x;
bus.y = y;
return Promise.resolve(true);
});
return helpers
.netscriptDelay(ctx, moveSpeed(bus.moveLvl), true)
.then(() => {
bus.isBusy = false;
if (findDevice([x, y])) {
helpers.log(ctx, () => `[${x}, ${y}] is occupied`);
return Promise.resolve(false);
}
bus.x = x;
bus.y = y;
return Promise.resolve(true);
})
.finally(() => {
bus.isBusy = false;
});
},
transfer:
(ctx) =>
Expand Down Expand Up @@ -153,39 +158,45 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {
fromDevice.isBusy = true;
toDevice.isBusy = true;

return helpers.netscriptDelay(ctx, transferSpeed(bus.transferLvl), true).then(() => {
fromDevice.isBusy = false;
toDevice.isBusy = false;
toDevice.content = toDevice.content.filter((item) => !input.includes(item));
toDevice.content.push(...output);

fromDevice.content = fromDevice.content.filter((item) => !output.includes(item));
fromDevice.content.push(...input);

switch (container.type) {
case DeviceType.ISocket: {
container.cooldownUntil = Date.now() + container.cooldown;
setTimeout(() => {
container.content = new Array(container.maxContent).fill(container.emitting);
}, container.cooldown);
break;
}
return helpers
.netscriptDelay(ctx, transferSpeed(bus.transferLvl), true)
.then(() => {
fromDevice.isBusy = false;
toDevice.isBusy = false;
toDevice.content = toDevice.content.filter((item) => !input.includes(item));
toDevice.content.push(...output);

fromDevice.content = fromDevice.content.filter((item) => !output.includes(item));
fromDevice.content.push(...input);

switch (container.type) {
case DeviceType.ISocket: {
container.cooldownUntil = Date.now() + container.cooldown;
setTimeout(() => {
container.content = new Array(container.maxContent).fill(container.emitting);
}, container.cooldown);
break;
}

case DeviceType.OSocket: {
if (inventoryMatches(container.currentRequest, container.content)) {
const gain = container.content.map((i) => vulnsMap[i]).reduce((a, b) => a + b, 0);
myrian.vulns += gain;
myrian.totalVulns += gain;
container.content = [];
const request = getNextISocketRequest(container.tier);
container.currentRequest = request;
container.maxContent = request.length;
case DeviceType.OSocket: {
if (inventoryMatches(container.currentRequest, container.content)) {
const gain = container.content.map((i) => vulnsMap[i]).reduce((a, b) => a + b, 0);
myrian.vulns += gain;
myrian.totalVulns += gain;
container.content = [];
const request = getNextISocketRequest(container.tier);
container.currentRequest = request;
container.maxContent = request.length;
}
break;
}
break;
}
}
return Promise.resolve(true);
});
return Promise.resolve(true);
})
.finally(() => {
fromDevice.isBusy = false;
toDevice.isBusy = false;
});
},
reduce:
(ctx) =>
Expand Down Expand Up @@ -224,12 +235,16 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {

bus.isBusy = true;
reducer.isBusy = true;
return helpers.netscriptDelay(ctx, reduceSpeed(bus.reduceLvl), true).then(() => {
bus.isBusy = false;
reducer.isBusy = false;
reducer.content = [recipe.output];
return Promise.resolve(true);
});
return helpers
.netscriptDelay(ctx, reduceSpeed(bus.reduceLvl), true)
.then(() => {
reducer.content = [recipe.output];
return Promise.resolve(true);
})
.finally(() => {
bus.isBusy = false;
reducer.isBusy = false;
});
},
upgradeMaxContent: (ctx) => (_id) => {
const id = helpers.deviceID(ctx, "id", _id);
Expand Down Expand Up @@ -327,32 +342,37 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {
const lockName = `lock-${busID}`;
const lock = NewLock(lockName, x, y);
lock.isBusy = true;
return helpers.netscriptDelay(ctx, installSpeed(bus.installLvl), true).then(() => {
bus.isBusy = false;
removeDevice(lockName);
switch (deviceType) {
case DeviceType.Bus: {
NewBus(name, x, y);
break;
}
case DeviceType.ISocket: {
NewISocket(name, x, y, componentTiers[0][Math.floor(Math.random() * componentTiers[0].length)]);
break;
}
case DeviceType.OSocket: {
NewOSocket(name, x, y);
break;
}
case DeviceType.Reducer: {
NewReducer(name, x, y);
break;
}
case DeviceType.Cache: {
NewCache(name, x, y);
return helpers
.netscriptDelay(ctx, installSpeed(bus.installLvl), true)
.then(() => {
bus.isBusy = false;
removeDevice(lockName);
switch (deviceType) {
case DeviceType.Bus: {
NewBus(name, x, y);
break;
}
case DeviceType.ISocket: {
NewISocket(name, x, y, componentTiers[0][Math.floor(Math.random() * componentTiers[0].length)]);
break;
}
case DeviceType.OSocket: {
NewOSocket(name, x, y);
break;
}
case DeviceType.Reducer: {
NewReducer(name, x, y);
break;
}
case DeviceType.Cache: {
NewCache(name, x, y);
}
}
}
return Promise.resolve(true);
});
return Promise.resolve(true);
})
.finally(() => {
bus.isBusy = false;
});
},
uninstallDevice: (ctx) => async (_bus, _coord) => {
const busID = helpers.string(ctx, "bus", _bus);
Expand All @@ -377,12 +397,18 @@ export function NetscriptMyrian(): InternalAPI<IMyrian> {

bus.isBusy = true;
placedDevice.isBusy = true;
return helpers.netscriptDelay(ctx, installSpeed(bus.installLvl), true).then(() => {
bus.isBusy = false;
placedDevice.isBusy = false;
removeDevice([x, y]);
return Promise.resolve(true);
});
return helpers
.netscriptDelay(ctx, installSpeed(bus.installLvl), true)
.then(() => {
bus.isBusy = false;
placedDevice.isBusy = false;
removeDevice([x, y]);
return Promise.resolve(true);
})
.finally(() => {
bus.isBusy = false;
placedDevice.isBusy = false;
});
},
};
}

0 comments on commit 99edf49

Please sign in to comment.