From 5469b25ce0b71e38981948f1650476b05364ad01 Mon Sep 17 00:00:00 2001 From: cheapie Date: Sun, 29 May 2016 20:47:04 -0500 Subject: Add support for multiple keywords --- README | 3 +- init.lua | 108 ++++++++++++++++++++++++++++++++++++++------------------------- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/README b/README index 0130cf1..51728e2 100644 --- a/README +++ b/README @@ -7,7 +7,8 @@ If they press "I do not agree", they are allowed to play normally (but without i At any point, the /rules command will show the rules. The /set_interact_spawn and /set_no_interact_spawn commands (requiring the server priv) will set the respective spawn point to your current location. To set the rules, either place a file called "newplayer-rules.txt" in the world directory, press the "Edit" button at the rules screen, or use the /editrules command (the latter two methods require the "server" priv). If this file does not exist, the mod will tell you where to create it if you attempt to view the rules. -If a keyword is set (Use /set_keyword to set it or /get_keyword to see what it's set to, both require server privs. It can also be edited from the rules editor.), then players will have to enter it into a provided text box before clicking the agree button. It is then possible to put the string "@KEYWORD" in the rules, which will be replaced with the keyword when they are shown to a player without interact. This substitution (intentionally) does not occur when the rules are being shown to somebody with interact (for example, if they use the /rules command because they want to re-read them)* so that they can't see the keyword, preventing them from telling other players what it is provided that it is changed freqently enough. +If one or more keywords are set (Use /getkeywords to see them, /addkeyword to add one, or /delkeyword to remove one, all three require server privs.), then players will have to enter a randomly-chosen entry from the list into a provided text box before clicking the agree button. It is then possible to put the string "@KEYWORD" in the rules, which will be replaced with the chosen keyword when they are shown to a player without interact. This substitution (intentionally) does not occur when the rules are being shown to somebody with interact (for example, if they use the /rules command because they want to re-read them)* so that they can't see the keyword, preventing them from telling other players what it is... or what they think it is, since the other player will likely have a different keyword assuming several are set. If a keyword is not set, players are not asked for it and simply pressing the "I agree" button is enough. + * Like that ever happens. diff --git a/init.lua b/init.lua index dacaa73..f24e278 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,15 @@ newplayer = {} -newplayer.keyword = minetest.setting_get("interact_keyword") -if newplayer.keyword == "" then newplayer.keyword = nil end +local f = io.open(minetest.get_worldpath()..DIR_DELIM.."newplayer-keywords.txt","r") +if f then + local d = f:read("*all") + newplayer.keywords = minetest.deserialize(d) + f:close() +else + newplayer.keywords = {} +end + +newplayer.assigned_keywords = {} newplayer.hudids = {} @@ -14,13 +22,21 @@ else newplayer.rules = "Rules file not found!\n\nThe file should be named \"newplayer-rules.txt\" and placed in the following location:\n\n"..minetest.get_worldpath()..DIR_DELIM end +function newplayer.savekeywords() + local f = io.open(minetest.get_worldpath()..DIR_DELIM.."newplayer-keywords.txt","w") + local d = minetest.serialize(newplayer.keywords) + f:write(d) + f:close() +end + function newplayer.showrulesform(name) - if newplayer.keyword then - newplayer.rules_subbed = string.gsub(newplayer.rules,"@KEYWORD",newplayer.keyword) + if #newplayer.keywords > 0 then + newplayer.assigned_keywords[name] = newplayer.keywords[math.random(1,#newplayer.keywords)] + newplayer.rules_subbed = string.gsub(newplayer.rules,"@KEYWORD",newplayer.assigned_keywords[name]) else newplayer.rules_subbed = newplayer.rules end - if newplayer.keyword and minetest.check_player_privs(name,{interact=true}) and not minetest.check_player_privs(name,{server=true}) then + if #newplayer.keywords > 0 and minetest.check_player_privs(name,{interact=true}) and not minetest.check_player_privs(name,{server=true}) then newplayer.rules_subbed_interact = string.gsub(newplayer.rules,"@KEYWORD",minetest.formspec_escape("[Hidden because you already have interact]")) else newplayer.rules_subbed_interact = newplayer.rules @@ -33,7 +49,7 @@ function newplayer.showrulesform(name) "textarea[0.25,1;8,7;rules;;"..newplayer.rules_subbed.."]".. "button[1,9;2,1;yes;I agree]".. "button[5,9;2,1;no;I do not agree]" - if newplayer.keyword then + if #newplayer.keywords > 0 then form_nointeract = form_nointeract.."field[0.25,8;8,1;keyword;Enter keyword from rules above:;]" end local hasinteract = minetest.check_player_privs(name,{interact=true}) @@ -41,7 +57,6 @@ function newplayer.showrulesform(name) if minetest.check_player_privs(name,{server=true}) then form_interact = form_interact.."button_exit[1,9;2,1;quit;OK]" form_interact = form_interact.."button[5,9;2,1;edit;Edit]" - form_interact = form_interact.."field[0.25,8;8,1;keyword;Keyword:;"..(newplayer.keyword or "").."]" else form_interact = form_interact.."button_exit[3,9;2,1;quit;OK]" end @@ -76,7 +91,7 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) local name = player:get_player_name() if formname == "newplayer:rules_nointeract" then if fields.yes then - if not newplayer.keyword or string.lower(fields.keyword) == string.lower(newplayer.keyword) then + if #newplayer.keywords == 0 or (not newplayer.assigned_keywords[name]) or string.lower(fields.keyword) == string.lower(newplayer.assigned_keywords[name]) then local privs = minetest.get_player_privs(name) privs.interact = true minetest.set_player_privs(name,privs) @@ -124,15 +139,6 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) f:write(fields.rules) f:close() newplayer.rules = fields.rules - if fields.keyword ~= "" then - newplayer.keyword = fields.keyword - minetest.setting_set("interact_keyword",fields.keyword) - minetest.setting_save() - else - newplayer.keyword = nil - minetest.setting_set("interact_keyword","") - minetest.setting_save() - end minetest.chat_send_player(name,"Rules/keyword updated successfully.") end else @@ -144,8 +150,7 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) "label[0,0;Editing Server Rules]".. "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]".. "button_exit[1,9;2,1;save;Save]".. - "button_exit[5,9;2,1;quit;Cancel]".. - "field[0.25,8;8,1;keyword;Keyword:;"..(newplayer.keyword or "").."]" + "button_exit[5,9;2,1;quit;Cancel]" minetest.show_formspec(name,"newplayer:editrules",form) end elseif formname == "newplayer:agreethanks" or formname == "newplayer:disagreewarning" then @@ -171,8 +176,7 @@ minetest.register_chatcommand("editrules",{ "label[0,0;Editing Server Rules]".. "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]".. "button_exit[1,9;2,1;save;Save]".. - "button_exit[5,9;2,1;quit;Cancel]".. - "field[0.25,8;8,1;keyword;Keyword:;"..(newplayer.keyword or "").."]" + "button_exit[5,9;2,1;quit;Cancel]" minetest.show_formspec(name,"newplayer:editrules",form) return true end} @@ -202,36 +206,54 @@ minetest.register_chatcommand("set_interact_spawn",{ end} ) -minetest.register_chatcommand("set_keyword",{ - params = "[keyword]", - description = "Set the keyword used to get interact", +minetest.register_chatcommand("getkeywords",{ + params = "", + description = "Gets the list of keywords used to obtain the interact privilege", privs = {server=true}, - func = function(name,param) - if param and param ~= "" then - newplayer.keyword = param - minetest.setting_set("interact_keyword",param) - minetest.setting_save() - return true, "Keyword set to: "..param + func = function(name) + local out = "" + if #newplayer.keywords > 0 then + out = "Currently configured keywords:" + for _,kw in pairs(newplayer.keywords) do + out = out.."\n"..kw + end else - newplayer.keyword = nil - minetest.setting_set("interact_keyword","") - minetest.setting_save() - return true, "Keyword cleared." + out = "No keywords are currently set." end + return true, out end} ) -minetest.register_chatcommand("get_keyword",{ - params = "", - description = "Shows the keyword used to get interact", +minetest.register_chatcommand("addkeyword",{ + params = "", + description = "Add a keyword to the list of keywords used to obtain the interact privilege", privs = {server=true}, - func = function(name) - local kw = newplayer.keyword - if kw then - return true, "Keyword is: "..kw - else - return true, "No keyword is currently set." + func = function(name,param) + if (not param) or param == "" then + return true, "ERROR: No keyword supplied" + end + table.insert(newplayer.keywords,param) + newplayer.savekeywords() + return true, string.format("Keyword \"%s\" added",param) + end} +) + +minetest.register_chatcommand("delkeyword",{ + params = "", + description = "Remove a keyword from the list of keywords used to obtain the interact privilege", + privs = {server=true}, + func = function(name,param) + if (not param) or param == "" then + return true, "ERROR: No keyword supplied" + end + for k,v in pairs(newplayer.keywords) do + if v == param then + newplayer.keywords[k] = nil + newplayer.savekeywords() + return true, string.format("Keyword \"%s\" removed",param) + end end + return true, string.format("ERROR: Keyword \"%s\" not found",param) end} ) -- cgit v1.2.3