Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/thoda-dev/du-simple-access-control. a script to control access with doors of force fields. Work with user name or ID or Organisation name or id
Something went wrong. Try again.
5.7 kB · 198 lines
Lua
at junie-init
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198--version 1.0.1
--LUA PARAMETERSlocal AllowedUsersId = "" --export: the list of all IDs, comma separatedlocal AllowedUsersName = "jericho" --export: the list of all names, comma separated (Case sensitive)local AllowedUserOrgsId = "" --export: the list of all organisation IDs, comma separatedlocal AllowedUserOrgsName = "Federatis" --export: the list of all organisation Names, comma separated (Case sensitive)
--Libraries--split is a function to split a string in a table with a delimiter written by Jerichofunction split(s,d)local t={}s:gsub("[^"..d.."]+",function(w)table.insert(t,w)end)return t end
--hide unit widgetunit.hideWidget()
--auto detecting the door, force fields and the screensDoors = {}Fields = {}Screens = {}Switches = {}for slotName,slot in pairs(unit) do if type(slot) == "table" and type(slot.export) == "table" and slot.getClass then local ElementClass = slot.getClass():lower() if ElementClass:find("doorunit") then table.insert(Doors, slot) elseif ElementClass:find("manualswitchunit") then table.insert(Switches, slot) slot.deactivate() elseif ElementClass:find("forcefieldunit") then table.insert(Fields, slot) slot.activate() elseif ElementClass:find("screenunit") then table.insert(Screens, slot) slot.activate() else system.print(ElementClass) end endend
function getRenderScript(access) local color = "red" local message = "Access Denied" if access then color = "green" message = "Access Granted" end return [[ setBackgroundColor(15/255,24/255,29/255) local rx,ry = getResolution() local cx, cy = getCursor() if vmode then ry,rx = getResolution() cy, cx = getCursor() cx = rx - cx if vmode_side == "right" then cy = ry - cy cx = rx - cxendend
local smallBold=loadFont('Play-Bold',40) local bigBold=loadFont('Play-Bold',64)
local front=createLayer() local back=createLayer() setDefaultStrokeColor( back,Shape_Line,0,0,0,0.5) setDefaultShadow( back,Shape_Line,6,0,0,0,0.5) setDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1) setDefaultFillColor( front,Shape_Text,0,0,0,1) setDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1) setDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)
local red = {177/255,42/255,42/255} local green = {34/255,177/255,76/255}
function renderHeader(title) local h = 55 addLine(back,0,h+12,rx,h+12) addBox(front,0,12,rx,h) addText(front,smallBold,title,44,h-12)end
setNextFillColor(front, ]] .. color .. [[[1], ]] .. color .. [[[2], ]] .. color .. [[[3], 1) setNextTextAlign(front,AlignH_Center,AlignV_Middle) addText(front,smallBold,"]] .. message .. [[", rx/2, ry/4)
setNextFillColor(front, ]] .. color .. [[[1], ]] .. color .. [[[2], ]] .. color .. [[[3], 1) setNextTextAlign(front,AlignH_Center,AlignV_Middle) addText(front,bigBold,"]] .. player.getName() .. [[", rx/2, ry/2)
renderHeader("Access Control System") requestAnimationFrame(10) ]]end
local AccessPermitted = false
function setRenderScriptOnScreens(access) for _,screen in pairs(Screens) do screen.setRenderScript(getRenderScript(access)) endend
function grantAccess() AccessPermitted = true for _,Door in pairs(Doors) do Door.open() --open the door end for _,Field in pairs(Fields) do Field.deactivate() --open the field end for _,Switch in pairs(Switches) do Switch.activate() --activate the swicth end setRenderScriptOnScreens(true)end
function closeAll() AccessPermitted = false for _,Door in pairs(Doors) do Door.close() --close the door end for _,Field in pairs(Fields) do Field.activate() --close the field end for _,Switch in pairs(Switches) do Switch.deactivate() --deactivate the switch endendcloseAll() --close all by default
function rejectAccess() closeAll() setRenderScriptOnScreens(false) unit.exit()end
--converting to tableslocal tPermittedUsersId = split(AllowedUsersId,',') or {}local tPermittedUsersName = split(AllowedUsersName,',') or {}local tPermittedOrgsId = split(AllowedUserOrgsId,',') or {}local tPermittedOrgsName = split(AllowedUserOrgsName,',') or {}
local player_id = player.getId()
for _,id in pairs(tPermittedUsersId) do if id == player_id then grantAccess() AccessPermitted = true break endend
if not AccessPermitted then local player_name = player.getName() for _,name in pairs(tPermittedUsersName) do if name == player_name then grantAccess() break end endend
if not AccessPermitted then local player_orgs_id = player.getOrgIds() for _,oid in pairs(player_orgs_id) do for _,id in pairs(tPermittedOrgsId) do if tonumber(id) == oid then grantAccess() break end end --if access is granted, end the loop if AccessPermitted then break end --if not a valid ID, veifying the org name local o = system.getOrganization(oid) if o.name then for _,name in pairs(tPermittedOrgsName) do if name == o.name then grantAccess() break end end end --if access is granted, end the loop if AccessPermitted then break end endend
if not AccessPermitted then rejectAccess()end