interactions.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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,1.5;7,0.8;all_orgs;View...,"..formspec_list(all_orgs)..";;]",
  62. "label[0.5,3;Your orgs:]",
  63. "dropdown[2,2.5;7,0.8;user_orgs;View...,"..formspec_list(user_orgs)..";;]",
  64. "label[0.5,4;All users:]",
  65. "dropdown[2,3.5;7,0.8;all_users;View...,"..formspec_list(all_users)..";;]",
  66. "label[0.5,5;Pending ("..user_pending_count.."):]",
  67. "dropdown[2,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. local pending_string = org.processes[k].name
  149. .." ["..k.."]"
  150. table.insert(pending, pending_string)
  151. num_pending = num_pending + 1
  152. end
  153. end
  154. table.sort(pending)
  155. -- set player context
  156. local user_context = {}
  157. user_context["current_org"] = org.name
  158. _contexts[user] = user_context
  159. -- set up formspec
  160. local formspec = {
  161. "formspec_version[4]",
  162. "size[10,8]",
  163. "hypertext[0.5,0.5;9,1;title;<big>Org: <b>"..
  164. minetest.formspec_escape(org.name).."</b>"..membership_toggle(org.name).."</big>]",
  165. "label[0.5,1.25;Parent: "..parent..membership_toggle(parent).."]",
  166. "label[0.5,2;Members:]",
  167. "dropdown[2,1.5;7,0.8;members;View...,"..formspec_list(members)..";;]",
  168. "label[0.5,3;Child orgs:]",
  169. "dropdown[2,2.5;7,0.8;children;View...,"..formspec_list(children)..";;]",
  170. "label[0.5,4;Modules:]",
  171. "dropdown[2,3.5;7,0.8;modules;View...,"..formspec_list(modules)..";;]",
  172. "label[0.5,5;Pending ("..num_pending.."):]",
  173. "dropdown[2,4.5;7,0.8;pending;View...,"..formspec_list(pending)..";;]",
  174. "button[0.5,7;1,0.8;refresh;Refresh]",
  175. "button[8.5,7;1,0.8;back;Back]",
  176. }
  177. local formspec_string = table.concat(formspec, "")
  178. -- present to player
  179. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  180. end
  181. -- receive input
  182. minetest.register_on_player_receive_fields(function (player, formname, fields)
  183. if formname == "modpol:org_dashboard" then
  184. local pname = player:get_player_name()
  185. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  186. -- just confirm the org still exists:
  187. if not org then
  188. modpol.interactions.message(pname, "Org no longer exists")
  189. modpol.interactions.dashboard(pname)
  190. return end
  191. -- okay, onward
  192. if nil then
  193. elseif fields.back then
  194. modpol.interactions.dashboard(pname)
  195. elseif fields.refresh then
  196. modpol.interactions.org_dashboard(pname, org.name)
  197. -- Put all dropdowns at the end
  198. -- Receiving modules
  199. elseif fields.members
  200. and fields.members ~= "View..." then
  201. modpol.interactions.user_dashboard(
  202. pname,
  203. fields.members,
  204. function()
  205. modpol.interactions.org_dashboard(
  206. pname, org.name)
  207. end
  208. )
  209. elseif fields.modules
  210. and fields.modules ~= "View..." then
  211. local module = nil
  212. for k,v in pairs(org.modules) do
  213. if fields.modules == v.name then
  214. module = v
  215. end end
  216. if module then
  217. modpol.interactions.binary_poll_user(
  218. pname,
  219. module.name..":\n"..
  220. module.desc.."\n"..
  221. "Proceed?",
  222. function(input)
  223. if input == "Yes" then
  224. org:call_module(module.slug, pname)
  225. elseif input == "No" then
  226. modpol.interactions.org_dashboard(
  227. pname, org.id)
  228. end
  229. end)
  230. end
  231. -- Receiving pending
  232. elseif fields.pending
  233. and fields.pending ~= "View..." then
  234. local pending = string.match(
  235. fields.pending,"%[(%d)%]")
  236. local process = org.processes[tonumber(pending)]
  237. if process then
  238. org:interact(process.id, pname)
  239. end
  240. -- Children
  241. elseif fields.children
  242. and fields.children ~= "View..." then
  243. local org_name = fields.children
  244. modpol.interactions.org_dashboard(pname, org_name)
  245. end
  246. end
  247. end)
  248. --- Function: modpol.interactions.user_dashboard
  249. -- Displays a dashboard about a particular user
  250. -- @param viewer Name of user viewing the dashboard (string)
  251. -- @param user Name of user being viewed (string)
  252. -- @param completion Optional function to call on Done button
  253. function modpol.interactions.user_dashboard(viewer, user, completion)
  254. local user_orgs = modpol.orgs.user_orgs(user)
  255. -- set player context
  256. local user_context = {}
  257. user_context["viewer"] = viewer
  258. user_context["user"] = user
  259. user_context["completion"] = completion
  260. _contexts[viewer] = user_context
  261. -- set up formspec
  262. local formspec = {
  263. "formspec_version[4]",
  264. "size[10,8]",
  265. "hypertext[0.5,0.5;9,1;title;<big>User: <b>"..user.."</b></big>]",
  266. "label[0.5,2;User's orgs:]",
  267. "dropdown[2,1.5;7,0.8;user_orgs;View...,"..formspec_list(user_orgs)..";;]",
  268. "button[0.5,7;1.5,0.8;message;Message]",
  269. "button_exit[8.5,7;1,0.8;close;Close]",
  270. }
  271. local formspec_string = table.concat(formspec, "")
  272. -- present to player
  273. minetest.show_formspec(viewer, "modpol:user_dashboard", formspec_string)
  274. end
  275. -- receive input
  276. minetest.register_on_player_receive_fields(function (player, formname, fields)
  277. if formname == "modpol:user_dashboard" then
  278. local contexts = _contexts[player:get_player_name()]
  279. -- check fields
  280. if nil then
  281. elseif fields.message then
  282. modpol.interactions.message_user(
  283. contexts.viewer, contexts.user
  284. )
  285. elseif fields.back then
  286. if contexts.completion then
  287. completion()
  288. else
  289. modpol.interactions.dashboard(
  290. contexts.viewer)
  291. end
  292. -- dropdown fields
  293. elseif fields.user_orgs
  294. and fields.user_orgs ~= "View..." then
  295. modpol.interactions.org_dashboard(
  296. contexts.viewer, fields.user_orgs)
  297. end
  298. end
  299. end)
  300. -- INTERACTION PRIMITIVES
  301. -- ======================
  302. -- Function: modpol.interactions.message
  303. -- Produces a brief message to a user
  304. -- input: user (string), message (string)
  305. -- output: displays message to specified user
  306. function modpol.interactions.message(user, message)
  307. if message then
  308. minetest.chat_send_player(user, message)
  309. end
  310. end
  311. --- Function: modpol.interactions.message_user
  312. -- Gets and sends a message from one user to another
  313. -- @param sender Name of user sending (string)
  314. -- @param recipient Name of user receiving (string)
  315. function modpol.interactions.message_user(sender, recipient)
  316. modpol.interactions.text_query(
  317. sender,
  318. "Message for "..recipient..":",
  319. function(input)
  320. modpol.interactions.message(
  321. recipient,
  322. input.." [from "..sender.."]")
  323. end
  324. )
  325. end
  326. --- Function: modpol.interactions.display
  327. -- Displays complex data to a user
  328. -- @param user Name of target user (string)
  329. -- @param title Title of display (string)
  330. -- @param message Content of message (string or table of strings)
  331. -- @param completion Optional function for what happens when user is done
  332. function modpol.interactions.display(
  333. user, title, message, completion)
  334. -- set up contexts
  335. _contexts[user]["completion"] = completion
  336. -- set up output
  337. local output = ""
  338. if type(message) == "table" then
  339. output = table.concat(message,"\n")
  340. elseif type(message) == "string" then
  341. output = message
  342. elseif type(message) == "number" then
  343. output = message
  344. else
  345. modpol.interactions.message(
  346. self.initiator, "Error: message not typed for display")
  347. if completion then completion() else
  348. modpol.intereactions.dashboard(user)
  349. end
  350. return
  351. end
  352. -- set up formspec
  353. local formspec = {
  354. "formspec_version[4]",
  355. "size[10,8]",
  356. "label[0.5,0.5;"..title.."]",
  357. "hypertext[0.5,1;9,5.5;display;<global background=black margin=10>"..output.."]",
  358. "button_exit[8.5,7;1,0.8;done;Done]",
  359. }
  360. local formspec_string = table.concat(formspec, "")
  361. -- present to player
  362. minetest.show_formspec(user, "modpol:display", formspec_string)
  363. end
  364. -- receive fields
  365. minetest.register_on_player_receive_fields(function (player, formname, fields)
  366. local pname = player:get_player_name()
  367. if formname == "modpol:display" then
  368. if fields.done and _contexts[pname].completion then
  369. minetest.close_formspec(pname, formname)
  370. _contexts[pname].completion()
  371. end
  372. end
  373. end)
  374. -- Function: modpol.interactions.text_query
  375. -- Overrides function at modpol/interactions.lua
  376. -- input: user (string), query (string), func (function)
  377. -- output: Applies "func" to user input
  378. function modpol.interactions.text_query(user, query, func)
  379. -- set up formspec
  380. local formspec = {
  381. "formspec_version[4]",
  382. "size[10,4]",
  383. "label[0.5,1;", minetest.formspec_escape(query), "]",
  384. "field[0.5,1.25;9,0.8;input;;]",
  385. "button[0.5,2.5;1,0.8;yes;OK]",
  386. }
  387. local formspec_string = table.concat(formspec, "")
  388. -- present to player
  389. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  390. -- put func in _contexts
  391. if _contexts[user] == nil then _contexts[user] = {} end
  392. _contexts[user]["text_query_func"] = func
  393. end
  394. -- receive fields
  395. minetest.register_on_player_receive_fields(function (player, formname, fields)
  396. if formname == "modpol:text_query" then
  397. local pname = player:get_player_name()
  398. local input = fields.input
  399. if not input then
  400. -- no input, do nothing
  401. else
  402. local func = _contexts[pname]["text_query_func"]
  403. if func then
  404. func(input)
  405. else
  406. modpol.interactions.message(pname, "text_query: " .. input)
  407. end
  408. end
  409. minetest.close_formspec(pname, formname)
  410. end
  411. end)
  412. -- Function: dropdown_query
  413. -- input: user (string), label (string), options (table of strings), func (function)
  414. -- func input: choice (string)
  415. -- output: calls func on user choice
  416. function modpol.interactions.dropdown_query(user, label, options, func)
  417. -- set up formspec
  418. local formspec = {
  419. "formspec_version[4]",
  420. "size[10,4]",
  421. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  422. "dropdown[0.5,1.25;9,0.8;input;View...,"..formspec_list(options)..";;]",
  423. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  424. }
  425. local formspec_string = table.concat(formspec, "")
  426. -- present to players
  427. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  428. -- put func in _contexts
  429. if _contexts[user] == nil then _contexts[user] = {} end
  430. _contexts[user]["dropdown_query_func"] = func
  431. end
  432. -- receive fields
  433. minetest.register_on_player_receive_fields(function (player, formname, fields)
  434. if formname == "modpol:dropdown_query" then
  435. local pname = player:get_player_name()
  436. if fields.cancel then
  437. minetest.close_formspec(pname, formname)
  438. elseif fields.input == "View..." then
  439. -- "View...", do nothing
  440. else
  441. local choice = fields.input
  442. local func = _contexts[pname]["dropdown_query_func"]
  443. if not choice then
  444. -- empty, do nothing
  445. elseif func then
  446. --causes issues with sequential dropdowns
  447. --minetest.close_formspec(pname, formname)
  448. func(choice)
  449. else
  450. minetest.close_formspec(pname, formname)
  451. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  452. end
  453. end
  454. end
  455. end)
  456. -- Function: modpol.binary_poll_user(user, question, function)
  457. -- Overrides function at modpol/interactions.lua
  458. -- Params: user (string), question (string), func (function)
  459. -- func input: user input (string: y/n)
  460. -- Output: Applies "func" to user input
  461. function modpol.interactions.binary_poll_user(user, question, func)
  462. -- set up formspec
  463. local formspec = {
  464. "formspec_version[4]",
  465. "size[8,4]",
  466. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  467. "button[1,2.5;1,0.8;yes;Yes]",
  468. "button[2,2.5;1,0.8;no;No]",
  469. --TODO can we enable text wrapping?
  470. --TODO we could use scroll boxes to contain the text
  471. }
  472. local formspec_string = table.concat(formspec, "")
  473. if _contexts[user] == nil then _contexts[user] = {} end
  474. _contexts[user]["binary_poll_func"] = func
  475. -- present to player
  476. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  477. end
  478. minetest.register_on_player_receive_fields(function (player, formname, fields)
  479. local pname = player:get_player_name()
  480. -- modpol:binary_poll
  481. if formname == "modpol:binary_poll_user" then
  482. local vote = nil
  483. if fields.yes then vote = fields.yes
  484. elseif fields.no then vote = fields.no
  485. end
  486. if vote then
  487. local func = _contexts[pname]["binary_poll_func"]
  488. if func then func(vote) end
  489. end
  490. minetest.close_formspec(pname, formname)
  491. end
  492. end)
  493. -- TESTING
  494. --testing command for "singleplayer"
  495. function modpol.msg(text)
  496. modpol.interactions.message("singleplayer",text)
  497. end