interactions.lua 13 KB

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