interactions.lua 21 KB

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