Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.02 KB | None | 0 0
  1. --Sets all the requires and such idk what these are actually called
  2. local event = require("event")
  3. local os = require("os")
  4. local serialization = require("serialization")
  5. local component = require("component")
  6. local modem = component.modem
  7. local running = true
  8. local dbUser = "root"
  9. local dbPass = "password"
  10. local currentGateType = ""
  11. local stopDial = false
  12. local db = require("db")
  13. local sg = component.stargate
  14. local split = require("split")
  15.  
  16. local function unknownEvent()
  17.     -- do nothing
  18. end
  19.  
  20. --Sets up tables to be filled
  21. local ports = {}
  22. local databases = {}
  23. local eventIDs = {}
  24. local gateTypes = {}
  25. local glyphConversion = {}
  26. local errors = {}
  27. local eventHandlers =
  28.     setmetatable(
  29.     {},
  30.     {
  31.         __index = function()
  32.             return unknownEvent
  33.         end
  34.     }
  35. )
  36.  
  37. --Sets up all network related things
  38. local function mainSetup()
  39.     --The main ports the dialing server uses
  40.     ports.dial = 69
  41.     ports.gateSignals = 70
  42.     ports.DB = 126
  43.  
  44.     --The name of the address database
  45.     databases.storedAddressesName = "sgAddresses"
  46.  
  47.     --Creates the table where the database is stored
  48.     databases.storedAddresses = {}
  49.  
  50.     --Creates all the gate types
  51.     gateTypes.MILKYWAY = "MW"
  52.     gateTypes.PEGASUS = "PG"
  53.     gateTypes.UNIVERSE = "UN"
  54.  
  55.     --Creates the tables where the glyph conversions are stored for each type
  56.     glyphConversion.MW = {}
  57.     glyphConversion.PG = {}
  58.     glyphConversion.UN = {}
  59.  
  60.     --Same glyph conversion as the one devised by FlamePoint#8908
  61.     glyphConversion.MW["q"] = "Point of Origin"
  62.     glyphConversion.MW["w"] = "Crater"
  63.     glyphConversion.MW["e"] = "Virgo"
  64.     glyphConversion.MW["r"] = "Bootes"
  65.     glyphConversion.MW["t"] = "Centaurus"
  66.     glyphConversion.MW["y"] = "Libra"
  67.     glyphConversion.MW["u"] = "Serpens Caput"
  68.     glyphConversion.MW["i"] = "Norma"
  69.     glyphConversion.MW["o"] = "Scorpius"
  70.     glyphConversion.MW["p"] = "Corona Australis"
  71.     glyphConversion.MW["["] = "Scutum"
  72.     glyphConversion.MW["]"] = "Sagittarius"
  73.     glyphConversion.MW["\\"] = "Aquila"
  74.     glyphConversion.MW["a"] = "Microscopium"
  75.     glyphConversion.MW["s"] = "Capricornus"
  76.     glyphConversion.MW["d"] = "Piscis Austrinus"
  77.     glyphConversion.MW["f"] = "Equuleus"
  78.     glyphConversion.MW["g"] = "Aquarius"
  79.     glyphConversion.MW["h"] = "Pegasus"
  80.     glyphConversion.MW["j"] = "Sculptor"
  81.     glyphConversion.MW["k"] = "Pisces"
  82.     glyphConversion.MW["l"] = "Andromeda"
  83.     glyphConversion.MW[";"] = "Triangulum"
  84.     glyphConversion.MW["'"] = "Aries"
  85.     glyphConversion.MW["z"] = "Perseus"
  86.     glyphConversion.MW["x"] = "Cetus"
  87.     glyphConversion.MW["c"] = "Taurus"
  88.     glyphConversion.MW["v"] = "Auriga"
  89.     glyphConversion.MW["b"] = "Eridanus"
  90.     glyphConversion.MW["n"] = "Orion"
  91.     glyphConversion.MW["m"] = "Canis Minor"
  92.     glyphConversion.MW[","] = "Monoceros"
  93.     glyphConversion.MW["."] = "Gemini"
  94.     glyphConversion.MW["/"] = "Hydra"
  95.     glyphConversion.MW["1"] = "Lynx"
  96.     glyphConversion.MW["2"] = "Cancer"
  97.     glyphConversion.MW["3"] = "Sextans"
  98.     glyphConversion.MW["4"] = "Leo Minor"
  99.     glyphConversion.MW["5"] = "Leo"
  100.  
  101.     --New pegasus glyph conversion devised by me
  102.     glyphConversion.PG["q"] = "Subido"
  103.     glyphConversion.PG["w"] = "Aaxel"
  104.     glyphConversion.PG["e"] = "Abrin"
  105.     glyphConversion.PG["r"] = "Acjesis"
  106.     glyphConversion.PG["t"] = "Aldeni"
  107.     glyphConversion.PG["y"] = "Alura"
  108.     glyphConversion.PG["u"] = "Amiwill"
  109.     glyphConversion.PG["i"] = "Arami"
  110.     glyphConversion.PG["o"] = "Avoniv"
  111.     glyphConversion.PG["p"] = "Baselai"
  112.     glyphConversion.PG["["] = "Bydo"
  113.     glyphConversion.PG["]"] = "Ca Po"
  114.     glyphConversion.PG["\\"] = "Danami"
  115.     glyphConversion.PG["a"] = "Dawnre"
  116.     glyphConversion.PG["s"] = "Ecrumig"
  117.     glyphConversion.PG["d"] = "Elenami"
  118.     glyphConversion.PG["f"] = "Gilltin"
  119.     glyphConversion.PG["g"] = "Hacemill"
  120.     glyphConversion.PG["h"] = "Hamlinto"
  121.     glyphConversion.PG["j"] = "Illume"
  122.     glyphConversion.PG["k"] = "Laylox"
  123.     glyphConversion.PG["l"] = "Lenchan"
  124.     glyphConversion.PG[";"] = "Olavii"
  125.     glyphConversion.PG["'"] = "Once el"
  126.     glyphConversion.PG["z"] = "Poco re"
  127.     glyphConversion.PG["x"] = "Ramnon"
  128.     glyphConversion.PG["c"] = "Recktic"
  129.     glyphConversion.PG["v"] = "Robandus"
  130.     glyphConversion.PG["b"] = "Roehi"
  131.     glyphConversion.PG["n"] = "Salma"
  132.     glyphConversion.PG["m"] = "Sandovi"
  133.     glyphConversion.PG[","] = "Setas"
  134.     glyphConversion.PG["."] = "Sibbron"
  135.     glyphConversion.PG["/"] = "Tahnan"
  136.     glyphConversion.PG["1"] = "Zamilloz"
  137.     glyphConversion.PG["2"] = "Zeo"
  138.  
  139.     --New universe glyph conversion devised by me
  140.     glyphConversion.UN["q"] = 17
  141.     glyphConversion.UN["w"] = 1
  142.     glyphConversion.UN["e"] = 2
  143.     glyphConversion.UN["r"] = 3
  144.     glyphConversion.UN["t"] = 4
  145.     glyphConversion.UN["y"] = 5
  146.     glyphConversion.UN["u"] = 6
  147.     glyphConversion.UN["i"] = 7
  148.     glyphConversion.UN["o"] = 8
  149.     glyphConversion.UN["p"] = 9
  150.     glyphConversion.UN["["] = 10
  151.     glyphConversion.UN["]"] = 11
  152.     glyphConversion.UN["\\"] = 12
  153.     glyphConversion.UN["a"] = 13
  154.     glyphConversion.UN["s"] = 14
  155.     glyphConversion.UN["d"] = 15
  156.     glyphConversion.UN["f"] = 16
  157.     glyphConversion.UN["g"] = 18
  158.     glyphConversion.UN["h"] = 19
  159.     glyphConversion.UN["j"] = 20
  160.     glyphConversion.UN["k"] = 21
  161.     glyphConversion.UN["l"] = 22
  162.     glyphConversion.UN[";"] = 23
  163.     glyphConversion.UN["'"] = 24
  164.     glyphConversion.UN["z"] = 25
  165.     glyphConversion.UN["x"] = 26
  166.     glyphConversion.UN["c"] = 27
  167.     glyphConversion.UN["v"] = 28
  168.     glyphConversion.UN["b"] = 29
  169.     glyphConversion.UN["n"] = 30
  170.     glyphConversion.UN["m"] = 31
  171.     glyphConversion.UN[","] = 32
  172.     glyphConversion.UN["."] = 33
  173.     glyphConversion.UN["/"] = 34
  174.     glyphConversion.UN["1"] = 35
  175.     glyphConversion.UN["2"] = 36
  176.  
  177.     --Gets the current gate type and sets currentGateType to the proper acronym for the gate
  178.     currentGateType = gateTypes[sg.getGateType()]
  179.     print("gate type is", currentGateType)
  180.  
  181.     --Opens all ports in the ports table
  182.     for k, v in pairs(ports) do
  183.         --Opens the ports
  184.         modem.open(ports[k])
  185.     end
  186.  
  187.     --Gets the address database from the database server
  188.     local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  189.         db.get_database(dbUser, dbPass, databases.storedAddressesName)
  190.  
  191.     local requestData = serialization.unserialize(requestdataRaw)
  192.  
  193.     --Prints the data got back from the database server
  194.     print(useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  195.  
  196.     --Checks if the database does not exist
  197.     if requestData == nil then
  198.         print("The database does not exist")
  199.  
  200.         --If it does not exist create it
  201.         local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  202.             db.create_database(dbUser, dbPass, databases.storedAddressesName)
  203.  
  204.         print("create_database", useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  205.     end
  206.  
  207.     --After making sure that the database exists and if not creating it, get the database to check for the sample address
  208.     local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  209.         db.get_database(dbUser, dbPass, databases.storedAddressesName)
  210.  
  211.     local requestData = serialization.unserialize(requestdataRaw)
  212.  
  213.     --Checks if the sample address does not exist
  214.     if type(requestData.sample) == "nil" then
  215.         print("The sample entry does not exist")
  216.  
  217.         --If not create it
  218.         print(
  219.             db.insert_entry(
  220.                 dbUser,
  221.                 dbPass,
  222.                 databases.storedAddressesName,
  223.                 "sample",
  224.                 {["MW"] = "wertyu", ["PG"] = "wertyu", ["UN"] = "wertyu"}
  225.             )
  226.         )
  227.  
  228.         print("insert_entry", useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  229.     end
  230. end
  231.  
  232. --This function dials an address
  233. local function dialAddress(address)
  234.     --Broadcasts on the gateSignals port that a dialing sequence has begun
  235.     modem.broadcast(ports.gateSignals, "begin_dial_sequence", serialization.serialize(address), #address)
  236.  
  237.     for i = 1, #address do
  238.         if stopDial then
  239.             modem.broadcast(ports.gateSignals, "dial_cancelled")
  240.             break
  241.         end
  242.         --Engages the symbol
  243.         local engageOutput = sg.engageSymbol(address[i])
  244.         if engageOutput == "stargate_spin" then
  245.             local _, _, _, symbolCount, lock, symbolName = event.pull("stargate_spin_chevron_engaged")
  246.  
  247.             --Broadcast on the gateSignals port that another symbol was dialed
  248.             modem.broadcast(ports.gateSignals, "symbol_dialed", i, #address)
  249.  
  250.             print(symbolCount, lock, symbolName)
  251.         end
  252.     end
  253.  
  254.     --After dialing engage the gate
  255.     print(sg.engageGate())
  256. end
  257.  
  258. --Handles modem messages
  259. function eventHandlers.modem_message(_, origin, port, _, requestType, ...)
  260.     print(origin, port, requestType, ...)
  261.     --Creates a table for storing functions
  262.     local requests =
  263.         setmetatable(
  264.         {},
  265.         {
  266.             __index = function()
  267.                 return unknownEvent
  268.             end
  269.         }
  270.     )
  271.  
  272.     --Creates a blank error
  273.     errors.noerror = ""
  274.  
  275.     --The function for getting the address database
  276.     function requests.get_database()
  277.         print("getting database")
  278.         local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  279.             db.get_database(dbUser, dbPass, databases.storedAddressesName)
  280.  
  281.         print("database got", useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  282.  
  283.         return useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw
  284.     end
  285.  
  286.     --The function for closing the gate
  287.     function requests.close_gate()
  288.         print("closing gate")
  289.         sg.disengageGate()
  290.     end
  291.  
  292.     --The function for ending a dial sequence
  293.     function requests.end_dial()
  294.         print("ending dial")
  295.         stopDial = true
  296.     end
  297.  
  298.     --The function for dialing the gate from a manual address
  299.     function requests.dial_address(address)
  300.         local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw = true, true, true, true, false
  301.  
  302.         --convert address to the correct format
  303.         local convertedAddress = split(address, "")
  304.  
  305.         table.insert(convertedAddress, "q")
  306.  
  307.         for i = 1, #convertedAddress do
  308.             convertedAddress[i] = glyphConversion[currentGateType][convertedAddress[i]]
  309.         end
  310.         --end convert
  311.  
  312.         dialAddress(convertedAddress)
  313.  
  314.         return useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw
  315.     end
  316.  
  317.     --The function for dialing the gate from the database
  318.     function requests.dial_database(addressName)
  319.         addressName = addressName:gsub("%s+", "")
  320.  
  321.         print("getting database")
  322.         local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  323.             db.get_database(dbUser, dbPass, databases.storedAddressesName)
  324.  
  325.         print("database got", useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  326.  
  327.         local databaseData = serialization.unserialize(requestdataRaw)
  328.  
  329.         print(requestdataRaw, addressName)
  330.  
  331.         print(serialization.serialize(databaseData[addressName]))
  332.  
  333.         if type(databaseData[addressName]) ~= "nil" then
  334.             --convert address to the correct format
  335.             local convertedAddress = split(databaseData[addressName][currentGateType], "")
  336.  
  337.             table.insert(convertedAddress, "q")
  338.  
  339.             for i = 1, #convertedAddress do
  340.                 convertedAddress[i] = glyphConversion[currentGateType][convertedAddress[i]]
  341.             end
  342.             --end convert
  343.  
  344.             --Dials address
  345.             dialAddress(convertedAddress)
  346.         end
  347.     end
  348.  
  349.     --The function for adding an address to the database
  350.     function requests.add_address(addressName, addressMW, addressPG, addressUN)
  351.         print("adding entry to database", addressName, addressMW, addressPG, addressUN)
  352.  
  353.         addressName = addressName:gsub("%s+", "")
  354.  
  355.         local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  356.             db.insert_entry(
  357.             dbUser,
  358.             dbPass,
  359.             databases.storedAddressesName,
  360.             addressName,
  361.             {["MW"] = addressMW, ["PG"] = addressPG, ["UN"] = addressUN}
  362.         )
  363.  
  364.         print("inserted got", useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  365.  
  366.         return useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw
  367.     end
  368.  
  369.     --The function for removing an address from the database
  370.     function requests.remove_address(addressName)
  371.         print("removing entry from database")
  372.  
  373.         addressName = addressName:gsub("%s+", "")
  374.  
  375.         local useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw =
  376.             db.remove_entry(dbUser, dbPass, databases.storedAddressesName, addressName)
  377.  
  378.         print("removed", useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw)
  379.  
  380.         return useraccepted, passaccepted, requestcompleted, requesterror, requestdataRaw
  381.     end
  382.  
  383.     --The function for getting the power data from the gate
  384.     function requests.get_power_data()
  385.         local storedEnergy = sg.getEnergyStored()
  386.         local maxEnergy = sg.getMaxEnergyStored()
  387.         local capacitorsInstalled = sg.getCapacitorsInstalled()
  388.  
  389.         print(storedEnergy, maxEnergy, capacitorsInstalled)
  390.  
  391.         return true, true, true, false, serialization.serialize({storedEnergy, maxEnergy, capacitorsInstalled})
  392.     end
  393.  
  394.     --The function for getting the gate type
  395.     function requests.get_gate_type()
  396.         return true, true, true, false, sg.getGateType()
  397.     end
  398.  
  399.     --Runs the correct function and gets the correct output
  400.     local userAccepted, passAccepted, requestCompleted, requestError, requestDataOutput = requests[requestType](...)
  401.  
  402.     --Prints the completed response to console
  403.     print(requestType, origin, ports.dial, requestDataOutput)
  404.     --Sends the completed response back to the origin
  405.     modem.send(origin, ports.dial, requestDataOutput)
  406. end
  407.  
  408. --Stops the program if the interrupted event gets triggered
  409. function eventHandlers.interrupted()
  410.     print("Server stopped")
  411.     running = false
  412. end
  413.  
  414. --Broadcasts on the gateSignals port that a chevron was engaged
  415. function eventHandlers.stargate_spin_chevron_engaged(_, _, symbolCount, lock, symbolName)
  416.     modem.broadcast(ports.gateSignals, "stargate_spin_chevron_engaged", symbolCount, lock, symbolName)
  417. end
  418.  
  419. --Broadcasts on the gateSignals port that there is an incoming wormhole
  420. function eventHandlers.stargate_incoming_wormhole(_, _, dialedAddressSize)
  421.     modem.broadcast(ports.gateSignals, "stargate_incoming_wormhole", dialedAddressSize)
  422. end
  423.  
  424. --Broadcasts on the gateSignals port that the gate is opening
  425. function eventHandlers.stargate_open(_, _, isInitiating)
  426.     modem.broadcast(ports.gateSignals, "stargate_open", isInitiating)
  427. end
  428.  
  429. --Broadcasts on the gateSignals port that the gate closed
  430. function eventHandlers.stargate_close(_, _)
  431.     modem.broadcast(ports.gateSignals, "stargate_close")
  432. end
  433.  
  434. --Broadcasts on the gateSignals port that the gate failed
  435. function eventHandlers.stargate_failed(_, _)
  436.     modem.broadcast(ports.gateSignals, "stargate_failed")
  437. end
  438.  
  439. --Broadcasts on the gateSignals port that there is a traveler
  440. function eventHandlers.stargate_traveler(_, _, inbound, player)
  441.     modem.broadcast(ports.gateSignals, "stargate_traveler", inbound, player)
  442. end
  443.  
  444. -- The main event handler as function to separate eventID from the remaining arguments
  445. local function handleEvent(eventID, ...)
  446.     if (eventID) then -- can be nil if no event was pulled for some time
  447.         eventHandlers[eventID](...) -- call the appropriate event handler with all remaining arguments
  448.     end
  449. end
  450.  
  451. --Sets up a lot of things
  452. mainSetup()
  453.  
  454. --Registers all the event listeners
  455. eventIDs.interrupted = event.listen("interrupted", handleEvent)
  456. eventIDs.modem_message = event.listen("modem_message", handleEvent)
  457. eventIDs.stargate_spin_chevron_engaged = event.listen("stargate_spin_chevron_engaged", handleEvent)
  458. eventIDs.stargate_incoming_wormhole = event.listen("stargate_incoming_wormhole", handleEvent)
  459. eventIDs.stargate_open = event.listen("stargate_open", handleEvent)
  460. eventIDs.stargate_close = event.listen("stargate_close", handleEvent)
  461. eventIDs.stargate_failed = event.listen("stargate_failed", handleEvent)
  462. eventIDs.stargate_traveler = event.listen("stargate_traveler", handleEvent)
  463.  
  464. --Prints to console that the server has successfully started
  465. print("Server started")
  466.  
  467. --Continues looping until running == false, which only occurs after an interrupted event
  468. while running do
  469.     os.sleep(0.1)
  470. end
  471.  
  472. --Cancells all event listeners
  473. for k, v in pairs(eventIDs) do
  474.     print("cancel " .. k, event.ignore(k, handleEvent))
  475. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement