interactions.lua 13 KB

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