Advertisement
electronic_steve

[OC] SGCP

Feb 18th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. local com=require"component"
  2. local event=require"event"
  3.  
  4. local sg=com.stargate
  5. local kp=com.os_keypad
  6.  
  7. local function resetTimer()require("component").os_keypad.setDisplay("Связать",7)_SG_Timer_ID=-1 end
  8.  
  9. local _SG_list={
  10.     "SHP",
  11.     "SPW",
  12.     "HLL",
  13.     "END",
  14.     "MNW"
  15. }
  16. local _SG_Add={
  17.     "446YHSP86",
  18.     "YAUVP39SA",
  19.     "1FEFVQ51V",
  20.     "ZJR2Z346W",
  21.     "EBHM0GLP0",
  22. }
  23. local _SG_Power={}
  24. local _SG_Timer_ID=-1
  25. 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
  26. local _SG_OldConnect=""
  27.  
  28. if require("filesystem").exists("/SGL.lua") then
  29.     for l in io.lines("/SGL.lua") do
  30.         local nm=l:sub(1,3)
  31.         local id=#_SG_list+1
  32.         kp.setKey(#_SG_list+1,nm,7)
  33.         table.insert(_SG_list,nm)
  34.         _SG_Add_R[l]=id
  35.         table.insert(_SG_Add,l)
  36.     end
  37. end
  38.  
  39. kp.setDisplay("Связать",7)
  40.  
  41. for i=1,12 do
  42.     if _SG_list[i] then
  43.         _SG_Power[i]=sg.energyToDial(_SG_Add[i])
  44.     end
  45.     kp.setKey(i,_SG_list[i] or "",7)
  46. end
  47.  
  48. event.listen("keypad",function(_,add,id,keyName)
  49.     if sg.remoteAddress()=="" or _SG_Add_R[_SG_OldConnect]then
  50.         if sg.remoteAddress()~="" then
  51.             local oid=_SG_Add_R[_SG_OldConnect]
  52.             if oid then
  53.                 kp.setKey(oid,_SG_list[oid],7)
  54.             end
  55.             sg.disconnect()
  56.             if oid==id then
  57.                 kp.setDisplay("Закрытие",4)
  58.                 event.cancel(_SG_Timer_ID)
  59.                 _SG_Timer_ID=event.timer(1,resetTimer)
  60.                 return
  61.             end
  62.         end
  63.         if not _SG_Add[id] then return end
  64.         local h,r
  65.         while true do
  66.             h,r=sg.dial(_SG_Add[id])
  67.             if not h and r:len()==16 then kp.setDisplay("Закрытие",4)os.sleep(1)sg.disconnect()else break end
  68.         end
  69.        
  70.         if h then
  71.             _SG_OldConnect=_SG_Add[id]
  72.             kp.setDisplay("связь",2)
  73.             kp.setKey(id,_SG_list[id],6)
  74.             return
  75.         else
  76.             local len,fir=r:len(),r:sub(1,1)
  77.             if len==32 and fir=="S" then -- нехватает энергии
  78.                 kp.setDisplay("Энергия",4)
  79.             elseif len==35 then -- врата заняты
  80.                 kp.setDisplay("Закрытие",4)
  81.             else
  82.                 kp.setDisplay("Ошибка",4)
  83.                 event.cancel(_SG_Timer_ID)
  84.                 _SG_Timer_ID=event.timer(1,resetTimer)
  85.                 error("[SGCP ERROR]"..r)
  86.             end
  87.         end
  88.        
  89.     else
  90.         kp.setDisplay("Закрытие",4)
  91.         sg.disconnect()
  92.         kp.setKey(id,_SG_OldConnect or id,7)
  93.         _SG_OldConnect=""
  94.     end
  95.     event.cancel(_SG_Timer_ID)
  96.     _SG_Timer_ID=event.timer(1,resetTimer)
  97. end)
  98.  
  99. event.listen("sgStargateStateChange",function(_,add,en,a)
  100.     if en=="Closing" then
  101.         local id=_SG_Add_R[_SG_OldConnect]
  102.         kp.setKey(id,_SG_list[id],7)
  103.         _SG_OldConnect=""
  104.     end
  105. end)
  106.  
  107. event.listen("sgDialIn",function(_,add,inadd)
  108.     _SG_OldConnect=inadd
  109.     local id=_SG_Add_R[inadd]
  110.     if id then  
  111.         kp.setKey(id,_SG_list[id],6)
  112.     else
  113.         local f=io.open("/SGL.lua","a")
  114.         f:write(inadd.."\n")
  115.         f:close()
  116.         local nm=inadd:sub(1,3)
  117.         local id=#_SG_list+1
  118.         kp.setKey(#_SG_list+1,nm,6)
  119.         table.insert(_SG_list,nm)
  120.         _SG_Add_R[inadd]=id
  121.         table.insert(_SG_Add,inadd)
  122.         error("[SGCP INFO] обнаружены новые врата: "..inadd)
  123.     end
  124. end)
  125.  
  126. event.timer(30,function()
  127.     local com=require"component"
  128.     local kp=com.os_keypad
  129.     local sg=com.stargate
  130.     local ava=sg.energyAvailable()
  131.     for i=1,#_SG_list do
  132.         local nm=_SG_list[i]
  133.         if i~=_SG_Add_R[_SG_OldConnect] then
  134.             if _SG_Power[i] then
  135.                 if _SG_Power[i]>ava then kp.setKey(i,nm,4) else kp.setKey(i,nm,7) end
  136.             else
  137.                 kp.setKey(i,nm,3)
  138.             end
  139.         end
  140.     end
  141. end,math.huge)
  142.  
  143. print("SGCP успешно запущена")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement