From cb7bbed2a843227dbd9f36a9393133f70a0123f1 Mon Sep 17 00:00:00 2001 From: Nico Hagelberg <16757571+nicou@users.noreply.github.com> Date: Wed, 3 Jul 2024 20:24:02 +0300 Subject: [PATCH] Add fusebox task priority (#67) * Fix the Doomba task so that it can be broken * Move getPriorityTasks under helpers * Fix fusebox breakTask() Allow breaking of tasks with 'initial' status. Don't break single use tasks that have already been used. Consider task priority when breaking fusebox tasks. --- db/redux/game/manualTasks.js | 5 ++--- src/rules/boxes/fuseboxes.js | 12 +++++++++--- src/rules/helpers.js | 8 ++++++++ src/rules/ship/eeHealth.js | 7 +------ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/db/redux/game/manualTasks.js b/db/redux/game/manualTasks.js index ce6e240e..7f54d3fc 100644 --- a/db/redux/game/manualTasks.js +++ b/db/redux/game/manualTasks.js @@ -3178,10 +3178,9 @@ blobs.push({ id: 'doomba', type: 'task', game: 'doomba', - singleUse: false, + singleUse: true, used: false, - eeType: 'lifesupport', - eeHealth: 0.2, + lifesupportHealth: 0.20, // fixes 20% status: 'initial', calibrationCount: 1, calibrationTime: 870, diff --git a/src/rules/boxes/fuseboxes.js b/src/rules/boxes/fuseboxes.js index d0dbbbf9..842fd713 100644 --- a/src/rules/boxes/fuseboxes.js +++ b/src/rules/boxes/fuseboxes.js @@ -2,7 +2,7 @@ import store, { watch } from '../../store/store'; import { CHANNELS, fireEvent } from '../../dmx'; import { logger } from '../../logger'; import { isNumber, isEqual } from 'lodash'; -import { chooseRandom, saveBlob } from '../helpers'; +import { chooseRandom, getPriorityTasks, saveBlob } from '../helpers'; // Fire DMX signals based on dmxFuse configuration watch(['data', 'box'], (boxes, previousBoxes, state) => { @@ -45,8 +45,14 @@ watch(['data', 'box'], (boxes, previousBoxes, state) => { function breakTask() { const allTasks = Object.values(store.getState().data.task); - const tasks = allTasks.filter(task => task.lifesupportHealth && task.status === 'fixed'); - const task = chooseRandom(tasks)[0]; + const tasks = allTasks.filter((task) => { + const isLifeSupportTask = Number.isFinite(task.lifesupportHealth); + const isBreakable = ['fixed', 'initial'].includes(task.status); + const isNotUsed = !task.singleUse || !task.used; + return isLifeSupportTask && isBreakable && isNotUsed; + }); + const priorityTasks = getPriorityTasks(tasks); + const task = chooseRandom(priorityTasks)[0]; if (!task) { logger.warn(`Cound not find life support task to break`); return; diff --git a/src/rules/helpers.js b/src/rules/helpers.js index 0605e36e..8423bd6e 100644 --- a/src/rules/helpers.js +++ b/src/rules/helpers.js @@ -158,3 +158,11 @@ export function* rng(seed = 1) { } } +/** + * Return the tasks with the highest priority + * @param {array} tasks + */ +export function getPriorityTasks(tasks) { + const maxPriority = tasks.reduce((max, task) => Math.max(max, task.priority ? task.priority : 0), -Infinity); + return tasks.filter(task => (task.priority ? task.priority : 0) === maxPriority); +} diff --git a/src/rules/ship/eeHealth.js b/src/rules/ship/eeHealth.js index 1f02cc3e..4429db39 100644 --- a/src/rules/ship/eeHealth.js +++ b/src/rules/ship/eeHealth.js @@ -1,4 +1,4 @@ -import { clamp, interval, chooseRandom } from '../helpers'; +import { clamp, interval, chooseRandom, getPriorityTasks } from '../helpers'; import { breakTask } from '../breakTask'; import store, { watch } from '../../store/store'; import { getEmptyEpsilonClient } from '../../integrations/emptyepsilon/client'; @@ -59,11 +59,6 @@ function computeHealth(brokenTasks) { return brokenTasks.reduce((health, task) => health - task.eeHealth, 1); } -function getPriorityTasks(tasks) { - const maxPriority = tasks.reduce((max, task) => Math.max(max, task.priority ? task.priority : 0), -1000000); - return tasks.filter(task => (task.priority ? task.priority : 0) === maxPriority); -} - function breakTasks(type, targetHealth) { const tasks = getEETasks(type); const brokenTasks = tasks.filter(isBroken);