Page 1 of 1
[DONE] GeoIP Country Login Message
Posted: August 14th, 2011, 2:13 pm
by ven000m
Really looking forward to see this /or/ if you can give me a step-by-step guide for 0.535_701.
http://www.mcquai.net/forum/viewtopic.php?f=6&t=28
Re: GeoIP again
Posted: August 14th, 2011, 6:31 pm
by boblol0909
His code works, to put it into fcraft make a new class file in the fcraft project called GeoIP.cs and and put in this code:
Show
Code: Select all
using System;
using System.Net;
using System.Xml;
using System.Data;
namespace fCraft
{
public static class GeoIP
{
public static string GetGeoLocationByIP(string strIPAddress)
{
//Create a WebRequest
WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + strIPAddress);
//Create a Proxy
WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + strIPAddress, true);
//Assign the proxy to the WebRequest
rssReq.Proxy = px;
//Set the timeout in Seconds for the WebRequest
rssReq.Timeout = 2000;
try
{
//Get the WebResponse
WebResponse rep = rssReq.GetResponse();
//Read the Response in a XMLTextReader
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
//Create a new DataSet
DataSet ds = new DataSet();
ds.ReadXml(xtr);
DataTable dt = ds.Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
if (dt.Rows[0]["Status"].ToString().Equals("true", StringComparison.CurrentCultureIgnoreCase))
{
if (strIPAddress != "127.0.0.1" && !strIPAddress.StartsWith("192.168."))
return dt.Rows[0]["CountryName"].ToString();
else
return "This Server";
}
}
return "";
}
catch
{
return "";
}
}
}
}
Then go to Server.cs in the fcraft project and find the method: public static string MakePlayerConnectedMessage( Player player, bool firstTime, World world ).
Replace everything inside the { } with :
Show
Code: Select all
if( player == null ) throw new ArgumentNullException( "player" );
if( world == null ) throw new ArgumentNullException( "world" );
bool canGetGeo = true;
if (GeoIP.GetGeoLocationByIP(player.Session.IP.ToString()) == "") canGetGeo = false;
string whereFrom = canGetGeo ? "from: " + GeoIP.GetGeoLocationByIP(player.Session.IP.ToString()) : "";
if( firstTime ) {
return String.Format( "&S{0} ({1}&S) connected, joined {2} {3}",
player.Name,
player.Info.Rank.GetClassyName(),
world.GetClassyName(),
whereFrom);
} else {
return String.Format( "&S{0} ({1}&S) connected again, joined {2} {3}",
player.Name,
player.Info.Rank.GetClassyName(),
world.GetClassyName(),
whereFrom);
}
That should work, tell me if it doesnt. This will say it when someone connects, i.e. "Boblol0909 connnected again, joined main from: UnitedStates"
Re: GeoIP again
Posted: August 15th, 2011, 5:01 pm
by fragmer
Well there you go, mod it if you want.
Re: GeoIP again
Posted: August 16th, 2011, 11:35 am
by ven000m
WORKED!
Re: [DONE] GeoIP Country Login Message
Posted: September 6th, 2011, 10:49 am
by ven000m
anyone skilled to update this for 0.600_dev ? ;P
Re: [DONE] GeoIP Country Login Message
Posted: September 6th, 2011, 10:59 am
by Jonty800
I've done this for 0.6x
Geoip.cs
Code: Select all
using System;
using System.Net;
using System.Xml;
using System.Data;
namespace fCraft
{
public static class GeoIP
{
public static string GetGeoLocationByIP(string strIPAddress)
{
//Create a WebRequest
WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + strIPAddress);
//Create a Proxy
WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + strIPAddress, true);
//Assign the proxy to the WebRequest
rssReq.Proxy = px;
//Set the timeout in Seconds for the WebRequest
rssReq.Timeout = 2000;
try
{
//Get the WebResponse
WebResponse rep = rssReq.GetResponse();
//Read the Response in a XMLTextReader
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());
//Create a new DataSet
DataSet ds = new DataSet();
ds.ReadXml(xtr);
DataTable dt = ds.Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
if (dt.Rows[0]["Status"].ToString().Equals("true", StringComparison.CurrentCultureIgnoreCase))
{
if (strIPAddress != "127.0.0.1" && !strIPAddress.StartsWith("192.168."))
return dt.Rows[0]["CountryName"].ToString();
else
return "Au70 Galaxy Headquarters";
}
}
return "";
}
catch
{
return "";
}
}
}
}
Replace this section in server.cs (Note I changed login from player.Name to player.ClassyName)
Code: Select all
public static string MakePlayerConnectedMessage( Player player, bool firstTime, World world )
{
if( player == null ) throw new ArgumentNullException( "player" );
if( world == null ) throw new ArgumentNullException( "world" );
bool canGetGeo = true;
if (GeoIP.GetGeoLocationByIP(player.IP.ToString()) == "") canGetGeo = false;
string whereFrom = canGetGeo ? "&Sfrom:&c " + GeoIP.GetGeoLocationByIP(player.IP.ToString()) : "";
if( firstTime ) {
return String.Format( "&S{0} ({1}&S) connected, joined {2} {3}",
player.ClassyName,
player.Info.Rank.ClassyName,
world.ClassyName,
whereFrom);
} else {
return String.Format( "&S{0} ({1}&S) connected again, joined {2} {3}",
player.ClassyName,
player.Info.Rank.ClassyName,
world.ClassyName,
whereFrom);
}
}
Re: [DONE] GeoIP Country Login Message
Posted: September 6th, 2011, 11:03 am
by Jonty800
Ah and also change the part in geoip for local host.
Re: [DONE] GeoIP Country Login Message
Posted: September 7th, 2011, 5:46 am
by Kitteh
I installed the code and it worked perfectly, But i woke up today and checked and it dosnt work anymore, i checked the source for any errors but none, :S
it dosnt show the countrys no more

Re: [DONE] GeoIP Country Login Message
Posted: September 7th, 2011, 5:14 pm
by Jonty800
Thats strange. I've never had a problem with it.
Except it says I'm from the United States, but I guess thats an issue with my ISP.
Perhaps you're not running the software with GeoIp in it? Maybe.
Also, Ven000m, the code I posted is for the latest dev version of fCraft. If you're using an earlier dev build, screenshot your coding software and I'll ammend the code for you =)
Re: [DONE] GeoIP Country Login Message
Posted: September 8th, 2011, 7:13 am
by ven000m
Its working for me

.
Thanks dude!