2
0

interactions.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. -- ===================================================================
  2. -- Function: modpol.binary_poll_user(user, question)
  3. -- Overwrites function at /interactions.lua
  4. -- presents a yes/no/abstain poll to a user, returns answer
  5. modpol.binary_poll_user = function(user, question)
  6. -- set up formspec
  7. local text = "Poll: " .. question
  8. local formspec = {
  9. "formspec_version[4]",
  10. "size[5,3]",
  11. "label[0.375,0.5;", minetest.formspec_escape(text), "]",
  12. "button[1,1.5;1,0.8;yes;Yes]",
  13. "button[2,1.5;1,0.8;no;No]",
  14. "button[3,1.5;1,0.8;abstain;Abstain]"
  15. --TKTK can we enable text wrapping?
  16. --TKTK we could use scroll boxes to contain the text
  17. }
  18. local formspec_string = table.concat(formspec, "")
  19. -- present to player
  20. minetest.show_formspec(user, "modpol:binary_poll", formspec_string)
  21. end
  22. --what to do
  23. minetest.register_on_player_receive_fields(function (player, formname, fields)
  24. -- modpol:poll
  25. if formname == "modpol:binary_poll" then
  26. local pname = player:get_player_name()
  27. local vote = nil
  28. if fields.yes then vote = fields.yes
  29. elseif fields.no then vote = fields.no
  30. elseif fields.abstain then vote = fields.abstain
  31. end
  32. if vote then
  33. minetest.chat_send_all(pname .. " voted " .. vote)
  34. --TKTK : we should send the message to all in that org, definately not to all players
  35. end
  36. minetest.close_formspec(pname, formname)
  37. return vote
  38. else -- if the form is not a recognized name
  39. return
  40. end
  41. end)