local STORE = require("pasta.store")
local WORD = require("pasta.word")
local log = require "@pasta_log"
local ACTOR = {}
local ACTOR_IMPL = {}
ACTOR_IMPL.__index = ACTOR_IMPL
local ACTOR_WORD_BUILDER_IMPL = {}
ACTOR_WORD_BUILDER_IMPL.__index = ACTOR_WORD_BUILDER_IMPL
function ACTOR_WORD_BUILDER_IMPL.entry(self, ...)
local values = { ... }
if #values > 0 then
self._word_builder:entry(...)
end
return self
end
function ACTOR_IMPL.create_word(self, key)
local builder = {
_word_builder = WORD.create_actor(self.name, key),
}
return setmetatable(builder, ACTOR_WORD_BUILDER_IMPL)
end
function ACTOR.get_or_create(name)
if not STORE.actors[name] then
local actor = {
name = name,
spot = nil,
}
setmetatable(actor, ACTOR_IMPL)
STORE.actors[name] = actor
end
return STORE.actors[name]
end
local PROXY_IMPL = {}
PROXY_IMPL.__index = PROXY_IMPL
function ACTOR.create_proxy(actor, act)
local proxy = {
actor = actor,
act = act,
}
return setmetatable(proxy, PROXY_IMPL)
end
function PROXY_IMPL.talk(self, text)
self.act:talk(self.actor, text)
end
function PROXY_IMPL.sakura_script(self, text)
self.act:sakura_script(self.actor, text)
end
function PROXY_IMPL.find_actor_handler(self, mode, key)
if mode ~= "word" then
return nil
end
local actor_value = self.actor[key]
if actor_value ~= nil then
return actor_value
end
local ok, SEARCH = pcall(require, "@pasta_search")
if ok and SEARCH then
local actor_scope = "__actor_" .. self.actor.name .. "__"
local result = SEARCH:search_word(key, actor_scope)
if result ~= nil then return result end
end
return nil
end
function PROXY_IMPL.find_handler(self, mode, key)
local handler = self:find_actor_handler(mode, key)
if handler ~= nil then
return handler
end
return self.act:find_act_handler(mode, key)
end
function PROXY_IMPL.expr_fn(self, key, ...)
local handler = self:find_handler("expr", key)
if type(handler) == "function" then
return handler(self, ...)
end
log.warn(string.format("proxy:expr_fn - handler not found: key='%s', mode='expr', via=proxy(%s)",
tostring(key), tostring(self.actor.name)))
return nil
end
function PROXY_IMPL.word(self, name)
if not name or name == "" then
return nil
end
local handler = self:find_handler("word", name)
if handler == nil then
log.warn(string.format("proxy:word - handler not found: key='%s', mode='word', via=proxy(%s)",
tostring(name), tostring(self.actor.name)))
return nil
end
if type(handler) == "function" then
return handler(self)
end
return tostring(handler)
end
for name, actor in pairs(STORE.actors) do
if type(actor) == "table" then
if actor.name == nil then
actor.name = name
end
setmetatable(actor, ACTOR_IMPL)
end
end
return ACTOR