THEJean_Kevin

Base Test

Apr 23rd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.12 KB | None | 0 0
  1. local os = require("os")
  2. local component = require("component")
  3. local colors = require("colors")
  4. local computer = require("computer")
  5. local fs = require("filesystem")
  6. local shell      = require("shell")
  7. local thread = require("thread")
  8. local sides = require("sides")
  9. local internet   = require("internet")
  10. local API
  11. local wm
  12.  
  13.  
  14. local args = { ... }
  15. if args[1] == "del" then
  16.     os.execute("del /lib/wm.lua")
  17.     os.execute("del /lib/API.lua")
  18. end
  19.  
  20. if require("filesystem").exists("/lib/wm.lua") then
  21.   wm = require("wm")
  22. elseif component.isAvailable("internet") then
  23.   os.execute("wget https://raw.githubusercontent.com/THEJean-Kevin/Gui/master/wm.lua /lib/wm.lua")
  24.   wm = require("wm")
  25. end
  26.  
  27. if require("filesystem").exists("/lib/API.lua") then
  28.   API = require("API")
  29. elseif component.isAvailable("internet") then
  30.   os.execute("wget https://raw.githubusercontent.com/THEJean-Kevin/Gui/master/API.lua /lib/API.lua")
  31.   API = require("API")
  32. end
  33. if fs.exists('/lib/json.lua') == false then
  34.   shell.execute('wget -fq "https://pastebin.com/raw/4UWzY19u" "/lib/json.lua"')
  35. end
  36. local json = require("json")
  37.  
  38. local shrc = io.open("/home/.shrc","w")
  39. shrc:write("/home/start.lua")
  40. shrc:close()
  41.  
  42.  
  43.  
  44.  
  45.  
  46. local gpu = component.gpu
  47.  
  48. local ScreenWidth, ScreenHeight = gpu.getResolution()
  49.  
  50.  
  51. local buttonadd = 0
  52. local GeneralList = {}
  53. local input_address, input_name, input_side, input_nameg,checkbox_offline
  54. local fnt = {}
  55. local activeMachine = nil
  56. local online = false
  57.  
  58. component.screen.setTouchModeInverted(true)
  59.  
  60. local windowGeneral = wm.newWindow(5, 5, math.floor(ScreenWidth/2) - 5, 20, "General")
  61. local windowAddGeneral = wm.newWindow(math.floor(ScreenWidth/2) - math.floor(ScreenWidth/4), 5, math.floor(ScreenWidth/2) - 5, 20, "Add Categorie")
  62. local windowAddMachine = wm.newWindow(math.floor(ScreenWidth/2) + 5, 5, math.floor(ScreenWidth/2) - 5, 20, "Add machine")
  63.  
  64.  
  65. function setRedstoneOutput(disable, windows)
  66.     actif = 0
  67.     if windows.name == "Spawner" then
  68.         for i = #windows.element, 1, -1 do
  69.             if actif == 3 then
  70.                 windows.element[i].state = false
  71.                 API.setElementState(windows.element[i].button, windows.element[i].state)
  72.                
  73.             end
  74.             if windows.element[i].state and not disable and online then
  75.                 redstone = component.proxy(component.get(windows.element[i].address))
  76.                 redstone.setOutput(tonumber(windows.element[i].face), 15)
  77.                 actif = actif + 1
  78.             else
  79.                 redstone = component.proxy(component.get(windows.element[i].address))
  80.                 redstone.setOutput(tonumber(windows.element[i].face), 0)
  81.             end
  82.         end
  83.         fnt.machinedraw(windows)
  84.         wm.redraw()
  85.     else
  86.         for i = #windows.element, 1, -1 do
  87.             needoffline = true
  88.             if windows.element[i].offline and not online then
  89.                 needoffline = false
  90.             end
  91.             if windows.element[i].state and needoffline then
  92.                 redstone = component.proxy(component.get(windows.element[i].address))
  93.                 redstone.setOutput(tonumber(windows.element[i].face), 15)
  94.             else
  95.                 redstone = component.proxy(component.get(windows.element[i].address))
  96.                 redstone.setOutput(tonumber(windows.element[i].face), 0)
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102.  
  103.  
  104.  
  105.  
  106. function moin(self,win)
  107.     if wm.getWindowsValue(win) ~= 1 then
  108.         wm.setWindowsValue(win, wm.getWindowsValue(win) - 1 )
  109.     end
  110.    
  111.     if win == windowGeneral then
  112.         fnt.Generaldraw()
  113.     else
  114.         fnt.machinedraw(activeMachine)
  115.     end
  116.     wm.redraw()
  117. end
  118.  
  119. function plus(self,win)
  120.     if win == windowGeneral then
  121.         if math.floor(wm.getWindowsValue(win) * 9) <= #GeneralList then
  122.             wm.setWindowsValue(win, wm.getWindowsValue(win) + 1 )
  123.             fnt.Generaldraw()  
  124.         end
  125.     else
  126.         if math.floor(wm.getWindowsValue(win) * 9) <= #activeMachine.element then
  127.             wm.setWindowsValue(win, wm.getWindowsValue(win) + 1 )
  128.             fnt.machinedraw(activeMachine)
  129.         end
  130.     end
  131.     wm.redraw()
  132. end
  133.  
  134.  
  135.  
  136.  
  137.  
  138. function okGeneralCallback(self,win)
  139.     local name = API.getElementText(input_nameg)
  140.    
  141.     if buttonadd == 1 then
  142.         fnt.Generaladd(name)
  143.     elseif buttonadd == 2 then
  144.         fnt.GeneralRemove(name)
  145.     end
  146.    
  147.     local file = io.open("/home/General","w")
  148.     for i = #GeneralList, 1, -1 do
  149.         file:write(GeneralList[i].name.."\n")
  150.     end
  151.     file:close()
  152.     fnt.Generaldraw()
  153.     wm.hideWindow(windowAddGeneral, true)
  154.     wm.redraw()
  155. end
  156.  
  157. function cancelGeneralCallback(self,win)
  158.     wm.hideWindow(windowAddGeneral, true)
  159. end
  160.  
  161.  
  162. function add_general(self,win)
  163.     buttonadd = 1
  164.     for i = #GeneralList, 1, -1 do
  165.         wm.hideWindow(GeneralList[i].windows, true)
  166.     end
  167.     wm.hideWindow(windowAddGeneral, false)
  168. end
  169.  
  170. function remove_general(self,win)
  171.     buttonadd = 2
  172.     for i = #GeneralList, 1, -1 do
  173.         wm.hideWindow(GeneralList[i].windows, true)
  174.     end
  175.     wm.hideWindow(windowAddGeneral, false)
  176. end
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. function okMachineCallback(self,win)
  185.     local text = API.getElementText(input_name)
  186.     local adress = API.getElementText(input_address)
  187.     local side = API.getElementText(input_side)
  188.     local ofline = API.getElementState(checkbox_offline)
  189.    
  190.    
  191.     if buttonadd == 1 then
  192.         fnt.machineadd(text,adress,side,ofline)
  193.     elseif buttonadd == 2 then
  194.         fnt.machineRemove(text)
  195.     end
  196.    
  197.     local file = io.open("/home/"..activeMachine.name,"w")
  198.     for i = #activeMachine.element, 1, -1 do
  199.         file:write(activeMachine.element[i].name .."\n")
  200.         file:write(activeMachine.element[i].address .."\n")
  201.         file:write(activeMachine.element[i].face .."\n")
  202.         file:write(tostring(activeMachine.element[i].offline) .."\n")
  203.     end
  204.     file:close()
  205.    
  206.     fnt.machinedraw(activeMachine)
  207.     wm.hideWindow(windowAddMachine, true)
  208.     wm.hideWindow(activeMachine.windows, false)
  209.     wm.redraw()
  210. end
  211.  
  212. function cancelMachineCallback(self,win)
  213.     wm.hideWindow(windowAddMachine, true)
  214.     wm.hideWindow(activeMachine.windows, false)
  215. end
  216.  
  217. function add_machine(self,win)
  218.     buttonadd = 1
  219.     for i = #GeneralList, 1, -1 do
  220.         wm.hideWindow(GeneralList[i].windows, true)
  221.     end
  222.     wm.hideWindow(windowAddMachine, false)
  223. end
  224.  
  225. function remove_machine(self,win)
  226.     buttonadd = 2
  227.     for i = #GeneralList, 1, -1 do
  228.         wm.hideWindow(GeneralList[i].windows, true)
  229.     end
  230.     wm.hideWindow(windowAddMachine, false)
  231. end
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. --General Windows
  245. function general_action(self,win)
  246.     activeMachine = nil
  247.     for i = #GeneralList, 1, -1 do
  248.         if GeneralList[i].name == API.getElementText(self) and API.getElementState(GeneralList[i].button) then
  249.             fnt.machinedraw(GeneralList[i])
  250.             activeMachine = GeneralList[i]
  251.             wm.hideWindow(GeneralList[i].windows, false)
  252.         else
  253.             wm.hideWindow(GeneralList[i].windows, true)
  254.             API.setElementState(GeneralList[i].button)
  255.         end
  256.     end
  257.  
  258. end
  259.  
  260. function fnt.Generaldraw()
  261.     page = wm.getWindowsValue(windowGeneral)
  262.     wm.clearElement(windowGeneral)
  263.     width = math.floor((math.floor(ScreenWidth/2) - 5) / 4 ) - 1
  264.     btn_moin = API.newButton(1, 1, width, 1, "<", moin, true)
  265.     btn_add_general = API.newButton(width + 2, 1, width, 1, "ADD", add_general, true)
  266.     btn_remove_general = API.newButton(width + width + 3, 1, width, 1, "REMOVE", remove_general, true)
  267.     btn_plus = API.newButton(width + width + width + 4, 1, width, 1, ">", plus, true)
  268.     wm.addElement(windowGeneral, btn_moin)
  269.     wm.addElement(windowGeneral, btn_plus)
  270.     wm.addElement(windowGeneral, btn_add_general)
  271.     wm.addElement(windowGeneral, btn_remove_general)
  272.    
  273.     for i = 1, 9, 1 do
  274.         local element = math.floor((page - 1) * 9) + i
  275.         if element > #GeneralList then
  276.             break
  277.         end
  278.         GeneralList[element].button = API.newButton(1, 1 + (i * 2), (math.floor(ScreenWidth/2) - 5), 1, GeneralList[element].name, general_action, false)
  279.         wm.addElement(windowGeneral, GeneralList[element].button)
  280.     end
  281. end
  282.  
  283. function fnt.Generaladd(name)
  284.     result = {
  285.         name = name,
  286.         button = nil,
  287.         windows = nil,
  288.         element = {},
  289.     }
  290.   table.insert(GeneralList, result)
  291.   fnt.GeneralsetWindows(result)
  292. end
  293.  
  294. function fnt.GeneralRemove(name)
  295.     for i = #GeneralList, 1, -1 do
  296.         if GeneralList[i].name == name then
  297.             for x = #GeneralList[i].element, 1, -1 do
  298.                 GeneralList[i].element[x].state = false
  299.             end
  300.             setRedstoneOutput(false, GeneralList[i])
  301.             table.remove(GeneralList, i)
  302.         end
  303.     end
  304.     os.execute("del "..name)
  305. end
  306.  
  307. function fnt.GeneralsetWindows(result)
  308.     result.windows = wm.newWindow(math.floor(ScreenWidth/2) + 5, 5, math.floor(ScreenWidth/2) - 5, 20, result.name)
  309.     fnt.getMachine(result)
  310. end
  311.  
  312. function fnt.getGeneral()
  313.     if fs.exists('/home/General') == false then
  314.         local file = io.open("/home/General","w")
  315.         file:close()
  316.     end
  317.     for line in io.lines("/home/General") do
  318.         fnt.Generaladd(line)
  319.     end
  320. end
  321.  
  322.  
  323. --SousWindows
  324. function action_machine(self,win)
  325.     for i = #activeMachine.element, 1, -1 do
  326.         if activeMachine.element[i].name == API.getElementText(self) then
  327.             activeMachine.element[i].state = not activeMachine.element[i].state
  328.             API.setElementState(activeMachine.element[i].button, activeMachine.element[i].state)
  329.             setRedstoneOutput(false,activeMachine)
  330.         end
  331.     end
  332. end
  333.  
  334. function fnt.machinedraw(tables)
  335.     page = wm.getWindowsValue(tables.windows)
  336.     wm.clearElement(tables.windows)
  337.     width = math.floor((math.floor(ScreenWidth/2) - 5) / 4 ) - 1
  338.     btn_moin = API.newButton(1, 1, width, 1, "<", moin, true)
  339.     btn_add_general = API.newButton(width + 2, 1, width, 1, "ADD", add_machine, true)
  340.     btn_remove_general = API.newButton(width + width + 3, 1, width, 1, "REMOVE", remove_machine, true)
  341.     btn_plus = API.newButton(width + width + width + 4, 1, width, 1, ">", plus, true)
  342.     wm.addElement(tables.windows, btn_moin)
  343.     wm.addElement(tables.windows, btn_plus)
  344.     wm.addElement(tables.windows, btn_add_general)
  345.     wm.addElement(tables.windows, btn_remove_general)
  346.  
  347.     for i = 1, 9, 1 do
  348.         local elements = math.floor((page - 1) * 9) + i
  349.         if elements > #tables.element then
  350.             break
  351.         end
  352.         tables.element[elements].button = API.newButton(1, 1 + (i * 2), (math.floor(ScreenWidth/2) - 5), 1, tables.element[elements].name, action_machine, false)
  353.         wm.addElement(tables.windows, tables.element[elements].button)
  354.         API.setElementState(tables.element[elements].button, tables.element[elements].state)
  355.     end
  356. end
  357.  
  358.  
  359.  
  360. function fnt.machineadd(text,adress,side,ofline)
  361.     result = {
  362.                 name = text,
  363.                 address = adress,
  364.                 face = side,
  365.                 state = false,
  366.                 button = nil,
  367.                 offline = ofline,
  368.             }
  369.   table.insert(activeMachine.element, result)
  370. end
  371.  
  372. function fnt.machineRemove(name)
  373.     for i = #activeMachine.element, 1, -1 do
  374.         if activeMachine.element[i].name == name then
  375.             activeMachine.element[i].state = false
  376.             setRedstoneOutput(false, activeMachine)
  377.             table.remove(activeMachine.element, i)
  378.         end
  379.     end
  380. end
  381.  
  382.  
  383.  
  384. function fnt.getMachine(result)
  385.     result.element = {}
  386.    
  387.     local step = 1
  388.    
  389.     if fs.exists('/home/'..result.name) == false then
  390.         local file = io.open("/home/"..result.name,"w")
  391.         file:close()
  392.     end
  393.    
  394.     for line in io.lines("/home/"..result.name) do
  395.         if step == 1 then
  396.             name = line
  397.             step = 2
  398.         elseif step == 2 then
  399.             address = line
  400.             step = 3
  401.         elseif step == 3 then
  402.             face = line
  403.             step = 4
  404.         elseif step == 4 then
  405.             if line == "true" then
  406.                 ofline = true
  407.             else
  408.                 ofline = false
  409.             end
  410.            
  411.             step = 1
  412.             bouton = {
  413.                 name = name,
  414.                 address = address,
  415.                 face = face,
  416.                 state = false,
  417.                 button = nil,
  418.                 offline = ofline
  419.             }
  420.             table.insert(result.element,bouton)
  421.         end
  422.     end
  423. end
  424.  
  425.  
  426. --addgeneral
  427. function fnt.addGeneraldraw()
  428.     label_name = API.newLabel(2,1, math.floor(ScreenWidth/2) - 5, 1, "Nom de la categorie")
  429.     input_nameg = API.newInput(2,3,math.floor(ScreenWidth/2) - 5, "")
  430.     btn_ok_general = API.newButton(12, 19, 4, 1, "ok", okGeneralCallback)
  431.     btn_cancel_general = API.newButton(26, 19, 6, 1, "cancel", cancelGeneralCallback)
  432.     wm.addElement(windowAddGeneral, label_name)
  433.     wm.addElement(windowAddGeneral, input_nameg)
  434.     wm.addElement(windowAddGeneral, btn_ok_general)
  435.     wm.addElement(windowAddGeneral, btn_cancel_general)
  436. end
  437.  
  438.  
  439.  
  440. --addMachine
  441. function fnt.windowsAddMachine()
  442.     label_name = API.newLabel(2,1, math.floor(ScreenWidth/2) - 5, 1, "Nom de la machine")
  443.     input_name = API.newInput(2,3,math.floor(ScreenWidth/2) - 5, "")
  444.     label_address = API.newLabel(2,5, math.floor(ScreenWidth/2) - 5, 1, "address du redstone opencomputer")
  445.     label_address_help = API.newLabel(2,7, math.floor(ScreenWidth/2) - 5, 1, "les 3 premier chiffre suffit")
  446.     input_address = API.newInput(2,9,math.floor(ScreenWidth/2) - 5, "")
  447.     label_side = API.newLabel(2,11, math.floor(ScreenWidth/2) - 5, 1, "numero de la face du block de redstone")
  448.     label_side_help = API.newLabel(2,13, math.floor(ScreenWidth/2) - 5, 1, "bottom = 0, top = 1,nord = 2, sud = 3,  west = 4, east = 5")
  449.     input_side = API.newInput(2,15,math.floor(ScreenWidth/2) - 5, "")
  450.     checkbox_offline = API.newCheckbox(2, 17, "Désactive le bouton si le joueurs est offline")
  451.     btn_ok_spawner = API.newButton(12, 19, 4, 1, "ok", okMachineCallback)
  452.     btn_cancel_spawner = API.newButton(26, 19, 6, 1, "cancel", cancelMachineCallback)
  453.     wm.addElement(windowAddMachine, label_name)
  454.     wm.addElement(windowAddMachine, label_address)
  455.     wm.addElement(windowAddMachine, label_side)
  456.     wm.addElement(windowAddMachine, label_address_help)
  457.     wm.addElement(windowAddMachine, label_side_help)
  458.     wm.addElement(windowAddMachine, input_name)
  459.     wm.addElement(windowAddMachine, input_address)
  460.     wm.addElement(windowAddMachine, input_side)
  461.     wm.addElement(windowAddMachine, checkbox_offline)
  462.     wm.addElement(windowAddMachine, btn_ok_spawner)
  463.     wm.addElement(windowAddMachine, btn_cancel_spawner)
  464.    
  465.     API.setElementAlignment(checkbox_offline, "right")
  466. end
  467.  
  468. function exitGui()
  469. local shrc = io.open("/home/.shrc","w")
  470. shrc:write("")
  471. shrc:close()
  472. wm.shutdown(true)
  473. end
  474.  
  475.  
  476.  
  477.  
  478. wm.startGui()
  479. wm.setTopText("Welcome")
  480. wm.setSystemMenuPos("right")
  481. wm.addSystemMenu("Exit", exitGui, nil, 1)
  482. wm.addSystemMenu("Shutdown", wm.shutdown, nil, 1)
  483. wm.addSystemMenu("Reboot", wm.shutdown, true, 1)
  484.  
  485. fnt.getGeneral()
  486. fnt.addGeneraldraw()
  487. fnt.windowsAddMachine()
  488. fnt.Generaldraw()
  489. wm.hideWindow(windowGeneral, false)
  490.  
  491. for i = #GeneralList, 1, -1 do
  492.     setRedstoneOutput(true,GeneralList[i])
  493. end
  494.  
  495.  
  496. local main_thread = thread.create(function() os.sleep(5)
  497.     while true do
  498.         local _,_,x,y = require("event").pull("touch")
  499.         if x and y then
  500.             wm.TouchEventListener(_, _, x, y)
  501.         end
  502.     end
  503. end)
  504.  
  505.  
  506.  
  507. local function getStringFromResponce(responce)
  508.   local ret = ""
  509.   local resp = responce()
  510.   while(resp~=nil) do
  511.     ret = ret..tostring(resp)
  512.     resp = responce()
  513.   end
  514.   return ret
  515. end
  516.  
  517. local function getHTTPData(url)
  518.   local ret = nil
  519.   local request, responce = pcall(internet.request, url)
  520.   if(request) then
  521.     ret = getStringFromResponce(responce)
  522.   end
  523.   return ret
  524. end
  525.  
  526. local function has_value(tab)
  527.   for index,value in ipairs(tab) do
  528.     if value == "Ao_Shinji" or value == "THEJean_Kevin" then
  529.       return true
  530.     end
  531.   end
  532.   return false
  533. end
  534.  
  535. local second_thread = thread.create(function()
  536.     while true do
  537.         os.sleep(5)
  538.         apiJson = json.decode(getHTTPData("https://api.mineaurion.com/v1/serveurs/revelation.mineaurion.com"))
  539.         if apiJson.joueurs ~= false then
  540.             if has_value(apiJson["joueurs"]) then
  541.                 online = true
  542.                 for i = #GeneralList, 1, -1 do
  543.                     setRedstoneOutput(false, GeneralList[i])
  544.                 end
  545.             else
  546.                 online = false
  547.                 for i = #GeneralList, 1, -1 do
  548.                     setRedstoneOutput(false, GeneralList[i])
  549.                 end
  550.             end
  551.         end
  552.     end
  553. end)
  554.  
  555. thread.waitForAll({second_thread,main_thread})
  556. return fnt
Advertisement
Add Comment
Please, Sign In to add comment