12
12
import org .apache .http .impl .client .HttpClientBuilder ;
13
13
14
14
import com .google .gson .Gson ;
15
+ import com .tamashenning .forgeanalyitcs .client .ForgeAnalyticsConstants ;
16
+ import com .tamashenning .forgeanalyitcs .client .ForgeAnalyticsSingleton ;
15
17
import com .tamashenning .forgeanalytics .models .AnalyticsModel ;
16
18
17
19
import net .minecraft .client .Minecraft ;
22
24
23
25
public class AnalyticsClient {
24
26
25
- private String pingClientTable = "ClientTable" ;
26
- private String pingServerTable = "ServerTable" ;
27
- private final int HASHCOUNT = 5 ;
28
-
29
27
public boolean UploadModel (AnalyticsModel model ) throws Exception {
30
28
Gson g = new Gson ();
31
29
String json = g .toJson (model );
@@ -58,9 +56,9 @@ public String CreateClientStartupPing() {
58
56
Gson g = new Gson ();
59
57
60
58
AnalyticsModel am = new AnalyticsModel ();
61
- am .Table = this .pingClientTable ;
59
+ am .Table = ForgeAnalyticsConstants .pingClientTable ;
62
60
am .Properties = new HashMap <String , String >();
63
- am .PartitionKey = "PING" ;
61
+ am .PartitionKey = ForgeAnalyticsConstants . pingClientStartCommand ;
64
62
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
65
63
am .Properties .putAll (this .getCommonValues ());
66
64
@@ -80,9 +78,42 @@ public String CreateServerStartupPing() {
80
78
Gson g = new Gson ();
81
79
82
80
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 ;
84
115
am .Properties = new HashMap <String , String >();
85
- am .PartitionKey = "PING" ;
116
+ am .PartitionKey = ForgeAnalyticsConstants . pingServerStopCommand ;
86
117
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
87
118
am .Properties .putAll (this .getCommonValues ());
88
119
am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
@@ -104,18 +135,80 @@ public String CreateServerStartupPing() {
104
135
am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
105
136
am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer )server ).getPublic ()));
106
137
}
138
+
139
+
140
+ String json = g .toJson (am );
141
+ return json ;
142
+ }
107
143
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
+
108
162
String json = g .toJson (am );
109
163
return json ;
110
164
}
111
165
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
+
112
201
113
202
private Map <String , String > getCommonValues () {
114
203
Map <String , String > commonValues = new HashMap <String , String >();
115
204
String activeModListCount = Integer .toString (net .minecraftforge .fml .common .Loader .instance ().getActiveModList ().size ());
116
205
String modListCount = Integer .toString (net .minecraftforge .fml .common .Loader .instance ().getModList ().size ());
117
206
String modList = "" ;
118
207
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 );
119
212
commonValues .put ("MinecraftVersion" , Minecraft .getMinecraft ().getVersion ());
120
213
commonValues .put ("ForgeVersion" , ForgeVersion .getVersion ());
121
214
commonValues .put ("MCPVersion" , ForgeVersion .mcpVersion );
@@ -136,7 +229,7 @@ private Map<String, String> getCommonValues() {
136
229
public String Anonymize (String data ) throws NoSuchAlgorithmException {
137
230
MessageDigest sha256 = MessageDigest .getInstance ("SHA-256" );
138
231
byte [] dataBytes = data .getBytes ();
139
- for (int i =0 ; i < this .HASHCOUNT ; i ++) {
232
+ for (int i =0 ; i < ForgeAnalyticsConstants .HASHCOUNT ; i ++) {
140
233
dataBytes = sha256 .digest (dataBytes );
141
234
}
142
235
0 commit comments