moved some functions from quest_api.lua into fs_manage_variables.lua

This commit is contained in:
Sokomine 2021-07-31 21:46:12 +02:00
parent 742ec53a6b
commit 23da9c8a27
3 changed files with 371 additions and 370 deletions

369
fs_manage_variables.lua Normal file
View File

@ -0,0 +1,369 @@
yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
local pname = player:get_player_name()
if(fields and fields.back_from_msg) then
yl_speak_up.show_fs(player, "manage_variables")
return
end
-- leave this formspec
if(fields and (fields.quit or fields.exit or fields.back)) then
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.show_fs(player, last_fs)
return
-- add a new variable?
elseif(fields and fields.add_variable) then
local error_msg = ""
if(not(fields.add_variable_name) or fields.add_variable_name == ""
or fields.add_variable_name:trim() == "") then
error_msg = "Please enter the name of your variable!"
-- limit names to something more sensible
elseif(string.len(fields.add_variable_name) > 30) then
error_msg = "The name of your variable name is too long.\n"..
"Only up to 30 characters are allowed."
elseif(string.len(fields.add_variable_name:trim()) < 2) then
error_msg = "The name of your variable name is too short.\n"..
"It has to be at least 2 characters long."
else
fields.add_variable_name = fields.add_variable_name:trim()
local res = yl_speak_up.add_quest_variable(pname, fields.add_variable_name)
-- not really an error msg here - but fascilitates output
error_msg = "A new variable named\n \""..
minetest.formspec_escape(fields.add_variable_name)..
"\"\nhas been created."
if(not(res)) then
error_msg = "Failed to create variable named\n \""..
minetest.formspec_escape(fields.add_variable_name).."\"."
else
-- slect this new variable
local var_list = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(var_list, pname)
table.sort(var_list)
local index = table.indexof(var_list, fields.add_variable_name)
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
end
end
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- show where this variable is used
elseif(fields and fields.show_var_usage and fields.show_var_usage ~= "") then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = yl_speak_up.get_list_of_usage_of_variable(
fields.list_var_names, pname, true,
"back_from_msg",
"Back to manage variables",
-- not an internal variable
false)
})
return
end
-- which variable is currently selected?
local var_list = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(var_list, pname)
table.sort(var_list)
-- a var name was selected in the dropdown list
if(fields and fields.list_var_names and fields.list_var_names ~= "") then
local index = table.indexof(var_list, fields.list_var_names)
if(fields.list_var_names == "Add variable:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
end
end
local var_name = var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1]
if(not(var_name)) then
var_name = ""
end
-- delete (empty) variable
if(fields
and ((fields.delete_variable and fields.delete_variable ~= "")
or (fields.delete_unused_variable and fields.delete_unused_variable ~= ""))
and var_name) then
local text = yl_speak_up.del_quest_variable(pname, var_name, nil)
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[10,2]"..
"label[0.2,0.0;Trying to delete variable \""..
minetest.formspec_escape(tostring(var_name))..
"\":\n"..text.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- revoke read or write access to a variable
elseif(fields
and ((fields.revoke_player_var_read_access and fields.revoke_player_var_read_access ~= "")
or (fields.revoke_player_var_write_access and fields.revoke_player_var_write_access ~= ""))
and var_name) then
local right = "read"
if(fields.revoke_player_var_write_access and fields.revoke_player_var_write_access ~= "") then
right = "write"
end
-- which player are we talking about?
local selected = yl_speak_up.speak_to[pname]["tmp_index_var_"..right.."_access"]
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
local tmp_list = {}
for k, v in pairs(pl_with_access) do
table.insert(tmp_list, k)
end
table.sort(tmp_list)
local grant_to = ""
if(selected > 1) then
grant_to = tmp_list[ selected-1 ]
end
local error_msg = ""
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
if(not(grant_to) or grant_to == "") then
error_msg = "For which player do you want to revoke "..right.." access?"
elseif(pname ~= yl_speak_up.npc_owner[ n_id ]
and not(minetest.check_player_privs(pname, {npc_talk_master=true}))) then
error_msg = "Only the owner of the NPC or players with\n"..
"the npc_talk_master priv can change this."
elseif(not(pl_with_access[ grant_to ])) then
error_msg = minetest.formspec_escape(grant_to).." doesn't have "..right..
" access\nto this variable. Nothing changed."
-- try to revoke access
elseif(not(yl_speak_up.manage_access_to_quest_variable(var_name, pname, grant_to,
right.."_access", nil))) then
error_msg = "An internal error occoured."
else
-- not really an error message here...rather a success message
error_msg = "Revoked "..right.." access to variable\n\""..
minetest.formspec_escape(var_name)..
"\"\nfor player "..minetest.formspec_escape(grant_to)..".\n"..
"Note: This will *not* affect existing preconditions/effects!"
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- grant read or write access to a variable
elseif(fields
and ((fields.grant_player_var_read_access and fields.grant_player_var_read_access ~= "")
or (fields.grant_player_var_write_access and fields.grant_player_var_write_access ~= ""))
and var_name) then
local right = "read"
if(fields.grant_player_var_write_access and fields.grant_player_var_write_access ~= "") then
right = "write"
end
local grant_to = fields[ "grant_player_var_"..right.."_access"]
local error_msg = ""
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
if(pname ~= yl_speak_up.npc_owner[ n_id ]
and not(minetest.check_player_privs(pname, {npc_talk_master=true}))) then
error_msg = "Only the owner of the NPC or players with\n"..
"the npc_talk_master priv can change this."
elseif(grant_to == pname) then
error_msg = "You already have "..right.." access to this variable."
elseif(pl_with_access[ grant_to ]) then
error_msg = minetest.formspec_escape(grant_to).." already has "..right..
" access\nto this variable."
elseif(not(minetest.check_player_privs(grant_to, {interact=true}))) then
error_msg = "Player \""..minetest.formspec_escape(grant_to).."\" not found."
-- try to grant access
elseif(not(yl_speak_up.manage_access_to_quest_variable(var_name, pname, grant_to,
right.."_access", true))) then
error_msg = "An internal error occoured."
else
-- not really an error message here...rather a success message
error_msg = "Granted "..right.." access to variable\n\""..
minetest.formspec_escape(var_name)..
"\"\nto player "..minetest.formspec_escape(grant_to).."."
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- the player clicked on a name in the list of players with read or write access
elseif(fields
and ((fields.list_var_read_access and fields.list_var_read_access ~= "")
or (fields.list_var_write_access and fields.list_var_write_access ~= ""))
and var_name) then
local right = "read"
if(fields.list_var_write_access and fields.list_var_write_access ~= "") then
right = "write"
end
local selected = fields[ "list_var_"..right.."_access"]
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
local tmp_list = {}
for k, v in pairs(pl_with_access) do
table.insert(tmp_list, k)
end
table.sort(tmp_list)
local index = table.indexof(tmp_list, selected)
if(selected == "Add player:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname]["tmp_index_var_"..right.."_access"] = index + 1
end
yl_speak_up.show_fs(player, "manage_variables")
return
-- a var name was selected in the dropdown list
elseif(fields and fields.list_var_names and fields.list_var_names ~= "") then
-- show the same formspec again, with a diffrent variable selected
yl_speak_up.show_fs(player, "manage_variables")
return
end
-- try to go back to the last formspec shown before this one
if(not(yl_speak_up.speak_to[pname])) then
return
end
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.show_fs(player, last_fs)
end
yl_speak_up.get_fs_manage_variables = function(player, param)
local pname = player:get_player_name()
-- variables owned by the player - including those with write access
local var_list = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(var_list, pname)
-- the yl_speak_up.create_dropdown_playerlist function needs a table - not a list
local table_of_vars = {}
for i, k in ipairs(var_list) do
table_of_vars[ k ] = true
end
-- "Add variable:" is currently selected
local additional_buttons = ""
if(not(yl_speak_up.speak_to[pname].tmp_index_variable)
or yl_speak_up.speak_to[pname].tmp_index_variable == 1
or not(var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1])) then
yl_speak_up.speak_to[pname].tmp_index_variable = 1
additional_buttons = "button[11.4,1.9;2.5,0.9;add_variable;Create variable]"..
"tooltip[add_variable;Create a new varialbe with the name\n"..
"you entered in the field to the left.]"
else
-- index 1 is "Add variable:"
local k = var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1]
local pl_with_read_access = yl_speak_up.get_access_list_for_var(k, pname, "read_access")
local pl_with_write_access = yl_speak_up.get_access_list_for_var(k, pname, "write_access")
local add_read_button = ""
local add_write_button = ""
if(not(yl_speak_up.speak_to[pname].tmp_index_var_read_access)
or yl_speak_up.speak_to[pname].tmp_index_var_read_access == 1) then
yl_speak_up.speak_to[pname].tmp_index_var_read_access = 1
add_read_button = "button[12.9,2.9;1.0,0.9;add_read_access;Add]"..
"tooltip[add_read_access;Grant the player whose name you entered\n"..
"you entered in the field to the left read access\n"..
"to your variable.]"
end
if(not(yl_speak_up.speak_to[pname].tmp_index_var_write_access)
or yl_speak_up.speak_to[pname].tmp_index_var_write_access == 1) then
yl_speak_up.speak_to[pname].tmp_index_var_write_access = 1
add_write_button = "button[12.9,3.9;1.0,0.9;add_write_access;Add]"..
"tooltip[add_write_access;Grant the player whose name you entered\n"..
"you entered in the field to the left *write* access\n"..
"to your variable.]"
end
local list_of_npc_users = "- none -"
local list_of_node_pos_users = "- none -"
-- expand name of variable k again
local k_long = yl_speak_up.add_pname_to_var(k, pname)
-- which npc and which node_pos use this variable? create a list for display
local c1 = 0
local c2 = 0
if(k_long
and yl_speak_up.player_vars[ k_long ]
and yl_speak_up.player_vars[ k_long ][ "$META$"]) then
local npc_users = yl_speak_up.get_variable_metadata(k_long, "used_by_npc")
c1 = #npc_users
if(npc_users and c1 > 0) then
list_of_npc_users = minetest.formspec_escape(table.concat(npc_users, ", "))
end
local node_pos_users = yl_speak_up.get_variable_metadata(k_long, "used_by_node_pos")
c2 = #node_pos_users
if(node_pos_users and c2 > 0) then
list_of_node_pos_users = minetest.formspec_escape(table.concat(
node_pos_users, ", "))
end
end
additional_buttons = "button[11.4,1.9;2.5,0.9;show_var_usage;Where is it used?]"..
"tooltip[show_var_usage;Show which NPC use this variable in which context.]"..
-- offer a dropdown list and a text input field for new varialbe names for adding
"label[0.2,3.05;Players with read access to this variable:]"..
yl_speak_up.create_dropdown_playerlist(player, pname,
pl_with_read_access,
yl_speak_up.speak_to[pname].tmp_index_var_read_access,
5.5, 2.9, 0.0, "list_var_read_access", "player", "Remove player from list",
"grant_player_var_read_access",
"Enter the name of the player that shall\n"..
"have read access to this variable.",
"revoke_player_var_read_access",
"If you click here, the selected player\n"..
"will no longer be able to add new\n"..
"pre(C)onditions which read your variable."
)..add_read_button..
"label[0.2,4.05;Players with *write* access to this variable:]"..
yl_speak_up.create_dropdown_playerlist(player, pname,
pl_with_write_access,
yl_speak_up.speak_to[pname].tmp_index_var_write_access,
5.5, 3.9, 0.0, "list_var_write_access", "player", "Remove player from list",
"grant_player_var_write_access",
"Enter the name of the player that shall\n"..
"have *write* access to this variable.",
"revoke_player_var_write_access",
"If you click here, the selected player\n"..
"will no longer be able to *write* new\n"..
"values into this variable."
)..add_write_button..
"label[0.2,5.05;Type of variable: "..
-- show variable type
minetest.colorize("#FFFF00",
(yl_speak_up.get_variable_metadata(k_long, "var_type")
or "String/text or numerical value, depending on how you use it"))..
".]"..
"label[0.2,6.05;This variable is used by the following "..
minetest.colorize("#FFFF00", tostring(c1)).." NPC:\n\t"..
-- those are only n_id - no need to formspec_escape that
minetest.colorize("#FFFF00", list_of_npc_users)..".]"..
"label[0.2,7.05;This variable is used by the following "..
minetest.colorize("#FFFF00", tostring(c2)).." node positions:\n\t"..
-- those are only pos_to_string(..) - no need to formspec_escape that
minetest.colorize("#FFFF00", list_of_node_pos_users)..".]"
end
return "size[16,8.5]"..
"label[5.0,0.0;* Manage your variables *]"..
"label[0.2,0.8;Note: Each variable will store a diffrent value for each player who "..
"interacts with the NPC.\n"..
"You can grant read and write access to other players for your "..
"variables so that they can also use them as well.]"..
"label[0.2,2.05;Your variables:]"..
-- offer a dropdown list and a text input field for new varialbe names for adding
yl_speak_up.create_dropdown_playerlist(player, pname,
table_of_vars, yl_speak_up.speak_to[pname].tmp_index_variable,
2.2, 1.9, 1.0, "list_var_names", "variable", "Delete selected variable",
"add_variable_name",
"Enter the name of the new variable you\n"..
"want to create.",
"delete_variable",
"If you click here, the selected variable\n"..
"will be deleted."
)..
additional_buttons..
"button[0.0,0.2;1.0,0.6;back;Back]"..
"button[6.0,8.0;1.0,0.6;back;Back]"
end

