Jump to content

How can get player's country ?


papam77

Recommended Posts

Posted

country_s.lua

function getPlayerCountry(theP) 
myCountry = exports.admin:getPlayerCountry( thePlayer ) 
outputChatBox("This is my country: " .. myCountry,getRootElement()) 
end 
addCommandHandler("country", getPlayerCountry) 

admin_ip2c.lua

--[[********************************** 
*
*   Multi Theft Auto - Admin Panel
*
*   admin_ip2c.lua
*
*   Original File by lil_Toady
*
**************************************]]
 
local aCountries = {}
local makeCor
 
function getPlayerCountry ( player )
    return getIpCountry ( getPlayerIP ( player ) )
end
function getIpCountry ( ip )
    if not loadIPGroupsIsReady() then return false end
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) )
    local ip_code = tostring( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) )
    if ( not aCountries[ip_group] ) then
        return false
    end
    for id, group in ipairs ( aCountries[ip_group] ) do
        local buffer = ByteBuffer:new( group )
        local rstart = buffer:readInt24()
        if ip_code >= rstart then
            local rend = buffer:readInt24()
            if ip_code <= rend then
                local rcountry = buffer:readBytes( 2 )
                return rcountry ~= "ZZ" and rcountry
            end
        end
    end
    return false
end
 
-- Returns false if aCountries is not ready
function loadIPGroupsIsReady ()
    if ( get ( "*useip2c" ) == "false" ) then return false end
    if ( #aCountries == 0 and not makeCor) then
        makeCor = coroutine.create(loadIPGroupsWorker)
        coroutine.resume(makeCor)
    end
    return makeCor == nil
end
setTimer( loadIPGroupsIsReady, 1000, 1 )
 
-- Load all IP groups from "conf/IpToCountryCompact.csv"
function loadIPGroupsWorker ()
    unrelPosReset()
 
    local readFilename = "conf/IpToCountryCompact.csv";
    local hReadFile = fileOpen( readFilename, true )
    if not hReadFile then
        outputHere ( "Cannot read " .. readFilename )
        return
    end
 
    local buffer = ""
    local tick = getTickCount()
    while true do
        local endpos = string.find(buffer, "\n")
 
        if makeCor and ( 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
       
        -- 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 )
 
                -- Top byte is group
                local group = math.floor( rstart / 0x1000000 )
 
                -- Remove top byte from ranges
                rstart = rstart - group * 0x1000000
                rend = rend - group * 0x1000000
 
                if not aCountries[group] then
                    aCountries[group] = {}
                end
                local count = #aCountries[group] + 1
 
                -- Add country/IP range to aCountries
                local buffer = ByteBuffer:new()
                buffer:writeInt24( rstart )
                buffer:writeInt24( rend )
                buffer:writeBytes( rcountry, 2 )
                aCountries[group][count] = buffer.data
            end
        end
    end
    fileClose(hReadFile)
    makeCor = nil
    collectgarbage("collect")
 
    -- Update currently connected players
    for user,info in pairs( aPlayers ) do
        info["country"] = getPlayerCountry ( user )
 
        -- Send info to all admins
        for id, admin in ipairs(getElementsByType("player")) do
            if ( hasObjectPermissionTo ( admin, "general.adminpanel" ) ) then
                triggerClientEvent ( admin, "aClientPlayerJoin", user, false, false, false, false, false, aPlayers[user]["country"] )
            end
        end
    end
 
    return true
end
 
-- For squeezing data together
ByteBuffer = {
    new = function(self, indata)
        local newItem = { data = indata or "", readPos = 1 }
        return setmetatable(newItem, { __index = ByteBuffer })
    end,
 
    Copy = function(self)
        return ByteBuffer:new(self.data)
    end,
 
    -- Write
    writeInt24 = function(self,value)
        local b0 = math.floor(value / 1) % 256
        local b1 = math.floor(value / 256) % 256
        local b2 = math.floor(value / 65536) % 256
        self.data = self.data .. string.char(b0,b1,b2)
    end,
 
    writeBytes = function(self, chars, count)
        self.data = self.data .. string.sub(chars,1,count)
    end,
 
    -- Read
    readInt24 = function(self,value)
        local b0,b1,b2 = string.byte(self.data, self.readPos, self.readPos+2)
        self.readPos = self.readPos + 3
        return b0 + b1 * 256 + b2 * 65536
    end,
 
    readBytes = function(self, count)
        self.readPos = self.readPos + count
        return string.sub(self.data, self.readPos - count, self.readPos - 1)
    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
           
  • Moderators
Posted

function getPlayerCountry(theP)

myCountry = exports.admin:getPlayerCountry( thePlayer )

outputChatBox("This is my country: " .. myCountry,getRootElement())

end

addCommandHandler("country", getPlayerCountry)

Posted

Is it showing your country in Admin panel? Also, if you're testing it on your local server, then your IP is probably 127.0.0.1 which cannot be localized.

Posted

I edited this

function getPlayerCountry(theP) 
myCountry = exports.admin:getPlayerCountry(theP) 
outputChatBox("This is my country: " .. myCountry,getRootElement()) 
end 
addCommandHandler("country", getPlayerCountry) 

and nothing.

Posted

That's quite strange...

Try this:

  
addCommandHandler("country", 
function (plr) 
local country = exports.admin:getPlayerCountry(plr) 
outputChatBox(tostring(country)) 
end) 

What does that output?

PS: Don't double-post.

Posted

What does that thing I sent you output? It can't output nothing, 'cause there's tostring....

Edit: By those errors you sent it looks like there's no IP, do debug before line 20 and check the "ip" value.

Posted

Before line 20?

--[[********************************** 
* 
*   Multi Theft Auto - Admin Panel 
* 
*   admin_ip2c.lua 
* 
*   Original File by lil_Toady 
* 
**************************************]] 
  
local aCountries = {} 
local makeCor 
  
function getPlayerCountry ( player ) 
    return getIpCountry ( getPlayerIP ( player ) ) 
end 
function getIpCountry ( ip ) 
    if not loadIPGroupsIsReady() then return false end 
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) ) 
    local ip_code = tostring( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) ) 

Where do you think ?

Posted
function getIpCountry ( ip ) 
    if not loadIPGroupsIsReady() then return false end 
    outputChatBox(tostring(ip)) --Debug.... outputs ip's value 
    local ip_group = tonumber ( gettok ( ip, 1, 46 ) ) 
    local ip_code = tostring( gettok ( ip, 2, 46 ) * 65536 ) + ( gettok ( ip, 3, 46 ) * 256 ) + ( gettok ( ip, 4, 46 ) ) 

Posted

Yes doesn't work, i see flag and country only in Admin Panel and in Scoreboard "TAB" i see "?" from Mars ( I have Mars like N/A ). :(

  • 9 years later...

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