manage only really existing quests; preparations for deleting quests

This commit is contained in:
Sokomine 2023-09-16 05:38:33 +02:00
parent 31f29beecd
commit e879e1e67a
2 changed files with 64 additions and 20 deletions

View File

@ -7,10 +7,11 @@ yl_speak_up.input_fs_manage_quests_add_new_entry = function(pname, entry_name)
"to players who search for one.",
"Enter a short description here describing what the quest is about.",
"Room for comments/notes")
-- TODO: might make sense to show the error message somewhere
-- might make sense to show the error message somewhere
if(res ~= "OK") then
return -1
return res
end
-- the new entry will be somewhere in it
local quest_list = yl_speak_up.get_sorted_quest_list(pname)
return table.indexof(quest_list, entry_name)
end
@ -18,9 +19,9 @@ end
-- helper functions for yl_speak_up.input_fs_manage_quests(..)
-- returns a text describing if deleting the quest worked
yl_speak_up.input_fs_manage_quests_del_old_entry = function(pname, entry_name)
return "NOT IMPLEMENTED YET"
-- delete (empty) variable
-- return yl_speak_up.del_quest_variable(pname, entry_name, nil)
-- get q_id from entry_name
local q_id = yl_speak_up.get_quest_id_by_var_name(entry_name, pname)
return yl_speak_up.del_quest(q_id, pname)
end
-- helper functions for yl_speak_up.input_fs_manage_quests(..)

View File

@ -1,4 +1,18 @@
-- general helper functions
-- TODO: not yet used
yl_speak_up.count_table_elements = function(table)
if(not(table) or type(table) ~= "table") then
return -1
end
local c = 0
for k, v in pairs(table) do
c = c + 1
end
return c
end
-- just some handling of variables
-- TODO: mark some vars as "need to be saved" while others are less important (i.e. timestamps)
@ -575,7 +589,7 @@ yl_speak_up.load_quest = function(q_id)
return
end
yl_speak_up.quests[q_id] = yl_speak_up.handle_json_nil_values(data)
return
return yl_speak_up.quests[q_id]
end
@ -693,11 +707,17 @@ end
-- delete a quest if possible
yl_speak_up.del_quest = function(q_id, pname)
if(not(q_id)) then
return "No quest ID given. Quest not found."
end
local quest = yl_speak_up.load_quest(q_id)
if(not(data)) then
if(not(quest)) then
return "Quest "..tostring(q_id).." does not exist."
end
if(quest.owner ~= pname) then
if(quest.owner ~= pname
and not(minetest.check_player_privs(pname, {npc_master=true}))
and not(minetest.check_player_privs(pname, {npc_talk_master=true}))
and not(minetest.check_player_privs(pname, {npc_talk_admin=true}))) then
return "Quest "..tostring(q_id).." is owned by "..tostring(quest.owner)..
".\n You can't delete it."
end
@ -710,20 +730,23 @@ yl_speak_up.del_quest = function(q_id, pname)
table.concat(quest.subquests, ", ")..
".\nPlease remove the subquests first!"
end
local quest_data = yl_speak_up.get_variable_metadata(k_long, "quest_data", true) or {}
if(quest_data and quest_data.steps and #quest_data.steps > 2) then
return "Quest "..tostring(q_id).." contains more than two steps:\n"..
table.concat(quest_data.steps, ", ")..
".\nPlease remove all steps apart from \"start\" and \"finish\" first!"
for k, v in pairs(quest.step_data) do
if(v) then
return "Quest "..tostring(q_id).." contains at least one remaining quest step.\n"..
"Please remove all steps first!"
end
end
-- TODO: actually delete the file?
-- TODO: set the quest variable back to no type
-- TODO: delete quest variable?
-- TODO: delete (empty?) quest variable? yl_speak_up.del_quest_variable(pname, entry_name, nil)
return "OK"
end
-- returns a list of all quest IDs to which the player has write access
-- TODO: function is unused
yl_speak_up.get_quest_owner_list = function(pname)
local var_list = yl_speak_up.get_quest_variables_with_write_access(pname)
local quest_id_list = {}
@ -732,7 +755,12 @@ yl_speak_up.get_quest_owner_list = function(pname)
if(t and t == "quest") then
local data = yl_speak_up.get_variable_metadata(var_name, "quest_data", true)
if(data and data["quest_nr"]) then
table.insert(quest_id_list, "q_"..tostring(data["quest_nr"]))
local q_id = "q_"..tostring(data["quest_nr"])
yl_speak_up.load_quest(q_id)
-- offer the quest only if it was loaded successfully
if(yl_speak_up.quests[q_id]) then
table.insert(quest_id_list, q_id)
end
end
end
end
@ -741,12 +769,19 @@ end
yl_speak_up.get_sorted_quest_list = function(pname)
local var_list = yl_speak_up.get_quest_variables_with_write_access(pname)
local quest_list = {}
for i, var_name in ipairs(var_list) do
local t = yl_speak_up.get_variable_metadata(var_name, "var_type")
if(t and t == "quest") then
table.insert(quest_list, var_name)
local has_privs = (minetest.check_player_privs(pname, {npc_master=true})
or minetest.check_player_privs(pname, {npc_talk_master=true})
or minetest.check_player_privs(pname, {npc_talk_admin=true}))
for q_id, data in pairs(yl_speak_up.quests) do
if(data and data.var_name) then
if(has_privs
or(data.owner and data.owner == pname)
or(table.indexof(
yl_speak_up.get_access_list_for_var(
data.var_name, pname, "write_access") ~= -1))) then
table.insert(quest_list, data.var_name)
end
end
end
yl_speak_up.strip_pname_from_varlist(quest_list, pname)
@ -766,6 +801,14 @@ yl_speak_up.get_quest_id_by_var_name = function(var_name, owner_name)
return q_id
end
end
-- TODO or we may have a leftover variable with no quest information stored
-- local var_type = yl_speak_up.get_variable_metadata(var_name, "var_type")
-- if("var_type" == "quest") then
-- -- this is no longer a quest variable - the quest is long gone
-- yl_speak_up.set_variable_metadata(var_name, owner_name, "var_type", nil, "string")
-- yl_speak_up.set_variable_metadata(var_name, owner_name, "quest_data", "quest_nr", nil)
-- yl_speak_up.set_variable_metadata(var_name, owner_name, "quest_data", "steps", nil)
-- end
return nil
end