mirror of
https://gitea.your-land.de/Sokomine/yl_speak_up.git
synced 2025-06-16 13:48:06 +02:00
added yl_speak_up.set_variable_metadata for cleaner setting of metadata
This commit is contained in:
parent
372a37f8e6
commit
9856842074
@ -646,27 +646,34 @@ end
|
||||
-- if true: grant access
|
||||
-- returns true if the variable was found
|
||||
yl_speak_up.manage_access_to_quest_variable = function(k, pname, grant_to_pname, what_to_grant, do_grant)
|
||||
k = yl_speak_up.add_pname_to_var(k, pname)
|
||||
-- grant or revoke priv
|
||||
if(not(do_grant)) then
|
||||
do_grant = nil
|
||||
end
|
||||
-- only read and write access can be granted
|
||||
if(not(what_to_grant) or (what_to_grant ~= "read_access" and what_to_grant ~= "write_access")) then
|
||||
return false
|
||||
end
|
||||
return yl_speak_up.set_variable_metadata(k, pname, what_to_grant, grant_to_pname, do_grant)
|
||||
end
|
||||
|
||||
|
||||
-- a more general way of setting metadata for variables
|
||||
yl_speak_up.set_variable_metadata = function(k, pname, meta_name, entry_name, new_value)
|
||||
k = yl_speak_up.add_pname_to_var(k, pname)
|
||||
-- delete/unset
|
||||
if(not(new_value)) then
|
||||
new_value = nil
|
||||
end
|
||||
-- the variable needs to exist
|
||||
if(not(yl_speak_up.player_vars[ k ])) then
|
||||
return false
|
||||
end
|
||||
-- make sure all the necessary tables exist
|
||||
if(not(yl_speak_up.player_vars[ k ][ "$META$" ])) then
|
||||
yl_speak_up.player_vars[ k ][ "$META$"] = { what_to_grant = {} }
|
||||
if( not(yl_speak_up.player_vars[ k ][ "$META$" ])) then
|
||||
yl_speak_up.player_vars[ k ][ "$META$" ] = { meta_name = {} }
|
||||
end
|
||||
if(not(yl_speak_up.player_vars[ k ][ "$META$" ][ what_to_grant ])) then
|
||||
yl_speak_up.player_vars[ k ][ "$META$" ][ what_to_grant ] = {}
|
||||
if( not(yl_speak_up.player_vars[ k ][ "$META$" ][ meta_name ])
|
||||
or type(yl_speak_up.player_vars[ k ][ "$META$" ][ meta_name ]) ~= "table") then
|
||||
yl_speak_up.player_vars[ k ][ "$META$" ][ meta_name ] = {}
|
||||
end
|
||||
yl_speak_up.player_vars[ k ][ "$META$"][ what_to_grant ][ grant_to_pname ] = do_grant
|
||||
yl_speak_up.player_vars[ k ][ "$META$"][ meta_name ][ entry_name ] = new_value
|
||||
yl_speak_up.save_quest_variables(true)
|
||||
return true
|
||||
end
|
||||
@ -768,46 +775,20 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
|
||||
last_modified = os.date(),
|
||||
}
|
||||
|
||||
-- make sure all variables have a metadata field
|
||||
-- delete all old entries that are not longer needed
|
||||
for k, v in pairs(yl_speak_up.player_vars) do
|
||||
if( yl_speak_up.player_vars[ k ][ "$META$" ]
|
||||
and yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc
|
||||
and type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) == "table"
|
||||
and yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc[ n_id ]) then
|
||||
-- delete old entries (the correct new ones will be set shortly after this)
|
||||
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = nil
|
||||
if(not(variables_p[ k ]) and not(variables_e[ k ])) then
|
||||
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, false)
|
||||
end
|
||||
end
|
||||
|
||||
-- save in the variables' metadata which NPC uses it
|
||||
-- (this is what we're mostly after - know which variable is used in which NPC)
|
||||
for k, v in pairs(variables_p) do
|
||||
-- if the variable does not exist: create it
|
||||
if(not(yl_speak_up.player_vars[ k ])) then
|
||||
yl_speak_up.player_vars[ k ] = {}
|
||||
end
|
||||
if(not(yl_speak_up.player_vars[ k ][ "$META$" ])) then
|
||||
yl_speak_up.player_vars[ k ][ "$META$"] = { used_by_npc = {} }
|
||||
end
|
||||
if(not(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc)
|
||||
or type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) ~= "table") then
|
||||
yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc = {}
|
||||
end
|
||||
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = true
|
||||
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true)
|
||||
end
|
||||
for k, v in pairs(variables_e) do
|
||||
-- if the variable does not exist: create it
|
||||
if(not(yl_speak_up.player_vars[ k ])) then
|
||||
yl_speak_up.player_vars[ k ] = {}
|
||||
end
|
||||
if(not(yl_speak_up.player_vars[ k ][ "$META$" ])) then
|
||||
yl_speak_up.player_vars[ k ][ "$META$"] = { used_by_npc = {} }
|
||||
end
|
||||
if(not(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc)
|
||||
or type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) ~= "table") then
|
||||
yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc = {}
|
||||
end
|
||||
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = true
|
||||
yl_speak_up.set_variable_metadata(k, pname, "used_by_npc", n_id, true)
|
||||
end
|
||||
-- force writing the data
|
||||
yl_speak_up.save_quest_variables(true)
|
||||
|
Loading…
Reference in New Issue
Block a user