interactions.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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_count = 0
  46. local user_pending = {}
  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. "hypertext[0.5,0.5;9,1;title;<big>Org dashboard</big>]",
  60. "label[0.5,2;All orgs:]",
  61. "dropdown[2.5,1.5;7,0.8;all_orgs;View...,"..formspec_list(all_orgs)..";;]",
  62. "label[0.5,3;Your orgs:]",
  63. "dropdown[2.5,2.5;7,0.8;user_orgs;View...,"..formspec_list(user_orgs)..";;]",
  64. "label[0.5,4;All users:]",
  65. "dropdown[2.5,3.5;7,0.8;all_users;View...,"..formspec_list(all_users)..";;]",
  66. "label[0.5,5;Pending ("..user_pending_count.."):]",
  67. "dropdown[2.5,4.5;7,0.8;pending;View...,"..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_users
  86. and fields.all_users ~= "View..." then
  87. modpol.interactions.user_dashboard(
  88. pname,
  89. fields.all_users,
  90. function()
  91. modpol.interactions.dashboard(pname)
  92. end
  93. )
  94. elseif fields.all_orgs or fields.user_orgs or fields.pending then
  95. local org_name = fields.all_orgs or fields.user_orgs or fields.pending
  96. if org_name ~= "View..." then
  97. modpol.interactions.org_dashboard(
  98. pname, org_name)
  99. end
  100. end
  101. end
  102. end)
  103. -- Function: modpol.interactions.org_dashboard
  104. -- Params: user (string), org_string (string or num)
  105. -- Output: Displays a menu of org-specific commands to the user
  106. function modpol.interactions.org_dashboard(user, org_string)
  107. -- prepare data
  108. local org = modpol.orgs.get_org(org_string)
  109. if not org then return nil end
  110. local function membership_toggle(org_display)
  111. local current_org = modpol.orgs.get_org(org_display)
  112. if current_org then
  113. if current_org:has_member(user) then
  114. return " (member)"
  115. end
  116. end
  117. return ""
  118. end
  119. -- identify parent
  120. local parent = modpol.orgs.get_org(org.parent)
  121. if parent then parent = parent.name
  122. else parent = "none" end
  123. -- prepare members menu
  124. local members = org.members
  125. -- prepare children menu
  126. local children = {}
  127. for k,v in ipairs(org.children) do
  128. local this_child = modpol.orgs.get_org(v)
  129. table.insert(children, this_child.name)
  130. end
  131. table.sort(children)
  132. -- prepare modules menu
  133. local modules = {}
  134. if modpol.modules then
  135. for k,v in pairs(modpol.modules) do
  136. if not v.hide and -- hide utility modules
  137. org.policies[k] then -- org includes it
  138. local module_entry = v.name
  139. table.insert(modules, module_entry)
  140. end
  141. end
  142. end
  143. table.sort(modules)
  144. -- prepare pending menu
  145. local pending = {}
  146. local num_pending = 0
  147. if org.pending[user] then
  148. for k,v in pairs(org.pending[user]) do
  149. if org.processes[k] ~= "deleted" then
  150. local pending_string = org.processes[k].name
  151. .." ["..k.."]"
  152. table.insert(pending, pending_string)
  153. num_pending = num_pending + 1
  154. end
  155. end
  156. end
  157. table.sort(pending)
  158. -- set player context
  159. local user_context = {}
  160. user_context["current_org"] = org.name
  161. _contexts[user] = user_context
  162. -- set up formspec
  163. local formspec = {
  164. "formspec_version[4]",
  165. "size[10,8]",
  166. "hypertext[0.5,0.5;9,1;title;<big>Org: <b>"..
  167. minetest.formspec_escape(org.name).."</b>"..membership_toggle(org.name).."</big>]",
  168. "label[0.5,1.25;Parent: "..parent..membership_toggle(parent).."]",
  169. "label[0.5,2;Members:]",
  170. "dropdown[2.5,1.5;7,0.8;members;View...,"..formspec_list(members)..";;]",
  171. "label[0.5,3;Child orgs:]",
  172. "dropdown[2.5,2.5;7,0.8;children;View...,"..formspec_list(children)..";;]",
  173. "label[0.5,4;Modules:]",
  174. "dropdown[2.5,3.5;7,0.8;modules;View...,"..formspec_list(modules)..";;]",
  175. "label[0.5,5;Pending ("..num_pending.."):]",
  176. "dropdown[2.5,4.5;7,0.8;pending;View...,"..formspec_list(pending)..";;]",
  177. "button[0.5,7;1,0.8;refresh;Refresh]",
  178. "button[8.5,7;1,0.8;back;Back]",
  179. }
  180. local formspec_string = table.concat(formspec, "")
  181. -- present to player
  182. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  183. end
  184. -- receive input
  185. minetest.register_on_player_receive_fields(function (player, formname, fields)
  186. if formname == "modpol:org_dashboard" then
  187. local pname = player:get_player_name()
  188. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  189. -- just confirm the org still exists:
  190. if not org then
  191. modpol.interactions.message(pname, "Org no longer exists")
  192. modpol.interactions.dashboard(pname)
  193. return end
  194. -- okay, onward
  195. if nil then
  196. elseif fields.back then
  197. modpol.interactions.dashboard(pname)
  198. elseif fields.refresh then
  199. modpol.interactions.org_dashboard(pname, org.name)
  200. -- Put all dropdowns at the end
  201. -- Receiving modules
  202. elseif fields.members
  203. and fields.members ~= "View..." then
  204. modpol.interactions.user_dashboard(
  205. pname,
  206. fields.members,
  207. function()
  208. modpol.interactions.org_dashboard(
  209. pname, org.name)
  210. end
  211. )
  212. elseif fields.modules
  213. and fields.modules ~= "View..." then
  214. local module = nil
  215. for k,v in pairs(modpol.modules) do
  216. if fields.modules == v.name
  217. and org.policies[v.slug] then
  218. module = v
  219. end end
  220. if module then
  221. modpol.interactions.binary_poll_user(
  222. pname,
  223. module.name..":\n"..
  224. module.desc.."\n"..
  225. modpol.interactions.get_policy_string(
  226. org.name, module.slug, ", ")..
  227. "\n".."Proceed?",
  228. function(input)
  229. if input == "Yes" then
  230. org:call_module(module.slug, pname)
  231. elseif input == "No" then
  232. modpol.interactions.org_dashboard(
  233. pname, org.id)
  234. end
  235. end)
  236. end
  237. -- Receiving pending
  238. elseif fields.pending
  239. and fields.pending ~= "View..." then
  240. local pending = string.match(
  241. fields.pending,"%[(%d)%]")
  242. local process = org.processes[tonumber(pending)]
  243. if process then
  244. org:interact(process.id, pname)
  245. end
  246. -- Children
  247. elseif fields.children
  248. and fields.children ~= "View..." then
  249. local org_name = fields.children
  250. modpol.interactions.org_dashboard(pname, org_name)
  251. end
  252. end
  253. end)
  254. --- Function: modpol.interactions.user_dashboard
  255. -- Displays a dashboard about a particular user
  256. -- @param viewer Name of user viewing the dashboard (string)
  257. -- @param user Name of user being viewed (string)
  258. -- @param completion Optional function to call on Done button
  259. function modpol.interactions.user_dashboard(viewer, user, completion)
  260. local user_orgs = modpol.orgs.user_orgs(user)
  261. -- set player context
  262. local user_context = {}
  263. user_context["viewer"] = viewer
  264. user_context["user"] = user
  265. user_context["completion"] = completion
  266. _contexts[viewer] = user_context
  267. -- set up formspec
  268. local formspec = {
  269. "formspec_version[4]",
  270. "size[10,8]",
  271. "hypertext[0.5,0.5;9,1;title;<big>User: <b>"..user.."</b></big>]",
  272. "label[0.5,2;User's orgs:]",
  273. "dropdown[2.5,1.5;7,0.8;user_orgs;View...,"..formspec_list(user_orgs)..";;]",
  274. "button[0.5,7;1.5,0.8;message;Message]",
  275. "button_exit[8.5,7;1,0.8;close;Close]",
  276. }
  277. local formspec_string = table.concat(formspec, "")
  278. -- present to player
  279. minetest.show_formspec(viewer, "modpol:user_dashboard", formspec_string)
  280. end
  281. -- receive input
  282. minetest.register_on_player_receive_fields(function (player, formname, fields)
  283. if formname == "modpol:user_dashboard" then
  284. local contexts = _contexts[player:get_player_name()]
  285. -- check fields
  286. if nil then
  287. elseif fields.message then
  288. modpol.interactions.message_user(
  289. contexts.viewer, contexts.user
  290. )
  291. elseif fields.back then
  292. if contexts.completion then
  293. completion()
  294. else
  295. modpol.interactions.dashboard(
  296. contexts.viewer)
  297. end
  298. -- dropdown fields
  299. elseif fields.user_orgs
  300. and fields.user_orgs ~= "View..." then
  301. modpol.interactions.org_dashboard(
  302. contexts.viewer, fields.user_orgs)
  303. end
  304. end
  305. end)
  306. -- INTERACTION PRIMITIVES
  307. -- ======================
  308. -- Function: modpol.interactions.message
  309. -- Produces a brief message to a user
  310. -- input: user (string), message (string)
  311. -- output: displays message to specified user
  312. function modpol.interactions.message(user, message)
  313. if message then
  314. minetest.chat_send_player(user, message)
  315. end
  316. end
  317. --- Function: modpol.interactions.message_user
  318. -- Gets and sends a message from one user to another
  319. -- @param sender Name of user sending (string)
  320. -- @param recipient Name of user receiving (string)
  321. function modpol.interactions.message_user(sender, recipient)
  322. modpol.interactions.text_query(
  323. sender,
  324. "Message for "..recipient..":",
  325. function(input)
  326. modpol.interactions.message(
  327. recipient,
  328. input.." [from "..sender.."]")
  329. end
  330. )
  331. end
  332. --- Function: modpol.interactions.display
  333. -- Displays complex data to a user
  334. -- @param user Name of target user (string)
  335. -- @param title Title of display (string)
  336. -- @param message Content of message (string or table of strings)
  337. -- @param completion Optional function for what happens when user is done
  338. function modpol.interactions.display(
  339. user, title, message, completion)
  340. -- set up contexts
  341. _contexts[user]["completion"] = completion
  342. -- set up output
  343. local output = ""
  344. if type(message) == "table" then
  345. output = table.concat(message,"\n")
  346. elseif type(message) == "string" then
  347. output = message
  348. elseif type(message) == "number" then
  349. output = message
  350. else
  351. modpol.interactions.message(
  352. self.initiator, "Error: message not typed for display")
  353. if completion then completion() else
  354. modpol.intereactions.dashboard(user)
  355. end
  356. return
  357. end
  358. -- set up formspec
  359. local formspec = {
  360. "formspec_version[4]",
  361. "size[10,8]",
  362. "label[0.5,0.5;"..title.."]",
  363. "hypertext[0.5,1;9,5.5;display;<global background=black margin=10>"..output.."]",
  364. "button_exit[8.5,7;1,0.8;done;Done]",
  365. }
  366. local formspec_string = table.concat(formspec, "")
  367. -- present to player
  368. minetest.show_formspec(user, "modpol:display", formspec_string)
  369. end
  370. -- receive fields
  371. minetest.register_on_player_receive_fields(function (player, formname, fields)
  372. local pname = player:get_player_name()
  373. if formname == "modpol:display" then
  374. if fields.done and _contexts[pname].completion then
  375. minetest.close_formspec(pname, formname)
  376. _contexts[pname].completion()
  377. end
  378. end
  379. end)
  380. -- Function: modpol.interactions.text_query
  381. -- Overrides function at modpol/interactions.lua
  382. -- input: user (string), query (string), func (function)
  383. -- output: Applies "func" to user input
  384. function modpol.interactions.text_query(user, query, func)
  385. -- set up formspec
  386. local formspec = {
  387. "formspec_version[4]",
  388. "size[10,6]",
  389. "label[0.5,1;", minetest.formspec_escape(query), "]",
  390. "field[0.5,3.25;9,0.8;input;;]",
  391. "button[0.5,4.5;1,0.8;yes;OK]",
  392. }
  393. local formspec_string = table.concat(formspec, "")
  394. -- present to player
  395. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  396. -- put func in _contexts
  397. if _contexts[user] == nil then _contexts[user] = {} end
  398. _contexts[user]["text_query_func"] = func
  399. end
  400. -- receive fields
  401. minetest.register_on_player_receive_fields(function (player, formname, fields)
  402. if formname == "modpol:text_query" then
  403. local pname = player:get_player_name()
  404. local input = fields.input
  405. if not input then
  406. -- no input, do nothing
  407. else
  408. local func = _contexts[pname]["text_query_func"]
  409. if func then
  410. func(input)
  411. else
  412. modpol.interactions.message(pname, "text_query: " .. input)
  413. end
  414. end
  415. minetest.close_formspec(pname, formname)
  416. end
  417. end)
  418. -- Function: dropdown_query
  419. -- input: user (string), label (string), options (table of strings), func (function)
  420. -- func input: choice (string)
  421. -- output: calls func on user choice
  422. function modpol.interactions.dropdown_query(user, label, options, func)
  423. -- set up formspec
  424. local formspec = {
  425. "formspec_version[4]",
  426. "size[10,4]",
  427. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  428. "dropdown[0.5,1.25;9,0.8;input;View...,"..formspec_list(options)..";;]",
  429. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  430. }
  431. local formspec_string = table.concat(formspec, "")
  432. -- present to players
  433. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  434. -- put func in _contexts
  435. if _contexts[user] == nil then _contexts[user] = {} end
  436. _contexts[user]["dropdown_query_func"] = func
  437. end
  438. -- receive fields
  439. minetest.register_on_player_receive_fields(function (player, formname, fields)
  440. if formname == "modpol:dropdown_query" then
  441. local pname = player:get_player_name()
  442. if fields.cancel then
  443. minetest.close_formspec(pname, formname)
  444. elseif fields.input == "View..." then
  445. -- "View...", do nothing
  446. else
  447. local choice = fields.input
  448. local func = _contexts[pname]["dropdown_query_func"]
  449. if not choice then
  450. -- empty, do nothing
  451. elseif func then
  452. --causes issues with sequential dropdowns
  453. --minetest.close_formspec(pname, formname)
  454. func(choice)
  455. else
  456. minetest.close_formspec(pname, formname)
  457. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  458. end
  459. end
  460. end
  461. end)
  462. --- Function: modpol.interactions.checkbox_query
  463. -- Allows user to select from a set of options
  464. -- @param user Name of user (string)
  465. -- @param label Query for user before options (string)
  466. -- @param options table of options and their checked status in the form {{"option_1_string", true}, {"option_2_string", false}}
  467. -- @param func function to be called with param "input", made up of the corrected table in the same format as the param options
  468. function modpol.interactions.checkbox_query(
  469. user, label, options, func)
  470. -- set up formspec
  471. -- prepare options
  472. local vertical = 0
  473. local checkbox_options = {}
  474. for i,v in ipairs(options) do
  475. local fs_line = ""
  476. vertical = i * .5
  477. fs_line = "checkbox[0,"..vertical..";checkbox_"..i..";"..
  478. minetest.formspec_escape(v[1])..";"..
  479. tostring(v[2]).."]"
  480. table.insert(checkbox_options, fs_line)
  481. end
  482. local max = vertical * 4
  483. local bar_height = vertical / 2
  484. local formspec = {
  485. "formspec_version[4]",
  486. "size[10,8]",
  487. "label[0.5,0.5;"..label.."]",
  488. "scrollbaroptions[arrows=default;max="..max..";smallstep=10;largestep=100;thumbsize="..bar_height.."]",
  489. "scrollbar[9,1;0.3,5.5;vertical;scroller;0]",
  490. "scroll_container[0.5,1;9,5.5;scroller;vertical]",
  491. }
  492. -- insert options
  493. for i,v in ipairs(checkbox_options) do
  494. table.insert(formspec, v)
  495. end
  496. table.insert(formspec,"scroll_container_end[]")
  497. table.insert(formspec,"button[0.5,7;1.5,0.8;submit;Submit]")
  498. table.insert(
  499. formspec,"button_exit[8,7;1.5,0.8;cancel;Cancel]")
  500. local formspec_string = table.concat(formspec, "")
  501. -- present to players
  502. minetest.show_formspec(user, "modpol:checkbox_query", formspec_string)
  503. -- put func in _contexts
  504. if _contexts[user] == nil then _contexts[user] = {} end
  505. _contexts[user]["checkbox_query_func"] = func
  506. _contexts[user]["checkbox_query_result"] =
  507. modpol.util.copy_table(options)
  508. end
  509. -- receive fields
  510. minetest.register_on_player_receive_fields(function (player, formname, fields)
  511. if formname == "modpol:checkbox_query" then
  512. local pname = player:get_player_name()
  513. -- start checking fields
  514. if fields.cancel then
  515. minetest.close_formspec(pname, formname)
  516. elseif fields.submit then
  517. -- send in result
  518. minetest.close_formspec(pname, formname)
  519. _contexts[pname].checkbox_query_func(
  520. _contexts[pname].checkbox_query_result)
  521. else
  522. for k,v in pairs(fields) do
  523. -- identify checkbox actions and flip bool
  524. if string.find(k,"checkbox_") then
  525. local index = tonumber(
  526. string.match(k,"%d+"))
  527. _contexts[pname].checkbox_query_result[index][2] =
  528. not _contexts[pname].checkbox_query_result[index][2]
  529. end
  530. end
  531. end
  532. end
  533. end)
  534. -- Function: modpol.binary_poll_user(user, question, function)
  535. -- Overrides function at modpol/interactions.lua
  536. -- Params: user (string), question (string), func (function)
  537. -- func input: user input (string: y/n)
  538. -- Output: Applies "func" to user input
  539. function modpol.interactions.binary_poll_user(user, question, func)
  540. -- set up formspec
  541. local formspec = {
  542. "formspec_version[4]",
  543. "size[8,6]",
  544. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  545. "button[1,5;1,0.8;yes;Yes]",
  546. "button[2,5;1,0.8;no;No]",
  547. --TODO can we enable text wrapping?
  548. --TODO we could use scroll boxes to contain the text
  549. }
  550. local formspec_string = table.concat(formspec, "")
  551. if _contexts[user] == nil then _contexts[user] = {} end
  552. _contexts[user]["binary_poll_func"] = func
  553. -- present to player
  554. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  555. end
  556. minetest.register_on_player_receive_fields(function (player, formname, fields)
  557. local pname = player:get_player_name()
  558. -- modpol:binary_poll
  559. if formname == "modpol:binary_poll_user" then
  560. local vote = nil
  561. if fields.yes then vote = fields.yes
  562. elseif fields.no then vote = fields.no
  563. end
  564. if vote then
  565. local func = _contexts[pname]["binary_poll_func"]
  566. if func then func(vote) end
  567. end
  568. minetest.close_formspec(pname, formname)
  569. end
  570. end)
  571. -- TESTING
  572. --testing command for "singleplayer"
  573. function modpol.msg(text)
  574. modpol.interactions.message("singleplayer",text)
  575. end