interactions.lua 12 KB

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