summaryrefslogtreecommitdiff
path: root/server-beta/src/pub/client.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server-beta/src/pub/client.lua')
-rw-r--r--server-beta/src/pub/client.lua46
1 files changed, 31 insertions, 15 deletions
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