interactions.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. -- INTERACTIONS.LUA (for Minetest)
  2. -- CONTEXTUAL STUFF
  3. -- ================
  4. -- First, set up contexts to enable passing across formspecs
  5. -- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts
  6. local _contexts = {}
  7. local function get_context(name)
  8. local context = _contexts[name] or {}
  9. _contexts[name] = context
  10. return context
  11. end
  12. minetest.register_on_leaveplayer(function(player)
  13. _contexts[player:get_player_name()] = nil
  14. end)
  15. -- table of formspec field responses
  16. local formspec_fields = {}
  17. -- UTILITIES
  18. -- =========
  19. -- Function: formspec_list
  20. -- for use generating option lists in formspecs from tables
  21. -- input: table of strings
  22. -- output: a formspec-ready list of the strings
  23. local function formspec_list(array)
  24. local escaped = {}
  25. if not array then
  26. return ""
  27. end
  28. for i = 1, #array do
  29. escaped[i] = minetest.formspec_escape(array[i])
  30. end
  31. return table.concat(escaped,",")
  32. end
  33. -- DASHBOARDS
  34. -- ==========
  35. -- Function: modpol.interactions.dashboard(user)
  36. -- Params: user (string)
  37. -- Q: Should this return a menu of commands relevant to the specific user?
  38. -- Output: Displays a menu of commands to the user
  39. -- TODO currently a manually curated list---needs major improvement
  40. function modpol.interactions.dashboard(user)
  41. -- prepare data
  42. -- to add: nested orgs map
  43. local all_orgs = modpol.orgs.list_all()
  44. local user_orgs = modpol.orgs.user_orgs(user)
  45. local all_users = modpol.list_users()
  46. -- set up formspec
  47. local formspec = {
  48. "formspec_version[4]",
  49. "size[10,8]",
  50. "label[0.5,0.5;M O D U L A R P O L I T I C S]",
  51. "label[0.5,2;All orgs:]",
  52. "dropdown[2,1.5;5,0.8;all_orgs;"..formspec_list(all_orgs)..";;]",
  53. "label[0.5,3;Your orgs:]",
  54. "dropdown[2,2.5;5,0.8;user_orgs;"..formspec_list(user_orgs)..";;]",
  55. "label[0.5,4;All users:]",
  56. "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]",
  57. "button[0.5,7;1,0.8;test_poll;Test poll]",
  58. "button[2,7;1,0.8;add_org;Add org]",
  59. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  60. "button_exit[8.5,7;1,0.8;close;Close]",
  61. }
  62. local formspec_string = table.concat(formspec, "")
  63. -- present to player
  64. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  65. end
  66. -- receive input
  67. minetest.register_on_player_receive_fields(function (player, formname, fields)
  68. if formname == "modpol:dashboard" then
  69. local pname = player:get_player_name()
  70. if nil then
  71. -- buttons first
  72. elseif fields.test_poll then
  73. modpol.interactions.binary_poll_org(pname, 1, "Poll question (yes/no):")
  74. elseif fields.add_org then
  75. modpol.interactions.add_org(pname, 1)
  76. elseif fields.remove_org then
  77. modpol.interactions.remove_org(pname)
  78. -- dropdowns need to go last
  79. elseif fields.close then
  80. minetest.close_formspec(pname, formname)
  81. elseif fields.all_orgs or fields.user_orgs then
  82. local org_name = fields.all_orgs or fields.user_orgs
  83. modpol.interactions.org_dashboard(pname, org_name)
  84. end
  85. end
  86. end)
  87. -- Function: modpol.interactions.org_dashboard
  88. -- Params: user (string), org_name (string)
  89. -- Output: Displays a menu of org-specific commands to the user
  90. function modpol.interactions.org_dashboard(user, org_name)
  91. -- prepare data
  92. local org = modpol.orgs.get_org(org_name)
  93. if not org then return nil end
  94. local is_member = org:has_member(user)
  95. local membership_toggle = function()
  96. local toggle_code = ""
  97. if is_member then
  98. toggle_code = toggle_code
  99. ..minetest.formspec_escape("leave")..";"
  100. ..minetest.formspec_escape("Leave").."]"
  101. else
  102. toggle_code = toggle_code
  103. ..minetest.formspec_escape("join")..";"
  104. ..minetest.formspec_escape("Join").."]"
  105. end
  106. return toggle_code
  107. end
  108. local children = {}
  109. for k,v in ipairs(org.children) do
  110. local this_child = modpol.orgs.get_org(v)
  111. table.insert(children, this_child.name)
  112. end
  113. -- set player context
  114. local user_context = {}
  115. user_context["current_org"] = org_name
  116. _contexts[user] = user_context
  117. -- set up formspec
  118. local formspec = {
  119. "formspec_version[4]",
  120. "size[10,8]",
  121. "label[0.5,0.5;Org: "..
  122. minetest.formspec_escape(org_name).."]",
  123. "label[0.5,1;Parent: TODO]",
  124. "button[8.5,0.5;1,0.8;"..membership_toggle(),
  125. "label[0.5,2;Members:]",
  126. "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  127. "label[0.5,3;Children:]",
  128. "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
  129. "label[0.5,4;Policies:]",
  130. "dropdown[2,3.5;5,0.8;policies;"..formspec_list(org.policies)..";;]",
  131. "label[0.5,5;Processes:]",
  132. "dropdown[2,4.5;5,0.8;processes;"..formspec_list(org.processes)..";;]",
  133. "button[0.5,7;1,0.8;test_poll;Test poll]",
  134. "button[2,7;1,0.8;add_child;Add child]",
  135. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  136. "button[8.5,7;1,0.8;back;Back]",
  137. }
  138. local formspec_string = table.concat(formspec, "")
  139. -- present to player
  140. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  141. end
  142. -- receive input
  143. minetest.register_on_player_receive_fields(function (player, formname, fields)
  144. if formname == "modpol:org_dashboard" then
  145. local pname = player:get_player_name()
  146. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  147. if nil then
  148. elseif fields.join then
  149. local new_request = {
  150. user = player,
  151. type = "add_member",
  152. params = {player}
  153. }
  154. org:make_request(new_request)
  155. --org:add_member(pname)
  156. modpol.interactions.org_dashboard(pname,org.name)
  157. elseif fields.leave then
  158. org:remove_member(pname)
  159. modpol.interactions.dashboard(pname)
  160. elseif fields.test_poll then
  161. modpol.interactions.binary_poll_org(pname, _contexts.pname.current_org.id, "Poll question (yes/no):")
  162. elseif fields.add_child then
  163. modpol.interactions.add_org(pname, org.id)
  164. elseif fields.remove_org then
  165. modpol.interactions.remove_org(pname)
  166. elseif fields.back then
  167. modpol.interactions.dashboard(pname)
  168. end
  169. end
  170. end)
  171. -- BASIC INTERACTION FUNCTIONS
  172. -- ===========================
  173. -- Function: modpol.interactions.message
  174. -- input: message (string)
  175. -- output
  176. modpol.interactions.message = function(user, message)
  177. minetest.chat_send_player(user, message)
  178. end
  179. -- Function: modpol.interactions.text_query
  180. -- Overrides function at modpol/interactions.lua
  181. -- input: Query (string), User (string)
  182. -- output: User response (string)
  183. -- TODO Need to switch "user" to index not name
  184. function modpol.interactions.text_query(user, query)
  185. -- set up formspec
  186. local formspec = {
  187. "formspec_version[4]",
  188. "size[10,4]",
  189. "label[0.5,1;", minetest.formspec_escape(query), "]",
  190. "field[0.5,1.25;9,0.8;input;;]",
  191. "button[0.5,2.5;1,0.8;yes;OK]",
  192. }
  193. local formspec_string = table.concat(formspec, "")
  194. -- present to players
  195. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  196. end
  197. -- receive fields
  198. minetest.register_on_player_receive_fields(function (player, formname, fields)
  199. if formname == "modpol:text_query" then
  200. local pname = player:get_player_name()
  201. if _contexts[pname] then
  202. _contexts[pname](fields.input)
  203. end
  204. minetest.close_formspec(pname, formname)
  205. end
  206. end)
  207. -- Function: dropdown_query
  208. -- input: user (string), label (string), options (table of strings)
  209. function modpol.interactions.dropdown_query(user, label, options)
  210. -- set up formspec
  211. local formspec = {
  212. "formspec_version[4]",
  213. "size[10,4]",
  214. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  215. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  216. "button[0.5,2.5;1,0.8;yes;OK]",
  217. }
  218. local formspec_string = table.concat(formspec, "")
  219. -- present to players
  220. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  221. end
  222. -- receive fields
  223. minetest.register_on_player_receive_fields(function (player, formname, fields)
  224. if formname == "modpol:dropdown_query" then
  225. local pname = player:get_player_name()
  226. if _contexts[pname] then
  227. _contexts[pname](fields.input)
  228. end
  229. minetest.close_formspec(pname, formname)
  230. end
  231. end)
  232. -- SECONDARY INTERACTIONS
  233. -- ======================
  234. -- Function: modpol.binary_poll_user(user, question)
  235. -- Overrides function at modpol/interactions.lua
  236. -- presents a yes/no poll to a user, returns answer
  237. --
  238. function modpol.interactions.binary_poll_user(user, question)
  239. -- set up formspec
  240. local text = "Poll: " .. question
  241. local formspec = {
  242. "formspec_version[4]",
  243. "size[5,3]",
  244. "label[0.375,0.5;", minetest.formspec_escape(text), "]",
  245. "button[1,1.5;1,0.8;yes;Yes]",
  246. "button[2,1.5;1,0.8;no;No]",
  247. --TKTK can we enable text wrapping?
  248. --TKTK we could use scroll boxes to contain the text
  249. }
  250. local formspec_string = table.concat(formspec, "")
  251. -- present to player
  252. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  253. end
  254. minetest.register_on_player_receive_fields(function (player, formname, fields)
  255. local pname = player:get_player_name()
  256. -- modpol:binary_poll
  257. if formname == "modpol:binary_poll_user" then
  258. local vote = nil
  259. if fields.yes then vote = fields.yes
  260. elseif fields.no then vote = fields.no
  261. elseif fields.abstain then vote = fields.abstain
  262. end
  263. if vote then
  264. modpol.interactions.message(pname, "Vote recorded")
  265. minetest.chat_send_all(pname .. " voted " .. vote)
  266. --TODO : we should send the message to all in that org, not to all players
  267. end
  268. minetest.close_formspec(pname, formname)
  269. return vote
  270. end
  271. end)
  272. -- COMPLEX INTERACTIONS
  273. -- ====================
  274. -- Function: modpol.interactions.binary_poll_org
  275. -- input: initator (user string), org (number)
  276. -- output: interaction begins
  277. function modpol.interactions.binary_poll_org(initiator, org)
  278. -- start formspec
  279. modpol.interactions.text_query(initiator, "Poll question (yes/no):")
  280. -- set user's context to followup function
  281. _contexts[initiator] =
  282. function(input)
  283. local users = modpol.list_users()
  284. for k,v in ipairs(users) do
  285. modpol.interactions.binary_poll_user(v, input)
  286. end
  287. _contexts[initiator] = nil
  288. end
  289. end
  290. -- Function: modpol.interactions.add_org
  291. -- input: initator (user string), base_org_id (ID)
  292. -- output: interaction begins
  293. function modpol.interactions.add_org(initiator, base_org_id)
  294. -- start formspec
  295. modpol.interactions.text_query(initiator, "Org name:")
  296. -- set user's context to followup function
  297. _contexts[initiator] = function(input)
  298. if input then
  299. local base_org = modpol.orgs.get_org(base_org_id)
  300. local result = base_org:add_org(input, initiator)
  301. if result then
  302. local message = input .. " created"
  303. modpol.interactions.message(initiator, message)
  304. end
  305. end
  306. _contexts[initiator] = nil
  307. modpol.interactions.dashboard(initiator)
  308. end
  309. end
  310. -- Function: modpol.interactions.remove_org
  311. -- input: initator (user string)
  312. -- output: interaction begins
  313. function modpol.interactions.remove_org(initiator)
  314. -- start formspec
  315. local orgs_list = modpol.orgs.list_all()
  316. local label = "Choose an org to remove:"
  317. modpol.interactions.dropdown_query(initiator, label, orgs_list)
  318. -- set user's context to followup function
  319. _contexts[initiator] = function(input)
  320. if input then
  321. local target_org = modpol.orgs.get_org(input)
  322. local result = target_org:delete()
  323. if result then
  324. local message = input .. " deleted"
  325. modpol.interactions.message(initiator, message)
  326. end
  327. end
  328. _contexts[initiator] = nil
  329. modpol.interactions.dashboard(initiator)
  330. end
  331. end