1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-26 03:52:00 +02:00

added weights to tasks

This commit is contained in:
grilledham 2018-01-28 18:33:25 +00:00
parent 6d0dfc297b
commit 5e65e4e970

View File

@ -20,6 +20,7 @@ end
global.callbacks = {} global.callbacks = {}
global.next_async_callback_time = -1 global.next_async_callback_time = -1
global.task_queue = global.task_queue or Queue.new() global.task_queue = global.task_queue or Queue.new()
global.total_task_weight = 0
local function on_tick() local function on_tick()
local queue = global.task_queue local queue = global.task_queue
for i = 1, get_task_per_tick() do for i = 1, get_task_per_tick() do
@ -29,8 +30,10 @@ local function on_tick()
if not success then if not success then
log(result) log(result)
Queue.pop(queue) Queue.pop(queue)
global.total_task_weight = global.total_task_weight - task.weight
elseif not result then elseif not result then
Queue.pop(queue) Queue.pop(queue)
global.total_task_weight = global.total_task_weight - task.weight
end end
end end
end end
@ -50,7 +53,7 @@ local function on_tick()
end end
function get_task_per_tick() function get_task_per_tick()
local size = Queue.size(global.task_queue) local size = global.total_task_weight
local apt = math.floor(math.log10(size + 1)) local apt = math.floor(math.log10(size + 1))
if apt < 1 then if apt < 1 then
return 1 return 1
@ -74,9 +77,10 @@ function Task.set_timeout(sec, callback, params)
end end
function Task.queue_task(func_name, params) function Task.queue_task(func_name, params, weight)
local queue = global.task_queue weight = weight or 1
Queue.push(queue, {func_name = func_name, params = params}) global.total_task_weight = global.total_task_weight + weight
Queue.push(global.task_queue, {func_name = func_name, params = params, weight = weight})
end end
Event.register(defines.events.on_tick, on_tick) Event.register(defines.events.on_tick, on_tick)