Raised This Month: $51 Target: $400
 12% 

[INC] GeoCity (Get Players City From IP)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
n3wton
Senior Member
Join Date: Mar 2010
Old 04-14-2010 , 20:16   [INC] GeoCity (Get Players City From IP)
Reply With Quote #1

Introduction

This include uses GeoLite City to calculate where a clients IP address "represents" there city to be.

Basically I took GeoLite Cities .csv file and put all the data into an SQlite database which can the be used to find the clients city.


Command

GeoipCity( const String:ip[], String:city[], const maxlength )

ip : IP Address of the client ( use sourcemods GetClientIP command )
city : A buffer to store the clients city in
maxlength : Maximum length of the buffer

Example

Code:
public OnClientConnected( client )
{
     new String:IP[16];
     new String:City[32];
     GetClientIP( client, IP, 16 );
     GeoipCity( IP, City, 32 );
     PrintToChat( client, "%N is from %s", client, City );
}
Installation

Download geocity.inc and put it in "addons\sourcemod\scripting\include".
Download GeoLiteCity.zip and extract GeoLiteCity.sq3 into "addons\sourcemod\data\sqlite" on the server.

Then just include geocity to your source code.
#include <geocity>

Note
1-There is a small amount of lag (< 0.1 second) when getting the city, so it is recomended that you only get the city on connect, then store it in a 2 dimensional array to call on when ever needed.

2-As the .zip containing the .sq3 database is ~50mb and these forums only allow up to 5mb, I will upload it to multiple download locations, though if someone has a server willing to host, please let me know.

3-This is still in a beta version, I think its 100% stable and I use it in one of my plugins, on my server and have had no problems.

Download

GeoLiteCity.zip {RapidShare}
GeoLiteCity.zip {MegaUpload}

Yours
N3wton
Attached Files
File Type: inc geocity.inc (4.8 KB, 1576 views)

Last edited by asherkin; 05-01-2012 at 08:10. Reason: Removed dead link to my site.
n3wton is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 04-15-2010 , 10:01   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #2

Although this has been done before, yours uses databases which is a great improvement (lag-wise at least). A couple of suggestions however
  1. all global variables and internal functions people arent supposed to directly access, make them "static stock" (it doesnt say in the article that you can use static for functions, or that you can use stock for variables, but it applies to them too)
  2. declare large local array variables (or small too!) with decl
  3. it seems the code is leaking memory by reconnecting to the database each time, discarding the potential existing database handle
  4. can you add support for threaded queries? dont remove the non-threaded stuff though, its nice to be able to choose

Good job implementing this on databases
__________________
plop
p3tsin is offline
n3wton
Senior Member
Join Date: Mar 2010
Old 04-15-2010 , 10:09   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #3

Thanks, I know its been done before, but his code doesn't actually work, as sourcemod only allows up to a 16 bisgned integer, and there for if the first block of your IP address is greater than 128 it doesn't work.

1. Thanks, I didn't really know how to write an include, so I figured when I post my code, someone will be able to help me tidy it up

2. ok, read the wiki page, never new that thanks

3. So I should make a global query handle?

4. I tried adding thread cos I knew it would stop the lag, but they comfused the hell out of me lol. Anyone know of any examples/tutorials?

and Thanks

EDIT:
Updated the first post with modified code. points 1, 2 and 3 have been added, still looking into threaded querys

Yours
N3wton

Last edited by n3wton; 04-15-2010 at 10:27.
n3wton is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 04-15-2010 , 10:27   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #4

2. Its described in the wiki
Quote:
Originally Posted by wiki
By default, all variables in Pawn are initialized to zero. If there is an explicit initializer, the variable is initialized to the expression after the = token. At a local scope, this can be a run-time expense. The decl keyword (which is only valid at local scope) was introduced to let users decide if they want variables initialized or not.
Basically, leaving variables uninitialized (when it doesnt matter) is faster

3. Nah, queries can be local. You should check if there already is an existing connection before opening a new one (referring to GeoDatabaseConnect())

4. Yeah I can imagine, your current implementation is not the most cleanest either, but I dont know nor can I be bothered to think of a better way . The best advice I can probably give you is to check out the wiki article, or see other plugins that use threaded queries. I wasnt really asking for threaded query support though, just thought it would be nice
__________________
plop

Last edited by p3tsin; 04-15-2010 at 10:29.
p3tsin is offline
n3wton
Senior Member
Join Date: Mar 2010
Old 04-15-2010 , 10:30   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #5

3. oh, I get you now i'll re-edit the include, and add what you meant

4. Never new there was an SQL wiki page I feel all warm inside now

Yours
N3wton
n3wton is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-15-2010 , 15:02   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #6

I totally forgot I left this uploading, Here is a direct link to the GeoLiteCity.zip. Obviously, if you have a premium account for either of the original download sites, use them as bandwidth is of course not unlimited.
__________________

Last edited by asherkin; 05-01-2012 at 08:10. Reason: Removed dead link.
asherkin is offline
n3wton
Senior Member
Join Date: Mar 2010
Old 04-15-2010 , 15:15   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #7

asherkin thanks allot

Yours
N3wton
n3wton is offline
panda82
Member
Join Date: Aug 2007
Old 06-17-2010 , 15:36   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #8

can you make the plugin redirect players to an external ip adress if they are from a lan network? For example if they connect from 192.x.x.x to redirect them to external ip adress?
panda82 is offline
psychonic

BAFFLED
Join Date: May 2008
Old 06-17-2010 , 16:17   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #9

Quote:
Originally Posted by panda82 View Post
can you make the plugin redirect players to an external ip adress if they are from a lan network? For example if they connect from 192.x.x.x to redirect them to external ip adress?
no
psychonic is offline
panda82
Member
Join Date: Aug 2007
Old 06-17-2010 , 16:18   Re: [INC] GeoCity (Get Players City From IP)
Reply With Quote #10

why no ?
panda82 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:44.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode