modpol/modpol/interactions/interactions.lua

201 lines
5.8 KiB
Lua

-- INTERACTIONS.LUA (CLI)
-- User interaction functions for Modular Politics
-- Called by modpol.lua
modpol.interactions = {}
-- DASHBOARDS
-- ==========
function modpol.interactions.login()
print("Log in as which user?")
local username = io.read()
while true do
local org = modpol.interactions.dashboard(username)
if org then
modpol.interactions.org_dashboard(username, org)
else
break
end
end
end
-- Function: modpol.interactions.dashboard(user)
-- Params: user (string)
-- Q: Should this return a menu of commands relevant to the specific user?
-- Output: Displays a menu of commands to the user
-- TKTK currently just prints all of modpol---needs major improvement
function modpol.interactions.dashboard(user)
print()
local org_list = modpol.orgs.list_all()
print('Select an org')
for i, org_name in ipairs(org_list) do
local org = modpol.orgs.get_org(org_name)
local indicator = ""
if org:has_pending_actions(user) then
indicator = "*"
end
print('['..i..']'..' '..org_name..indicator)
end
local sel = io.read()
local sel_org = org_list[tonumber(sel)]
if not sel_org then return end
modpol.interactions.org_dashboard(user, sel_org)
end
-- Function: modpol.interactions.org_dashboard
-- Params: user (string), org_name (string)
-- Output: Displays a menu of org-specific commands to the user
function modpol.interactions.org_dashboard(user, org_name)
print()
local org = modpol.orgs.get_org(org_name)
if not org then return nil end
local children = {}
for k,v in ipairs(org.children) do
local this_child = modpol.orgs.get_org(v)
table.insert(children, this_child.name)
end
local processes = {}
for k,v in ipairs(org.processes) do
print(k, v)
local this_request = org.requests[v.request_id]
if type(this_request) == "table" then
local active = ''
if org.pending[user] then
if org.pending[user][v.id] then
active = '*'
end
end
local req_str = "[" .. v.id .. "] " ..
active .. this_request.type
if this_request.params[1] then
req_str = req_str ": " ..
table.concat(this_request.params, ", ")
end
local req_str = v.id .. " (" .. this_request.type .. " -> " .. table.concat(this_request.params, ", ") .. ")" .. active
table.insert(processes, req_str)
end
end
-- set up output
print("Org: " .. org_name)
print("Members: " .. table.concat(org.members, ", "))
print("Children: " .. table.concat(children, ", "))
print("Processes: " .. table.concat(processes, ", "))
print('Interact with which process?')
local sel = io.read()
local process = org.processes[tonumber(sel)]
if not process then return end
process:interact(user)
end
-- Function: modpol.interactions.policy_dashboard
-- input: user (string), org_id (int), policy (string)
-- if policy is nil, enables creating a new policy
-- output: opens a dashboard for viewing/editing policy details
-- TODO
-- Function: modpol.interactions.message
-- input: user (string), message (string)
-- output: prints message to CLI
function modpol.interactions.message(user, message)
print(user .. ": " .. message)
end
-- Function: modpol.interactions.text_query
-- input: User (string), Query (string), func (function)
-- func input: user input (string)
-- output: Applies "func" to user input
function modpol.interactions.text_query(user, query, func)
print(user .. ": " .. query)
answer = io.read()
func(answer)
end
-- Function: dropdown_query
-- input: user (string), label (string), options (table of strings), func(choice) (function)
-- func input: choice (string)
-- output: calls func on choice
function modpol.interactions.dropdown_query(user, label, options, func)
-- set up options
local options_display = ""
local options_number = 0
for k,v in ipairs(options) do
options_display = options_display .. k .. ". " ..
options[k] .. "\n"
options_number = options_number + 1
end
options_display = options_display .. "Select number:"
if options_number == 0 then
print("Error: No options given for dropdown")
return nil
end
-- begin displaying
print(user .. ": " .. label)
print(options_display)
-- read input and produce output
local answer
answer = io.read()
answer = tonumber(answer)
if answer then
if answer >= 1 and answer <= options_number then
print("Selection: " .. options[answer])
func(options[answer])
else
print("Error: Not in dropdown range")
return nil
end
else
print("Error: Must be a number")
return nil
end
end
-- Function: modpol.binary_poll_user(user, question)
-- Params: user (string), question (string), func (function)
-- func input: user input (string: y/n)
-- Output: Applies "func" to user input
function modpol.interactions.binary_poll_user(user, question, func)
local query = "Poll for " .. user .. " (y/n): ".. question
local answer
repeat
print(query)
answer = io.read()
until answer == "y" or answer == "n"
if answer == "y" then
modpol.interactions.message(user, "Response recorded")
func("Yes")
elseif answer == "n" then
modpol.interactions.message(user, "Response recorded")
func("No")
else
modpol.interactions.message(user, "Error: invalid response")
end
end
-- COMPLEX INTERACTIONS
-- ====================
-- Function: modpol.interactions.message_org
-- input: initiator (string), org_id (number), message (string)
-- output: broadcasts message to all org members
-- Function: modpol.interactions.binary_poll_org
-- input: initator (user string), org_id (number)
-- output: gets question from initiator, asks all org members, broadcasts answers