diff options
Diffstat (limited to 'server-beta/src/pub')
-rw-r--r-- | server-beta/src/pub/brave.lua | 22 | ||||
-rw-r--r-- | server-beta/src/pub/client.lua | 46 |
2 files changed, 50 insertions, 18 deletions
diff --git a/server-beta/src/pub/brave.lua b/server-beta/src/pub/brave.lua index c9fd0afe..7662a39e 100644 --- a/server-beta/src/pub/brave.lua +++ b/server-beta/src/pub/brave.lua @@ -5,7 +5,23 @@ local m = {} m.type = 'pub.brave' --- 注册成为勇者 -function m:register(id) - self.taskpad = thread.channel('taskpad' .. id) - self.waiter = thread.channel('waiter' .. id) +function m.register(id) + m.taskpad = thread.channel('taskpad' .. id) + m.waiter = thread.channel('waiter' .. id) + m.start() end + +--- 开始找工作 +function m.start() + while true do + local suc, name, id, params = m.taskpad:pop() + if not suc then + -- 找不到工作的勇者,只好睡觉 + thread.sleep(0.01) + end + local result = require(name)(params) + m.waiter:push(id, result) + end +end + +return m diff --git a/server-beta/src/pub/client.lua b/server-beta/src/pub/client.lua index 5ac24684..005bf9ce 100644 --- a/server-beta/src/pub/client.lua +++ b/server-beta/src/pub/client.lua @@ -1,13 +1,14 @@ local thread = require 'bee.thread' local utility = require 'utility' local task = require 'task' +local type = require 'type' local braveTemplate = [[ package.path = %q package.cpath = %q local brave = require 'pub.brave' -brave:register(%d) +brave.register(%d) ]] ---@class pub_client @@ -17,13 +18,13 @@ m.braves = {} --- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务 ---@param num integer -function m:recruitBraves(num) +function m.recruitBraves(num) for _ = 1, num do - local id = #self.braves + 1 + local id = #m.braves + 1 log.info('Create pub brave:', id) thread.newchannel('taskpad' .. id) thread.newchannel('waiter' .. id) - self.braves[id] = { + m.braves[id] = { id = id, taskpad = thread.channel('taskpad' .. id), waiter = thread.channel('waiter' .. id), @@ -40,42 +41,57 @@ function m:recruitBraves(num) end --- 勇者是否有空 -function m:isIdle(brave) - return brave.currentTask == nil and not next(brave.taskList) +function m.isIdle(brave) + return next(brave.taskList) == nil end --- 给勇者推送任务 -function m:pushTask(brave, name, ...) +function m.pushTask(brave, name, params) local taskID = brave.counter() local co = coroutine.running() - brave.taskpad:push(name, taskID, ...) + brave.taskpad:push(name, taskID, params) brave.taskList[taskID] = co return coroutine.yield(co) end --- 从勇者处接收任务反馈 -function m:popTask(brave, id, ...) +function m.popTask(brave, id, params) local co = brave.taskList[id] if not co then log.warn(('Brave pushed unknown task result: [%d] => [%d]'):format(brave.id, id)) return end brave.taskList[id] = nil - coroutine.resume(co, ...) + coroutine.resume(co, params) end --- 发布任务 ---@parma name string -function m:task(name, ...) +function m.task(name, params) local _, main = coroutine.running() if main then - error('不能在主协程中发布任务') + error('不能在主线程中发布任务') end - for _, brave in ipairs(self.braves) do - if self:isIdle(brave) then - return self:pushTask(brave, name, ...) + for _, brave in ipairs(m.braves) do + if m.isIdle(brave) then + return m.pushTask(brave, name, params) end end end +--- 接收反馈 +function m.recieve() + local _, main = coroutine.running() + if main then + error('不能在主线程中接收反馈') + end + for _, brave in ipairs(m.braves) do + local suc, id, result + if not suc then + goto CONTINUE + end + ::CONTINUE:: + end +end + return m |