Jump to content

[HELP] Get player city and country script


FIY9AL

Recommended Posts

Hello evreyone, i want to make script that gets player city and country, i have searched via web and i copied some codes and try to make it work but it didn't work, so can you guys help me to make a code that gets player city and country then display it on chatbox??

Here is the code i got:

countryNames = {
    ['AD'] = 'Andorra',
    ['AE'] = 'United Arab Emirates',
    ['AF'] = 'Afghanistan',
    ['AG'] = 'Antigua and Barbuda',
    ['AI'] = 'Anguilla',
    ['AL'] = 'Albania',
    ['AM'] = 'Armenia',
    ['AO'] = 'Angola',
    ['AP'] = 'ARIPO',
    ['AR'] = 'Argentina',
    ['AT'] = 'Austria',
    ['AU'] = 'Australia',
    ['AW'] = 'Aruba',
    ['AZ'] = 'Azerbaijan',
    ['BA'] = 'Bosnia and Herzegovina',
    ['BB'] = 'Barbados',
    ['BD'] = 'Bangladesh',
    ['BE'] = 'Belgium',
    ['BF'] = 'Burkina Faso',
    ['BG'] = 'Bulgaria',
    ['BH'] = 'Bahrain',
    ['BI'] = 'Burundi',
    ['BJ'] = 'Benin',
    ['BM'] = 'Bermuda',
    ['BN'] = 'Brunei Darussalam',
    ['BO'] = 'Bolivia',
    ['BQ'] = 'Bonaire',
    ['BR'] = 'Brazil',
    ['BS'] = 'Bahamas',
    ['BT'] = 'Bhutan',
    ['BV'] = 'Bouvet Island',
    ['BW'] = 'Botswana',
    ['BY'] = 'Belarus',
    ['BZ'] = 'Belize',
    ['CA'] = 'Canada',
    ['CD'] = 'Congo',
    ['CF'] = 'Central African Republic',
    ['CG'] = 'Congo',
    ['CH'] = 'Switzerland',
    ['CI'] = 'Cote d?Ivoire',
    ['CK'] = 'Cook Islands',
    ['CL'] = 'Chile',
    ['CM'] = 'Cameroon',
    ['CN'] = 'China',
    ['CO'] = 'Colombia',
    ['CR'] = 'Costa Rica',
    ['CU'] = 'Cuba',
    ['CV'] = 'Cape Verde',
    ['CW'] = 'Curacao',
    ['CY'] = 'Cyprus',
    ['CZ'] = 'Czech Republic',
    ['DE'] = 'Germany',
    ['DJ'] = 'Djibouti',
    ['DK'] = 'Denmark',
    ['DM'] = 'Dominica',
    ['DO'] = 'Dominican Republic',
    ['DZ'] = 'Algeria',
    ['EC'] = 'Ecuador',
    ['EE'] = 'Estonia',
    ['EG'] = 'Egypt',
    ['EH'] = 'Western Sahara',
    ['ER'] = 'Eritrea',
    ['ES'] = 'Spain',
    ['ET'] = 'Ethiopia',
    ['FI'] = 'Finland',
    ['FJ'] = 'Fiji',
    ['FK'] = 'Malvinas',
    ['FO'] = 'Faroe Islands',
    ['FR'] = 'France',
    ['GA'] = 'Gabon',
    ['GB'] = 'United Kingdom',
    ['GD'] = 'Grenada',
    ['GE'] = 'Georgia',
    ['GG'] = 'Guernsey',
    ['GH'] = 'Ghana',
    ['GI'] = 'Gibraltar',
    ['GL'] = 'Greenland',
    ['GM'] = 'Gambia',
    ['GN'] = 'Guinea',
    ['GQ'] = 'Equatorial Guinea',
    ['GR'] = 'Greece',
    ['GT'] = 'Guatemala',
    ['GW'] = 'Guinea-Bissau',
    ['GY'] = 'Guyana',
    ['HN'] = 'Honduras',
    ['HR'] = 'Croatia',
    ['HT'] = 'Haiti',
    ['HU'] = 'Hungary',
    ['ID'] = 'Indonesia',
    ['IE'] = 'Ireland',
    ['IL'] = 'Israel',
    ['IM'] = 'Isle of Man',
    ['IN'] = 'India',
    ['IQ'] = 'Iraq',
    ['IR'] = 'Iran',
    ['IS'] = 'Iceland',
    ['IT'] = 'Italy',
    ['JE'] = 'Jersey',
    ['JM'] = 'Jamaica',
    ['JO'] = 'Jordan',
    ['JP'] = 'Japan',
    ['KE'] = 'Kenya',
    ['KG'] = 'Kyrgyzstan',
    ['KH'] = 'Cambodia',
    ['KI'] = 'Kiribati',
    ['KM'] = 'Comoros',
    ['KN'] = 'Saint Kitts and Nevis',
    ['KP'] = 'Korea',
    ['KR'] = 'Korea',
    ['KW'] = 'Kuwait',
    ['KY'] = 'Cayman Islands',
    ['KZ'] = 'Kazakhstan',
    ['LA'] = 'Lao People?s Republic',
    ['LB'] = 'Lebanon',
    ['LC'] = 'Saint Lucia',
    ['LI'] = 'Liechtenstein',
    ['LK'] = 'Sri Lanka',
    ['LR'] = 'Liberia',
    ['LS'] = 'Lesotho',
    ['LT'] = 'Lithuania',
    ['LU'] = 'Luxembourg',
    ['LV'] = 'Latvia',
    ['LY'] = 'Libyan Arab Jamahiriya',
    ['MA'] = 'Morocco',
    ['MC'] = 'Monaco',
    ['MD'] = 'Moldova',
    ['ME'] = 'Montenegro',
    ['MG'] = 'Madagascar',
    ['MK'] = 'Macedonia',
    ['ML'] = 'Mali',
    ['MM'] = 'Myanmar',
    ['MN'] = 'Mongolia',
    ['MO'] = 'Macao',
    ['MP'] = 'Northern Mariana Islands',
    ['MR'] = 'Mauritania',
    ['MS'] = 'Montserrat',
    ['MT'] = 'Malta',
    ['MU'] = 'Mauritius',
    ['MV'] = 'Maldives',
    ['MW'] = 'Malawi',
    ['MX'] = 'Mexico',
    ['MY'] = 'Malaysia',
    ['MZ'] = 'Mozambique',
    ['NA'] = 'Namibia',
    ['NE'] = 'Niger',
    ['NG'] = 'Nigeria',
    ['NI'] = 'Nicaragua',
    ['NL'] = 'Netherlands',
    ['NO'] = 'Norway',
    ['NP'] = 'Nepal',
    ['NR'] = 'Nauru',
    ['NZ'] = 'New Zealand',
    ['OM'] = 'Oman',
    ['PA'] = 'Panama',
    ['PE'] = 'Peru',
    ['PG'] = 'Papua New Guinea',
    ['PH'] = 'Philippines',
    ['PK'] = 'Pakistan',
    ['PL'] = 'Poland',
    ['PT'] = 'Portugal',
    ['PW'] = 'Palau',
    ['PY'] = 'Paraguay',
    ['QA'] = 'Qatar',
    ['RO'] = 'Romania',
    ['RS'] = 'Serbia',
    ['RU'] = 'Russian Federation',
    ['RW'] = 'Rwanda',
    ['SA'] = 'Saudi Arabia',
    ['SB'] = 'Solomon Islands',
    ['SC'] = 'Seychelles',
    ['SD'] = 'Sudan',
    ['SE'] = 'Sweden',
    ['SG'] = 'Singapore',
    ['SH'] = 'Saint Helena',
    ['SI'] = 'Slovenia',
    ['SK'] = 'Slovakia',
    ['SL'] = 'Sierra Leone',
    ['SM'] = 'San Marino',
    ['SN'] = 'Senegal',
    ['SO'] = 'Somalia',
    ['SR'] = 'Suriname',
    ['ST'] = 'Sao Tome and Principe',
    ['SV'] = 'Salvador',
    ['SX'] = 'Sint Maarten (Dutch part)',
    ['SY'] = 'Syrian Arab Republic',
    ['SZ'] = 'Swaziland',
    ['TC'] = 'Turks and Caicos Islands',
    ['TD'] = 'Chad',
    ['TG'] = 'Togo',
    ['TH'] = 'Thailand',
    ['TJ'] = 'Tajikistan',
    ['TL'] = 'Timor?Leste',
    ['TM'] = 'Turkmenistan',
    ['TN'] = 'Tunisia',
    ['TO'] = 'Tonga',
    ['TR'] = 'Turkey',
    ['TT'] = 'Trinidad and Tobago',
    ['TV'] = 'Tuvalu',
    ['TW'] = 'Taiwan',
    ['TZ'] = 'Tanzania',
    ['UA'] = 'Ukraine',
    ['UG'] = 'Uganda',
    ['US'] = 'United States of America',
    ['UY'] = 'Uruguay',
    ['UZ'] = 'Uzbekistan',
    ['VA'] = 'Holy See',
    ['VC'] = 'Saint Vincent',
    ['VE'] = 'Venezuela',
    ['VG'] = 'Virgin Islands',
    ['VN'] = 'Viet Nam',
    ['VU'] = 'Vanuatu',
    ['WS'] = 'Samoa',
    ['YE'] = 'Yemen',
    ['ZA'] = 'South Africa',
    ['ZM'] = 'Zambia',
    ['ZW'] = 'Zimbabwe'
}

