TaskHelpers

Utility functions for common task operations

helpers

Library Implementation

-- TaskHelpers.lua
-- Utility functions for task implementation

local TaskHelpers = {}

-- Calculate elapsed time
function TaskHelpers.getElapsedTime(startTime)
    return vrf.getSimulationTime() - startTime
end

-- Check if timeout exceeded
function TaskHelpers.isTimeout(startTime, timeoutSeconds)
    return TaskHelpers.getElapsedTime(startTime) > timeoutSeconds
end

-- Store data in task state
function TaskHelpers.setData(state, key, value)
    if not state.data then
        state.data = {}
    end
    state.data[key] = value
end

-- Retrieve data from task state
function TaskHelpers.getData(state, key, defaultValue)
    if not state.data then
        return defaultValue
    end
    return state.data[key] or defaultValue
end

-- Log with timestamp
function TaskHelpers.log(state, message)
    local elapsed = state.startTime and TaskHelpers.getElapsedTime(state.startTime) or 0
    local logMsg = string.format("[%.2fs] %s", elapsed, message)
    vrf.log(logMsg)
    return logMsg
end

-- Format status message
function TaskHelpers.formatStatus(state, message)
    local progress = 0
    if state.subtasks and #state.subtasks > 0 then
        local completed = 0
        for _, st in ipairs(state.subtasks) do
            if st.status == "COMPLETE" then
                completed = completed + 1
            end
        end
        progress = (completed / #state.subtasks) * 100
    end

    return string.format("%s (%.0f%% complete)", message, progress)
end

-- Wait for duration
function TaskHelpers.wait(state, durationSeconds)
    if not state.waitStartTime then
        state.waitStartTime = vrf.getSimulationTime()
        return false
    end

    local elapsed = vrf.getSimulationTime() - state.waitStartTime
    if elapsed >= durationSeconds then
        state.waitStartTime = nil
        return true
    end

    return false
end

return TaskHelpers
1873 characters

Usage Example

-- Usage example:
local TaskHelpers = require("TaskHelpers")

function tick()
    -- Check for timeout
    if TaskHelpers.isTimeout(taskState.startTime, 300) then
        return "FAILURE", "Task timeout (5 minutes)"
    end

    -- Store temporary data
    TaskHelpers.setData(taskState, "lastCheckTime", vrf.getSimulationTime())

    -- Log progress
    TaskHelpers.log(taskState, "Executing subtask: " .. currentSubtaskName)

    -- Format status with progress
    local status = TaskHelpers.formatStatus(taskState, "Task in progress")

    -- Wait for cooldown
    if taskState.current == "COOLDOWN" then
        if TaskHelpers.wait(taskState, 10) then
            taskState.current = "READY"
        end
    end

    return "CONTINUE", status
end

Quick Reference

Import Statement

local TaskHelpers = require("TaskHelpers")

Category

helpers

Key Features

  • Time and timeout management
  • Data storage and retrieval
  • Logging with timestamps
  • Status formatting and progress display