Skip to content

Commit

Permalink
属性系统支持复合属性
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed May 30, 2024
1 parent c5061c4 commit d2e901a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions server/business/attr/attribute_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ end
-------------------------------------------------------------------------
--属性回写
function AttributeAgent:on_attr_writeback(player, player_id)
local share_attrs = player:get_share_attrs()
local share_attrs = player:load_share_attrs()
local ok, code = player:call_lobby("rpc_attr_writeback", share_attrs, quanta.service)
if qfailed(code, ok) then
player:merge_share_attrs(share_attrs)
log_err("[AttributeAgent][on_attr_writeback] writeback failed attrs={}, player={}, code={}", share_attrs, player_id, code)
end
player:set_write_attrs({})
end

--rpc协议
Expand Down
17 changes: 13 additions & 4 deletions server/business/component/attr_component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ local log_debug = logger.debug
local tinsert = table.insert
local supper = string.upper
local sformat = string.format
local qmerge = qtable.merge

local event_mgr = quanta.get("event_mgr")
local config_mgr = quanta.get("config_mgr")
Expand Down Expand Up @@ -185,10 +184,12 @@ function AttrComponent:watch_attr(trigger, attr_id, handler)
self:watch_event(trigger, sformat("on_attr_changed_%s", attr_id), handler)
end

--取消观察属性
function AttrComponent:unwatch_attr(trigger, attr_id)
self:unwatch_event(trigger, sformat("on_attr_changed_%s", attr_id))
end

--属性变化处理
function AttrComponent:on_attr_changed(attr_id, attr, value, oldval, service_id)
if self:is_load_success() then
if (not service_id) and attr.share then
Expand Down Expand Up @@ -284,6 +285,7 @@ function AttrComponent:load_attrs(attrs)
end
end

--收集全量共享属性
function AttrComponent:collect_share_attrs(service_name)
local attrs = {}
for attr_id, value in pairs(attrs) do
Expand All @@ -295,17 +297,23 @@ function AttrComponent:collect_share_attrs(service_name)
return attrs
end

--获取变化的共享属性
function AttrComponent:load_share_attrs()
local attrs = self.share_attrs
self.share_attrs = {}
return attrs
end

--合并变化的共享属性
function AttrComponent:merge_share_attrs(attrs)
local nattrs = self.share_attrs
self.share_attrs = qmerge(attrs, nattrs)
for attr_id, args in pairs(attrs) do
if not self.share_attrs[attr_id] then
self.share_attrs[attr_id] = args
end
end
end

--编码属性
function AttrComponent:encode_attr(attr_id, attr)
local value = self.attrs[attr_id]
if attr.type == "int" then
Expand All @@ -320,7 +328,7 @@ function AttrComponent:encode_attr(attr_id, attr)
return { attr_id = attr_id, attr_b = value }
end

--package_attrs
--打包需要同步的属性
function AttrComponent:package_attrs(range)
local attrs = {}
for attr_id, attr in pairs(self.attr_set) do
Expand All @@ -331,6 +339,7 @@ function AttrComponent:package_attrs(range)
return attrs
end

--属性更新处理
function AttrComponent:on_attr_update()
local attrs, battrs = {}, {}
for attr_id, attr in pairs(self.sync_attrs) do
Expand Down

0 comments on commit d2e901a

Please sign in to comment.