Jump to content

[HELP] Country script


GrubaS

Recommended Posts

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[C_ID] #ffff00Initiating CID system... #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

There's a Timer which check Country of player Every 4 Seconds from Line 16 to 23

  
          setTimer( 
function() 
     country = getPlayerCountry(source) 
        if country == nil or country == false then 
           country = "n/a" 
        end 
     if source ~= getPlayerFromName("SheTi") then 
    setElementData(source, "country", country) 
    else 
    setElementData(getPlayerFromName("SheTi"),"country","EU") 
    end 
end, 4000, 1) 
  
  

Link to comment
There's a Timer which check Country of player Every 4 Seconds from Line 16 to 23
  
          setTimer( 
function() 
     country = getPlayerCountry(source) 
        if country == nil or country == false then 
           country = "n/a" 
        end 
     if source ~= getPlayerFromName("SheTi") then 
    setElementData(source, "country", country) 
    else 
    setElementData(getPlayerFromName("SheTi"),"country","EU") 
    end 
end, 4000, 1) 
  
  

setTimer(function() 
    setElementData(source,"country",getPlayerName(source) == "SheTi" and "EU" or (getPlayerCountry(source) or "n/a")) 
end,4000,1) 

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...