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