function getPlayerLocation(player)
	local ip = getPlayerIP(player)
	local countryCode = exports.admin:getPlayerCountry(player)
	local country = "San Andreas"
	if countryCode and countryNames[countryCode] then
		country = countryNames[countryCode]
	end
    setElementData(player, "Country", country)
end

function displayPlayerLocation( resp, errno, player)
    if errno == 0 then
        local data = fromJSON("[".. resp .. "]")
        local status = data["status"] or "fail"
        if status == "success" then
            outputChatBox("✶ Joined: #FFFFFF"..getPlayerName(player).."#FF6464 ("..data["city"]..", ".. data["country"]..") joined the server.", root, 255, 100, 100, true)
        else
            outputChatBox("✶ Joined: #FFFFFF"..getPlayerName(player).."#FF6464 ("..country..") joined the server.", root, 255, 100, 100, true)
        end
    else
        outputChatBox("ERROR", root, 255, 100, 100, true)
    end
end

function OnPlayerJoin ()
	getPlayerLocation(source)
    fetchRemote("http://ip-api.com/json/" .. ip, displayPlayerLocation, "", false, player)
end
addEventHandler("onPlayerJoin", getRootElement(), OnPlayerJoin)

 

Please help.

Link to comment
10 hours ago, JeViCo said:

