forked from Sokomine/yl_speak_up
154 lines
5.1 KiB
Lua
154 lines
5.1 KiB
Lua
|
|
-- 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)
|
|
-- TODO: save state whenever a new variable is added - or else if the last change was more than x minutes ago?
|
|
|
|
-- the keys are of the form:
|
|
-- <player name> <blank> <variable name> (makes it easier to grant read access)
|
|
-- the values are of the form:
|
|
-- <current player name as key> : <value of variable for that player as value>
|
|
yl_speak_up.player_vars = {}
|
|
|
|
|
|
-- new variables have to be added somehow
|
|
yl_speak_up.add_quest_variable = function(owner_name, variable_name)
|
|
local k = tostring(owner_name).." "..tostring(variable_name)
|
|
if(not(owner_name) or not(variable_name)) then
|
|
return false
|
|
end
|
|
-- create a new empty table;
|
|
-- keys will be the names of players for which values are set
|
|
yl_speak_up.player_vars[ k ] = {}
|
|
return true
|
|
end
|
|
|
|
|
|
-- accidentally created or no longer needed variables need to be deleted somehow
|
|
yl_speak_up.del_quest_variable = function(owner_name, variable_name)
|
|
local k = tostring(owner_name).." "..tostring(variable_name)
|
|
if(not(owner_name) or not(variable_name)) then
|
|
return false
|
|
end
|
|
yl_speak_up.player_vars[ k ] = nil
|
|
end
|
|
|
|
|
|
-- set the value of a variable used by a player in an NPC;
|
|
-- returns false if the variable cannot be set (i.e. does not exist)
|
|
yl_speak_up.set_quest_variable_value = function(player_name, variable_name, new_value)
|
|
-- the owner name is alrady encoded in the variable name
|
|
local k = tostring(variable_name)
|
|
if(not(variable_name) or not(player_name) or not(yl_speak_up.player_vars[ k ])) then
|
|
return false
|
|
end
|
|
yl_speak_up.player_vars[ k ][ player_name ] = new_value
|
|
return true
|
|
end
|
|
|
|
|
|
-- get the value of a variable used by a player in an NPC;
|
|
-- returns nil if the variable does not exist
|
|
yl_speak_up.get_quest_variable_value = function(player_name, variable_name)
|
|
-- the owner name is alrady encoded in the variable name
|
|
local k = tostring(variable_name)
|
|
if(not(variable_name) or not(player_name) or not(yl_speak_up.player_vars[ k ])) then
|
|
return nil
|
|
end
|
|
return yl_speak_up.player_vars[ k ][ player_name ]
|
|
end
|
|
|
|
|
|
-- which variables can player pname read and use in preconditions?
|
|
-- returns a sorted list
|
|
yl_speak_up.get_quest_variables_with_read_access = function(pname)
|
|
if(not(pname)) then
|
|
return {}
|
|
end
|
|
local liste = {}
|
|
for k, v in pairs(yl_speak_up.player_vars) do
|
|
local parts = string.split(k, " ")
|
|
if(parts and parts[1] and parts[1] == pname) then
|
|
table.insert(liste, k)
|
|
end
|
|
end
|
|
-- TODO: insert those vars owned by other players where this one has read access to
|
|
table.sort(liste)
|
|
return liste
|
|
end
|
|
|
|
|
|
-- which variables can player pname write and use in effects/results?
|
|
yl_speak_up.get_quest_variables_with_write_access = function(pname)
|
|
if(not(pname)) then
|
|
return {}
|
|
end
|
|
local liste = {}
|
|
for k, v in pairs(yl_speak_up.player_vars) do
|
|
local parts = string.split(k, " ")
|
|
if(parts and parts[1] and parts[1] == pname) then
|
|
table.insert(liste, k)
|
|
end
|
|
end
|
|
-- TODO: insert those vars owned by other players where this one has write access to
|
|
table.sort(liste)
|
|
return liste
|
|
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
|
|
-- add a new variable?
|
|
if(fields and fields.add_variable) then
|
|
if(not(fields.add_variable_name) or fields.add_variable_name == ""
|
|
or fields.add_variable_name:trim() == "") then
|
|
yl_speak_up.show_fs(player, "msg", {
|
|
input_to = "yl_speak_up:manage_variables",
|
|
formspec = "size[6,2]"..
|
|
"label[0.2,0.5;Please enter the name of your variable!]"..
|
|
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
|
|
return
|
|
end
|
|
-- TODO: limit names to something more sensible?
|
|
fields.add_variable_name = fields.add_variable_name:trim()
|
|
local res = yl_speak_up.add_quest_variable(pname, fields.add_variable_name)
|
|
local text = "A new variable named\n \""..tostring(fields.add_variable_name)..
|
|
"\"\nhas been created."
|
|
if(not(res)) then
|
|
text = "Failed to create variable named\n \""..
|
|
tostring(fields.add_variable_name).."\"."
|
|
end
|
|
yl_speak_up.show_fs(player, "msg", {
|
|
input_to = "yl_speak_up:manage_variables",
|
|
formspec = "size[6,2]"..
|
|
"label[0.2,0.0;"..minetest.formspec_escape(text).."]"..
|
|
"button[1.5,1.5;2,0.9;back_from_msg;Back]"})
|
|
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)
|
|
return "size[12,4]"..
|
|
"label[2.0,-0.2;* Manage your variables *]"..
|
|
"label[0.2,1.0;Create this new variable:]"..
|
|
"field[3.7,1.3;6.0,0.6;add_variable_name;;]"..
|
|
"button[9.4,1.0;2.5,0.6;add_variable;Create variable]"..
|
|
"tooltip[add_variable;Enter the name of your new variable.]"..
|
|
-- TODO: delete variable
|
|
"button[2.0,3.5;1.0,0.6;back;Back]"
|
|
end
|