1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
local thread = require 'bee.thread'
local utility = require 'utility'
local task = require 'task'
local errLog = thread.channel 'errlog'
local type = type
local braveTemplate = [[
package.path = %q
package.cpath = %q
log = require 'brave.log'
local brave = require 'brave'
brave.register(%d)
]]
---@class pub
local m = {}
m.type = 'pub'
m.braves = {}
m.ability = {}
m.taskList = {}
--- 注册酒馆的功能
function m.on(name, callback)
m.ability[name] = callback
end
--- 招募勇者,勇者会从公告板上领取任务,完成任务后到看板娘处交付任务
---@param num integer
function m.recruitBraves(num)
for _ = 1, num do
local id = #m.braves + 1
log.info('Create brave:', id)
thread.newchannel('taskpad' .. id)
thread.newchannel('waiter' .. id)
m.braves[id] = {
id = id,
taskpad = thread.channel('taskpad' .. id),
waiter = thread.channel('waiter' .. id),
thread = thread.thread(braveTemplate:format(
package.path,
package.cpath,
id
)),
taskList = {},
counter = utility.counter(),
currentTask = nil,
}
end
end
--- 勇者是否有空
function m.isIdle(brave)
return next(brave.taskList) == nil
end
--- 给勇者推送任务
function m.pushTask(brave, name, params)
local taskID = brave.counter()
brave.taskpad:push(name, taskID, params)
return task.wait(function (waker)
brave.taskList[taskID] = waker
end)
end
--- 从勇者处接收任务反馈
function m.popTask(brave, id, result)
local waker = brave.taskList[id]
if not waker then
log.warn(('Brave pushed unknown task result: # %d => [%d]'):format(brave.id, id))
return
end
brave.taskList[id] = nil
m.checkWaitingTask(brave)
waker(result)
end
--- 从勇者处接收报告
function m.popReport(brave, name, params)
local abil = m.ability[name]
if not abil then
log.warn(('Brave pushed unknown report: # %d => %q'):format(brave.id, name))
return
end
abil(params, brave)
end
--- 发布任务
---@parma name string
function m.task(name, params)
for _, brave in ipairs(m.braves) do
if m.isIdle(brave) then
return m.pushTask(brave, name, params)
end
end
-- 如果所有勇者都在战斗,那么把任务缓存到队列里
-- 当有勇者提交任务反馈后,尝试把按顺序将堆积任务
-- 交给该勇者
m.taskList[#m.taskList+1] = function (brave)
return m.pushTask(brave, name, params)
end
end
--- 检查堆积任务
function m.checkWaitingTask(brave)
if #m.taskList == 0 then
return
end
-- 如果勇者还有其他活要忙,那么让他继续忙去吧
if next(brave.taskList) then
return
end
local waiting = table.remove(m.taskList, 1)
waiting(brave)
end
--- 接收反馈
---|返回接收到的反馈数量
---@return integer
function m.recieve()
for _, brave in ipairs(m.braves) do
local suc, id, result = brave.waiter:pop()
if not suc then
goto CONTINUE
end
if type(id) == 'string' then
m.popReport(brave, id, result)
else
m.popTask(brave, id, result)
end
task.sleep(0)
::CONTINUE::
end
end
--- 检查伤亡情况
function m.checkDead()
while true do
local suc, err = errLog:pop()
if not suc then
break
end
log.error('Brave is dead!: ' .. err)
end
end
return m
|