-
Notifications
You must be signed in to change notification settings - Fork 12
/
bot.lua
63 lines (50 loc) · 1.52 KB
/
bot.lua
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
--
-- Bot for story playing
local bot = { }
--- Play a story by bot
-- @param story Story: a story instance
-- @param instructor function: function that will be return the answer index
-- @param params.print boolean: print a game log to console or not, false by default
-- @return string: a log of the game
function bot.play(story, instructor, params)
local params = params or { print = false }
local log = { }
local step = 1
local function output(text)
if params.print then print(text) end
table.insert(log, text)
end
story:begin()
while story:can_continue() or story:can_choose() do
local paragraphs = story:continue()
for _, paragraph in ipairs(paragraphs or { }) do
local text = paragraph.text or ''
if paragraph.tags then
local hashtag = #text > 0 and ' #' or '#'
text = text .. hashtag .. table.concat(paragraph.tags, ' #')
end
output(text)
end
if not story:can_choose() then break end
local choices = story:get_choices()
local answer = instructor(choices, step)
step = step + 1
-- Check for a signal to emergency exit
if answer == -1 then
return nil
end
output('')
for i, choice in ipairs(choices) do
local prefix = (i == answer and '>' or i) .. ') '
local text = prefix .. choice.text
if choice.tags then
text = text .. ' #' .. table.concat(choice.tags, ' #')
end
output(text)
end
output('')
story:choose(answer)
end
return table.concat(log, '\n')
end
return bot