Testing cascading functions in interactions
This commit is contained in:
		| @@ -80,7 +80,16 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) | ||||
|          if nil then | ||||
|          -- buttons first | ||||
|          elseif fields.test_poll then | ||||
|             modpol.interactions.binary_poll_org(pname, 1, "Poll question (yes/no):") | ||||
|             local func_announce_vote = function(vote) | ||||
|                modpol.interacts.message( | ||||
|                   pname, pname .. " voted " .. vote) | ||||
|             end | ||||
|             local func_start_poll = function(input) | ||||
|                modpol.interactions.binary_poll_user( | ||||
|                   pname, input, func_announce_vote) | ||||
|             end | ||||
|             modpol.interactions.text_query( | ||||
|                pname,"Poll question:",func_start_poll) | ||||
|          elseif fields.add_org then | ||||
|             modpol.interactions.add_org(pname, 1) | ||||
|          elseif fields.remove_org then | ||||
| @@ -227,10 +236,10 @@ end | ||||
|  | ||||
| -- Function: modpol.interactions.text_query | ||||
| -- Overrides function at modpol/interactions.lua | ||||
| -- input: Query (string), User (string) | ||||
| -- output: Saves user response (string) to _contexts[pname] | ||||
| -- TODO Need to switch "user" to index not name | ||||
| function modpol.interactions.text_query(user, query) | ||||
| -- input: user (string), query (string), func (function) | ||||
| --   func input: user input (string) | ||||
| -- output: Applies "func" to user input | ||||
| function modpol.interactions.text_query(user, query, func) | ||||
|    -- set up formspec | ||||
|     local formspec = { | ||||
|        "formspec_version[4]", | ||||
| @@ -242,15 +251,22 @@ function modpol.interactions.text_query(user, query) | ||||
|     local formspec_string = table.concat(formspec, "") | ||||
|     -- present to players | ||||
|     minetest.show_formspec(user, "modpol:text_query", formspec_string) | ||||
|     -- put func in _contexts | ||||
|     _contexts[user] = {} | ||||
|     _contexts[user]["text_query_func"] = func | ||||
| end | ||||
| -- receive fields | ||||
| minetest.register_on_player_receive_fields(function (player, formname, fields) | ||||
|       if formname == "modpol:text_query" then | ||||
|          local pname = player:get_player_name() | ||||
|          if _contexts[pname] then | ||||
|             _contexts[pname](fields.input) | ||||
|          end | ||||
|          local input = fields.input | ||||
|          minetest.close_formspec(pname, formname) | ||||
|          if _contexts[pname]["text_query_func"] then | ||||
|             local func = _contexts[pname]["text_query_func"] | ||||
|             func(input) | ||||
|          else | ||||
|             modpol.interactions.message(pname, "text_query: " .. input) | ||||
|          end | ||||
|       end | ||||
| end) | ||||
|  | ||||
| @@ -285,11 +301,12 @@ end) | ||||
| -- SECONDARY INTERACTIONS | ||||
| -- ====================== | ||||
|  | ||||
| -- Function: modpol.binary_poll_user(user, question) | ||||
| -- Function: modpol.binary_poll_user(user, question, function) | ||||
| -- Overrides function at modpol/interactions.lua | ||||
| -- presents a yes/no poll to a user, returns answer | ||||
| --  | ||||
| function modpol.interactions.binary_poll_user(user, question) | ||||
| -- Params: user (string), question (string), func (function) | ||||
| --   func input: user input (string: y/n) | ||||
| -- Output: Applies "func" to user input | ||||
| function modpol.interactions.binary_poll_user(user, question, func) | ||||
|    -- set up formspec | ||||
|    local text = "Poll: " .. question | ||||
|    local formspec = { | ||||
| @@ -302,10 +319,12 @@ function modpol.interactions.binary_poll_user(user, question) | ||||
|       --TKTK we could use scroll boxes to contain the text | ||||
|    } | ||||
|    local formspec_string = table.concat(formspec, "") | ||||
|    _contexts[user] = {} | ||||
|    _contexts[user]["binary_poll_func"] = func | ||||
|    -- present to player | ||||
|    minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string) | ||||
|       modpol.interactions.message(user,"should have showed formspec: " .. question) | ||||
| end | ||||
|  | ||||
| minetest.register_on_player_receive_fields(function (player, formname, fields) | ||||
|       local pname = player:get_player_name() | ||||
|       -- modpol:binary_poll | ||||
| @@ -313,15 +332,13 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) | ||||
|          local vote = nil | ||||
|          if fields.yes then vote = fields.yes | ||||
|          elseif fields.no then vote = fields.no | ||||
|          elseif fields.abstain then vote = fields.abstain | ||||
|          end | ||||
|          if vote then | ||||
|             modpol.interactions.message(pname, "Vote recorded") | ||||
|             minetest.chat_send_all(pname .. " voted " .. vote) | ||||
|             --TODO : we should send the message to all in that org, not to all players | ||||
|          end | ||||
|          minetest.close_formspec(pname, formname) | ||||
|          return vote | ||||
|          if vote then | ||||
|             modpol.interactions.message(pname, "Vote recorded") | ||||
|             local func = _contexts[user]["binary_poll_func"] | ||||
|             func(vote) | ||||
|          end | ||||
|       end | ||||
| end) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user