Как узнать какие сервера

SomaGnoma

Известный
442
152
Lua:
local copas = require 'copas'
local http = require 'copas.http'


function getSAMPMasterList()
    local masterList = {}
    local response, code, headers, status = httpRequest("http://lists.sa-mp.com/0.3.7/internet")
    if response then
        for str in string.gmatch(response,"([^\n\r]+)") do
            table.insert(masterList, str)
        end
    end
    return masterList
end


function httpRequest(request, body, handler) -- copas.http
    -- start polling task
    if not copas.running then
        copas.running = true
        lua_thread.create(function()
            wait(0)
            while not copas.finished() do
                local ok, err = copas.step(0)
                if ok == nil then error(err) end
                wait(0)
            end
            copas.running = false
        end)
    end
    -- do request
    if handler then
        return copas.addthread(function(r, b, h)
            copas.setErrorHandler(function(err) h(nil, err) end)
            h(http.request(r, b))
        end, request, body, handler)
    else
        local results
        local thread = copas.addthread(function(r, b)
            copas.setErrorHandler(function(err) results = {nil, err} end)
            results = table.pack(http.request(r, b))
        end, request, body)
        while coroutine.status(thread) ~= 'dead' do wait(0) end
        return table.unpack(results)
    end
end
Кидает запрос на сайт со списком серверов во вкладке Internet и возвращает его в массиве.
Юзается так вота
Lua:
local moderi_delom_zaymites;

-- В имууги окне
if imgui.Button(u8"Обновить список серверов в гниющей игре") then
    moderi_delom_zaymites = getSAMPMasterList() -- не юзать в цикле! от запросов очко разорвет...
end
if moderi_delom_zaymites ~= nil then
    for i = 1, #moderi_delom_zaymites do
        imgui.Selectable(moderi_delom_zaymites[i])
    end
end