Jump to content

[Country From Scoreboard] Add City?


Recommended Posts

Hello, How are you?

Well I have a simple doubt. this script created by the author countryId timic

How Can I Add, Parents and the City side?

Example: Gustavo | Brazil Rio de janeiro

IMPORTANT: 
 
Of course you'll have to put it in the ACL.xml file.
(ACL means Access Control List)
 
Example:
a) Open ACL.XML
b) Add after the following line
c)
d) Save it
e) Restart the server
f) Type in console refresh or type /refresh in chatbox
g) Start it (Type in console start or type /start in chatbox)
 
]]
 
g_Root = getRootElement()
 
function Initiate()
    country = getPlayerCountry(source)
        if country == nil or country == false then
           country = "n/a"
        end
    setElementData(source, "country", country)
end
addEventHandler('onPlayerJoin', g_Root, Initiate)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),function()
-- You can remove this if you want
outputChatBox('#ffffff[BemVindo] #ffff00Divirtam-se #ffffffDONE', getRootElement(), 255, 100, 100, true)
        call(getResourceFromName("scoreboard"),"addScoreboardColumn","Country", getRootElement(), 9, 50)
        setTimer(
function()
     country = getPlayerCountry(source)
        if country == nil or country == false then
           country = "n/a"
        end
    setElementData(source, "country", country)
end, 4000, 1)
 
    end
)
aCountries = {}
 
 
function getPlayerCountry ( player )
    return getIpCountry ( getPlayerIP ( player ) )
