AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] GeoCity (Get Players City From IP) (https://forums.alliedmods.net/showthread.php?t=124288)

n3wton 04-14-2010 20:16

[INC] GeoCity (Get Players City From IP)
 
1 Attachment(s)
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

p3tsin 04-15-2010 10:01

Re: [INC] GeoCity (Get Players City From IP)
 
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 :up:

n3wton 04-15-2010 10:09

Re: [INC] GeoCity (Get Players City From IP)
 
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

p3tsin 04-15-2010 10:27

Re: [INC] GeoCity (Get Players City From IP)
 
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 :wink:

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

n3wton 04-15-2010 10:30

Re: [INC] GeoCity (Get Players City From IP)
 
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

asherkin 04-15-2010 15:02

Re: [INC] GeoCity (Get Players City From IP)
 
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.

n3wton 04-15-2010 15:15

Re: [INC] GeoCity (Get Players City From IP)
 
asherkin thanks allot :)

Yours
N3wton

panda82 06-17-2010 15:36

Re: [INC] GeoCity (Get Players City From IP)
 
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?

psychonic 06-17-2010 16:17

Re: [INC] GeoCity (Get Players City From IP)
 
Quote:

Originally Posted by panda82 (Post 1211710)
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

panda82 06-17-2010 16:18

Re: [INC] GeoCity (Get Players City From IP)
 
why no ?

Afronanny 06-17-2010 18:18

Re: [INC] GeoCity (Get Players City From IP)
 
Quote:

Originally Posted by panda82 (Post 1211765)
why no ?

As I said in IRC:
Quote:

<Afronanny> Redirect = Bad.

panda82 06-17-2010 18:26

Re: [INC] GeoCity (Get Players City From IP)
 
:( its not a redirect server. the plugin i want it to run on the same server. reconnect players from lan on the same server just on the real ip adress.

n3wton 06-17-2010 18:44

Re: [INC] GeoCity (Get Players City From IP)
 
This uses the built in source mod commands to get the IP, i have no control over how the IP is obtained, sorry.

Afronanny 06-17-2010 19:06

Re: [INC] GeoCity (Get Players City From IP)
 
Quote:

Originally Posted by panda82 (Post 1211882)
:( its not a redirect server. the plugin i want it to run on the same server. reconnect players from lan on the same server just on the real ip adress.


That makes no sense. People playing on the lan ARE playing on the real server. Also, make another thread with your topic rather than pirating an existing one.

panda82 06-17-2010 19:11

Re: [INC] GeoCity (Get Players City From IP)
 
Quote:

Originally Posted by Afronanny (Post 1211917)
That makes no sense. People playing on the lan ARE playing on the real server. Also, make another thread with your topic rather than pirating an existing one.

Yes but if I type status i see 192 ip adresses not the real ones...

GoD-Tony 06-20-2010 02:13

Re: [INC] GeoCity (Get Players City From IP)
 
I think this is a great idea and I want to use this on my server, but with a database that size I think threading support will be necessary. You have to keep in mind that on a mapchange, getting the cities of 40+ people with a 0.1s lag per person will have a noticeable affect.

Hope you're still actively working on this :D

Darkthrone 07-06-2010 22:29

Re: [INC] GeoCity (Get Players City From IP)
 
how we can manually update geolitecity db for use with this inc?

turk89 07-13-2010 20:30

Re: [INC] GeoCity (Get Players City From IP)
 
Edit: Problem solved.

Darkthrone 07-13-2010 21:08

Re: [INC] GeoCity (Get Players City From IP)
 
n3wton, you should look at this

Arg! 07-14-2010 02:00

Re: [INC] GeoCity (Get Players City From IP)
 
1 Attachment(s)
Attached is a fixed version which doesnt error on attempting to close the SQL handle.

PharaohsPaw 07-20-2010 17:20

Re: [INC] GeoCity (Get Players City From IP)
 
Hello,

Discovered this as result of updates to the Connect Announce plugin which we've been using a slightly older version of (until today :)

Curious what your take would be on having geocity.inc use the actual GeoLiteCity.dat file you can pull in from the net instead, as an option.

The reason I'm asking is because we have HLStatsX CE running on our servers and this already uses a copy of GeoLiteCity.dat, in fact you can simply script pulling updated .dat files in via /etc/crontab. If we're already using a copy of GeoLiteCity.dat for HLStatsX that we update regularly, it seems more sensible to have other sourcemod plugins also use the same .dat file, especially with regard to keeping things updated. Update one copy of the database and everything that uses it uses the updated copy. (symlinks are a beautiful thing sometimes)

Looks like Sourcemod uses .dat format already too, ie configs/geoip/GeoIP.dat.

If anybody can point me at some relevant coding documentation I'm willing to take a look at this.

Also noticed that the sourcemod base directory in GeoDatabaseConnect() is more or less hardcoded in this .inc file to look in addons/sourcemod/... to find the .sq3 file under the sourcemod directory. Is there a way to express the sm_basedir as a variable instead, so folks running multiple servers off of one set of gameserver files can have it find the database file without having to custom-compile a copy of the plugin(s) using geocity.inc customized for each server's sourcemod base directory location?

Thanks,
PharaohsPaw

Darkthrone 07-21-2010 01:11

Re: [INC] GeoCity (Get Players City From IP)
 
see here

Krufftys Killers 12-17-2014 21:35

Re: [INC] GeoCity (Get Players City From IP)
 
dose any body got the GeoLiteCity.sq3 can you please post it?
Thanks


All times are GMT -4. The time now is 18:16.

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