interactions.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. -- INTERACTIONS.LUA (CLI)
  2. -- User interaction functions for Modpol
  3. -- Called by modpol.lua
  4. modpol.interactions = {}
  5. -- DASHBOARDS
  6. -- ==========
  7. -- Function: modpol.interactions.dashboard(user)
  8. -- Params: user (string)
  9. -- Q: Should this return a menu of commands relevant to the specific user?
  10. -- Output: Displays a menu of commands to the user
  11. -- TKTK currently just prints all of modpol---needs major improvement
  12. function modpol.interactions.dashboard(user)
  13. -- adds user to root org if not already in it
  14. if not modpol.instance:has_member(user) then
  15. modpol.instance:add_member(user)
  16. end
  17. local all_users = modpol.instance:list_members()
  18. print('\n-=< MODPOL DASHBOARD >=-')
  19. print('All orgs: (user orgs indicated by *)')
  20. for id, org in ipairs(modpol.orgs.array) do
  21. if type(org) == "table" then
  22. local indicator = ""
  23. if org:has_member(user) then indicator = "*" end
  24. print('['..id..'] '..indicator..org.name)
  25. end
  26. end
  27. -- pending list
  28. local user_pending = {}
  29. local user_pending_count = 0
  30. for k,v in ipairs(modpol.orgs.array) do
  31. if v.pending and v.pending[user] then
  32. if modpol.util.num_pairs(v.pending[user]) ~= 0 then
  33. table.insert(user_pending, v.name)
  34. user_pending_count = user_pending_count + 1
  35. end
  36. end
  37. end
  38. print('Pending actions in: '..table.concat(user_pending,', '))
  39. print('All users: ' .. table.concat(all_users, ', '))
  40. print()
  41. print("Commands: (O)rg, (U)ser, (R)eset, Enter to close")
  42. local sel = io.read()
  43. if sel == "O" or sel == "o" then
  44. print('Access which org id?')
  45. sel = io.read()
  46. print()
  47. if modpol.orgs.array[tonumber(sel)] then
  48. local sel_org = modpol.orgs.array[tonumber(sel)].name
  49. modpol.interactions.org_dashboard(user, sel_org)
  50. else
  51. print("Org id not found")
  52. modpol.interactions.dashboard(user)
  53. end
  54. elseif sel == "U" or sel == "u" then
  55. print("Access which user?")
  56. sel = io.read()
  57. print()
  58. if modpol.instance:has_member(sel) then
  59. modpol.interactions.user_dashboard(
  60. user, sel,
  61. function()
  62. modpol.interactions.dashboard(user)
  63. end
  64. )
  65. elseif sel == "" then
  66. return
  67. else
  68. print("User name not found")
  69. modpol.interactions.dashboard(user)
  70. end
  71. elseif sel == "R" or sel == "r" then
  72. modpol.instance.members = {}
  73. modpol.orgs.reset()
  74. print("Orgs and users reset")
  75. modpol.interactions.dashboard(user)
  76. else
  77. print("Invalid input, try again")
  78. modpol.interactions.dashboard(user)
  79. end
  80. end
  81. -- Function: modpol.interactions.org_dashboard
  82. -- Params: user (string), org_string (string or id)
  83. -- Output: Displays a menu of org-specific commands to the user
  84. function modpol.interactions.org_dashboard(user, org_string)
  85. local org = modpol.orgs.get_org(org_string)
  86. if not org then return nil end
  87. -- identify parent
  88. local parent = ""
  89. if org.id == 1 then
  90. parent = "none"
  91. else
  92. parent = modpol.orgs.get_org(org.parent).name
  93. end
  94. -- identify children
  95. local children = {}
  96. for k,v in ipairs(org.children) do
  97. local this_child = modpol.orgs.get_org(v)
  98. table.insert(children, this_child.name)
  99. end
  100. -- prepare modules menu
  101. local modules = {}
  102. if org.modules then
  103. for k,v in pairs(org.modules) do
  104. if not v.hide then -- hide utility modules
  105. local module_entry = v.slug
  106. table.insert(modules, module_entry)
  107. end
  108. end
  109. end
  110. table.sort(modules)
  111. -- list pending
  112. local process_msg = #org.processes .. " total processes"
  113. if org.pending[user] then
  114. process_msg = process_msg .. " (" ..
  115. modpol.util.num_pairs(org.pending[user]) .. " pending)"
  116. else
  117. process_msg = process_msg .. " (0 pending)"
  118. end
  119. -- set up output
  120. print('\n-=< ORG DASHBOARD >=-')
  121. print("Org: " .. org.name)
  122. print("Parent: " .. parent)
  123. print("Members: " .. table.concat(org.members, ", "))
  124. print("Child orgs: " .. table.concat(children, ", "))
  125. print("Modules: " .. table.concat(modules, ", "))
  126. print("Pending: " .. process_msg)
  127. print()
  128. print("Commands: (M)odules, (P)ending, (B)ack")
  129. local sel = io.read()
  130. print()
  131. if sel == 'm' or sel == 'M' then
  132. print("Type module name: ")
  133. local module_sel = io.read()
  134. print()
  135. local module_result = false
  136. for k,v in ipairs(modules) do
  137. if v == module_sel then
  138. module_result = true
  139. end
  140. end
  141. if module_result then
  142. org:call_module(module_sel, user)
  143. else
  144. print("Error: Module not found.")
  145. end
  146. elseif sel == 'p' or sel == 'P' then
  147. local processes = {}
  148. print("All processes: (* indicates pending)")
  149. for i,v in ipairs(org.processes) do
  150. local active = ''
  151. if org.pending[user] then
  152. if org.pending[user][v.id] then
  153. active = '*'
  154. end
  155. end
  156. print("["..v.id.."] "..v.slug..active)
  157. end
  158. print()
  159. print("Interact with which one (use [id] number)?")
  160. local to_interact = io.read()
  161. local process = org.processes[tonumber(to_interact)]
  162. if not process then return end
  163. if org:has_pending_actions(user) then
  164. if org.pending[user][process.id] then
  165. org:interact(process.id, user)
  166. end
  167. end
  168. elseif sel == 'b' or sel == 'B' then
  169. modpol.interactions.dashboard(user)
  170. else
  171. print("Command not found")
  172. modpol.interactions.org_dashboard(user, org.name)
  173. end
  174. end
  175. --- Function: modpol.interactions.user_dashboard
  176. -- Displays a dashboard about a particular user
  177. -- @param viewer Name of user viewing the dashboard (string)
  178. -- @param user Name of user being viewed (string)
  179. -- @param completion Optional function to call on Done button
  180. function modpol.interactions.user_dashboard(viewer, user, completion)
  181. local user_orgs = {}
  182. local user_modules = {}
  183. print("\n-=< USER DASHBOARD: "..user.." >=-")
  184. print("User's orgs:")
  185. for id, org in ipairs(modpol.orgs.array) do
  186. if type(org) == "table" then
  187. if org:has_member(user) then
  188. print(org.name)
  189. end
  190. end
  191. end
  192. print()
  193. print("Commands: (M)essage user, Enter when done")
  194. local sel = io.read()
  195. if sel == "M" or sel == "m" then
  196. modpol.interactions.message_user(
  197. viewer, user)
  198. completion()
  199. else
  200. completion()
  201. end
  202. end
  203. -- buttons: message, done
  204. -- INTERACTION PRIMITIVES
  205. -- ======================
  206. -- Function: modpol.interactions.message
  207. -- Produces a brief message to a user
  208. -- input: user (string), message (string)
  209. -- output: prints message to CLI
  210. function modpol.interactions.message(user, message)
  211. print(user .. ": " .. message)
  212. end
  213. --- Function: modpol.interactions.message_user
  214. -- Gets and sends a message from one user to another
  215. -- @param sender Name of user sending (string)
  216. -- @param recipient Name of user receiving (string)
  217. function modpol.interactions.message_user(sender, recipient)
  218. print("Enter your message for "..recipient..":")
  219. local sel = io.read()
  220. modpol.interactions.message(
  221. recipient,
  222. sel.." [from "..sender.."]")
  223. end
  224. --- Function: modpol.interactions.display
  225. -- Displays complex data to a user
  226. -- @param user Name of target user (string)
  227. -- @param message Content of message (string or table of strings)
  228. -- @param done Optional function for what happens when user is done
  229. function modpol.interactions.display(user, message, completion)
  230. local output = ""
  231. if type(message) == table then
  232. output = table.concat(message,"\n")
  233. elseif type(message) == string then
  234. output = message
  235. elseif type(message) == number then
  236. output = message
  237. else
  238. return nil, "Error: message not typed for display"
  239. end
  240. print(message)
  241. print("\nEnter to continue")
  242. io.read()
  243. if completion then completion() else
  244. modpol.intereactions.dashboard(user)
  245. end
  246. end
  247. -- Function: modpol.interactions.text_query
  248. -- input: User (string), Query (string), func (function)
  249. -- func input: user input (string)
  250. -- output: Applies "func" to user input
  251. function modpol.interactions.text_query(user, query, func)
  252. print(user .. ": " .. query)
  253. answer = io.read()
  254. func(answer)
  255. end
  256. -- Function: dropdown_query
  257. -- input: user (string), label (string), options (table of strings), func(choice) (function)
  258. -- func input: choice (string)
  259. -- output: calls func on choice
  260. function modpol.interactions.dropdown_query(user, label, options, func)
  261. -- set up options
  262. local options_display = ""
  263. local options_number = 0
  264. for k,v in ipairs(options) do
  265. options_display = options_display .. k .. ". " ..
  266. options[k] .. "\n"
  267. options_number = options_number + 1
  268. end
  269. options_display = options_display .. "Select number:"
  270. if options_number == 0 then
  271. print("Error: No options given for dropdown")
  272. return nil
  273. end
  274. -- begin displaying
  275. print(user .. ": " .. label)
  276. print(options_display)
  277. -- read input and produce output
  278. local answer
  279. answer = io.read()
  280. answer = tonumber(answer)
  281. if answer then
  282. if answer >= 1 and answer <= options_number then
  283. print("Selection: " .. options[answer])
  284. func(options[answer])
  285. else
  286. print("Error: Not in dropdown range")
  287. return nil
  288. end
  289. else
  290. print("Error: Must be a number")
  291. return nil
  292. end
  293. end
  294. -- Function: modpol.binary_poll_user(user, question)
  295. -- Params: user (string), question (string), func (function)
  296. -- func input: user input (string: y/n)
  297. -- Output: Applies "func" to user input
  298. function modpol.interactions.binary_poll_user(user, question, func)
  299. local query = "Poll for " .. user .. " (y/n): ".. question
  300. local answer
  301. repeat
  302. print(query)
  303. answer = io.read()
  304. until answer == "y" or answer == "n"
  305. if answer == "y" then
  306. modpol.interactions.message(user, "Response recorded")
  307. func("Yes")
  308. elseif answer == "n" then
  309. modpol.interactions.message(user, "Response recorded")
  310. func("No")
  311. else
  312. modpol.interactions.message(user, "Error: invalid response")
  313. end
  314. end
  315. -- COMPLEX INTERACTIONS
  316. -- ====================
  317. -- Function: modpol.interactions.message_org
  318. -- input: initiator (string), org (number or string), message (string)
  319. -- output: broadcasts message to all org members
  320. function modpol.interactions.message_org(initiator, org, message)
  321. local this_org = modpol.orgs.get_org(org)
  322. local users = this_org:list_members()
  323. for k,v in ipairs(users) do
  324. modpol.interactions.message(v, message)
  325. end
  326. end
  327. -- Function: modpol.interactions.binary_poll_org
  328. -- input: initator (user string), org_id (number)
  329. -- output: gets question from initiator, asks all org members, broadcasts answers
  330. -- TESTING
  331. --testing command
  332. function modpol.msg(text)
  333. modpol.interactions.message("TEST MSG: ",text)
  334. end