interactions.lua 12 KB

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