Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local com=require"component"
- local event=require"event"
- local sg=com.stargate
- local kp=com.os_keypad
- local function resetTimer()require("component").os_keypad.setDisplay("Связать",7)_SG_Timer_ID=-1 end
- local _SG_list={
- "SHP",
- "SPW",
- "HLL",
- "END",
- "MNW"
- }
- local _SG_Add={
- "446YHSP86",
- "YAUVP39SA",
- "1FEFVQ51V",
- "ZJR2Z346W",
- "EBHM0GLP0",
- }
- local _SG_Power={}
- local _SG_Timer_ID=-1
- local _SG_Add_R={} for n,k in pairs(_SG_Add) do _SG_Add_R[k]=n _SG_Add_R[k:sub(1,7)]=n end
- local _SG_OldConnect=""
- if require("filesystem").exists("/SGL.lua") then
- for l in io.lines("/SGL.lua") do
- local nm=l:sub(1,3)
- local id=#_SG_list+1
- kp.setKey(#_SG_list+1,nm,7)
- table.insert(_SG_list,nm)
- _SG_Add_R[l]=id
- table.insert(_SG_Add,l)
- end
- end
- kp.setDisplay("Связать",7)
- for i=1,12 do
- if _SG_list[i] then
- _SG_Power[i]=sg.energyToDial(_SG_Add[i])
- end
- kp.setKey(i,_SG_list[i] or "",7)
- end
- event.listen("keypad",function(_,add,id,keyName)
- if sg.remoteAddress()=="" or _SG_Add_R[_SG_OldConnect]then
- if sg.remoteAddress()~="" then
- local oid=_SG_Add_R[_SG_OldConnect]
- if oid then
- kp.setKey(oid,_SG_list[oid],7)
- end
- sg.disconnect()
- if oid==id then
- kp.setDisplay("Закрытие",4)
- event.cancel(_SG_Timer_ID)
- _SG_Timer_ID=event.timer(1,resetTimer)
- return
- end
- end
- if not _SG_Add[id] then return end
- local h,r
- while true do
- h,r=sg.dial(_SG_Add[id])
- if not h and r:len()==16 then kp.setDisplay("Закрытие",4)os.sleep(1)sg.disconnect()else break end
- end
- if h then
- _SG_OldConnect=_SG_Add[id]
- kp.setDisplay("связь",2)
- kp.setKey(id,_SG_list[id],6)
- return
- else
- local len,fir=r:len(),r:sub(1,1)
- if len==32 and fir=="S" then -- нехватает энергии
- kp.setDisplay("Энергия",4)
- elseif len==35 then -- врата заняты
- kp.setDisplay("Закрытие",4)
- else
- kp.setDisplay("Ошибка",4)
- event.cancel(_SG_Timer_ID)
- _SG_Timer_ID=event.timer(1,resetTimer)
- error("[SGCP ERROR]"..r)
- end
- end
- else
- kp.setDisplay("Закрытие",4)
- sg.disconnect()
- kp.setKey(id,_SG_OldConnect or id,7)
- _SG_OldConnect=""
- end
- event.cancel(_SG_Timer_ID)
- _SG_Timer_ID=event.timer(1,resetTimer)
- end)
- event.listen("sgStargateStateChange",function(_,add,en,a)
- if en=="Closing" then
- local id=_SG_Add_R[_SG_OldConnect]
- kp.setKey(id,_SG_list[id],7)
- _SG_OldConnect=""
- end
- end)
- event.listen("sgDialIn",function(_,add,inadd)
- _SG_OldConnect=inadd
- local id=_SG_Add_R[inadd]
- if id then
- kp.setKey(id,_SG_list[id],6)
- else
- local f=io.open("/SGL.lua","a")
- f:write(inadd.."\n")
- f:close()
- local nm=inadd:sub(1,3)
- local id=#_SG_list+1
- kp.setKey(#_SG_list+1,nm,6)
- table.insert(_SG_list,nm)
- _SG_Add_R[inadd]=id
- table.insert(_SG_Add,inadd)
- error("[SGCP INFO] обнаружены новые врата: "..inadd)
- end
- end)
- event.timer(30,function()
- local com=require"component"
- local kp=com.os_keypad
- local sg=com.stargate
- local ava=sg.energyAvailable()
- for i=1,#_SG_list do
- local nm=_SG_list[i]
- if i~=_SG_Add_R[_SG_OldConnect] then
- if _SG_Power[i] then
- if _SG_Power[i]>ava then kp.setKey(i,nm,4) else kp.setKey(i,nm,7) end
- else
- kp.setKey(i,nm,3)
- end
- end
- end
- end,math.huge)
- print("SGCP успешно запущена")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement