Skip to content

Commit b13ba0e

Browse files
author
Tamas Henning
committed
I think good stuff???
1 parent 7ff2849 commit b13ba0e

File tree

4 files changed

+167
-10
lines changed

4 files changed

+167
-10
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.tamashenning.forgeanalyitcs.client;
2+
3+
public class ForgeAnalyticsConstants {
4+
public final static String pingClientTable = "ClientTable";
5+
public final static String pingClientStartCommand = "PING";
6+
public final static String pingServerTable = "ServerTable";
7+
public final static String pingServerStartCommand = "PING_START";
8+
public final static String pingServerStopCommand = "PING_STOP";
9+
10+
public final static String pingClientKeepAlive = "KEEPALIVE";
11+
public final static String pingServerKeepAlive = "KEEPALIVE";
12+
13+
public final static int HASHCOUNT = 5;
14+
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.tamashenning.forgeanalyitcs.client;
2+
3+
import java.math.BigInteger;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.security.SecureRandom;
6+
7+
import com.tamashenning.forgeanalytics.AnalyticsClient;
8+
9+
public class ForgeAnalyticsSingleton {
10+
private static ForgeAnalyticsSingleton instance = null;
11+
private AnalyticsClient ac = new AnalyticsClient();
12+
13+
public String SessionID = "";
14+
15+
protected ForgeAnalyticsSingleton() {
16+
SecureRandom random = new SecureRandom();
17+
try {
18+
SessionID = ac.Anonymize(new BigInteger(130, random).toString(32));
19+
} catch (NoSuchAlgorithmException e) {
20+
// TODO Auto-generated catch block
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
public static ForgeAnalyticsSingleton getInstance() {
26+
if (instance == null) {
27+
instance = new ForgeAnalyticsSingleton();
28+
}
29+
30+
return instance;
31+
}
32+
33+
}

src/java/com/tamashenning/forgeanalytics/AnalyticsClient.java

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.apache.http.impl.client.HttpClientBuilder;
1313

1414
import com.google.gson.Gson;
15+
import com.tamashenning.forgeanalyitcs.client.ForgeAnalyticsConstants;
16+
import com.tamashenning.forgeanalyitcs.client.ForgeAnalyticsSingleton;
1517
import com.tamashenning.forgeanalytics.models.AnalyticsModel;
1618

1719
import net.minecraft.client.Minecraft;
@@ -22,10 +24,6 @@
2224

2325
public class AnalyticsClient {
2426

25-
private String pingClientTable = "ClientTable";
26-
private String pingServerTable = "ServerTable";
27-
private final int HASHCOUNT = 5;
28-
2927
public boolean UploadModel(AnalyticsModel model) throws Exception {
3028
Gson g = new Gson();
3129
String json = g.toJson(model);
@@ -58,9 +56,9 @@ public String CreateClientStartupPing() {
5856
Gson g = new Gson();
5957

6058
AnalyticsModel am = new AnalyticsModel();
61-
am.Table = this.pingClientTable;
59+
am.Table = ForgeAnalyticsConstants.pingClientTable;
6260
am.Properties = new HashMap<String, String>();
63-
am.PartitionKey = "PING";
61+
am.PartitionKey = ForgeAnalyticsConstants.pingClientStartCommand;
6462
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
6563
am.Properties.putAll(this.getCommonValues());
6664

@@ -80,9 +78,42 @@ public String CreateServerStartupPing() {
8078
Gson g = new Gson();
8179

8280
AnalyticsModel am = new AnalyticsModel();
83-
am.Table = this.pingServerTable;
81+
am.Table = ForgeAnalyticsConstants.pingServerTable;
82+
am.Properties = new HashMap<String, String>();
83+
am.PartitionKey = ForgeAnalyticsConstants.pingServerStartCommand;
84+
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
85+
am.Properties.putAll(this.getCommonValues());
86+
am.Properties.put("ServerDifficulty", MinecraftServer.getServer().getDifficulty().toString());
87+
88+
MinecraftServer server = MinecraftServer.getServer();
89+
90+
if (MinecraftServer.getServer().isDedicatedServer()) {
91+
// Running dedicated...
92+
try {
93+
am.Properties.put("ServerHostHash", this.Anonymize(server.getHostname()));
94+
} catch (NoSuchAlgorithmException e) {
95+
// TODO Auto-generated catch block
96+
e.printStackTrace();
97+
}
98+
}
99+
else {
100+
// Running internal...
101+
am.Properties.put("ServerHostHash", "localhost");
102+
am.Properties.put("IsDemo", Boolean.toString(server.isDemo()));
103+
am.Properties.put("IsLanMode", Boolean.toString(((IntegratedServer)server).getPublic()));
104+
}
105+
106+
String json = g.toJson(am);
107+
return json;
108+
}
109+
110+
public String CreateServerStoppedPing() {
111+
Gson g = new Gson();
112+
113+
AnalyticsModel am = new AnalyticsModel();
114+
am.Table = ForgeAnalyticsConstants.pingServerTable;
84115
am.Properties = new HashMap<String, String>();
85-
am.PartitionKey = "PING";
116+
am.PartitionKey = ForgeAnalyticsConstants.pingServerStopCommand;
86117
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
87118
am.Properties.putAll(this.getCommonValues());
88119
am.Properties.put("ServerDifficulty", MinecraftServer.getServer().getDifficulty().toString());
@@ -104,18 +135,80 @@ public String CreateServerStartupPing() {
104135
am.Properties.put("IsDemo", Boolean.toString(server.isDemo()));
105136
am.Properties.put("IsLanMode", Boolean.toString(((IntegratedServer)server).getPublic()));
106137
}
138+
139+
140+
String json = g.toJson(am);
141+
return json;
142+
}
107143

144+
public String CreateClientKeepAlivePing() {
145+
Gson g = new Gson();
146+
147+
AnalyticsModel am = new AnalyticsModel();
148+
am.Table = ForgeAnalyticsConstants.pingClientTable;
149+
am.Properties = new HashMap<String, String>();
150+
am.PartitionKey = ForgeAnalyticsConstants.pingClientKeepAlive;
151+
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
152+
am.Properties.putAll(this.getCommonValues());
153+
154+
try {
155+
// TODO figure this out...
156+
am.Properties.put("UserHash", this.Anonymize(Minecraft.getMinecraft().thePlayer.getUniqueID().toString()));
157+
} catch (NoSuchAlgorithmException e) {
158+
// TODO Auto-generated catch block
159+
e.printStackTrace();
160+
}
161+
108162
String json = g.toJson(am);
109163
return json;
110164
}
111165

166+
public String CreateServerKeepAlivePing() {
167+
Gson g = new Gson();
168+
169+
AnalyticsModel am = new AnalyticsModel();
170+
am.Table = ForgeAnalyticsConstants.pingServerTable;
171+
am.Properties = new HashMap<String, String>();
172+
am.PartitionKey = ForgeAnalyticsConstants.pingServerKeepAlive;
173+
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
174+
am.Properties.putAll(this.getCommonValues());
175+
am.Properties.put("ServerDifficulty", MinecraftServer.getServer().getDifficulty().toString());
176+
177+
MinecraftServer server = MinecraftServer.getServer();
178+
179+
if (MinecraftServer.getServer().isDedicatedServer()) {
180+
// Running dedicated...
181+
try {
182+
am.Properties.put("ServerHostHash", this.Anonymize(server.getHostname()));
183+
} catch (NoSuchAlgorithmException e) {
184+
// TODO Auto-generated catch block
185+
e.printStackTrace();
186+
}
187+
am.Properties.put("ConnectedUsers", Integer.toString(server.getCurrentPlayerCount()));
188+
}
189+
else {
190+
// Running internal...
191+
am.Properties.put("ServerHostHash", "localhost");
192+
am.Properties.put("IsDemo", Boolean.toString(server.isDemo()));
193+
am.Properties.put("IsLanMode", Boolean.toString(((IntegratedServer)server).getPublic()));
194+
}
195+
196+
197+
String json = g.toJson(am);
198+
return json;
199+
}
200+
112201

113202
private Map<String, String> getCommonValues() {
114203
Map<String, String> commonValues = new HashMap<String, String>();
115204
String activeModListCount = Integer.toString(net.minecraftforge.fml.common.Loader.instance().getActiveModList().size());
116205
String modListCount = Integer.toString(net.minecraftforge.fml.common.Loader.instance().getModList().size());
117206
String modList = "";
118207

208+
commonValues.put("JavaVersion", System.getProperty("java.version"));
209+
commonValues.put("JavaMaxRAM", Long.toString(Runtime.getRuntime().maxMemory()));
210+
commonValues.put("JavaAllocatedRAM", Long.toString(Runtime.getRuntime().totalMemory()));
211+
commonValues.put("SessionID", ForgeAnalyticsSingleton.getInstance().SessionID);
119212
commonValues.put("MinecraftVersion", Minecraft.getMinecraft().getVersion());
120213
commonValues.put("ForgeVersion", ForgeVersion.getVersion());
121214
commonValues.put("MCPVersion", ForgeVersion.mcpVersion);
@@ -136,7 +229,7 @@ private Map<String, String> getCommonValues() {
136229
public String Anonymize(String data) throws NoSuchAlgorithmException {
137230
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
138231
byte[] dataBytes = data.getBytes();
139-
for (int i=0; i< this.HASHCOUNT; i++) {
232+
for (int i=0; i< ForgeAnalyticsConstants.HASHCOUNT; i++) {
140233
dataBytes = sha256.digest(dataBytes);
141234
}
142235

src/java/com/tamashenning/forgeanalytics/ForgeAnalyticsMod.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.tamashenning.forgeanalytics;
22

3+
import com.tamashenning.forgeanalyitcs.client.ForgeAnalyticsSingleton;
34
import com.tamashenning.forgeanalytics.commands.AnalyticsCommands;
45
import com.tamashenning.forgeanalytics.proxies.CommonProxy;
56

7+
import net.minecraft.client.Minecraft;
68
import net.minecraftforge.fml.common.Mod;
79
import net.minecraftforge.fml.common.Mod.EventHandler;
810
import net.minecraftforge.fml.common.SidedProxy;
@@ -12,6 +14,9 @@
1214
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
1315
import net.minecraftforge.fml.common.event.FMLServerStartedEvent;
1416
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
17+
import net.minecraftforge.fml.common.event.FMLServerStoppedEvent;
18+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
19+
import net.minecraftforge.fml.common.gameevent.TickEvent;
1520
import net.minecraftforge.fml.relauncher.Side;
1621
import net.minecraftforge.fml.relauncher.SideOnly;
1722

@@ -54,8 +59,19 @@ public void serverStarted(FMLServerStartedEvent e) {
5459
// TODO Auto-generated catch block
5560
e1.printStackTrace();
5661
}
57-
5862
}
63+
64+
@EventHandler
65+
public void serverStopped(FMLServerStoppedEvent e) {
66+
AnalyticsClient ac = new AnalyticsClient();
67+
try {
68+
ac.UploadModel(ac.CreateServerStoppedPing());
69+
} catch (Exception e1) {
70+
// TODO Auto-generated catch block
71+
e1.printStackTrace();
72+
}
73+
}
74+
5975
@SideOnly(Side.CLIENT)
6076
@EventHandler
6177
public void fmlLoaded(FMLLoadCompleteEvent e){

0 commit comments

Comments
 (0)