SHOW:
|
|
- or go back to the newest paste.
| 1 | --- Turret Control | |
| 2 | -- Helps manage OpenModularTurrets turret blocks that have serial IO | |
| 3 | -- @Author: Brodur | |
| 4 | -- @Version: 2.3 | |
| 5 | -- @Requires: | |
| 6 | -- https://pastebin.com/WvF00A71 : JSON Utility functions | |
| 7 | -- https://pastebin.com/pLpe4zPb : Menu functions | |
| 8 | -- https://pastebin.com/SKwM2UZH : Lua JSON Library | |
| 9 | ||
| 10 | local term = require("term")
| |
| 11 | local component = require("component")
| |
| 12 | local event = require("event")
| |
| 13 | local json = require("json")
| |
| 14 | local menu = require("menu")
| |
| 15 | local jsonutil = require("jsonutil")
| |
| 16 | local fs = require("filesystem")
| |
| 17 | ||
| 18 | local m = {}
| |
| 19 | local tb = "turret_base" | |
| 20 | local lb = "-------------------------------" | |
| 21 | ||
| 22 | local mmopts = {
| |
| 23 | "Summary", | |
| 24 | "Targeting", | |
| 25 | "User Admin", | |
| 26 | "Push Config", | |
| 27 | "Exit" | |
| 28 | } | |
| 29 | ||
| 30 | local db = {}
| |
| 31 | local toRemove = {}
| |
| 32 | local jsondir = "/home/settings.json" | |
| 33 | ||
| 34 | --- Trims strings | |
| 35 | -- Gets rid of trailing white space or special characters. | |
| 36 | -- @param s The string to trim. | |
| 37 | -- @return The trimmed string. | |
| 38 | function trim(s) | |
| 39 | return (s:gsub("^%s*(.-)%s*$", "%1"))
| |
| 40 | end | |
| 41 | ||
| 42 | --- Summary output | |
| 43 | -- Outputs the summary of a selected turret. | |
| 44 | function m.summary() | |
| 45 | term.setCursor(1,1) | |
| 46 | print(mmopts[1] .. "\n") | |
| 47 | print("Trusted players")
| |
| 48 | print(lb) | |
| 49 | ||
| 50 | for k,v in pairs(db.users) do | |
| 51 | print(" - " .. k, v and "[Admin]" or "[Trusted]")
| |
| 52 | end | |
| 53 | print("\nAttacks")
| |
| 54 | print(lb) | |
| 55 | print("Mobs:\t\t" .. tostring(db.targets.setAttacksMobs))
| |
| 56 | print("Neutrals:\t" .. tostring(db.targets.setAttacksNeutrals))
| |
| 57 | print("Players:\t" .. tostring(db.targets.setAttacksPlayers))
| |
| 58 | ||
| 59 | print("\n\nPress enter to continue...")
| |
| 60 | term.read() | |
| 61 | end | |
| 62 | ||
| 63 | --- Set Attack | |
| 64 | -- Sets the attack setting for all turrets. | |
| 65 | -- @param flag 1 or 2, corresponds to true or false, toggles the attack mode. | |
| 66 | -- @param option Which option was chosen in the target function | |
| 67 | function m.setAttack(flag, option) | |
| 68 | local methods = {"setAttacksMobs", "setAttacksNeutrals", "setAttacksPlayers"}
| |
| 69 | local arguments = {true, false}
| |
| 70 | db.targets[methods[option]] = arguments[flag] | |
| 71 | db.hasChanges = true | |
| 72 | end | |
| 73 | ||
| 74 | ---Target | |
| 75 | -- Selects the targeting parameter | |
| 76 | function m.target() | |
| 77 | local options = {"Attack Mobs", "Attack Neutrals", "Attack Players", "Exit"}
| |
| 78 | local opt = -1 | |
| 79 | while opt ~= #options do | |
| 80 | opt = menu.list(options, mmopts[2]) | |
| 81 | if opt ~= #options then | |
| 82 | local flag = menu.dialog(options[opt], "True", "False") | |
| 83 | m.setAttack(flag, opt) | |
| 84 | else break end | |
| 85 | end | |
| 86 | end | |
| 87 | ||
| 88 | --- Add Trusted Player | |
| 89 | -- Adds a trusted player to all turrets, either with or without admin privileges. | |
| 90 | -- @param player The player to add. | |
| 91 | -- @param usrType 1 for trusted, 2 for admin. | |
| 92 | function m.addTrustedPlayer(player, usrType) | |
| 93 | local args = {false, true}
| |
| 94 | for k,_ in pairs(db.users) do | |
| 95 | if player == k then error("Cannot add a user that already exists!") end
| |
| 96 | end | |
| 97 | db.users[player] = args[usrType] | |
| 98 | db.hasChanges = true | |
| 99 | end | |
| 100 | ||
| 101 | --- Remove Trusted Player | |
| 102 | -- Removes a user from the trusted players list. | |
| 103 | -- @param player The user to remove. | |
| 104 | function m.removeTrustedPlayer(player) | |
| 105 | toRemove[#toRemove+1] = player | |
| 106 | db.users[player] = nil | |
| 107 | db.hasChanges = true | |
| 108 | end | |
| 109 | ||
| 110 | --- Users | |
| 111 | -- Launches the add or remove user dialog | |
| 112 | function m.users() | |
| 113 | local options = {"Add a trusted user", "Remove a trused user","Exit"}
| |
| 114 | local opt = -1 | |
| 115 | while opt ~= #options do | |
| 116 | opt = menu.list(options, mmopts[3]) | |
| 117 | if opt == 1 then | |
| 118 | local userTypes = {"Trusted", "Admin"}
| |
| 119 | term.write("Add a trusted player: ")
| |
| 120 | local player = trim(term.read()) | |
| 121 | local usrType = menu.dialog("User or Admin?", "User", "Admin")
| |
| 122 | local opt = menu.dialog("Add \'" .. player .. "\' as " .. userTypes[usrType] .." type user?", "Confirm", "Cancel")
| |
| 123 | if opt == 1 then | |
| 124 | m.addTrustedPlayer(player, usrType) | |
| 125 | end | |
| 126 | end | |
| 127 | if opt == 2 then | |
| 128 | local users = {"Cancel"}
| |
| 129 | for k,v in pairs(db.users) do users[#users+1] = k end | |
| 130 | local user = -1 | |
| 131 | while user ~= 1 do | |
| 132 | user = menu.list(users, "Select a user") | |
| 133 | if user ~= 1 then | |
| 134 | local player = users[user] | |
| 135 | local confirm = menu.dialog("Remove \'" .. player .. "\' from trusted users?", "Confirm", "Cancel")
| |
| 136 | if confirm == 1 then | |
| 137 | m.removeTrustedPlayer(player) | |
| 138 | table.remove(users, user) | |
| 139 | end | |
| 140 | end | |
| 141 | end | |
| 142 | end | |
| 143 | end | |
| 144 | end | |
| 145 | ||
| 146 | --- Distribute Json | |
| 147 | -- Disseminates the settings from the database to all turrets. | |
| 148 | function m.distribJson() | |
| 149 | for _,v in pairs(db.turrets) do | |
| 150 | for _,player in pairs(toRemove) do | |
| 151 | component.invoke(v, "removeTrustedPlayer", player) | |
| 152 | end | |
| 153 | ||
| 154 | for user,priv in pairs(db.users) do | |
| 155 | component.invoke(v, "addTrustedPlayer", user, priv) | |
| 156 | end | |
| 157 | for meth,bool in pairs(db.targets) do | |
| 158 | component.invoke(v, meth, bool) | |
| 159 | end | |
| 160 | end | |
| 161 | toRemove= {}
| |
| 162 | end | |
| 163 | ||
| 164 | --- Sub menu | |
| 165 | -- Determines which menu function to call. | |
| 166 | -- @param index The selected index in the main menu options table. | |
| 167 | function m.subMenu(index) | |
| 168 | local turrets = {}
| |
| 169 | for k in pairs(component.list(tb)) do turrets[#turrets+1] = k end | |
| 170 | db.turrets = turrets | |
| 171 | ||
| 172 | if index == mmopts[1] then m.summary() end | |
| 173 | if index == mmopts[2] then m.target() end | |
| 174 | if index == mmopts[3] then m.users() end | |
| 175 | if index == mmopts[4] then m.distribJson() end | |
| 176 | end | |
| 177 | ||
| 178 | --- On Load | |
| 179 | -- Initializes the database and grabs the initial turrets. | |
| 180 | function m.onLoad() | |
| 181 | if not fs.exists(jsondir) then | |
| 182 | db = {
| |
| 183 | hasChanges=false, | |
| 184 | targets={setAttacksMobs=false,
| |
| 185 | setAttacksNeutrals=false, | |
| 186 | setAttacksPlayers=false | |
| 187 | }, | |
| 188 | turrets = {},
| |
| 189 | users = {}
| |
| 190 | } | |
| 191 | jsonutil.save(db, jsondir) | |
| 192 | end | |
| 193 | ||
| 194 | db = jsonutil.load(jsondir) | |
| 195 | for k in pairs(component.list(tb)) do db.turrets[#db.turrets+1] = k end | |
| 196 | m.main() | |
| 197 | end | |
| 198 | ||
| 199 | --- Main | |
| 200 | -- Launches the main logic and displays the top level menu. | |
| 201 | function m.main() | |
| 202 | local mmoptTitle = "Main Menu" | |
| 203 | local mmopt = -1 | |
| 204 | ||
| 205 | while mmopt ~= #mmopts do | |
| 206 | if db.hasChanges then | |
| 207 | jsonutil.save(db, jsondir) | |
| 208 | db.hasChanges = false | |
| 209 | m.distribJson() | |
| 210 | end | |
| 211 | if mmopt ~= #mmopts then | |
| 212 | mmopt = menu.list(mmopts, mmoptTitle) | |
| 213 | m.subMenu(mmopts[mmopt]) | |
| 214 | end | |
| 215 | end | |
| 216 | end | |
| 217 | ||
| 218 | --- On Component Added | |
| 219 | -- Push the stored config when a new turret is detected. | |
| 220 | -- @param eventType The type of event, passed by event listener. | |
| 221 | -- @param address The hardware address of the new component. | |
| 222 | -- @param componentType The type of the new component. | |
| 223 | -- @todo figure out why the event listener is not firing all of the time, until that is figured disregard this function | |
| 224 | -- function onComponentAdded(eventType, address, componentType) | |
| 225 | -- if componentType == "turret_base" then | |
| 226 | -- distribJson() | |
| 227 | -- end | |
| 228 | -- end | |
| 229 | ||
| 230 | -- event.listen("component_added", onComponentAdded)
| |
| 231 | m.onLoad() | |
| 232 | return m |