Advertisement
LuaCrawler

ICBM-main-server

Mar 5th, 2014
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. --[[
  2. This is the main server that takes missile launcher channel numbers and links them to labels assaigned by the user
  3. This program is made by LuaCrawler
  4. If this breaks something I take no responisbillity
  5. ]]
  6. -- License
  7. --[[
  8.     ICBM main server. Handles all the launch requests.
  9.     Copyright (C) 2014  LuaCrawler
  10.  
  11.     This program is free software: you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation, either version 3 of the License, or
  14.     any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23. ]]
  24.  
  25. -- Variables
  26. local modem = peripheral.wrap( "top" )
  27. local modemSide = "top"
  28. local lModem = peripheral.wrap("back")
  29. local lModemSide = "back"
  30. local dir = fs.exists("var/launchers")
  31. local fId = fs.exists("var/launchers/Ids.txt")
  32. local fRead = fs.open("var/launchers/Ids.txt", "r")
  33. local function startup()
  34.     term.clear()
  35.     term.setCursorPos(1,1)
  36.     print("Server console log:")
  37.     if dir == false then
  38.         fs.makeDir("var/launchers")
  39.         print("Main database directory created")
  40.     end
  41.    
  42.     -- Check to see if correct modems are attached
  43.     if peripheral.isPresent("top") == false and peripheral.isPresent("back") == false then
  44.         print("You need a modem attached to the top and back of this computer")
  45.         return false
  46.     elseif peripheral.isPresent("top") == true and peripheral.isPresent("back") == false then
  47.         print("Please add a modem to the back")
  48.         return false
  49.     else
  50.         if peripheral.isPresent("top") == false and peripheral.isPresent("back") == true then
  51.             print("Please add a modem to the top")
  52.             return false
  53.         end
  54.     end
  55.     if peripheral.getType("top") == "modem" and peripheral.getType("back") == "modem" then
  56.         modem.open(10)
  57.         lModem.open(11)
  58.         print("Modems opened; Server ready!")
  59.     end
  60. end
  61.  
  62.  
  63. startup()
  64.  
  65. local tIds = {}
  66.  
  67. while true do
  68.   local launched = false
  69.   local _e, _side, recChan, resChan, msg = os.pullEvent("modem_message") -- get modem messages
  70.     if _side == modemSide then
  71.         for k,v in pairs(tIds) do -- check ids table for received label
  72.             if msg == "launch "..k then --if the label is already stored with a id then
  73.                 print("Launch command recieved from:"..resChan)
  74.                 print(k..":   "..v)
  75.                 modem.transmit(v,v,"launch") -- launch message
  76.                 launched = true
  77.             end
  78.         end
  79.     end
  80.     if not launched and _side == lModemSide then
  81.         local file = fs.open("var/launchers/Ids.txt","w")
  82.         msg = tostring(msg)
  83.         tIds[msg] = resChan --adds label with id to table
  84.         print(textutils.serialize(tIds))
  85.         function save(table,msg)
  86.             file.write(textutils.serialize(tIds))
  87.             file.close()
  88.         end
  89.         save(tIds, msg)
  90.   end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement