interactions.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.list_users()
  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. if nil then
  165. elseif fields.join then
  166. org:add_member(pname)
  167. modpol.interactions.org_dashboard(pname,org.name)
  168. elseif fields.leave then
  169. org:remove_member(pname)
  170. modpol.interactions.dashboard(pname)
  171. elseif fields.test_poll then
  172. modpol.interactions.binary_poll_org(
  173. pname, org.id,
  174. function(input)
  175. modpol.interactions.message_org(
  176. pname,
  177. org.id,
  178. "New response: " .. input)
  179. end)
  180. elseif fields.add_child then
  181. modpol.interactions.text_query(
  182. pname, "Child org name:",
  183. function(input)
  184. org:add_org(input,pname)
  185. modpol.interactions.message_org(
  186. pname,
  187. org.id,
  188. "Child org created: " .. input)
  189. end)
  190. elseif fields.remove_org then
  191. modpol.interactions.message_org(
  192. pname,
  193. org.id,
  194. "Removing org: " .. org.name)
  195. org:delete()
  196. modpol.interactions.org_dashboard(pname,org.name)
  197. elseif fields.back then
  198. modpol.interactions.dashboard(pname)
  199. -- Put all dropdowns at the end
  200. -- Receiving modules
  201. elseif fields.modules
  202. and fields.modules ~= "View..." then
  203. local module = fields.modules
  204. org:call_module(module, pname)
  205. modpol.interactions.org_dashboard(pname,org.name)
  206. -- Receiving actions
  207. elseif fields.actions
  208. and fields.actions ~= "View..." then
  209. local action = string.match(
  210. fields.actions,"%[(%d)%]")
  211. local process = org.processes[tonumber(action)]
  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. -- BASIC 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. minetest.chat_send_player(user, message)
  240. end
  241. -- Function: modpol.interactions.text_query
  242. -- Overrides function at modpol/interactions.lua
  243. -- input: user (string), query (string), func (function)
  244. -- output: Applies "func" to user input
  245. function modpol.interactions.text_query(user, query, func)
  246. -- set up formspec
  247. local formspec = {
  248. "formspec_version[4]",
  249. "size[10,4]",
  250. "label[0.5,1;", minetest.formspec_escape(query), "]",
  251. "field[0.5,1.25;9,0.8;input;;]",
  252. "button[0.5,2.5;1,0.8;yes;OK]",
  253. }
  254. local formspec_string = table.concat(formspec, "")
  255. -- present to player
  256. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  257. -- put func in _contexts
  258. if _contexts[user] == nil then _contexts[user] = {} end
  259. _contexts[user]["text_query_func"] = func
  260. end
  261. -- receive fields
  262. minetest.register_on_player_receive_fields(function (player, formname, fields)
  263. if formname == "modpol:text_query" then
  264. local pname = player:get_player_name()
  265. local input = fields.input
  266. if not input then
  267. -- no input, do nothing
  268. else
  269. local func = _contexts[pname]["text_query_func"]
  270. if func then
  271. func(input)
  272. else
  273. modpol.interactions.message(pname, "text_query: " .. input)
  274. end
  275. end
  276. minetest.close_formspec(pname, formname)
  277. end
  278. end)
  279. -- Function: dropdown_query
  280. -- input: user (string), label (string), options (table of strings), func (function)
  281. -- func input: choice (string)
  282. -- output: calls func on user choice
  283. function modpol.interactions.dropdown_query(user, label, options, func)
  284. -- set up formspec
  285. local formspec = {
  286. "formspec_version[4]",
  287. "size[10,4]",
  288. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  289. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  290. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  291. }
  292. local formspec_string = table.concat(formspec, "")
  293. -- present to players
  294. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  295. -- put func in _contexts
  296. if _contexts[user] == nil then _contexts[user] = {} end
  297. _contexts[user]["dropdown_query_func"] = func
  298. end
  299. -- receive fields
  300. minetest.register_on_player_receive_fields(function (player, formname, fields)
  301. if formname == "modpol:dropdown_query" then
  302. local pname = player:get_player_name()
  303. if fields.cancel ~= "cancel" then
  304. local choice = fields.input
  305. local func = _contexts[pname]["dropdown_query_func"]
  306. if not choice then
  307. -- no choice, do nothing
  308. elseif func then
  309. func(choice)
  310. else
  311. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  312. end
  313. end
  314. minetest.close_formspec(pname, formname)
  315. end
  316. end)
  317. -- Function: modpol.binary_poll_user(user, question, function)
  318. -- Overrides function at modpol/interactions.lua
  319. -- Params: user (string), question (string), func (function)
  320. -- func input: user input (string: y/n)
  321. -- Output: Applies "func" to user input
  322. function modpol.interactions.binary_poll_user(user, question, func)
  323. -- set up formspec
  324. local formspec = {
  325. "formspec_version[4]",
  326. "size[5,3]",
  327. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  328. "button[1,1.5;1,0.8;yes;Yes]",
  329. "button[2,1.5;1,0.8;no;No]",
  330. --TODO can we enable text wrapping?
  331. --TODO we could use scroll boxes to contain the text
  332. }
  333. local formspec_string = table.concat(formspec, "")
  334. if _contexts[user] == nil then _contexts[user] = {} end
  335. _contexts[user]["binary_poll_func"] = func
  336. -- present to player
  337. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  338. end
  339. minetest.register_on_player_receive_fields(function (player, formname, fields)
  340. local pname = player:get_player_name()
  341. -- modpol:binary_poll
  342. if formname == "modpol:binary_poll_user" then
  343. local vote = nil
  344. if fields.yes then vote = fields.yes
  345. elseif fields.no then vote = fields.no
  346. end
  347. if vote then
  348. modpol.interactions.message(pname, "Responded " .. vote)
  349. local func = _contexts[pname]["binary_poll_func"]
  350. if func then func(vote) end
  351. end
  352. minetest.close_formspec(pname, formname)
  353. end
  354. end)