interactions.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. elseif input == "No" then
  205. modpol.interactions.org_dashboard(
  206. pname, org.id)
  207. end
  208. end)
  209. end
  210. -- Receiving pending
  211. elseif fields.pending
  212. and fields.pending ~= "View..." then
  213. local pending = string.match(
  214. fields.pending,"%[(%d)%]")
  215. local process = org.processes[tonumber(pending)]
  216. if process then
  217. org:interact(process.id, pname)
  218. end
  219. -- Children
  220. elseif fields.children
  221. and fields.children ~= "View..." then
  222. local org_name = fields.children
  223. modpol.interactions.org_dashboard(pname, org_name)
  224. end
  225. end
  226. end)
  227. -- Function: modpol.interactions.policy_dashboard
  228. -- input: user (string), org_id (int), policy (string)
  229. -- output: opens a dashboard for viewing/editing policy details
  230. -- TODO
  231. function modpol.interactions.policy_dashboard(
  232. user, org_id, policy)
  233. modpol.interactions.message(
  234. user,
  235. "Not yet implemented: " .. policy)
  236. end
  237. -- INTERACTION FUNCTIONS
  238. -- =====================
  239. -- Function: modpol.interactions.message
  240. -- input: user (string), message (string)
  241. -- output: displays message to specified user
  242. function modpol.interactions.message(user, message)
  243. if message then
  244. minetest.chat_send_player(user, message)
  245. end
  246. end
  247. -- Function: modpol.interactions.text_query
  248. -- Overrides function at modpol/interactions.lua
  249. -- input: user (string), query (string), func (function)
  250. -- output: Applies "func" to user input
  251. function modpol.interactions.text_query(user, query, func)
  252. -- set up formspec
  253. local formspec = {
  254. "formspec_version[4]",
  255. "size[10,4]",
  256. "label[0.5,1;", minetest.formspec_escape(query), "]",
  257. "field[0.5,1.25;9,0.8;input;;]",
  258. "button[0.5,2.5;1,0.8;yes;OK]",
  259. }
  260. local formspec_string = table.concat(formspec, "")
  261. -- present to player
  262. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  263. -- put func in _contexts
  264. if _contexts[user] == nil then _contexts[user] = {} end
  265. _contexts[user]["text_query_func"] = func
  266. end
  267. -- receive fields
  268. minetest.register_on_player_receive_fields(function (player, formname, fields)
  269. if formname == "modpol:text_query" then
  270. local pname = player:get_player_name()
  271. local input = fields.input
  272. if not input then
  273. -- no input, do nothing
  274. else
  275. local func = _contexts[pname]["text_query_func"]
  276. if func then
  277. func(input)
  278. else
  279. modpol.interactions.message(pname, "text_query: " .. input)
  280. end
  281. end
  282. minetest.close_formspec(pname, formname)
  283. end
  284. end)
  285. -- Function: dropdown_query
  286. -- input: user (string), label (string), options (table of strings), func (function)
  287. -- func input: choice (string)
  288. -- output: calls func on user choice
  289. function modpol.interactions.dropdown_query(user, label, options, func)
  290. -- Add "View..." to the top of the list
  291. table.insert(options,1,"View...")
  292. -- set up formspec
  293. local formspec = {
  294. "formspec_version[4]",
  295. "size[10,4]",
  296. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  297. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  298. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  299. }
  300. local formspec_string = table.concat(formspec, "")
  301. -- present to players
  302. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  303. -- put func in _contexts
  304. if _contexts[user] == nil then _contexts[user] = {} end
  305. _contexts[user]["dropdown_query_func"] = func
  306. end
  307. -- receive fields
  308. minetest.register_on_player_receive_fields(function (player, formname, fields)
  309. if formname == "modpol:dropdown_query" then
  310. local pname = player:get_player_name()
  311. if fields.cancel then
  312. minetest.close_formspec(pname, formname)
  313. elseif fields.input == "View..." then
  314. -- "View...", do nothing
  315. else
  316. local choice = fields.input
  317. local func = _contexts[pname]["dropdown_query_func"]
  318. if not choice then
  319. -- empty, do nothing
  320. elseif func then
  321. --causes issues with sequential dropdowns
  322. --minetest.close_formspec(pname, formname)
  323. func(choice)
  324. else
  325. minetest.close_formspec(pname, formname)
  326. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  327. end
  328. end
  329. end
  330. end)
  331. -- Function: modpol.binary_poll_user(user, question, function)
  332. -- Overrides function at modpol/interactions.lua
  333. -- Params: user (string), question (string), func (function)
  334. -- func input: user input (string: y/n)
  335. -- Output: Applies "func" to user input
  336. function modpol.interactions.binary_poll_user(user, question, func)
  337. -- set up formspec
  338. local formspec = {
  339. "formspec_version[4]",
  340. "size[8,4]",
  341. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  342. "button[1,2.5;1,0.8;yes;Yes]",
  343. "button[2,2.5;1,0.8;no;No]",
  344. --TODO can we enable text wrapping?
  345. --TODO we could use scroll boxes to contain the text
  346. }
  347. local formspec_string = table.concat(formspec, "")
  348. if _contexts[user] == nil then _contexts[user] = {} end
  349. _contexts[user]["binary_poll_func"] = func
  350. -- present to player
  351. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  352. end
  353. minetest.register_on_player_receive_fields(function (player, formname, fields)
  354. local pname = player:get_player_name()
  355. -- modpol:binary_poll
  356. if formname == "modpol:binary_poll_user" then
  357. local vote = nil
  358. if fields.yes then vote = fields.yes
  359. elseif fields.no then vote = fields.no
  360. end
  361. if vote then
  362. local func = _contexts[pname]["binary_poll_func"]
  363. if func then func(vote) end
  364. end
  365. minetest.close_formspec(pname, formname)
  366. end
  367. end)
  368. -- TESTING
  369. --testing command for "singleplayer"
  370. function modpol.msg(text)
  371. modpol.interactions.message("singleplayer",text)
  372. end