codeluaeveryday Posted February 26, 2013 Share Posted February 26, 2013 (edited) Hello all! I am not sure how many people have noticed that the Admin resource has outdated IP tables, and sometimes people which joined my server had the an unknown location. There is a simple way of overcoming this issue using Application programming interface (API), this allows us to access other websites applications to obtain data and in this case we will access a constantly updated IP API to get the location. This code can be easily melded into any purpose and even the possibility of blocking certain countries from joining the server. We will be using http://ipinfodb.com to collect the players country. IPInfoDB provides you with 2 calls per second, anything more is delayed by one second, this is good because we can then store this data in a table! IPInfoDB says that they are using a less accurate database, but its very accurate, they update it themselves, and if you answer the register properly you will only be helping. Dependencies: Title Case function (Add it to your code):function titlecase(str) result='' for word in string.gfind(str, "%S+") do local first = string.sub(word,1,1) result = (result .. string.upper(first) .. string.lower(string.sub(word,2)) .. ' ') end return result end Steps: 1) Getting an API key from IPInfoDB A) Goto http://ipinfodb.com/register.phpB) Fill in the details on the register page and click register.C) You will need to go to your email to confirm your account.D) When you log-in you will now see your API Key at the bottom of the grey box, keep that somewhere, we will need it shortly.EXAMPLE: API Key aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2) Creating the join code addEventHandler('onPlayerJoin', root, function() fetchRemote("http://api.ipinfodb.com/v3/ip-country/?key=Your_API_Key&format=xml&ip="..getPlayerIP(source), countryResult, "", false, source) end ) Replace Your_API_Key with your API key from IPInfoDB. The above code is downloading the source of a file which contains info on the player's country. Now we need to create the function called countryResult, this contains the returned data. 3) Returning the page data sent from the fetchRemote() This function contains the data returned from the fetchRemote, aka the Country and the IP. Add the below to your code:function countryResult(responseData, errno, thePlayer) startEntry, endEntry = responseData:find('') startEnding, endEnding = responseData:find('', endEntry+1) country = titlecase(responseData:sub(endEntry+1, startEnding - 1)) startEntry, endEntry = responseData:find('') startEnding, endEnding = responseData:find('', endEntry+1) ip = responseData:sub(endEntry+1, startEnding - 1) oldIPS[ip] = country outputChatBox('#0099FF[JOIN]#FFFFFF '.. getPlayerName(thePlayer) ..'#FFFFFF has joined from '..(country), root, 255, 100, 100,true) end 4) Storing the ip and country to save server bandwidth. Use tables of this. Firstly put a table at the beggining at line one:oldIPS = { } Now add this to the beginning of your onPlayerJoin event: if oldIPS[getPlayerIP(source)] then outputChatBox('#0099FF[JOIN]#FFFFFF '.. getPlayerName(thePlayer) ..'#FFFFFF has joined from '..(oldIPS[getPlayerIP(source)]), root, 255, 100, 100,true) return end 5) Adding a simple output for when a player changes their nickname Add the below to your code:addEventHandler('onPlayerChangeNick', root, function(oldNick, newNick) outputChatBox('#0099FF[NICK]#FFFFFF ' .. oldNick .. '#FFFFFF is now known as ' .. newNick, root, 255, 100, 100,true) end ) 6) Adding the quit message and using the table to get the country This uses the stored countries to output the players country when they quit, add the below to your code:addEventHandler('onPlayerQuit', root, function(reason) outputChatBox('#0099FF[QUIT]#FFFFFF ' .. getPlayerName(source) .. '#FFFFFF has left from '.. oldIPS[getPlayerIP(source)] .. ' [' .. reason .. ']', root, 255, 100, 100,true) end ) Full Code (If your copying the full code, then replace the Your_API_Key with the key you obtained from Step 1: oldIPS = { } function titlecase(str) result='' for word in string.gfind(str, "%S+") do local first = string.sub(word,1,1) result = (result .. string.upper(first) .. string.lower(string.sub(word,2)) .. ' ') end return result end function countryResult(responseData, errno, thePlayer) startEntry, endEntry = responseData:find('') startEnding, endEnding = responseData:find('', endEntry+1) country = titlecase(responseData:sub(endEntry+1, startEnding - 1)) startEntry, endEntry = responseData:find('') startEnding, endEnding = responseData:find('', endEntry+1) ip = responseData:sub(endEntry+1, startEnding - 1) oldIPS[ip] = country outputChatBox('#0099FF[JOIN]#FFFFFF '.. getPlayerName(thePlayer) ..'#FFFFFF has joined from '..(country), root, 255, 100, 100,true) end addEventHandler('onPlayerJoin', root, function() if oldIPS[getPlayerIP(source)] then outputChatBox('#0099FF[JOIN]#FFFFFF '.. getPlayerName(thePlayer) ..'#FFFFFF has joined from '..(oldIPS[getPlayerIP(source)]), root, 255, 100, 100,true) return end fetchRemote("http://api.ipinfodb.com/v3/ip-country/?key=Your_API_Key&format=xml&ip="..getPlayerIP(source), countryResult, "", false, source) end ) addEventHandler('onPlayerChangeNick', root, function(oldNick, newNick) outputChatBox('#0099FF[NICK]#FFFFFF ' .. oldNick .. '#FFFFFF is now known as ' .. newNick, root, 255, 100, 100,true) end ) addEventHandler('onPlayerQuit', root, function(reason) outputChatBox('#0099FF[QUIT]#FFFFFF ' .. getPlayerName(source) .. '#FFFFFF has left from '.. oldIPS[getPlayerIP(source)] .. ' [' .. reason .. ']', root, 255, 100, 100,true) end ) Edited March 5, 2013 by Guest 1 Link to comment
Scripting Moderators Sarrum Posted February 27, 2013 Scripting Moderators Share Posted February 27, 2013 Well done, good job. https://code.google.com/p/mtasa-resources/source/detail?r=909 Link to comment
Anderl Posted February 27, 2013 Share Posted February 27, 2013 Well organized, well explained.. good job Link to comment
codeluaeveryday Posted February 27, 2013 Author Share Posted February 27, 2013 Thanks guys, and omg... I didn't know they updated the IP tables, but they will go out of date sooner or later. Link to comment
Max+ Posted February 28, 2013 Share Posted February 28, 2013 Good Job Csmit195 But if the Code Orgnized it well Be Better = Link to comment
codeluaeveryday Posted February 28, 2013 Author Share Posted February 28, 2013 Max, dude the code is organized, its very organized, it's because the [lua] box is too small to display the code. Link to comment
Anderl Posted February 28, 2013 Share Posted February 28, 2013 Good Job Csmit195 But if the Code Orgnized it well Be Better = And what do you mean by code organized? The code is organized, there is not much more you could do on it (note that I said not much more, not nothing). Link to comment
codeluaeveryday Posted March 27, 2013 Author Share Posted March 27, 2013 No problem Markeloff Link to comment
Recommended Posts