end
function getIpCountry ( ip )
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
    local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
    if ( #aCountries == 0 ) then
        loadIPGroups ()
    end
    if ( not aCountries[ip_group] ) then
        aCountries[ip_group] = {}
    end
    for id, group in ipairs ( aCountries[ip_group] ) do
        if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
            return group.rcountry
        end
    end
    return false
end
 
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroups ()
    unrelPosReset()
 
    local readFilename = "conf/IpToCountryCompact.csv";
    local hReadFile = fileOpen( readFilename, true )
    if not hReadFile then
        outputHere ( "Cannot read " .. readFilename )
        return
    end
 
    local buffer = ""
    while true do
        local endpos = string.find(buffer, "\n")
 
        -- If can't find CR, try to load more from the file
        if not endpos then
            if fileIsEOF( hReadFile ) then
                break
            end
            buffer = buffer .. fileRead( hReadFile, 500 )
        end
 
        if endpos then
            -- Process line
            local line = string.sub(buffer, 1, endpos - 1)
            buffer = string.sub(buffer, endpos + 1)
 
            local parts = split( line, string.byte(',') )
            if #parts > 2 then
                local rstart = tonumber(parts[1])
                local rend = tonumber(parts[2])
                local rcountry = parts[3]
 
                -- Relative to absolute numbers
                rstart = unrelRange ( rstart )
                rend = unrelRange ( rend )
 
                local group = math.floor( rstart / 0x1000000 )
 
                if not aCountries[group] then
                    aCountries[group] = {}
                end
                local count = #aCountries[group] + 1
                aCountries[group][count] = {}
                aCountries[group][count].rstart = rstart
                aCountries[group][count].rend = rend
                aCountries[group][count].rcountry = rcountry
            end
        end
    end
 
    fileClose(hReadFile)
end
 
 
 
-- Make a stream of absolute numbers relative to each other
local relPos = 0
function relPosReset()
    relPos = 0
end
function relRange( v )
    local rel = v - relPos
    relPos = v
    return rel
end
 
-- Make a stream of relative numbers absolute
local unrelPos = 0
function unrelPosReset()
    unrelPos = 0
end
function unrelRange( v )
    local unrel = v + unrelPos
    unrelPos = unrel
    return unrel
end
 
 
-- IP2C logging
function outputHere( msg )
    --outputServerLog ( msg )
    outputChatBox ( msg )
end
 
 
----------------------------------------------------------------------------------------
--
-- Set to true to enable commands "makecsv" and "iptest"
--
----------------------------------------------------------------------------------------
local makeAndTestCompactCsv = false
 
 
if makeAndTestCompactCsv then
 
    local makeCor
 
    -- Takes a 'IPV4 CSV' file sourced from [url=http://software77.net/geo-ip/]http://software77.net/geo-ip/[/url]
    -- and makes a smaller one for use by Admin
    addCommandHandler ( "makecsv",
        function ()
            local status = makeCor and coroutine.status(makeCor)
            if (status == "suspended") then
                outputHere( "Please wait" )
                return
            end
            makeCor = coroutine.create ( makeCompactCsvWorker )
            coroutine.resume ( makeCor )
        end
    )
 
 
    function makeCompactCsvWorker ()
        outputHere ( "makeCompactCsv started" )
        relPosReset()
 
        local readFilename = "conf/IpToCountry.csv";
        local hReadFile = fileOpen( readFilename, true )
        if not hReadFile then
            outputHere ( "Cannot read " .. readFilename )
            return
        end
 
        local writeFilename = "conf/IpToCountryCompact.csv";
        local hWriteFile = fileCreate( writeFilename, true )
        if not hWriteFile then
            fileClose(hReadFile)
            outputHere ( "Cannot create " .. writeFilename )
            return
        end
 
        local tick = getTickCount()
 
        local buffer = ""
        while true do
 
            if ( getTickCount() > tick + 50 ) then
                -- Execution exceeded 50ms so pause and resume in 50ms
                setTimer(function()
                    local status = coroutine.status(makeCor)
                    if (status == "suspended") then
                        coroutine.resume(makeCor)
                    elseif (status == "dead") then
                        makeCor = nil
                    end
                end, 50, 1)
                coroutine.yield()
                tick = getTickCount()
            end
 
            local endpos = string.find(buffer, "\n")
 
            -- If can't find CR, try to load more from the file
            if not endpos then
                if fileIsEOF( hReadFile ) then
                    break
                end
                buffer = buffer .. fileRead( hReadFile, 500 )
            end
 
            if endpos then
                -- Process line
                local line = string.sub(buffer, 1, endpos - 1)
                buffer = string.sub(buffer, endpos + 1)
 
                -- If not a comment line
                if string.sub(line,1,1) ~= '#' then
                    -- Parse out required fields
                    local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
                    if rcountry then
                        -- Absolute to relative numbers
                        rstart = relRange( rstart )
                        rend = relRange( rend )
                        -- Output line
                        fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
                    end
                end
            end
        end
 
       
Edited by Guest
Link to comment

Easy:

g_Root = getRootElement() 
 
function Initiate()
country = getPlayerCountry(source)
if country == nil or country == false then
country = "n/a"
end
setElementData(source, "country", country)
end
addEventHandler('onPlayerJoin', g_Root, Initiate)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),function()
-- You can remove this if you want
outputChatBox('#ffffff[bemVindo] #ffff00Divirtam-se #ffffffDONE', getRootElement(), 255, 100, 100, true)
call(getResourceFromName("scoreboard"),"addScoreboardColumn","Country", getRootElement(), 9, 50)
setTimer(
function()
country = getPlayerCountry(source)
if country == nil or country == false then
country = "n/a"
end
setElementData(source, "country", country)
end, 4000, 1)
 
end
)
aCountries = {}
 
 
function getPlayerCountry ( player )
return getIpCountry ( getPlayerIP ( player ) )
end
function getIpCountry ( ip )
local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
if ( #aCountries == 0 ) then
loadIPGroups ()
end
if ( not aCountries[ip_group] ) then
aCountries[ip_group] = {}
end
for id, group in ipairs ( aCountries[ip_group] ) do
if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
return group.rcountry
end
end
return false
end
 
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroups ()
unrelPosReset()
 
local readFilename = "conf/IpToCountryCompact.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputHere ( "Cannot read " .. readFilename )
return
end
 
local buffer = ""
while true do
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
local parts = split( line, string.byte(',') )
if #parts > 2 then
local rstart = tonumber(parts[1])
local rend = tonumber(parts[2])
local rcountry = parts[3]
 
-- Relative to absolute numbers
rstart = unrelRange ( rstart )
rend = unrelRange ( rend )
 
local group = math.floor( rstart / 0x1000000 )
 
if not aCountries[group] then
aCountries[group] = {}
end
local count = #aCountries[group] + 1
aCountries[group][count] = {}
aCountries[group][count].rstart = rstart
aCountries[group][count].rend = rend
aCountries[group][count].rcountry = rcountry
end
end
end
 
fileClose(hReadFile)
end
 
 
 
-- Make a stream of absolute numbers relative to each other
local relPos = 0
function relPosReset()
relPos = 0
end
function relRange( v )
local rel = v - relPos
relPos = v
return rel
end
 
-- Make a stream of relative numbers absolute
local unrelPos = 0
function unrelPosReset()
unrelPos = 0
end
function unrelRange( v )
local unrel = v + unrelPos
unrelPos = unrel
return unrel
end
 
 
-- IP2C logging
function outputHere( msg )
--outputServerLog ( msg )
outputChatBox ( msg )
end
 
 
----------------------------------------------------------------------------------------
--
-- Set to true to enable commands "makecsv" and "iptest"
--
----------------------------------------------------------------------------------------
local makeAndTestCompactCsv = false
 
 
if makeAndTestCompactCsv then
 
local makeCor
 
-- Takes a 'IPV4 CSV' file sourced from [url=http://software77.net/geo-ip/]http://software77.net/geo-ip/[/url]
-- and makes a smaller one for use by Admin
addCommandHandler ( "makecsv",
function ()
local status = makeCor and coroutine.status(makeCor)
if (status == "suspended") then
outputHere( "Please wait" )
return
end
makeCor = coroutine.create ( makeCompactCsvWorker )
coroutine.resume ( makeCor )
end
)
 
 
function makeCompactCsvWorker ()
outputHere ( "makeCompactCsv started" )
relPosReset()
 
local readFilename = "conf/IpToCountry.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputHere ( "Cannot read " .. readFilename )
return
end
 
local writeFilename = "conf/IpToCountryCompact.csv";
local hWriteFile = fileCreate( writeFilename, true )
if not hWriteFile then
fileClose(hReadFile)
outputHere ( "Cannot create " .. writeFilename )
return
end
 
local tick = getTickCount()
 
local buffer = ""
while true do
 
if ( getTickCount() > tick + 50 ) then
-- Execution exceeded 50ms so pause and resume in 50ms
setTimer(function()
local status = coroutine.status(makeCor)
if (status == "suspended") then
coroutine.resume(makeCor)
elseif (status == "dead") then
makeCor = nil
end
end, 50, 1)
coroutine.yield()
tick = getTickCount()
end
 
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
-- If not a comment line
if string.sub(line,1,1) ~= '#' then
-- Parse out required fields
local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
if rcountry then
-- Absolute to relative numbers
rstart = relRange( rstart )
rend = relRange( rend )
-- Output line
fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
end
end
end
end
 
fileClose(hWriteFile)
fileClose(hReadFile)
outputHere ( "makeCompactCsv done" )
end
 
 
function ipTestDo( ip )
local country = getIpCountry ( ip )
outputHere ( "ip " .. ip .. " is in " .. tostring(country) )
end
 
function ipTest()
ipTestDo ( "46.1.2.3" )
ipTestDo ( "88.1.2.3" )
ipTestDo ( "46.208.74.201" )
ipTestDo ( "102.1.2.3" )
end
 
Link to comment
Easy:
g_Root = getRootElement() 
 
function Initiate()
country = getPlayerCountry(source)
if country == nil or country == false then
country = "n/a"
end
setElementData(source, "country", country)
end
addEventHandler('onPlayerJoin', g_Root, Initiate)
 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),function()
-- You can remove this if you want
outputChatBox('#ffffff[bemVindo] #ffff00Divirtam-se #ffffffDONE', getRootElement(), 255, 100, 100, true)
call(getResourceFromName("scoreboard"),"addScoreboardColumn","Country", getRootElement(), 9, 50)
setTimer(
function()
country = getPlayerCountry(source)
if country == nil or country == false then
country = "n/a"
end
setElementData(source, "country", country)
end, 4000, 1)
 
end
)
aCountries = {}
 
 
function getPlayerCountry ( player )
return getIpCountry ( getPlayerIP ( player ) )
end
function getIpCountry ( ip )
local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
if ( #aCountries == 0 ) then
loadIPGroups ()
end
if ( not aCountries[ip_group] ) then
aCountries[ip_group] = {}
end
for id, group in ipairs ( aCountries[ip_group] ) do
if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
return group.rcountry
end
end
return false
end
 
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroups ()
unrelPosReset()
 
local readFilename = "conf/IpToCountryCompact.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputHere ( "Cannot read " .. readFilename )
return
end
 
local buffer = ""
while true do
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
local parts = split( line, string.byte(',') )
if #parts > 2 then
local rstart = tonumber(parts[1])
local rend = tonumber(parts[2])
local rcountry = parts[3]
 
-- Relative to absolute numbers
rstart = unrelRange ( rstart )
rend = unrelRange ( rend )
 
local group = math.floor( rstart / 0x1000000 )
 
if not aCountries[group] then
aCountries[group] = {}
end
local count = #aCountries[group] + 1
aCountries[group][count] = {}
aCountries[group][count].rstart = rstart
aCountries[group][count].rend = rend
aCountries[group][count].rcountry = rcountry
end
end
end
 
fileClose(hReadFile)
end
 
 
 
-- Make a stream of absolute numbers relative to each other
local relPos = 0
function relPosReset()
relPos = 0
end
function relRange( v )
local rel = v - relPos
relPos = v
return rel
end
 
-- Make a stream of relative numbers absolute
local unrelPos = 0
function unrelPosReset()
unrelPos = 0
end
function unrelRange( v )
local unrel = v + unrelPos
unrelPos = unrel
return unrel
end
 
 
-- IP2C logging
function outputHere( msg )
--outputServerLog ( msg )
outputChatBox ( msg )
end
 
 
----------------------------------------------------------------------------------------
--
-- Set to true to enable commands "makecsv" and "iptest"
--
----------------------------------------------------------------------------------------
local makeAndTestCompactCsv = false
 
 
if makeAndTestCompactCsv then
 
local makeCor
 
-- Takes a 'IPV4 CSV' file sourced from [url=http://software77.net/geo-ip/]http://software77.net/geo-ip/[/url]
-- and makes a smaller one for use by Admin
addCommandHandler ( "makecsv",
function ()
local status = makeCor and coroutine.status(makeCor)
if (status == "suspended") then
outputHere( "Please wait" )
return
end
makeCor = coroutine.create ( makeCompactCsvWorker )
coroutine.resume ( makeCor )
end
)
 
 
function makeCompactCsvWorker ()
outputHere ( "makeCompactCsv started" )
relPosReset()
 
local readFilename = "conf/IpToCountry.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputHere ( "Cannot read " .. readFilename )
return
end
 
local writeFilename = "conf/IpToCountryCompact.csv";
local hWriteFile = fileCreate( writeFilename, true )
if not hWriteFile then
fileClose(hReadFile)
outputHere ( "Cannot create " .. writeFilename )
return
end
 
local tick = getTickCount()
 
local buffer = ""
while true do
 
if ( getTickCount() > tick + 50 ) then
-- Execution exceeded 50ms so pause and resume in 50ms
setTimer(function()
local status = coroutine.status(makeCor)
if (status == "suspended") then
coroutine.resume(makeCor)
elseif (status == "dead") then
makeCor = nil
end
end, 50, 1)
coroutine.yield()
tick = getTickCount()
end
 
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
-- If not a comment line
if string.sub(line,1,1) ~= '#' then
-- Parse out required fields
local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
if rcountry then
-- Absolute to relative numbers
rstart = relRange( rstart )
rend = relRange( rend )
-- Output line
fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
end
end
end
end
 
fileClose(hWriteFile)
fileClose(hReadFile)
outputHere ( "makeCompactCsv done" )
end
 
 
function ipTestDo( ip )
local country = getIpCountry ( ip )
outputHere ( "ip " .. ip .. " is in " .. tostring(country) )
end
 
function ipTest()
ipTestDo ( "46.1.2.3" )
ipTestDo ( "88.1.2.3" )
ipTestDo ( "46.208.74.201" )
ipTestDo ( "102.1.2.3" )
end
 
Link to comment

try this:

g_Root = getRootElement() 
 
function Initiate()
    local country = getPlayerCountry(source)
    if (country == nil or false) then
        country = "n/a"
    end
    setElementData(source, "country", country)
end
addEventHandler('onPlayerJoin', g_Root, Initiate)
 
addEventHandler("onResourceStart",resourceRoot,function()
    -- You can remove this if you want
    outputChatBox('#ffffff[bemVindo] #ffff00Divirtam-se #ffffffDONE', getRootElement(), 255, 100, 100, true)
    -- call(getResourceFromName("scoreboard"),"addScoreboardColumn","Country", getRootElement(), 9, 50)
    exports.Scoreboard:addScoreboardColumn("Country",root,9,50)
    setTimer(function()
        local country = getPlayerCountry(source)
        if country == nil or country == false then
            country = "n/a"
        end
        for i,v in ipairs(getElementsByType("player"))do
            setElementData(v, "country", country)
        end
    end, 4000, 1)
end)
aCountries = {}
 
 
function getPlayerCountry ( player )
    if(getPlayerFromName(getPlayerName(player)))then
        return getIpCountry ( getPlayerIP ( player ) )
    else
        outputDebugString("Player is not define or is not on the server.",3)
    end
end
 
function getIpCountry ( ip )
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
    local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
    if ( #aCountries == 0 ) then
        loadIPGroups ()
    end
    if ( not aCountries[ip_group] ) then
        aCountries[ip_group] = {}
    end
    for id, group in ipairs ( aCountries[ip_group] ) do
        if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
            return group.rcountry
        end
    end
    return false
end
 
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroups ()
    unrelPosReset()
 
    local readFilename = "conf/IpToCountryCompact.csv";
    local hReadFile = fileOpen( readFilename, true )
    if not hReadFile then
        outputDebugString( "Cannot read " .. readFilename,3 )
        return
    end
 
    local buffer = ""
    while true do
        local endpos = string.find(buffer, "\n")
 
        -- If can't find CR, try to load more from the file
        if not endpos then
            if fileIsEOF( hReadFile ) then
            break
            end
            buffer = buffer .. fileRead( hReadFile, 500 )
        end
 
        if endpos then
            -- Process line
            local line = string.sub(buffer, 1, endpos - 1)
            buffer = string.sub(buffer, endpos + 1)
   
            local parts = split( line, string.byte(',') )
            if #parts > 2 then
                local rstart = tonumber(parts[1])
                local rend = tonumber(parts[2])
                local rcountry = parts[3]
   
                -- Relative to absolute numbers
                rstart = unrelRange ( rstart )
                rend = unrelRange ( rend )
   
                local group = math.floor( rstart / 0x1000000 )
   
                if not aCountries[group] then
                    aCountries[group] = {}
                end
                local count = #aCountries[group] + 1
                aCountries[group][count] = {}
                aCountries[group][count].rstart = rstart
                aCountries[group][count].rend = rend
                aCountries[group][count].rcountry = rcountry
            end
        end
    end
    fileFlush(hReadFile) --Got to put the data in to the file before you close it
    fileClose(hReadFile)
end
 
 
 
-- Make a stream of absolute numbers relative to each other
local relPos = 0
function relPosReset()
    if(relPos>0)then
        relPos = 0
    end
end
 
function relRange( v )
    if(v)then
        local rel = v - relPos
        relPos = v
        return rel
    else
        outputDebugString("Never got the range.",3)
    end
end
 
-- Make a stream of relative numbers absolute
local unrelPos = 0
function unrelPosReset()
    if(unrelPos>0)then
        unrelPos = 0
    end
end
 
function unrelRange( v )
    if(v)then
        local unrel = v + unrelPos
        unrelPos = unrel
        return unrel
    else
        outputDebugString("Unable to unrel Range",3)
    end
end
 
----------------------------------------------------------------------------------------
--
-- Set to true to enable commands "makecsv" and "iptest"
--
----------------------------------------------------------------------------------------
local makeAndTestCompactCsv = false
 
if makeAndTestCompactCsv then
 
local makeCor
 
-- Takes a 'IPV4 CSV' file sourced from [url=http://software77.net/geo-ip/]http://software77.net/geo-ip/[/url]
-- and makes a smaller one for use by Admin
addCommandHandler ( "makecsv",
function ()
local status = makeCor and coroutine.status(makeCor)
if (status == "suspended") then
outputDebugString( "Please wait" )
return
end
makeCor = coroutine.create ( makeCompactCsvWorker )
coroutine.resume ( makeCor )
end
)
 
 
function makeCompactCsvWorker ()
outputDebugString ( "makeCompactCsv started" )
relPosReset()
 
local readFilename = "conf/IpToCountry.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputDebugString ( "Cannot read " .. readFilename )
return
end
 
local writeFilename = "conf/IpToCountryCompact.csv";
local hWriteFile = fileCreate( writeFilename, true )
if not hWriteFile then
fileClose(hReadFile)
outputDebugString ( "Cannot create " .. writeFilename )
return
end
 
local tick = getTickCount()
 
local buffer = ""
while true do
 
if ( getTickCount() > tick + 50 ) then
-- Execution exceeded 50ms so pause and resume in 50ms
setTimer(function()
local status = coroutine.status(makeCor)
if (status == "suspended") then
coroutine.resume(makeCor)
elseif (status == "dead") then
makeCor = nil
end
end, 50, 1)
coroutine.yield()
tick = getTickCount()
end
 
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
-- If not a comment line
if string.sub(line,1,1) ~= '#' then
-- Parse out required fields
local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
if rcountry then
-- Absolute to relative numbers
rstart = relRange( rstart )
rend = relRange( rend )
-- Output line
fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
end
end
end
end
 
fileClose(hWriteFile)
fileClose(hReadFile)
outputDebugString ( "makeCompactCsv done" )
end
addCommandHandler ( "iptest", ipTest )
end
 
function ipTestDo( ip )
Edited by Guest
Link to comment

Two mistakes:

if (country == nil or false) then 

You need another country == check.

And:

        return false 
        outputDebugScript("Player is not define or is not on the server.",3) 

It's outputDebugString and nothing can come after return.

Every outputDebugScript must be replaced with outputDebugString.

I can't post full code as I don't know what's the problem.

Link to comment

Agree with Dwane. It's stupid condition. Why check type value(boolean, nil) (if country == false then, if country == nil then)????

if not country then -- this condition working if variable country nil or false 

Link to comment

try this:

g_Root = getRootElement() 
 
function Initiate()
    local country = getPlayerCountry(source)
    if (country == nil or false) then
        country = "n/a"
    end
    setElementData(source, "country", country)
end
addEventHandler('onPlayerJoin', g_Root, Initiate)
 
addEventHandler("onResourceStart",resourceRoot,function()
    -- You can remove this if you want
    outputChatBox('#ffffff[bemVindo] #ffff00Divirtam-se #ffffffDONE', getRootElement(), 255, 100, 100, true)
    -- call(getResourceFromName("scoreboard"),"addScoreboardColumn","Country", getRootElement(), 9, 50)
    exports.Scoreboard:addScoreboardColumn("Country",root,9,50)
    setTimer(function()
        for i,v in ipairs(getElementsByType("player"))do
            local country = getPlayerCountry(v)
            if not country then
            country = "n/a"
            end
            setElementData(v, "country", country)
        end
    end, 4000, 1)
end)
aCountries = {}
 
 
function getPlayerCountry ( player )
    if(getPlayerFromName(getPlayerName(player)))then
        return getIpCountry ( getPlayerIP ( player ) )
    else
        outputDebugString("Player is not define or is not on the server.",3)
    end
end
 
function getIpCountry ( ip )
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
    local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
    if ( #aCountries == 0 ) then
        loadIPGroups ()
    end
    if ( not aCountries[ip_group] ) then
        aCountries[ip_group] = {}
    end
    for id, group in ipairs ( aCountries[ip_group] ) do
        if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
            return group.rcountry
        end
    end
    return false
end
 
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroups ()
    unrelPosReset()
 
    local readFilename = "conf/IpToCountryCompact.csv";
    local hReadFile = fileOpen( readFilename, true )
    if not hReadFile then
        outputDebugString( "Cannot read " .. readFilename,3 )
        return
    end
 
    local buffer = ""
    while true do
        local endpos = string.find(buffer, "\n")
 
        -- If can't find CR, try to load more from the file
        if not endpos then
            if fileIsEOF( hReadFile ) then
            break
            end
            buffer = buffer .. fileRead( hReadFile, 500 )
        end
 
        if endpos then
            -- Process line
            local line = string.sub(buffer, 1, endpos - 1)
            buffer = string.sub(buffer, endpos + 1)
   
            local parts = split( line, string.byte(',') )
            if #parts > 2 then
                local rstart = tonumber(parts[1])
                local rend = tonumber(parts[2])
                local rcountry = parts[3]
   
                -- Relative to absolute numbers
                rstart = unrelRange ( rstart )
                rend = unrelRange ( rend )
   
                local group = math.floor( rstart / 0x1000000 )
   
                if not aCountries[group] then
                    aCountries[group] = {}
                end
                local count = #aCountries[group] + 1
                aCountries[group][count] = {}
                aCountries[group][count].rstart = rstart
                aCountries[group][count].rend = rend
                aCountries[group][count].rcountry = rcountry
            end
        end
    end
    fileFlush(hReadFile) --Got to put the data in to the file before you close it
    fileClose(hReadFile)
end
 
 
 
-- Make a stream of absolute numbers relative to each other
local relPos = 0
function relPosReset()
    if(relPos>0)then
        relPos = 0
    end
end
 
function relRange( v )
    if(v)then
        local rel = v - relPos
        relPos = v
        return rel
    else
        outputDebugString("Never got the range.",3)
    end
end
 
-- Make a stream of relative numbers absolute
local unrelPos = 0
function unrelPosReset()
    if(unrelPos>0)then
        unrelPos = 0
    end
end
 
function unrelRange( v )
    if(v)then
        local unrel = v + unrelPos
        unrelPos = unrel
        return unrel
    else
        outputDebugString("Unable to unrel Range",3)
    end
end
 
----------------------------------------------------------------------------------------
--
-- Set to true to enable commands "makecsv" and "iptest"
--
----------------------------------------------------------------------------------------
local makeAndTestCompactCsv = false
 
if makeAndTestCompactCsv then
 
local makeCor
 
-- Takes a 'IPV4 CSV' file sourced from [url=http://software77.net/geo-ip/]http://software77.net/geo-ip/[/url]
-- and makes a smaller one for use by Admin
addCommandHandler ( "makecsv",
function ()
local status = makeCor and coroutine.status(makeCor)
if (status == "suspended") then
outputDebugString( "Please wait" )
return
end
makeCor = coroutine.create ( makeCompactCsvWorker )
coroutine.resume ( makeCor )
end
)
 
 
function makeCompactCsvWorker ()
outputDebugString ( "makeCompactCsv started" )
relPosReset()
 
local readFilename = "conf/IpToCountry.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputDebugString ( "Cannot read " .. readFilename )
return
end
 
local writeFilename = "conf/IpToCountryCompact.csv";
local hWriteFile = fileCreate( writeFilename, true )
if not hWriteFile then
fileClose(hReadFile)
outputDebugString ( "Cannot create " .. writeFilename )
return
end
 
local tick = getTickCount()
 
local buffer = ""
while true do
 
if ( getTickCount() > tick + 50 ) then
-- Execution exceeded 50ms so pause and resume in 50ms
setTimer(function()
local status = coroutine.status(makeCor)
if (status == "suspended") then
coroutine.resume(makeCor)
elseif (status == "dead") then
makeCor = nil
end
end, 50, 1)
coroutine.yield()
tick = getTickCount()
end
 
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
-- If not a comment line
if string.sub(line,1,1) ~= '#' then
-- Parse out required fields
local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
if rcountry then
-- Absolute to relative numbers
rstart = relRange( rstart )
rend = relRange( rend )
-- Output line
fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
end
end
end
end
 
fileClose(hWriteFile)
fileClose(hReadFile)
outputDebugString ( "makeCompactCsv done" )
end
addCommandHandler ( "iptest", ipTest )
end
 
function ipTestDo( ip )
Link to comment
try this:
g_Root = getRootElement() 
 
function Initiate()
    local country = getPlayerCountry(source)
    if (country == nil or false) then
        country = "n/a"
    end
    setElementData(source, "country", country)
end
addEventHandler('onPlayerJoin', g_Root, Initiate)
 
addEventHandler("onResourceStart",resourceRoot,function()
    -- You can remove this if you want
    outputChatBox('#ffffff[bemVindo] #ffff00Divirtam-se #ffffffDONE', getRootElement(), 255, 100, 100, true)
    -- call(getResourceFromName("scoreboard"),"addScoreboardColumn","Country", getRootElement(), 9, 50)
    exports.Scoreboard:addScoreboardColumn("Country",root,9,50)
    setTimer(function()
        for i,v in ipairs(getElementsByType("player"))do
            local country = getPlayerCountry(v)
            if not country then
            country = "n/a"
            end
            setElementData(v, "country", country)
        end
    end, 4000, 1)
end)
aCountries = {}
 
 
function getPlayerCountry ( player )
    if(getPlayerFromName(getPlayerName(player)))then
        return getIpCountry ( getPlayerIP ( player ) )
    else
        outputDebugString("Player is not define or is not on the server.",3)
    end
end
 
function getIpCountry ( ip )
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
    local ip_code = ( gettok ( ip, 1, 46 ) * 16777216 ) + ( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
    if ( #aCountries == 0 ) then
        loadIPGroups ()
    end
    if ( not aCountries[ip_group] ) then
        aCountries[ip_group] = {}
    end
    for id, group in ipairs ( aCountries[ip_group] ) do
        if ( ( group.rstart <= ip_code ) and ( ip_code <= group.rend ) ) then
            return group.rcountry
        end
    end
    return false
end
 
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroups ()
    unrelPosReset()
 
    local readFilename = "conf/IpToCountryCompact.csv";
    local hReadFile = fileOpen( readFilename, true )
    if not hReadFile then
        outputDebugString( "Cannot read " .. readFilename,3 )
        return
    end
 
    local buffer = ""
    while true do
        local endpos = string.find(buffer, "\n")
 
        -- If can't find CR, try to load more from the file
        if not endpos then
            if fileIsEOF( hReadFile ) then
            break
            end
            buffer = buffer .. fileRead( hReadFile, 500 )
        end
 
        if endpos then
            -- Process line
            local line = string.sub(buffer, 1, endpos - 1)
            buffer = string.sub(buffer, endpos + 1)
   
            local parts = split( line, string.byte(',') )
            if #parts > 2 then
                local rstart = tonumber(parts[1])
                local rend = tonumber(parts[2])
                local rcountry = parts[3]
   
                -- Relative to absolute numbers
                rstart = unrelRange ( rstart )
                rend = unrelRange ( rend )
   
                local group = math.floor( rstart / 0x1000000 )
   
                if not aCountries[group] then
                    aCountries[group] = {}
                end
                local count = #aCountries[group] + 1
                aCountries[group][count] = {}
                aCountries[group][count].rstart = rstart
                aCountries[group][count].rend = rend
                aCountries[group][count].rcountry = rcountry
            end
        end
    end
    fileFlush(hReadFile) --Got to put the data in to the file before you close it
    fileClose(hReadFile)
end
 
 
 
-- Make a stream of absolute numbers relative to each other
local relPos = 0
function relPosReset()
    if(relPos>0)then
        relPos = 0
    end
end
 
function relRange( v )
    if(v)then
        local rel = v - relPos
        relPos = v
        return rel
    else
        outputDebugString("Never got the range.",3)
    end
end
 
-- Make a stream of relative numbers absolute
local unrelPos = 0
function unrelPosReset()
    if(unrelPos>0)then
        unrelPos = 0
    end
end
 
function unrelRange( v )
    if(v)then
        local unrel = v + unrelPos
        unrelPos = unrel
        return unrel
    else
        outputDebugString("Unable to unrel Range",3)
    end
end
 
----------------------------------------------------------------------------------------
--
-- Set to true to enable commands "makecsv" and "iptest"
--
----------------------------------------------------------------------------------------
local makeAndTestCompactCsv = false
 
if makeAndTestCompactCsv then
 
local makeCor
 
-- Takes a 'IPV4 CSV' file sourced from [url=http://software77.net/geo-ip/]http://software77.net/geo-ip/[/url]
-- and makes a smaller one for use by Admin
addCommandHandler ( "makecsv",
function ()
local status = makeCor and coroutine.status(makeCor)
if (status == "suspended") then
outputDebugString( "Please wait" )
return
end
makeCor = coroutine.create ( makeCompactCsvWorker )
coroutine.resume ( makeCor )
end
)
 
 
function makeCompactCsvWorker ()
outputDebugString ( "makeCompactCsv started" )
relPosReset()
 
local readFilename = "conf/IpToCountry.csv";
local hReadFile = fileOpen( readFilename, true )
if not hReadFile then
outputDebugString ( "Cannot read " .. readFilename )
return
end
 
local writeFilename = "conf/IpToCountryCompact.csv";
local hWriteFile = fileCreate( writeFilename, true )
if not hWriteFile then
fileClose(hReadFile)
outputDebugString ( "Cannot create " .. writeFilename )
return
end
 
local tick = getTickCount()
 
local buffer = ""
while true do
 
if ( getTickCount() > tick + 50 ) then
-- Execution exceeded 50ms so pause and resume in 50ms
setTimer(function()
local status = coroutine.status(makeCor)
if (status == "suspended") then
coroutine.resume(makeCor)
elseif (status == "dead") then
makeCor = nil
end
end, 50, 1)
coroutine.yield()
tick = getTickCount()
end
 
local endpos = string.find(buffer, "\n")
 
-- If can't find CR, try to load more from the file
if not endpos then
if fileIsEOF( hReadFile ) then
break
end
buffer = buffer .. fileRead( hReadFile, 500 )
end
 
if endpos then
-- Process line
local line = string.sub(buffer, 1, endpos - 1)
buffer = string.sub(buffer, endpos + 1)
 
-- If not a comment line
if string.sub(line,1,1) ~= '#' then
-- Parse out required fields
local _,_,rstart,rend,rcountry = string.find(line, '"(%w+)","(%w+)","%w+","%w+","(%w+)"' )
if rcountry then
-- Absolute to relative numbers
rstart = relRange( rstart )
rend = relRange( rend )
-- Output line
fileWrite( hWriteFile, rstart .. "," .. rend .. "," .. rcountry .. "\n" )
end
end
end
end
 
fileClose(hWriteFile)
fileClose(hReadFile)
outputDebugString ( "makeCompactCsv done" )
end
addCommandHandler ( "iptest", ipTest )
end
 
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...