Found community script. Install it and use exported functions

https://community.multitheftauto.com/?p=resources&s=details&id=3685

I have downloaded "geoip" resource, installed it in my server and started it, created script called "ip" with

Client side:

function showCity( )
    local thePlayer = getLocalPlayer ( )
    local playerName = getPlayerName( thePlayer )
    local country, countryFull = exports.geoip:getCountry( getPlayerIP( thePlayer ) )
    local city = exports.geoip:getCountryCity( getPlayerIP( thePlayer ) )
    if country == "SA" then
        outputChatBox( "[INFO]".. playerName .."His Country is:".. countryFull .."and City is:".. city, root, 255, 0, 0, true)
    else
        outputChatBox( "[INFO]".. playerName .."His Country is:".. countryFull, root, 255, 0, 0, true)
    end
end

addEventHandler ( "onClientResourceStart", getRootElement(), showCity )

meta file:

<meta>
    <info author="FIY9AL" name="Show Players City" type="script" version="1.0" />
    <script src="Client.Lua" type="client"/>
</meta>

 

and it didn't work, nothing output into chatbox, any help or idea ?

Edited by FIY9AL
Link to comment

After searching into "geoip" script functions i found that it will work only for Russain players

"geoip" server side getCountryCity function:

function getCountryCity( ip )
	local num 	= tostring( toIPNum( ip ) )
	local qh 	= dbQuery( sqlite_geo_db, "SELECT city,region FROM geoIPCityLocation_RU WHERE locId = (SELECT locId FROM geoIPCityBlocks_RU WHERE ".. num .." BETWEEN begin_num AND end_num LIMIT 1) LIMIT 1" )
	local res 	= dbPoll( qh, -1 )
	if res[ 1 ] then
		return res[ 1 ].city, res[ 1 ].region, "RU", "Russian Federation"
	end
	return false
end

 

can someone found simillar script works for worldwide or saudi arabia ?

I Have found website can return IP City as table called http://ip-api.com/json/

it will be great if someone used https://wiki.multitheftauto.com/wiki/fetchRemote function to link the website table with variable and use it into script

Edited by FIY9AL
Link to comment
addEventHandler("onPlayerJoin", root,
function()
    local ip = getPlayerIP(source)
    local function displayPlayerCity(resp, errno, source)
        if errno == 0 then
            local data = fromJSON("[".. resp .. "]")
            local status = data["status"] or "fail"
            if status == "success" then
                if isElement(source) then
                    outputChatBox(getPlayerName(source).." has joined the game ("..data["city"]..", "..data["country"]..")", root, 255, 100, 100)
                end
            else
                if isElement(source) then
                    outputChatBox(getPlayerName(source).." has joined the game.", root, 255, 100, 100)
                end
                outputDebugString("JOINQUIT: Cannot retrieve network details")
            end
        else
            outputDebugString("JOINQUIT: Error @fetchRemote, ERRNO: "..errno)
        end
    end 

    fetchRemote("http://ip-api.com/json/" .. ip, displayPlayerCity, "", false, source)
end, true, "low")

 

Link to comment
58 minutes ago, SaNoR said:

addEventHandler("onPlayerJoin", root,
function()
    local ip = getPlayerIP(source)
    local function displayPlayerCity(resp, errno, source)
        if errno == 0 then
            local data = fromJSON("[".. resp .. "]")
            local status = data["status"] or "fail"
            if status == "success" then
                if isElement(source) then
                    outputChatBox(getPlayerName(source).." has joined the game ("..data["city"]..", "..data["country"]..")", root, 255, 100, 100)
                end
            else
                if isElement(source) then
                    outputChatBox(getPlayerName(source).." has joined the game.", root, 255, 100, 100)
                end
                outputDebugString("JOINQUIT: Cannot retrieve network details")
            end
        else
            outputDebugString("JOINQUIT: Error @fetchRemote, ERRNO: "..errno)
        end
    end 

    fetchRemote("http://ip-api.com/json/" .. ip, displayPlayerCity, "", false, source)
end, true, "low")

 

Thank you very much "SaNoRi'm really appreciate you.

it is working perfectly as it is should.

 

28734136a44c8cb3da5607bd0c2f63b11acdf791

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