csmit195

CamingLord: Resident Control/Code

Jun 7th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | None | 0 0
  1. local bedrockPath='Build/' if OneOS then OneOS.LoadAPI('/System/API/Bedrock.lua', false)elseif fs.exists(bedrockPath..'/Bedrock')then os.loadAPI(bedrockPath..'/Bedrock')else if http then print('Downloading Bedrock...')local h=http.get('http://pastebin.com/raw.php?i=0MgKNqpN')if h then local f=fs.open(bedrockPath..'/Bedrock','w')f.write(h.readAll())f.close()h.close()os.loadAPI(bedrockPath..'/Bedrock')else error('Failed to download Bedrock. Is your internet working?') end else error('This program needs to download Bedrock to work. Please enable HTTP.') end end if Bedrock then Bedrock.BasePath = bedrockPath Bedrock.ProgramPath = shell.getRunningProgram() end
  2.  
  3. local receivingComputer = 475
  4. local currentSelection
  5.  
  6. rednet.open('back')
  7. local program
  8.  
  9. function init()
  10.     program = Bedrock:Initialise()
  11.     program:Run(function()
  12.         program:LoadView('main')
  13.        
  14.         -- Load Button (loadBtn)
  15.         program:GetObject('loadBtn').OnClick = function(self, event, side, x, y)
  16.             -- Request file from other computer
  17.             program:StartTimer(updateResidentList, 0.1)
  18.         end
  19.        
  20.         -- Add Button (addBtn)
  21.         program:GetObject('addBtn').OnClick = function(self, event, side, x, y)
  22.             -- Request file from other computer
  23.             addNew()
  24.         end
  25.        
  26.         -- Save Button (saveBtn)
  27.         program:GetObject('saveBtn').OnClick = function(self, event, side, x, y)
  28.             -- Send file to pc.
  29.             local newResidents = ''
  30.             for key, item in ipairs(program:GetObject('residentList').Items) do
  31.                 newResidents = newResidents .. item .. '\n'
  32.             end
  33.             rednet.send(receivingComputer, newResidents, 'resident_controller')
  34.         end
  35.        
  36.         -- Delete Button (deleteBtn)
  37.         program:GetObject('deleteBtn').OnClick = function(self, event, side, x, y)
  38.             -- Remove from Items, tried to remvoe the key, only works with new table element.
  39.             if ( currentSelection ) then
  40.                 program:DisplayAlertWindow('Are you sure?', 'Delete "'..currentSelection..'"?', {'No', 'Yes'}, function(value)
  41.                     if ( value == 'Yes' ) then
  42.                         local newResidents = {}
  43.                         for key, item in ipairs(program:GetObject('residentList').Items) do
  44.                             if ( item ~= currentSelection ) then
  45.                                 table.insert(newResidents, item)
  46.                             end
  47.                         end
  48.                         program:GetObject('residentList').Items = newResidents
  49.                     end
  50.                 end)
  51.             end
  52.         end
  53.        
  54.         program:GetObject('residentList').OnSelect = function(_, item)
  55.             currentSelection = item
  56.         end
  57.     end)
  58. end
  59.  
  60. function addNew()
  61.     program:AddObject({
  62.         Name="add_window",
  63.         X=3,
  64.         Y="30%",
  65.         Width=22,
  66.         ShadowColour = "transparent",
  67.         Height=3,
  68.         Type="Window",
  69.         Title="Add New",
  70.         Children={
  71.             {
  72.                 X=1,
  73.                 Y=2,
  74.                 Width=22,
  75.                 BackgroundColour="white",
  76.                 Placeholder="Name Here",
  77.                 PlaceholderTextColour="grey",
  78.                 Height=1,
  79.                 Name="add_editbox",
  80.                 Type="TextBox"
  81.             },
  82.             {
  83.                 X=17,
  84.                 Y=3,
  85.                 Width=6,
  86.                 BackgroundColour="green",
  87.                 Height=1,
  88.                 Text="Add",
  89.                 Name="add_btn",
  90.                 Type="Button"
  91.             }
  92.         }
  93.     })
  94.     program:GetObject('add_btn').OnClick = function(self, event, side, x, y)
  95.         -- Request file from other computer
  96.         local newResident = program:GetObject('add_editbox').Text
  97.         local newResidents = {}
  98.         for key, item in ipairs(program:GetObject('residentList').Items) do
  99.             table.insert(newResidents, item)
  100.         end
  101.         table.insert(newResidents, newResident)
  102.         program:GetObject('residentList').Items = newResidents
  103.         program:GetObject('add_window'):Close()
  104.     end
  105. end
  106.  
  107. function updateResidentList()
  108.     rednet.send(receivingComputer, 'rc_load', 'resident_controller')
  109.     -- Wait for reply
  110.     local senderId, message, protocol = rednet.receive('resident_controller', 1)
  111.     if ( senderId and senderId == receivingComputer ) then
  112.         local residents = split(message, '\n')
  113.         program:GetObject('residentList').Items = residents
  114.     elseif ( not senderId ) then
  115.         program:DisplayAlertWindow('No Connection', ' Not in range or offline!', {'Retry', 'Later'}, function(value)
  116.             if ( value == 'Retry' ) then
  117.                 program:StartTimer(updateResidentList, 0.1)
  118.             end
  119.         end)
  120.     end
  121. end
  122.  
  123. -- Utility Functions
  124. function split(str,sep)
  125.    local sep, fields = sep or ":", {}
  126.    local pattern = string.format("([^%s]+)", sep)
  127.    str:gsub(pattern, function(c) fields[#fields+1] = c end)
  128.    return fields
  129. end
  130.  
  131. -- Start Init Function
  132. init()
Add Comment
Please, Sign In to add comment