View File

@ -51,6 +51,8 @@ dofile(modpath .. "inventory.lua")
dofile(modpath .. "trade_simple.lua")
-- easily accessible list of all trades the NPC offers
dofile(modpath .. "trade_list.lua")
-- manage quest variables: add, delete, manage access rights etc.
dofile(modpath .. "fs_manage_variables.lua")
-- handle variables for quests for player-owned NPC
dofile(modpath .. "quest_api.lua")
-- editing the npc with the staff:

View File

@ -1,8 +1,6 @@
-- just some handling of variables
-- TODO: handle read (and write?) access for other players
-- TODO: add a function to check if the player has read/write access
-- TODO: mark some vars as "need to be saved" while others are less important (i.e. timestamps)
-- the keys are of the form:
@ -202,374 +200,6 @@ yl_speak_up.get_quest_variables_with_write_access = function(pname)
end
yl_speak_up.input_fs_manage_variables = function(player, formname, fields)
local pname = player:get_player_name()
if(fields and fields.back_from_msg) then
yl_speak_up.show_fs(player, "manage_variables")
return
end
-- leave this formspec
if(fields and (fields.quit or fields.exit or fields.back)) then
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.show_fs(player, last_fs)
return
-- add a new variable?
elseif(fields and fields.add_variable) then
local error_msg = ""
if(not(fields.add_variable_name) or fields.add_variable_name == ""
or fields.add_variable_name:trim() == "") then
error_msg = "Please enter the name of your variable!"
-- limit names to something more sensible
elseif(string.len(fields.add_variable_name) > 30) then
error_msg = "The name of your variable name is too long.\n"..
"Only up to 30 characters are allowed."
elseif(string.len(fields.add_variable_name:trim()) < 2) then
error_msg = "The name of your variable name is too short.\n"..
"It has to be at least 2 characters long."
else
fields.add_variable_name = fields.add_variable_name:trim()
local res = yl_speak_up.add_quest_variable(pname, fields.add_variable_name)
-- not really an error msg here - but fascilitates output
error_msg = "A new variable named\n \""..
minetest.formspec_escape(fields.add_variable_name)..
"\"\nhas been created."
if(not(res)) then
error_msg = "Failed to create variable named\n \""..
minetest.formspec_escape(fields.add_variable_name).."\"."
else
-- slect this new variable
local var_list = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(var_list, pname)
table.sort(var_list)
local index = table.indexof(var_list, fields.add_variable_name)
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
end
end
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- show where this variable is used
elseif(fields and fields.show_var_usage and fields.show_var_usage ~= "") then
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = yl_speak_up.get_list_of_usage_of_variable(
fields.list_var_names, pname, true,
"back_from_msg",
"Back to manage variables",
-- not an internal variable
false)
})
return
end
-- which variable is currently selected?
local var_list = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(var_list, pname)
table.sort(var_list)
-- a var name was selected in the dropdown list
if(fields and fields.list_var_names and fields.list_var_names ~= "") then
local index = table.indexof(var_list, fields.list_var_names)
if(fields.list_var_names == "Add variable:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname].tmp_index_variable = index + 1
end
end
local var_name = var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1]
if(not(var_name)) then
var_name = ""
end
-- delete (empty) variable
if(fields
and ((fields.delete_variable and fields.delete_variable ~= "")
or (fields.delete_unused_variable and fields.delete_unused_variable ~= ""))
and var_name) then
local text = yl_speak_up.del_quest_variable(pname, var_name, nil)
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[10,2]"..
"label[0.2,0.0;Trying to delete variable \""..
minetest.formspec_escape(tostring(var_name))..
"\":\n"..text.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- revoke read or write access to a variable
elseif(fields
and ((fields.revoke_player_var_read_access and fields.revoke_player_var_read_access ~= "")
or (fields.revoke_player_var_write_access and fields.revoke_player_var_write_access ~= ""))
and var_name) then
local right = "read"
if(fields.revoke_player_var_write_access and fields.revoke_player_var_write_access ~= "") then
right = "write"
end
-- which player are we talking about?
local selected = yl_speak_up.speak_to[pname]["tmp_index_var_"..right.."_access"]
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
local tmp_list = {}
for k, v in pairs(pl_with_access) do
table.insert(tmp_list, k)
end
table.sort(tmp_list)
local grant_to = ""
if(selected > 1) then
grant_to = tmp_list[ selected-1 ]
end
local error_msg = ""
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
if(not(grant_to) or grant_to == "") then
error_msg = "For which player do you want to revoke "..right.." access?"
elseif(pname ~= yl_speak_up.npc_owner[ n_id ]
and not(minetest.check_player_privs(pname, {npc_talk_master=true}))) then
error_msg = "Only the owner of the NPC or players with\n"..
"the npc_talk_master priv can change this."
elseif(not(pl_with_access[ grant_to ])) then
error_msg = minetest.formspec_escape(grant_to).." doesn't have "..right..
" access\nto this variable. Nothing changed."
-- try to revoke access
elseif(not(yl_speak_up.manage_access_to_quest_variable(var_name, pname, grant_to,
right.."_access", nil))) then
error_msg = "An internal error occoured."
else
-- not really an error message here...rather a success message
error_msg = "Revoked "..right.." access to variable\n\""..
minetest.formspec_escape(var_name)..
"\"\nfor player "..minetest.formspec_escape(grant_to)..".\n"..
"Note: This will *not* affect existing preconditions/effects!"
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- grant read or write access to a variable
elseif(fields
and ((fields.grant_player_var_read_access and fields.grant_player_var_read_access ~= "")
or (fields.grant_player_var_write_access and fields.grant_player_var_write_access ~= ""))
and var_name) then
local right = "read"
if(fields.grant_player_var_write_access and fields.grant_player_var_write_access ~= "") then
right = "write"
end
local grant_to = fields[ "grant_player_var_"..right.."_access"]
local error_msg = ""
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
if(pname ~= yl_speak_up.npc_owner[ n_id ]
and not(minetest.check_player_privs(pname, {npc_talk_master=true}))) then
error_msg = "Only the owner of the NPC or players with\n"..
"the npc_talk_master priv can change this."
elseif(grant_to == pname) then
error_msg = "You already have "..right.." access to this variable."
elseif(pl_with_access[ grant_to ]) then
error_msg = minetest.formspec_escape(grant_to).." already has "..right..
" access\nto this variable."
elseif(not(minetest.check_player_privs(grant_to, {interact=true}))) then
error_msg = "Player \""..minetest.formspec_escape(grant_to).."\" not found."
-- try to grant access
elseif(not(yl_speak_up.manage_access_to_quest_variable(var_name, pname, grant_to,
right.."_access", true))) then
error_msg = "An internal error occoured."
else
-- not really an error message here...rather a success message
error_msg = "Granted "..right.." access to variable\n\""..
minetest.formspec_escape(var_name)..
"\"\nto player "..minetest.formspec_escape(grant_to).."."
end
yl_speak_up.show_fs(player, "msg", {
input_to = "yl_speak_up:manage_variables",
formspec = "size[6,2]"..
"label[0.2,0.0;"..error_msg.."]"..
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
return
-- the player clicked on a name in the list of players with read or write access
elseif(fields
and ((fields.list_var_read_access and fields.list_var_read_access ~= "")
or (fields.list_var_write_access and fields.list_var_write_access ~= ""))
and var_name) then
local right = "read"
if(fields.list_var_write_access and fields.list_var_write_access ~= "") then
right = "write"
end
local selected = fields[ "list_var_"..right.."_access"]
local pl_with_access = yl_speak_up.get_access_list_for_var(var_name, pname, right.."_access")
local tmp_list = {}
for k, v in pairs(pl_with_access) do
table.insert(tmp_list, k)
end
table.sort(tmp_list)
local index = table.indexof(tmp_list, selected)
if(selected == "Add player:") then
index = 0
end
if(index and index > -1) then
yl_speak_up.speak_to[pname]["tmp_index_var_"..right.."_access"] = index + 1
end
yl_speak_up.show_fs(player, "manage_variables")
return
-- a var name was selected in the dropdown list
elseif(fields and fields.list_var_names and fields.list_var_names ~= "") then
-- show the same formspec again, with a diffrent variable selected
yl_speak_up.show_fs(player, "manage_variables")
return
end
-- try to go back to the last formspec shown before this one
if(not(yl_speak_up.speak_to[pname])) then
return
end
local last_fs = yl_speak_up.speak_to[pname][ "working_at" ]
yl_speak_up.show_fs(player, last_fs)
end
yl_speak_up.get_fs_manage_variables = function(player, param)
local pname = player:get_player_name()
-- variables owned by the player - including those with write access
local var_list = yl_speak_up.get_quest_variables(pname, true)
-- make names of own variables shorter
yl_speak_up.strip_pname_from_varlist(var_list, pname)
-- the yl_speak_up.create_dropdown_playerlist function needs a table - not a list
local table_of_vars = {}
for i, k in ipairs(var_list) do
table_of_vars[ k ] = true
end
-- "Add variable:" is currently selected
local additional_buttons = ""
if(not(yl_speak_up.speak_to[pname].tmp_index_variable)
or yl_speak_up.speak_to[pname].tmp_index_variable == 1
or not(var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1])) then
yl_speak_up.speak_to[pname].tmp_index_variable = 1
additional_buttons = "button[11.4,1.9;2.5,0.9;add_variable;Create variable]"..
"tooltip[add_variable;Create a new varialbe with the name\n"..
"you entered in the field to the left.]"
else
-- index 1 is "Add variable:"
local k = var_list[ yl_speak_up.speak_to[pname].tmp_index_variable - 1]
local pl_with_read_access = yl_speak_up.get_access_list_for_var(k, pname, "read_access")
local pl_with_write_access = yl_speak_up.get_access_list_for_var(k, pname, "write_access")
local add_read_button = ""
local add_write_button = ""
if(not(yl_speak_up.speak_to[pname].tmp_index_var_read_access)
or yl_speak_up.speak_to[pname].tmp_index_var_read_access == 1) then
yl_speak_up.speak_to[pname].tmp_index_var_read_access = 1
add_read_button = "button[12.9,2.9;1.0,0.9;add_read_access;Add]"..
"tooltip[add_read_access;Grant the player whose name you entered\n"..
"you entered in the field to the left read access\n"..
"to your variable.]"
end
if(not(yl_speak_up.speak_to[pname].tmp_index_var_write_access)
or yl_speak_up.speak_to[pname].tmp_index_var_write_access == 1) then
yl_speak_up.speak_to[pname].tmp_index_var_write_access = 1
add_write_button = "button[12.9,3.9;1.0,0.9;add_write_access;Add]"..
"tooltip[add_write_access;Grant the player whose name you entered\n"..
"you entered in the field to the left *write* access\n"..
"to your variable.]"
end
local list_of_npc_users = "- none -"
local list_of_node_pos_users = "- none -"
-- expand name of variable k again
local k_long = yl_speak_up.add_pname_to_var(k, pname)
-- which npc and which node_pos use this variable? create a list for display
local c1 = 0
local c2 = 0
if(k_long
and yl_speak_up.player_vars[ k_long ]
and yl_speak_up.player_vars[ k_long ][ "$META$"]) then
local npc_users = yl_speak_up.get_variable_metadata(k_long, "used_by_npc")
c1 = #npc_users
if(npc_users and c1 > 0) then
list_of_npc_users = minetest.formspec_escape(table.concat(npc_users, ", "))
end
local node_pos_users = yl_speak_up.get_variable_metadata(k_long, "used_by_node_pos")
c2 = #node_pos_users
if(node_pos_users and c2 > 0) then
list_of_node_pos_users = minetest.formspec_escape(table.concat(
node_pos_users, ", "))
end
end
additional_buttons = "button[11.4,1.9;2.5,0.9;show_var_usage;Where is it used?]"..
"tooltip[show_var_usage;Show which NPC use this variable in which context.]"..
-- offer a dropdown list and a text input field for new varialbe names for adding
"label[0.2,3.05;Players with read access to this variable:]"..
yl_speak_up.create_dropdown_playerlist(player, pname,
pl_with_read_access,
yl_speak_up.speak_to[pname].tmp_index_var_read_access,
5.5, 2.9, 0.0, "list_var_read_access", "player", "Remove player from list",
"grant_player_var_read_access",
"Enter the name of the player that shall\n"..
"have read access to this variable.",
"revoke_player_var_read_access",
"If you click here, the selected player\n"..
"will no longer be able to add new\n"..
"pre(C)onditions which read your variable."
)..add_read_button..
"label[0.2,4.05;Players with *write* access to this variable:]"..
yl_speak_up.create_dropdown_playerlist(player, pname,
pl_with_write_access,
yl_speak_up.speak_to[pname].tmp_index_var_write_access,
5.5, 3.9, 0.0, "list_var_write_access", "player", "Remove player from list",
"grant_player_var_write_access",
"Enter the name of the player that shall\n"..
"have *write* access to this variable.",
"revoke_player_var_write_access",
"If you click here, the selected player\n"..
"will no longer be able to *write* new\n"..
"values into this variable."
)..add_write_button..
"label[0.2,5.05;Type of variable: "..
-- show variable type
minetest.colorize("#FFFF00",
(yl_speak_up.get_variable_metadata(k_long, "var_type")
or "String/text or numerical value, depending on how you use it"))..
".]"..
"label[0.2,6.05;This variable is used by the following "..
minetest.colorize("#FFFF00", tostring(c1)).." NPC:\n\t"..
-- those are only n_id - no need to formspec_escape that
minetest.colorize("#FFFF00", list_of_npc_users)..".]"..
"label[0.2,7.05;This variable is used by the following "..
minetest.colorize("#FFFF00", tostring(c2)).." node positions:\n\t"..
-- those are only pos_to_string(..) - no need to formspec_escape that
minetest.colorize("#FFFF00", list_of_node_pos_users)..".]"
end
return "size[16,8.5]"..
"label[5.0,0.0;* Manage your variables *]"..
"label[0.2,0.8;Note: Each variable will store a diffrent value for each player who "..
"interacts with the NPC.\n"..
"You can grant read and write access to other players for your "..
"variables so that they can also use them as well.]"..
"label[0.2,2.05;Your variables:]"..
-- offer a dropdown list and a text input field for new varialbe names for adding
yl_speak_up.create_dropdown_playerlist(player, pname,
table_of_vars, yl_speak_up.speak_to[pname].tmp_index_variable,
2.2, 1.9, 1.0, "list_var_names", "variable", "Delete selected variable",
"add_variable_name",
"Enter the name of the new variable you\n"..
"want to create.",
"delete_variable",
"If you click here, the selected variable\n"..
"will be deleted."
)..
additional_buttons..
"button[0.0,0.2;1.0,0.6;back;Back]"..
"button[6.0,8.0;1.0,0.6;back;Back]"
end
-- variables are personalized; they are prefixed by "$ <PLAYER_NAME> <VAR_NAME>"
-- helper function;