Set up dropdowns in dashboard
This commit is contained in:
		| @@ -38,6 +38,21 @@ function modpol.orgs.list_all() | |||||||
|    return org_table |    return org_table | ||||||
| end | end | ||||||
|  |  | ||||||
|  | -- Function: modpol.orgs.user_orgs(user) | ||||||
|  | -- input: user (string) | ||||||
|  | -- output: table of strings of org names | ||||||
|  | function modpol.orgs.user_orgs(user) | ||||||
|  |    local all_orgs = modpol.orgs.list_all() | ||||||
|  |    local user_orgs = {} | ||||||
|  |    for i,v in ipairs(all_orgs) do | ||||||
|  |       local this_table = modpol.orgs.get_org(v) | ||||||
|  |       if this_table:has_member(user) then | ||||||
|  |          table.insert(user_orgs,v) | ||||||
|  |       end | ||||||
|  |    end | ||||||
|  |    return user_orgs | ||||||
|  | end | ||||||
|  |  | ||||||
| -- =========================================== | -- =========================================== | ||||||
| -- deletes all orgs except for the instance | -- deletes all orgs except for the instance | ||||||
| function modpol.orgs.reset() | function modpol.orgs.reset() | ||||||
| @@ -188,7 +203,7 @@ end | |||||||
| -- adds a user to an org | -- adds a user to an org | ||||||
| function modpol.orgs:add_member(user) | function modpol.orgs:add_member(user) | ||||||
|     -- trys to fill in empty spots first |     -- trys to fill in empty spots first | ||||||
|     empty_index = self:get_member_index('') |     local empty_index = self:get_member_index('') | ||||||
|     if empty_index then |     if empty_index then | ||||||
|         self.members[empty_index] = user |         self.members[empty_index] = user | ||||||
|     else |     else | ||||||
|   | |||||||
| @@ -1,5 +1,7 @@ | |||||||
| -- =================================================================== | -- INTERACTIONS.LUA (for Minetest) | ||||||
|  |  | ||||||
| -- CONTEXTUAL STUFF | -- CONTEXTUAL STUFF | ||||||
|  | -- ================ | ||||||
|  |  | ||||||
| -- First, set up contexts to enable passing across formspecs | -- First, set up contexts to enable passing across formspecs | ||||||
| -- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts | -- https://rubenwardy.com/minetest_modding_book/en/players/formspecs.html#contexts | ||||||
| @@ -17,8 +19,26 @@ end) | |||||||
| -- table of formspec field responses | -- table of formspec field responses | ||||||
| local formspec_fields = {} | local formspec_fields = {} | ||||||
|  |  | ||||||
| -- =================================================================== |  | ||||||
|  | -- UTILITIES | ||||||
|  | -- ========= | ||||||
|  |  | ||||||
|  | -- Function: formspec_list | ||||||
|  | -- for use generating option lists in formspecs from tables | ||||||
|  | -- input: table of strings | ||||||
|  | -- output: a formspec-ready list of the strings | ||||||
|  | local function formspec_list(array) | ||||||
|  |    local escaped = {} | ||||||
|  |    for i = 1, #array do | ||||||
|  |       escaped[i] = minetest.formspec_escape(array[i]) | ||||||
|  |    end | ||||||
|  |    return table.concat(escaped,",") | ||||||
|  | end | ||||||
|  |  | ||||||
|  |  | ||||||
| -- MAIN MODPOL DASHBOARD | -- MAIN MODPOL DASHBOARD | ||||||
|  | -- ===================== | ||||||
|  |  | ||||||
| -- Function: modpol.interactions.dashboard(user) | -- Function: modpol.interactions.dashboard(user) | ||||||
| -- Params: user (string) | -- Params: user (string) | ||||||
| -- Q: Should this return a menu of commands relevant to the specific user? | -- Q: Should this return a menu of commands relevant to the specific user? | ||||||
| @@ -31,17 +51,22 @@ function modpol.interactions.dashboard(user) | |||||||
|    for key,value in pairs(command_list) do |    for key,value in pairs(command_list) do | ||||||
|       commands = commands .. "/" .. value .. " " |       commands = commands .. "/" .. value .. " " | ||||||
|    end |    end | ||||||
|    local orgs = "Orgs: " .. |    local all_orgs = modpol.orgs.list_all() | ||||||
|       table.concat(modpol.orgs.list_all(), ", ") |    local user_orgs = modpol.orgs.user_orgs(user) | ||||||
|    local users = "Players: " |    local all_users = modpol.list_users() | ||||||
|       .. table.concat(modpol.list_users(), ", ") |  | ||||||
|    -- set up formspec |    -- set up formspec | ||||||
|     local formspec = { |     local formspec = { | ||||||
|        "formspec_version[4]", |        "formspec_version[4]", | ||||||
|        "size[10,8]", |        "size[10,8]", | ||||||
|        "label[0.5,0.5;", minetest.formspec_escape(commands), "]", |        "label[0.5,0.5;MODULAR POLITICS]", | ||||||
|        "label[0.5,1.5;", minetest.formspec_escape(orgs), "]", |        "label[0.5,2;All orgs:]", | ||||||
|        "label[0.5,2.5;", minetest.formspec_escape(users), "]", |        "dropdown[2,1.5;5,0.8;user_orgs;"..formspec_list(all_orgs)..";;]", | ||||||
|  |        "label[0.5,3;Your orgs:]", | ||||||
|  |        "dropdown[2,2.5;5,0.8;input;"..formspec_list(user_orgs)..";;]", | ||||||
|  |        "label[0.5,4;All users:]", | ||||||
|  |        "dropdown[2,3.5;5,0.8;input;"..formspec_list(all_users)..";;]", | ||||||
|  |        "label[0.5,5;Processes:]", | ||||||
|  |        "dropdown[2,4.5;5,0.8;input;TBA;;]", | ||||||
|        "button[0.5,7;1,0.8;test_poll;Test poll]", |        "button[0.5,7;1,0.8;test_poll;Test poll]", | ||||||
|        "button[2,7;1,0.8;add_org;Add org]", |        "button[2,7;1,0.8;add_org;Add org]", | ||||||
|        "button[3.5,7;1.5,0.8;remove_org;Remove org]", |        "button[3.5,7;1.5,0.8;remove_org;Remove org]", | ||||||
| @@ -65,7 +90,9 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) | |||||||
|       end |       end | ||||||
| end) | end) | ||||||
|  |  | ||||||
| -- BASIC INTERACTION FUNCTIONS -- |  | ||||||
|  | -- BASIC INTERACTION FUNCTIONS | ||||||
|  | -- =========================== | ||||||
|  |  | ||||||
| -- Function: modpol.interactions.message | -- Function: modpol.interactions.message | ||||||
| -- input: message (string) | -- input: message (string) | ||||||
| @@ -103,16 +130,16 @@ minetest.register_on_player_receive_fields(function (player, formname, fields) | |||||||
|       end |       end | ||||||
| end) | end) | ||||||
|  |  | ||||||
|  |  | ||||||
| -- Function: dropdown_query | -- Function: dropdown_query | ||||||
| -- input: user (string), label (string), options (table of strings) | -- input: user (string), label (string), options (table of strings) | ||||||
| function modpol.interactions.dropdown_query(user, label, options) | function modpol.interactions.dropdown_query(user, label, options) | ||||||
|    -- set up formspec |    -- set up formspec | ||||||
|    local options_string = table.concat(options,",") |  | ||||||
|    local formspec = { |    local formspec = { | ||||||
|       "formspec_version[4]", |       "formspec_version[4]", | ||||||
|       "size[10,4]", |       "size[10,4]", | ||||||
|       "label[0.5,1;", minetest.formspec_escape(label), "]", |       "label[0.5,1;"..minetest.formspec_escape(label).."]", | ||||||
|       "dropdown[0.5,1.25;9,0.8;input;".. minetest.formspec_escape(options_string)..";;]", |       "dropdown[0.5,1.25;9,0.8;input;"..formspec_list(options)..";;]", | ||||||
|       "button[0.5,2.5;1,0.8;yes;OK]", |       "button[0.5,2.5;1,0.8;yes;OK]", | ||||||
|    } |    } | ||||||
|    local formspec_string = table.concat(formspec, "") |    local formspec_string = table.concat(formspec, "") | ||||||
| @@ -190,7 +217,6 @@ function modpol.interactions.binary_poll_org(initiator, org) | |||||||
|             modpol.interactions.binary_poll_user(v, input) |             modpol.interactions.binary_poll_user(v, input) | ||||||
|          end |          end | ||||||
|          _contexts[initiator] = nil |          _contexts[initiator] = nil | ||||||
|          modpol.interactions.dashboard(initiator) |  | ||||||
|       end |       end | ||||||
| end | end | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user