interactions.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 P O L]",
  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. table.insert(modules, module_entry)
  125. end
  126. end
  127. end
  128. table.sort(modules)
  129. table.insert(modules,1,"View...")
  130. -- prepare pending menu
  131. local pending = {}
  132. local num_pending = 0
  133. if org.pending[user] then
  134. for k,v in pairs(org.pending[user]) do
  135. local pending_string = org.processes[k].name
  136. .." ["..k.."]"
  137. table.insert(pending, pending_string)
  138. num_pending = num_pending + 1
  139. end
  140. end
  141. table.sort(pending)
  142. table.insert(pending,1,"View...")
  143. -- set player context
  144. local user_context = {}
  145. user_context["current_org"] = org.name
  146. _contexts[user] = user_context
  147. -- set up formspec
  148. local formspec = {
  149. "formspec_version[4]",
  150. "size[10,8]",
  151. "label[0.5,0.5;Org: "..
  152. minetest.formspec_escape(org.name)..membership_toggle(org.name).."]",
  153. "label[0.5,1;Parent: "..parent..membership_toggle(parent).."]",
  154. "label[0.5,2;Members:]",
  155. "dropdown[2,1.5;7,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  156. "label[0.5,3;Children:]",
  157. "dropdown[2,2.5;7,0.8;children;"..formspec_list(children)..";;]",
  158. "label[0.5,4;Modules:]",
  159. "dropdown[2,3.5;7,0.8;modules;"..formspec_list(modules)..";;]",
  160. "label[0.5,5;Pending ("..num_pending.."):]",
  161. "dropdown[2,4.5;7,0.8;pending;"..formspec_list(pending)..";;]",
  162. "button[0.5,7;1,0.8;refresh;Refresh]",
  163. "button[8.5,7;1,0.8;back;Back]",
  164. }
  165. local formspec_string = table.concat(formspec, "")
  166. -- present to player
  167. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  168. end
  169. -- receive input
  170. minetest.register_on_player_receive_fields(function (player, formname, fields)
  171. if formname == "modpol:org_dashboard" then
  172. local pname = player:get_player_name()
  173. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  174. -- just confirm the org still exists:
  175. if not org then
  176. modpol.interactions.message(pname, "Org no longer exists")
  177. modpol.interactions.dashboard(pname)
  178. return end
  179. -- okay, onward
  180. if nil then
  181. elseif fields.back then
  182. modpol.interactions.dashboard(pname)
  183. elseif fields.refresh then
  184. modpol.interactions.org_dashboard(pname,org.name)
  185. -- Put all dropdowns at the end
  186. -- Receiving modules
  187. elseif fields.modules
  188. and fields.modules ~= "View..." then
  189. local module = nil
  190. for k,v in pairs(org.modules) do
  191. if fields.modules == v.name then
  192. module = v
  193. end end
  194. if module then
  195. modpol.interactions.binary_poll_user(
  196. pname,
  197. module.name..":\n"..
  198. module.desc.."\n"..
  199. "Proceed?",
  200. function(input)
  201. if input == "Yes" then
  202. org:call_module(module.slug, pname)
  203. end
  204. end)
  205. end
  206. -- Receiving pending
  207. elseif fields.pending
  208. and fields.pending ~= "View..." then
  209. local pending = string.match(
  210. fields.pending,"%[(%d)%]")
  211. local process = org.processes[tonumber(pending)]
  212. if process then
  213. org:interact(process.id, pname)
  214. end
  215. -- Children
  216. elseif fields.children
  217. and fields.children ~= "View..." then
  218. local org_name = fields.children
  219. modpol.interactions.org_dashboard(pname, org_name)
  220. end
  221. end
  222. end)
  223. -- Function: modpol.interactions.policy_dashboard
  224. -- input: user (string), org_id (int), policy (string)
  225. -- output: opens a dashboard for viewing/editing policy details
  226. -- TODO
  227. function modpol.interactions.policy_dashboard(
  228. user, org_id, policy)
  229. modpol.interactions.message(
  230. user,
  231. "Not yet implemented: " .. policy)
  232. end
  233. -- INTERACTION FUNCTIONS
  234. -- =====================
  235. -- Function: modpol.interactions.message
  236. -- input: user (string), message (string)
  237. -- output: displays message to specified user
  238. function modpol.interactions.message(user, message)
  239. if message then
  240. minetest.chat_send_player(user, message)
  241. end
  242. end
  243. -- Function: modpol.interactions.text_query
  244. -- Overrides function at modpol/interactions.lua
  245. -- input: user (string), query (string), func (function)
  246. -- output: Applies "func" to user input
  247. function modpol.interactions.text_query(user, query, func)
  248. -- set up formspec
  249. local formspec = {
  250. "formspec_version[4]",
  251. "size[10,4]",
  252. "label[0.5,1;", minetest.formspec_escape(query), "]",
  253. "field[0.5,1.25;9,0.8;input;;]",
  254. "button[0.5,2.5;1,0.8;yes;OK]",
  255. }
  256. local formspec_string = table.concat(formspec, "")
  257. -- present to player
  258. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  259. -- put func in _contexts
  260. if _contexts[user] == nil then _contexts[user] = {} end
  261. _contexts[user]["text_query_func"] = func
  262. end
  263. -- receive fields
  264. minetest.register_on_player_receive_fields(function (player, formname, fields)
  265. if formname == "modpol:text_query" then
  266. local pname = player:get_player_name()
  267. local input = fields.input
  268. if not input then
  269. -- no input, do nothing
  270. else
  271. local func = _contexts[pname]["text_query_func"]
  272. if func then
  273. func(input)
  274. else
  275. modpol.interactions.message(pname, "text_query: " .. input)
  276. end
  277. end
  278. minetest.close_formspec(pname, formname)
  279. end
  280. end)
  281. -- Function: dropdown_query
  282. -- input: user (string), label (string), options (table of strings), func (function)
  283. -- func input: choice (string)
  284. -- output: calls func on user choice
  285. function modpol.interactions.dropdown_query(user, label, options, func)
  286. -- Add "View..." to the top of the list
  287. table.insert(options,1,"View...")
  288. -- set up formspec
  289. local formspec = {
  290. "formspec_version[4]",
  291. "size[10,4]",
  292. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  293. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  294. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  295. }
  296. local formspec_string = table.concat(formspec, "")
  297. -- present to players
  298. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  299. -- put func in _contexts
  300. if _contexts[user] == nil then _contexts[user] = {} end
  301. _contexts[user]["dropdown_query_func"] = func
  302. end
  303. -- receive fields
  304. minetest.register_on_player_receive_fields(function (player, formname, fields)
  305. if formname == "modpol:dropdown_query" then
  306. local pname = player:get_player_name()
  307. if fields.cancel then
  308. minetest.close_formspec(pname, formname)
  309. elseif fields.input == "View..." then
  310. -- "View...", do nothing
  311. else
  312. local choice = fields.input
  313. local func = _contexts[pname]["dropdown_query_func"]
  314. if not choice then
  315. -- empty, do nothing
  316. elseif func then
  317. --causes issues with sequential dropdowns
  318. --minetest.close_formspec(pname, formname)
  319. func(choice)
  320. else
  321. minetest.close_formspec(pname, formname)
  322. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  323. end
  324. end
  325. end
  326. end)
  327. -- Function: modpol.binary_poll_user(user, question, function)
  328. -- Overrides function at modpol/interactions.lua
  329. -- Params: user (string), question (string), func (function)
  330. -- func input: user input (string: y/n)
  331. -- Output: Applies "func" to user input
  332. function modpol.interactions.binary_poll_user(user, question, func)
  333. -- set up formspec
  334. local formspec = {
  335. "formspec_version[4]",
  336. "size[8,4]",
  337. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  338. "button[1,2.5;1,0.8;yes;Yes]",
  339. "button[2,2.5;1,0.8;no;No]",
  340. --TODO can we enable text wrapping?
  341. --TODO we could use scroll boxes to contain the text
  342. }
  343. local formspec_string = table.concat(formspec, "")
  344. if _contexts[user] == nil then _contexts[user] = {} end
  345. _contexts[user]["binary_poll_func"] = func
  346. -- present to player
  347. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  348. end
  349. minetest.register_on_player_receive_fields(function (player, formname, fields)
  350. local pname = player:get_player_name()
  351. -- modpol:binary_poll
  352. if formname == "modpol:binary_poll_user" then
  353. local vote = nil
  354. if fields.yes then vote = fields.yes
  355. elseif fields.no then vote = fields.no
  356. end
  357. if vote then
  358. local func = _contexts[pname]["binary_poll_func"]
  359. if func then func(vote) end
  360. end
  361. minetest.close_formspec(pname, formname)
  362. end
  363. end)