This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
NullSchritt's profile picture

Creating A List From Url

Started by NullSchritt, 22 July 2013 - 11:22 AM
NullSchritt #1
Posted 22 July 2013 - 01:22 PM
Hello all, I have been using comptuercraft for a while to control the stargates plugin on my server, however currently it requires the user to type in the destination.

I would very much like to have a list they can scroll through and select which address they want to go to.

Below is the URL that serves the list of names.

http://projectbuilder.info/fetch.php?cmd=page&ID=gatedirectory

I'm still rather new to LUA, so I cannot figure out how to split this string up into an array, and then make a list from it, any help would be appreciated.

Below is my current code(I would still like an option in the menu to allow them to enter a custom address, as not all addresses are "public"):


for k,v in pairs( peripheral.getNames()) do
  if peripheral.getType(v) == "monitor" then
    mon = peripheral.wrap(v)
  end
end
local function centerprint(msg, y)
  local w, h = term.getSize()
  term.setCursorPos(math.floor((w-#msg)/2) + (#msg % 2 == 0 and 1 or 0), y or h/2)
  print(msg)
end
local function moncenterprint(msg, y)
  local w, h = mon.getSize()
  mon.setCursorPos(math.floor((w-#msg)/2) + (#msg % 2 == 0 and 1 or 0), y or h/2)
  mon.write(msg)
end
os.pullEvent=os.pullEventRaw
cb = peripheral.wrap("bottom")
out = peripheral.wrap("top")
id = os.getComputerLabel()
while true do
 
moncenterprint("StarGate Dialing", 1)
moncenterprint("Computer", 2)
  centerprint("Stargate Auto Dialer", 2)
  centerprint("Please enter a destination:", 3)
 
  destination = read()
 
	 content = http.get("http://smp.projectbuilder.info/fetch.php?cmd=page&ID=dialgate&buffer=0&name=" .. destination)
	 s = content.readAll()
 
  if s == 0 then
	 centerprint("Sorry, but you are not authorized to connect to this gate.")
mon.clear()
    moncenterprint("Connection Not", 1)
    moncenterprint("Authorized", 2)
    elseif s == "" then
  centerprint("Sorry but your destination does not seem to exist. Try being less specific.")
mon.clear()
moncenterprint("Destination Does", 1)
moncenterprint("Not Exist", 2)
  else
	 centerprint("Dialing '".. s .. "'", 4)
mon.clear()
moncenterprint("Dialing", 1)
moncenterprint("'".. s .. "'", 2)
	  cb.setCommand("gate activate " .. id .. " " .. s)
	  cb.runCommand()
  end
sleep(10)
term.clear()
mon.clear()
end

Any help would be much appreciated.
Grim Reaper #2
Posted 22 July 2013 - 01:58 PM
You can use this code to do that:


local gateListUrl = "http://projectbuilder.info/fetch.php?cmd=page&ID=gatedirectory"
local gateList    = {}

local function parseRawGateList (rawGateList)
    if not rawGateList then
        return
    end

    local gateList = {}
    for gateName in rawGateList:gmatch ("|-(.-)|") do
        table.insert (gateList, gateName)
    end

    return gateList
end

local function downloadGateList (url)
    local rawGateListHandle = http.get (url)

    if rawGateListHandle then
        local rawGateList = rawGateListHandle.readAll()

        rawGateListHandle.close()
        return rawGateList
    end
end

gateList = parseRawGateList (downloadGateList (gateListUrl))

if gateList then
    for _, gate in pairs (gateList) do
        print (gate)
    end

    return
end

print ("Failed to download gate list.")

The above code attempts to request the page where your list is stored, parse all of the names of the gates that you have listed into a table, and printing out all of the gates if they were downloaded/parsed properly.

When I tried to test the code above, I couldn't seem to download the page while in-game, but my browser can open it just fine. I think there may be something wrong with the interaction between http.request and PHP, but that's just my guess :P/>

I think http.post might have to be used, but I don't seem to be able to get that to work. Perhaps my understanding of it is too simple.
H4X0RZ #3
Posted 22 July 2013 - 02:07 PM
You could split it so:

local function split(String, regex)
  local found = {}
  for str in string.gmatch("[^"..regex.."]+") do
    table.insert(found, str)
  end
  return found
end

local connection = http.get(YourWebsite)
local gates = split(connection.readAll(),"|")
Now you can create a menu out of the table "gates" :D/>/>
NullSchritt #4
Posted 09 August 2013 - 05:19 PM
Thanks all this is helpful, but how can I make it so that when the user actually selects one of the gates from the list it submits that text to a url?

In other words how do I know which item the user has selected when they select it/how do I make a function execute when the user selects an item?

Thanks again for all the help!
NullSchritt #5
Posted 09 August 2013 - 05:37 PM
You could split it so:

local function split(String, regex)
  local found = {}
  for str in string.gmatch("[^"..regex.."]+") do
    table.insert(found, str)
  end
  return found
end

local connection = http.get(YourWebsite)
local gates = split(connection.readAll(),"|")
Now you can create a menu out of the table "gates" :D/>/>/>
Also thanks for the advice, but I have no idea how to access or use tables (:
Sorry I'm a bit of a noob with LUA.
NullSchritt #6
Posted 09 August 2013 - 06:26 PM
You can use this code to do that:


local gateListUrl = "http://projectbuilder.info/fetch.php?cmd=page&ID=gatedirectory"
local gateList    = {}

local function parseRawGateList (rawGateList)
    if not rawGateList then
        return
    end

    local gateList = {}
    for gateName in rawGateList:gmatch ("|-(.-)|") do
        table.insert (gateList, gateName)
    end

    return gateList
end

local function downloadGateList (url)
    local rawGateListHandle = http.get (url)

    if rawGateListHandle then
        local rawGateList = rawGateListHandle.readAll()

        rawGateListHandle.close()
        return rawGateList
    end
end

gateList = parseRawGateList (downloadGateList (gateListUrl))

if gateList then
    for _, gate in pairs (gateList) do
        print (gate)
    end

    return
end

print ("Failed to download gate list.")

The above code attempts to request the page where your list is stored, parse all of the names of the gates that you have listed into a table, and printing out all of the gates if they were downloaded/parsed properly.

When I tried to test the code above, I couldn't seem to download the page while in-game, but my browser can open it just fine. I think there may be something wrong with the interaction between http.request and PHP, but that's just my guess :P/>/>

I think http.post might have to be used, but I don't seem to be able to get that to work. Perhaps my understanding of it is too simple.
It doesn't work in the code because automated queries are blocked on the main domain to help prevent DDoS attacks, a special sub-domain must be used for automated requests.