interactions.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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,0.8;test_poll;Test poll]",
  56. "button[2,7;1,0.8;add_org;Add org]",
  57. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  58. "button[5.5,7;1.5,0.8;reset_orgs;Reset orgs]",
  59. "button_exit[8.5,7;1,0.8;close;Close]",
  60. }
  61. local formspec_string = table.concat(formspec, "")
  62. -- present to player
  63. minetest.show_formspec(user, "modpol:dashboard", formspec_string)
  64. end
  65. -- receive input
  66. minetest.register_on_player_receive_fields(function (player, formname, fields)
  67. if formname == "modpol:dashboard" then
  68. local pname = player:get_player_name()
  69. if nil then
  70. -- buttons first
  71. elseif fields.test_poll then
  72. -- FOR TESTING PURPOSES ONLY
  73. modpol.interactions.text_query(
  74. pname,"Poll question:",
  75. function(input)
  76. modpol.interactions.binary_poll_user(
  77. pname, input,
  78. function(vote)
  79. modpol.interactions.message(
  80. pname, pname .. " voted " .. vote)
  81. end)
  82. end)
  83. elseif fields.add_org then
  84. modpol.interactions.add_org(pname, 1)
  85. elseif fields.remove_org then
  86. modpol.interactions.remove_org(pname)
  87. elseif fields.reset_orgs then
  88. modpol.orgs.reset()
  89. modpol.instance:add_member(pname)
  90. modpol.interactions.dashboard(pname)
  91. -- Put all dropdowns at the end
  92. elseif fields.close then
  93. minetest.close_formspec(pname, formname)
  94. elseif fields.all_orgs or fields.user_orgs then
  95. local org_name = fields.all_orgs or fields.user_orgs
  96. modpol.interactions.org_dashboard(pname, org_name)
  97. end
  98. end
  99. end)
  100. -- Function: modpol.interactions.org_dashboard
  101. -- Params: user (string), org_name (string)
  102. -- Output: Displays a menu of org-specific commands to the user
  103. function modpol.interactions.org_dashboard(user, org_name)
  104. -- prepare data
  105. local org = modpol.orgs.get_org(org_name)
  106. if not org then return nil end
  107. local is_member = org:has_member(user)
  108. local membership_toggle = function()
  109. local toggle_code = ""
  110. if is_member then
  111. toggle_code = toggle_code
  112. ..minetest.formspec_escape("leave")..";"
  113. ..minetest.formspec_escape("Leave").."]"
  114. else
  115. toggle_code = toggle_code
  116. ..minetest.formspec_escape("join")..";"
  117. ..minetest.formspec_escape("Join").."]"
  118. end
  119. return toggle_code
  120. end
  121. -- identify parent
  122. local parent = modpol.orgs.get_org(org.parent)
  123. if parent then parent = parent.name
  124. else parent = "none" end
  125. -- prepare children menu
  126. local children = {"View..."}
  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. -- prepare policies menu
  132. local policies = {"View..."}
  133. for k,v in pairs(org.policies) do
  134. table.insert(policies, k .. ": " ..
  135. org.policies[k].process_type)
  136. end
  137. table.insert(policies, "Add policy")
  138. -- prepare processes menu
  139. local processes = {"View..."}
  140. for k,v in ipairs(org.processes) do
  141. local this_request = org.requests[v.request_id]
  142. if type(this_request) == "table" then
  143. local active = ''
  144. if org.pending[user] then
  145. if org.pending[user][v.id] then
  146. active = '*'
  147. end
  148. end
  149. local req_str = "[" .. v.id .. "] " ..
  150. active .. this_request.type
  151. if this_request.params[1] then
  152. req_str = req_str .. ": " ..
  153. table.concat(this_request.params, ", ")
  154. end
  155. table.insert(processes, req_str)
  156. end
  157. end
  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. "label[0.5,0.5;Org: "..
  167. minetest.formspec_escape(org_name).."]",
  168. "label[0.5,1;Parent: "..parent.."]",
  169. "button[8.5,0.5;1,0.8;"..membership_toggle(),
  170. "label[0.5,2;Members:]",
  171. "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(org.members)..";;]",
  172. "label[0.5,3;Children:]",
  173. "dropdown[2,2.5;5,0.8;children;"..formspec_list(children)..";;]",
  174. "label[0.5,4;Policies:]",
  175. "dropdown[2,3.5;5,0.8;policies;"..formspec_list(policies)..";;]",
  176. "label[0.5,5;Processes:]",
  177. "dropdown[2,4.5;5,0.8;processes;"..formspec_list(processes)..";;]",
  178. "button[0.5,7;1,0.8;test_poll;Test poll]",
  179. "button[2,7;1,0.8;add_child;Add child]",
  180. "button[3.5,7;1.5,0.8;remove_org;Remove org]",
  181. "button[8.5,7;1,0.8;back;Back]",
  182. }
  183. local formspec_string = table.concat(formspec, "")
  184. -- present to player
  185. minetest.show_formspec(user, "modpol:org_dashboard", formspec_string)
  186. end
  187. -- receive input
  188. minetest.register_on_player_receive_fields(function (player, formname, fields)
  189. if formname == "modpol:org_dashboard" then
  190. local pname = player:get_player_name()
  191. local org = modpol.orgs.get_org(_contexts[pname].current_org)
  192. if nil then
  193. elseif fields.join then
  194. local new_request = {
  195. user = pname,
  196. type = "add_member",
  197. params = {pname}
  198. }
  199. org:make_request(new_request)
  200. modpol.interactions.org_dashboard(pname,org.name)
  201. elseif fields.leave then
  202. org:remove_member(pname)
  203. modpol.interactions.dashboard(pname)
  204. elseif fields.test_poll then
  205. modpol.interactions.binary_poll_org(
  206. pname, org.id,
  207. function(input)
  208. modpol.interactions.message_org(
  209. pname,
  210. org.id,
  211. "New response: " .. input)
  212. end)
  213. elseif fields.add_child then
  214. modpol.interactions.text_query(
  215. pname, "Child org name:",
  216. function(input)
  217. local new_request = {
  218. user = pname,
  219. type = "add_org",
  220. params = {input}
  221. }
  222. org:make_request(new_request)
  223. modpol.interactions.message(pname,"requested")
  224. modpol.interactions.org_dashboard(
  225. pname,org.name)
  226. end)
  227. elseif fields.remove_org then
  228. local new_request = {
  229. user = pname,
  230. type = "delete",
  231. params = {}
  232. }
  233. org:make_request(new_request)
  234. modpol.interactions.org_dashboard(pname,org.name)
  235. elseif fields.back then
  236. modpol.interactions.dashboard(pname)
  237. -- Put all dropdowns at the end
  238. elseif fields.policies
  239. and fields.policies ~= "View..." then
  240. local policy
  241. if fields.policies == "Add policy" then
  242. policy = nil
  243. elseif fields.policies == "View..." then
  244. return
  245. else
  246. policy = string.match(fields.policies,"(%w+)%:")
  247. end
  248. modpol.interactions.policy_dashboard(
  249. pname, org.id, policy)
  250. elseif fields.processes
  251. and fields.processes ~= "View..." then
  252. local sel = string.match(fields.processes,"%[(%d)%]")
  253. local process = org.processes[tonumber(sel)]
  254. if process then
  255. process:interact(pname)
  256. end
  257. elseif fields.children
  258. and fields.children ~= "View..." then
  259. local org_name = fields.children
  260. modpol.interactions.org_dashboard(pname, org_name)
  261. end
  262. end
  263. end)
  264. -- Function: modpol.interactions.policy_dashboard
  265. -- input: user (string), org_id (int), policy (string)
  266. -- output: opens a dashboard for viewing/editing policy details
  267. -- TODO
  268. function modpol.interactions.policy_dashboard(
  269. user, org_id, policy)
  270. modpol.interactions.message(
  271. user,
  272. "Not yet implemented: " .. policy)
  273. end
  274. -- BASIC INTERACTION FUNCTIONS
  275. -- ===========================
  276. -- Function: modpol.interactions.message
  277. -- input: message (string)
  278. -- output
  279. function modpol.interactions.message(user, message)
  280. minetest.chat_send_player(user, message)
  281. end
  282. -- Function: modpol.interactions.text_query
  283. -- Overrides function at modpol/interactions.lua
  284. -- input: user (string), query (string), func (function)
  285. -- func input: user input (string)
  286. -- output: Applies "func" to user input
  287. function modpol.interactions.text_query(user, query, func)
  288. -- set up formspec
  289. local formspec = {
  290. "formspec_version[4]",
  291. "size[10,4]",
  292. "label[0.5,1;", minetest.formspec_escape(query), "]",
  293. "field[0.5,1.25;9,0.8;input;;]",
  294. "button[0.5,2.5;1,0.8;yes;OK]",
  295. }
  296. local formspec_string = table.concat(formspec, "")
  297. -- present to players
  298. minetest.show_formspec(user, "modpol:text_query", formspec_string)
  299. -- put func in _contexts
  300. if _contexts[user] == nil then _contexts[user] = {} end
  301. _contexts[user]["text_query_func"] = func
  302. end
  303. -- receive fields
  304. minetest.register_on_player_receive_fields(function (player, formname, fields)
  305. if formname == "modpol:text_query" then
  306. local pname = player:get_player_name()
  307. local input = fields.input
  308. if not input then
  309. -- no input, do nothing
  310. else
  311. local func = _contexts[pname]["text_query_func"]
  312. if func then
  313. func(input)
  314. else
  315. modpol.interactions.message(pname, "text_query: " .. input)
  316. end
  317. end
  318. minetest.close_formspec(pname, formname)
  319. end
  320. end)
  321. -- Function: dropdown_query
  322. -- input: user (string), label (string), options (table of strings), func (function)
  323. -- func input: choice (string)
  324. -- output: calls func on user choice
  325. function modpol.interactions.dropdown_query(user, label, options, func)
  326. -- set up formspec
  327. local formspec = {
  328. "formspec_version[4]",
  329. "size[10,4]",
  330. "label[0.5,1;"..minetest.formspec_escape(label).."]",
  331. "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]",
  332. "button[0.5,2.5;1,0.8;cancel;Cancel]",
  333. }
  334. local formspec_string = table.concat(formspec, "")
  335. -- present to players
  336. minetest.show_formspec(user, "modpol:dropdown_query", formspec_string)
  337. -- put func in _contexts
  338. if _contexts[user] == nil then _contexts[user] = {} end
  339. _contexts[user]["dropdown_query_func"] = func
  340. end
  341. -- receive fields
  342. minetest.register_on_player_receive_fields(function (player, formname, fields)
  343. if formname == "modpol:dropdown_query" then
  344. local pname = player:get_player_name()
  345. if fields.cancel ~= "cancel" then
  346. local choice = fields.input
  347. local func = _contexts[pname]["dropdown_query_func"]
  348. if not choice then
  349. -- no choice, do nothing
  350. elseif func then
  351. func(choice)
  352. else
  353. modpol.interactions.message(pname, "dropdown_query: " .. choice)
  354. end
  355. end
  356. minetest.close_formspec(pname, formname)
  357. end
  358. end)
  359. -- SECONDARY INTERACTIONS
  360. -- ======================
  361. -- Function: modpol.binary_poll_user(user, question, function)
  362. -- Overrides function at modpol/interactions.lua
  363. -- Params: user (string), question (string), func (function)
  364. -- func input: user input (string: y/n)
  365. -- Output: Applies "func" to user input
  366. function modpol.interactions.binary_poll_user(user, question, func)
  367. -- set up formspec
  368. local formspec = {
  369. "formspec_version[4]",
  370. "size[5,3]",
  371. "label[0.375,0.5;",minetest.formspec_escape(question), "]",
  372. "button[1,1.5;1,0.8;yes;Yes]",
  373. "button[2,1.5;1,0.8;no;No]",
  374. --TKTK can we enable text wrapping?
  375. --TKTK we could use scroll boxes to contain the text
  376. }
  377. local formspec_string = table.concat(formspec, "")
  378. if _contexts[user] == nil then _contexts[user] = {} end
  379. _contexts[user]["binary_poll_func"] = func
  380. -- present to player
  381. minetest.show_formspec(user, "modpol:binary_poll_user", formspec_string)
  382. end
  383. minetest.register_on_player_receive_fields(function (player, formname, fields)
  384. local pname = player:get_player_name()
  385. -- modpol:binary_poll
  386. if formname == "modpol:binary_poll_user" then
  387. local vote = nil
  388. if fields.yes then vote = fields.yes
  389. elseif fields.no then vote = fields.no
  390. end
  391. if vote then
  392. modpol.interactions.message(pname, "Responded " .. vote)
  393. local func = _contexts[pname]["binary_poll_func"]
  394. if func then func(vote) end
  395. end
  396. minetest.close_formspec(pname, formname)
  397. end
  398. end)
  399. -- COMPLEX INTERACTIONS
  400. -- ====================
  401. -- Function: modpol.interactions.message_org
  402. -- input: initiator (string), org_id (number), message (string)
  403. -- output: broadcasts message to all org members
  404. function modpol.interactions.message_org(initiator, org_id, message)
  405. local org = modpol.orgs.get_org(org_id)
  406. local users = org:list_members()
  407. for k,v in ipairs(users) do
  408. modpol.interactions.message(v, message)
  409. end
  410. end
  411. -- Function: modpol.interactions.binary_poll_org
  412. -- input: initator (user string), org_id (number)
  413. -- output: gets question from initiator, asks all org members, broadcasts answers
  414. -- TODO for testing. This should be implemented as a request.
  415. function modpol.interactions.binary_poll_org(initiator, org_id, func)
  416. local org = modpol.orgs.get_org(org_id)
  417. local users = org:list_members()
  418. modpol.interactions.text_query(
  419. initiator, "Yes/no poll question:",
  420. function(input)
  421. for k,v in ipairs(users) do
  422. modpol.interactions.binary_poll_user(v, input, func)
  423. end
  424. end)
  425. end
  426. -- Function: modpol.interactions.add_org
  427. -- input: initator (user string), base_org_id (ID)
  428. -- output: interaction begins
  429. -- GODMODE
  430. function modpol.interactions.add_org(user, base_org_id)
  431. modpol.interactions.text_query(
  432. user,"Org name:",
  433. function(input)
  434. local base_org = modpol.orgs.get_org(1)
  435. local result = base_org:add_org(input, user)
  436. local message = input .. " created"
  437. modpol.interactions.message(user, message)
  438. modpol.interactions.dashboard(user)
  439. end)
  440. end
  441. -- Function: modpol.interactions.remove_org
  442. -- input: initator (user string)
  443. -- output: interaction begins
  444. -- GODMODE
  445. function modpol.interactions.remove_org(user)
  446. -- start formspec
  447. local orgs_list = modpol.orgs.list_all()
  448. local label = "Choose an org to remove:"
  449. modpol.interactions.dropdown_query(
  450. user, label, orgs_list,
  451. function(input)
  452. if input then
  453. local target_org = modpol.orgs.get_org(input)
  454. local result = target_org:delete()
  455. local message = input .. " deleted"
  456. modpol.interactions.message(user, message)
  457. end
  458. modpol.interactions.dashboard(user)
  459. end)
  460. end