Re-add Stat Tracker. Disabled while we set up the backend.

This commit is contained in:
Zack Piispanen 2013-11-06 23:41:50 -05:00
parent f527e02932
commit bd2f98c203
3 changed files with 87 additions and 3 deletions

83
TShockAPI/StatTracker.cs Normal file
View file

@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Web;
namespace TShockAPI
{
public class StatTracker
{
public StatTracker()
{
ThreadPool.QueueUserWorkItem(SendUpdate);
}
private HttpWebResponse GetResponseNoException(HttpWebRequest req)
{
try
{
return (HttpWebResponse)req.GetResponse();
}
catch (WebException we)
{
var resp = we.Response as HttpWebResponse;
if (resp == null)
throw;
return resp;
}
}
private void SendUpdate(object info)
{
Thread.Sleep(1000*60*15);
var data = new JsonData
{
port = Terraria.Netplay.serverPort,
currentPlayers = TShock.Utils.ActivePlayers(),
maxPlayers = TShock.Config.MaxSlots,
systemRam = 2097152,
systemCPUClock = 2516582,
version = TShock.VersionNum.ToString(),
terrariaVersion = Terraria.Main.versionNumber2
};
var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data);
var encoded = HttpUtility.UrlEncode(serialized);
var uri = String.Format("http://127.0.0.1:8000?data={0}", encoded);
var client = (HttpWebRequest)WebRequest.Create(uri);
try
{
using (var resp = GetResponseNoException(client))
{
if (resp.StatusCode != HttpStatusCode.OK)
{
throw new IOException("Server did not respond with an OK.");
}
}
}
catch (Exception e)
{
Console.WriteLine("StatTracker has failed: {0}", e.Message);
return;
}
ThreadPool.QueueUserWorkItem(SendUpdate);
}
}
public struct JsonData
{
public int port;
public int currentPlayers;
public int maxPlayers;
public int systemRam;
public int systemCPUClock;
public string version;
public string terrariaVersion;
}
}

View file

@ -70,6 +70,7 @@ namespace TShockAPI
public static SecureRest RestApi; public static SecureRest RestApi;
public static RestManager RestManager; public static RestManager RestManager;
public static Utils Utils = Utils.Instance; public static Utils Utils = Utils.Instance;
public static StatTracker StatTracker;
/// <summary> /// <summary>
/// Used for implementing REST Tokens prior to the REST system starting up. /// Used for implementing REST Tokens prior to the REST system starting up.
/// </summary> /// </summary>
@ -596,8 +597,6 @@ namespace TShockAPI
Lighting.lightMode = 2; Lighting.lightMode = 2;
FixChestStacks(); FixChestStacks();
} }
private void FixChestStacks() private void FixChestStacks()

View file

@ -68,6 +68,7 @@
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="TerrariaServer, Version=1.14.0.0, Culture=neutral, processorArchitecture=x86"> <Reference Include="TerrariaServer, Version=1.14.0.0, Culture=neutral, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<ExecutableExtension>.exe</ExecutableExtension> <ExecutableExtension>.exe</ExecutableExtension>
@ -125,6 +126,7 @@
<Compile Include="Rest\RestObject.cs" /> <Compile Include="Rest\RestObject.cs" />
<Compile Include="Rest\RestVerbs.cs" /> <Compile Include="Rest\RestVerbs.cs" />
<Compile Include="Rest\SecureRest.cs" /> <Compile Include="Rest\SecureRest.cs" />
<Compile Include="StatTracker.cs" />
<Compile Include="Utils.cs" /> <Compile Include="Utils.cs" />
<Compile Include="TShock.cs" /> <Compile Include="TShock.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
@ -176,7 +178,7 @@
</PropertyGroup> </PropertyGroup>
<ProjectExtensions> <ProjectExtensions>
<VisualStudio> <VisualStudio>
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" /> <UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
</VisualStudio> </VisualStudio>
</ProjectExtensions> </ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.