interactions.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. -- INTERACTIONS.LUA (for Minetest)
  2. -- CONTEXTUAL STUFF
  3. -- ================
  4. -- _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. -- UTILITIES
  16. -- =========
  17. -- Function: formspec_list
  18. -- for use generating option lists in formspecs from tables
  19. -- input: table of strings
  20. -- output: a formspec-ready list of the strings
  21. local function formspec_list(array)
  22. local escaped = {}
  23. if not array then
  24. return ""
  25. end
  26. for i = 1, #array do
  27. escaped[i] = minetest.formspec_escape(array[i])
  28. end
  29. return table.concat(escaped,",")
  30. end
  31. -- DASHBOARDS
  32. -- ==========
  33. -- Function: modpol.interactions.dashboard(user)
  34. -- Params: user (string)
  35. -- Q: Should this return a menu of commands relevant to the specific user?
  36. -- Output: Displays a menu of commands to the user
  37. -- TODO currently a manually curated list---needs major improvement
  38. function modpol.interactions.dashboard(user)
  39. -- prepare data
  40. -- to add: nested orgs map
  41. local all_orgs = modpol.orgs.list_all()
  42. local user_orgs = modpol.orgs.user_orgs(user)
  43. local all_users = modpol.instance:list_members()
  44. -- set up formspec
  45. local formspec = {
  46. "formspec_version[4]",
  47. "size[10,6]",
  48. "label[0.5,0.5;M O D U L A R P O L I T I C S]",
  49. "label[0.5,2;All orgs:]",
  50. "dropdown[2,1.5;5,0.8;all_orgs;"..formspec_list(all_orgs)..";;]",
  51. "label[0.5,3;Your orgs:]",
  52. "dropdown[2,2.5;5,0.8;user_orgs;"..formspec_list(user_orgs)..";;]",
  53. "label[0.5,4;All users:]",
  54. "dropdown[2,3.5;5,0.8;all_users;"..formspec_list(all_users)..";;]",
  55. "button_exit[8.5,5;1,0.8;close;Close]",
  56. }
  57. local formspec_string = table.concat(formspec, "")
  58. -- present to player
  59. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  60. end
  61. -- receive input
  62. minetest.register_on_player_receive_fields(function (player, formname, fields)
  63. if formname == "modpol:dashboard" then
  64. local pname = player:get_player_name()
  65. if nil then
  66. -- buttons first
  67. -- none here right now!
  68. -- Put all dropdowns at the end
  69. elseif fields.close then
  70. minetest.close_formspec(pname, formname)
  71. elseif fields.all_orgs or fields.user_orgs then
  72. local org_name = fields.all_orgs or fields.user_orgs
  73. modpol.interactions.org_dashboard(pname, org_name)
  74. end
  75. end
  76. end)
  77. -- Function: modpol.interactions.org_dashboard
  78. -- Params: user (string), org_string (string or num)
  79. -- Output: Displays a menu of org-specific commands to the user
  80. function modpol.interactions.org_dashboard(user, org_string)
  81. -- prepare data
  82. local org = modpol.orgs.get_org(org_string)
  83. if not org then return nil end
  84. local function membership_toggle(org_display)
  85. local current_org = modpol.orgs.get_org(org_display)
  86. if current_org then
  87. if current_org:has_member(user) then
  88. return " (member)"
  89. end
  90. end
  91. return ""
  92. end
  93. -- identify parent
  94. local parent = modpol.orgs.get_org(org.parent)
  95. if parent then parent = parent.name
  96. else parent = "none" end
  97. -- prepare children menu
  98. local children = {"View..."}
  99. for k,v in ipairs(org.children) do
  100. local this_child = modpol.orgs.get_org(v)
  101. table.insert(children, this_child.name)
  102. end
  103. -- prepare modules menu
  104. local modules = {"View..."}
  105. if org.modules then
  106. for k,v in pairs(org.modules) do
  107. if not v.hide then -- hide utility modules
  108. local module_entry = v.name..
  109. " ["..v.slug.."]"
  110. table.insert(modules, module_entry)
  111. end
  112. end
  113. end
  114. -- prepare pending menu
  115. local pending = {"View..."}
  116. local num_pending = 0
  117. if org.pending[user] then
  118. for k,v in pairs(org.pending[user]) do
  119. local pending_string = org.processes[k].name
  120. .." ["..k.."]"
  121. table.insert(pending, pending_string)
  122. num_pending = num_pending + 1
  123. end
  124. end
  125. -- set player context
  126. local user_context = {}
  127. user_context["current_org"] = org.name
  128. _contexts[user] = user_context
  129. -- set up formspec
  130. local formspec = {
  131. "formspec_version[4]",
  132. "size[10,8]",
  133. "label[0.5,0.5;Org: "..
  134. minetest.formspec_escape(org.name)..membership_toggle(org.name).."]",
  135. "label[0.5,1;Parent: "..parent..membership_toggle(parent).."]",
  136. "label[0.5,2;Members:]",
  137. "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  138. "label[0.5,3;Children:]",
  139. "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
  140. "label[0.5,4;Modules:]",
  141. "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]",
  142. "label[0.5,5;Pending ("..num_pending.."):]",
  143. "dropdown[2,4.5;5,0.8;pending;"..formspec_list(pending)..";;]",
  144. "button[8.5,7;1,0.8;back;Back]",
  145. }
  146. local formspec_string = table.concat(formspec, "")
  147. -- present to player
  148. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  149. end
  150. -- receive input
  151. minetest.register_on_player_receive_fields(function (player, formname, fields)
  152. if formname == "modpol:org_dashboard" then
  153. local pname = player:get_player_name()
  154. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  155. -- just confirm the org still exists:
  156. if not org then
  157. modpol.interactions.message(pname, "Org no longer exists")
  158. modpol.interactions.dashboard(pname)
  159. return end
  160. -- okay, onward
  161. if nil then
  162. elseif fields.back then
  163. modpol.interactions.dashboard(pname)
  164. -- Put all dropdowns at the end
  165. -- Receiving modules
  166. elseif fields.modules
  167. and fields.modules ~= "View..." then
  168. local module = string.match(
  169. fields.modules,"%[(.*)%]")
  170. modpol.interactions.binary_poll_user(
  171. pname,
  172. modpol.modules[module].name.."\n"..
  173. modpol.modules[module].desc.."\n"..
  174. "Proceed?",
  175. function(input)
  176. if input == "Yes" then
  177. org:call_module(module, pname)
  178. end
  179. end)
  180. -- Receiving pending
  181. elseif fields.pending
  182. and fields.pending ~= "View..." then
  183. local pending = string.match(
  184. fields.pending,"%[(%d)%]")
  185. local process = org.processes[tonumber(pending)]
  186. if process then
  187. org:interact(process.id, pname)
  188. end
  189. -- Children
  190. elseif fields.children
  191. and fields.children ~= "View..." then
  192. local org_name = fields.children
  193. modpol.interactions.org_dashboard(pname, org_name)
  194. end
  195. end
  196. end)
  197. -- Function: modpol.interactions.policy_dashboard
  198. -- input: user (string), org_id (int), policy (string)
  199. -- output: opens a dashboard for viewing/editing policy details
  200. -- TODO
  201. function modpol.interactions.policy_dashboard(
  202. user, org_id, policy)
  203. modpol.interactions.message(
  204. user,
  205. "Not yet implemented: " .. policy)
  206. end
  207. -- INTERACTION FUNCTIONS
  208. -- =====================
  209. -- Function: modpol.interactions.message
  210. -- input: user (string), message (string)
  211. -- output: displays message to specified user
  212. function modpol.interactions.message(user, message)
  213. if message then
  214. minetest.chat_send_player(user, message)
  215. end
  216. end
  217. -- Function: modpol.interactions.text_query
  218. -- Overrides function at modpol/interactions.lua
  219. -- input: user (string), query (string), func (function)
  220. -- output: Applies "func" to user input
  221. function modpol.interactions.text_query(user, query, func)
  222. -- set up formspec
  223. local formspec = {
  224. "formspec_version[4]",
  225. "size[10,4]",
  226. "label[0.5,1;", minetest.formspec_escape(query), "]",
  227. "field[0.5,1.25;9,0.8;input;;]",
  228. "button[0.5,2.5;1,0.8;yes;OK]",
  229. }
  230. local formspec_string = table.concat(formspec, "")
  231. -- present to player
  232. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  233. -- put func in _contexts
  234. if _contexts[user] == nil then _contexts[user] = {} end
  235. _contexts[user]["text_query_func"] = func
  236. end
  237. -- receive fields
  238. minetest.register_on_player_receive_fields(function (player, formname, fields)
  239. if formname == "modpol:text_query" then
  240. local pname = player:get_player_name()
  241. local input = fields.input
  242. if not input then
  243. -- no input, do nothing
  244. else
  245. local func = _contexts[pname]["text_query_func"]
  246. if func then
  247. func(input)
  248. else
  249. modpol.interactions.message(pname, "text_query: " .. input)
  250. end
  251. end
  252. minetest.close_formspec(pname, formname)
  253. end
  254. end)
  255. -- Function: dropdown_query
  256. -- input: user (string), label (string), options (table of strings), func (function)
  257. -- func input: choice (string)
  258. -- output: calls func on user choice
  259. function modpol.interactions.dropdown_query(user, label, options, func)
  260. -- set up formspec
  261. local formspec = {
  262. "formspec_version[4]",
  263. "size[10,4]",
  264. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  265. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  266. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  267. }
  268. local formspec_string = table.concat(formspec, "")
  269. -- present to players
  270. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  271. -- put func in _contexts
  272. if _contexts[user] == nil then _contexts[user] = {} end
  273. _contexts[user]["dropdown_query_func"] = func
  274. end
  275. -- receive fields
  276. minetest.register_on_player_receive_fields(function (player, formname, fields)
  277. if formname == "modpol:dropdown_query" then
  278. local pname = player:get_player_name()
  279. if fields.cancel == "cancel" then
  280. -- cancel, do nothing
  281. else
  282. local choice = fields.input
  283. local func = _contexts[pname]["dropdown_query_func"]
  284. if not choice then
  285. -- empty, do nothing
  286. elseif func then
  287. func(choice)
  288. else
  289. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  290. end
  291. end
  292. minetest.close_formspec(pname, formname)
  293. end
  294. end)
  295. -- Function: modpol.binary_poll_user(user, question, function)
  296. -- Overrides function at modpol/interactions.lua
  297. -- Params: user (string), question (string), func (function)
  298. -- func input: user input (string: y/n)
  299. -- Output: Applies "func" to user input
  300. function modpol.interactions.binary_poll_user(user, question, func)
  301. -- set up formspec
  302. local formspec = {
  303. "formspec_version[4]",
  304. "size[8,4]",
  305. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  306. "button[1,2.5;1,0.8;yes;Yes]",
  307. "button[2,2.5;1,0.8;no;No]",
  308. --TODO can we enable text wrapping?
  309. --TODO we could use scroll boxes to contain the text
  310. }
  311. local formspec_string = table.concat(formspec, "")
  312. if _contexts[user] == nil then _contexts[user] = {} end
  313. _contexts[user]["binary_poll_func"] = func
  314. -- present to player
  315. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  316. end
  317. minetest.register_on_player_receive_fields(function (player, formname, fields)
  318. local pname = player:get_player_name()
  319. -- modpol:binary_poll
  320. if formname == "modpol:binary_poll_user" then
  321. local vote = nil
  322. if fields.yes then vote = fields.yes
  323. elseif fields.no then vote = fields.no
  324. end
  325. if vote then
  326. local func = _contexts[pname]["binary_poll_func"]
  327. if func then func(vote) end
  328. end
  329. minetest.close_formspec(pname, formname)
  330. end
  331. end)