interactions.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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[0.5,7;1.5,0.8;reset_orgs;Reset orgs]",
  56. "button_exit[8.5,7;1,0.8;close;Close]",
  57. }
  58. local formspec_string = table.concat(formspec, "")
  59. -- present to player
  60. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  61. end
  62. -- receive input
  63. minetest.register_on_player_receive_fields(function (player, formname, fields)
  64. if formname == "modpol:dashboard" then
  65. local pname = player:get_player_name()
  66. if nil then
  67. -- buttons first
  68. elseif fields.reset_orgs then
  69. modpol.orgs.reset()
  70. modpol.instance:add_member(pname)
  71. modpol.interactions.dashboard(pname)
  72. -- Put all dropdowns at the end
  73. elseif fields.close then
  74. minetest.close_formspec(pname, formname)
  75. elseif fields.all_orgs or fields.user_orgs then
  76. local org_name = fields.all_orgs or fields.user_orgs
  77. modpol.interactions.org_dashboard(pname, org_name)
  78. end
  79. end
  80. end)
  81. -- Function: modpol.interactions.org_dashboard
  82. -- Params: user (string), org_name (string)
  83. -- Output: Displays a menu of org-specific commands to the user
  84. function modpol.interactions.org_dashboard(user, org_name)
  85. -- prepare data
  86. local org = modpol.orgs.get_org(org_name)
  87. if not org then return nil end
  88. local is_member = org:has_member(user)
  89. local membership_toggle = function()
  90. local toggle_code = ""
  91. if is_member then
  92. toggle_code = toggle_code
  93. ..minetest.formspec_escape("leave")..";"
  94. ..minetest.formspec_escape("Leave").."]"
  95. else
  96. toggle_code = toggle_code
  97. ..minetest.formspec_escape("join")..";"
  98. ..minetest.formspec_escape("Join").."]"
  99. end
  100. return toggle_code
  101. end
  102. -- identify parent
  103. local parent = modpol.orgs.get_org(org.parent)
  104. if parent then parent = parent.name
  105. else parent = "none" end
  106. -- prepare children menu
  107. local children = {"View..."}
  108. for k,v in ipairs(org.children) do
  109. local this_child = modpol.orgs.get_org(v)
  110. table.insert(children, this_child.name)
  111. end
  112. -- prepare modules menu
  113. local modules = {"View..."}
  114. if org.modules then
  115. for k,v in pairs(org.modules) do
  116. table.insert(modules, v.slug)
  117. end
  118. end
  119. -- prepare actions menu
  120. local actions = {"View..."}
  121. local num_actions = 0
  122. if org.pending[user] then
  123. for k,v in pairs(org.pending[user]) do
  124. local action_string = "[" .. k .. "] " ..
  125. org.processes[k].name
  126. table.insert(actions, action_string)
  127. num_actions = num_actions + 1
  128. end
  129. end
  130. -- set player context
  131. local user_context = {}
  132. user_context["current_org"] = org_name
  133. _contexts[user] = user_context
  134. -- set up formspec
  135. local formspec = {
  136. "formspec_version[4]",
  137. "size[10,8]",
  138. "label[0.5,0.5;Org: "..
  139. minetest.formspec_escape(org_name).."]",
  140. "label[0.5,1;Parent: "..parent.."]",
  141. "button[8.5,0.5;1,0.8;"..membership_toggle(),
  142. "label[0.5,2;Members:]",
  143. "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  144. "label[0.5,3;Children:]",
  145. "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
  146. "label[0.5,4;Modules:]",
  147. "dropdown[2,3.5;5,0.8;modules;"..formspec_list(modules)..";;]",
  148. "label[0.5,5;Actions ("..num_actions.."):]",
  149. "dropdown[2,4.5;5,0.8;actions;"..formspec_list(actions)..";;]",
  150. "button[0.5,7;1,0.8;test_poll;Test poll]",
  151. "button[2,7;1,0.8;add_child;Add child]",
  152. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  153. "button[8.5,7;1,0.8;back;Back]",
  154. }
  155. local formspec_string = table.concat(formspec, "")
  156. -- present to player
  157. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  158. end
  159. -- receive input
  160. minetest.register_on_player_receive_fields(function (player, formname, fields)
  161. if formname == "modpol:org_dashboard" then
  162. local pname = player:get_player_name()
  163. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  164. -- just confirm the org still exists:
  165. if not org then
  166. modpol.interactions.message(pname, "Org no longer exists")
  167. modpol.interactions.dashboard(pname)
  168. return end
  169. -- okay, onward
  170. if nil then
  171. elseif fields.join then
  172. org:add_member(pname)
  173. modpol.interactions.org_dashboard(pname,org.name)
  174. elseif fields.leave then
  175. org:remove_member(pname)
  176. modpol.interactions.dashboard(pname)
  177. elseif fields.test_poll then
  178. modpol.interactions.binary_poll_org(
  179. pname, org.id,
  180. function(input)
  181. modpol.interactions.message_org(
  182. pname,
  183. org.id,
  184. "New response: " .. input)
  185. end)
  186. elseif fields.add_child then
  187. modpol.interactions.text_query(
  188. pname, "Child org name:",
  189. function(input)
  190. org:add_org(input,pname)
  191. modpol.interactions.message_org(
  192. pname,
  193. org.id,
  194. "Child org created: " .. input)
  195. end)
  196. elseif fields.remove_org then
  197. modpol.interactions.message_org(
  198. pname,
  199. org.id,
  200. "Removing org: " .. org.name)
  201. org:delete()
  202. modpol.interactions.org_dashboard(pname,org.name)
  203. elseif fields.back then
  204. modpol.interactions.dashboard(pname)
  205. -- Put all dropdowns at the end
  206. -- Receiving modules
  207. elseif fields.modules
  208. and fields.modules ~= "View..." then
  209. local module = fields.modules
  210. modpol.interactions.binary_poll_user(
  211. pname,
  212. modpol.modules[module].name.."\n"..
  213. modpol.modules[module].desc.."\n"..
  214. "Proceed?",
  215. function(input)
  216. if input == "Yes" then
  217. org:call_module(module, pname)
  218. end
  219. end)
  220. -- Receiving actions
  221. elseif fields.actions
  222. and fields.actions ~= "View..." then
  223. local action = string.match(
  224. fields.actions,"%[(%d)%]")
  225. local process = org.processes[tonumber(action)]
  226. if process then
  227. org:interact(process.id, pname)
  228. end
  229. -- Children
  230. elseif fields.children
  231. and fields.children ~= "View..." then
  232. local org_name = fields.children
  233. modpol.interactions.org_dashboard(pname, org_name)
  234. end
  235. end
  236. end)
  237. -- Function: modpol.interactions.policy_dashboard
  238. -- input: user (string), org_id (int), policy (string)
  239. -- output: opens a dashboard for viewing/editing policy details
  240. -- TODO
  241. function modpol.interactions.policy_dashboard(
  242. user, org_id, policy)
  243. modpol.interactions.message(
  244. user,
  245. "Not yet implemented: " .. policy)
  246. end
  247. -- INTERACTION FUNCTIONS
  248. -- =====================
  249. -- Function: modpol.interactions.message
  250. -- input: user (string), message (string)
  251. -- output: displays message to specified user
  252. function modpol.interactions.message(user, message)
  253. if message then
  254. minetest.chat_send_player(user, message)
  255. end
  256. end
  257. -- Function: modpol.interactions.text_query
  258. -- Overrides function at modpol/interactions.lua
  259. -- input: user (string), query (string), func (function)
  260. -- output: Applies "func" to user input
  261. function modpol.interactions.text_query(user, query, func)
  262. -- set up formspec
  263. local formspec = {
  264. "formspec_version[4]",
  265. "size[10,4]",
  266. "label[0.5,1;", minetest.formspec_escape(query), "]",
  267. "field[0.5,1.25;9,0.8;input;;]",
  268. "button[0.5,2.5;1,0.8;yes;OK]",
  269. }
  270. local formspec_string = table.concat(formspec, "")
  271. -- present to player
  272. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  273. -- put func in _contexts
  274. if _contexts[user] == nil then _contexts[user] = {} end
  275. _contexts[user]["text_query_func"] = func
  276. end
  277. -- receive fields
  278. minetest.register_on_player_receive_fields(function (player, formname, fields)
  279. if formname == "modpol:text_query" then
  280. local pname = player:get_player_name()
  281. local input = fields.input
  282. if not input then
  283. -- no input, do nothing
  284. else
  285. local func = _contexts[pname]["text_query_func"]
  286. if func then
  287. func(input)
  288. else
  289. modpol.interactions.message(pname, "text_query: " .. input)
  290. end
  291. end
  292. minetest.close_formspec(pname, formname)
  293. end
  294. end)
  295. -- Function: dropdown_query
  296. -- input: user (string), label (string), options (table of strings), func (function)
  297. -- func input: choice (string)
  298. -- output: calls func on user choice
  299. function modpol.interactions.dropdown_query(user, label, options, func)
  300. -- set up formspec
  301. local formspec = {
  302. "formspec_version[4]",
  303. "size[10,4]",
  304. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  305. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  306. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  307. }
  308. local formspec_string = table.concat(formspec, "")
  309. -- present to players
  310. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  311. -- put func in _contexts
  312. if _contexts[user] == nil then _contexts[user] = {} end
  313. _contexts[user]["dropdown_query_func"] = func
  314. end
  315. -- receive fields
  316. minetest.register_on_player_receive_fields(function (player, formname, fields)
  317. if formname == "modpol:dropdown_query" then
  318. local pname = player:get_player_name()
  319. if fields.cancel == "cancel" then
  320. -- cancel, do nothing
  321. else
  322. local choice = fields.input
  323. local func = _contexts[pname]["dropdown_query_func"]
  324. if not choice then
  325. -- empty, do nothing
  326. elseif func then
  327. func(choice)
  328. else
  329. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  330. end
  331. end
  332. minetest.close_formspec(pname, formname)
  333. end
  334. end)
  335. -- Function: modpol.binary_poll_user(user, question, function)
  336. -- Overrides function at modpol/interactions.lua
  337. -- Params: user (string), question (string), func (function)
  338. -- func input: user input (string: y/n)
  339. -- Output: Applies "func" to user input
  340. function modpol.interactions.binary_poll_user(user, question, func)
  341. -- set up formspec
  342. local formspec = {
  343. "formspec_version[4]",
  344. "size[8,4]",
  345. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  346. "button[1,2.5;1,0.8;yes;Yes]",
  347. "button[2,2.5;1,0.8;no;No]",
  348. --TODO can we enable text wrapping?
  349. --TODO we could use scroll boxes to contain the text
  350. }
  351. local formspec_string = table.concat(formspec, "")
  352. if _contexts[user] == nil then _contexts[user] = {} end
  353. _contexts[user]["binary_poll_func"] = func
  354. -- present to player
  355. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  356. end
  357. minetest.register_on_player_receive_fields(function (player, formname, fields)
  358. local pname = player:get_player_name()
  359. -- modpol:binary_poll
  360. if formname == "modpol:binary_poll_user" then
  361. local vote = nil
  362. if fields.yes then vote = fields.yes
  363. elseif fields.no then vote = fields.no
  364. end
  365. if vote then
  366. local func = _contexts[pname]["binary_poll_func"]
  367. if func then func(vote) end
  368. end
  369. minetest.close_formspec(pname, formname)
  370. end
  371. end)