interactions.lua 21 KB

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