1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.scores;
|
21
|
|
22
|
import org.distorted.object.RubikObjectList;
|
23
|
|
24
|
import java.io.InputStream;
|
25
|
import java.net.HttpURLConnection;
|
26
|
import java.net.URL;
|
27
|
import java.net.UnknownHostException;
|
28
|
|
29
|
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
public class RubikScoresDownloader implements Runnable
|
34
|
{
|
35
|
public interface Receiver
|
36
|
{
|
37
|
void receive(String[][][] country, String[][][] name, float[][][] time);
|
38
|
void exception(String exception);
|
39
|
}
|
40
|
|
41
|
public static final int MAX_PLACES = 10;
|
42
|
|
43
|
private static final int DOWNLOAD = 0;
|
44
|
private static final int SUBMIT = 1;
|
45
|
private static final int IDLE = 2;
|
46
|
|
47
|
private final String[] hex = {
|
48
|
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
|
49
|
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
|
50
|
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
|
51
|
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
|
52
|
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
|
53
|
"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
|
54
|
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
|
55
|
"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
|
56
|
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
|
57
|
"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
|
58
|
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
|
59
|
"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
|
60
|
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
|
61
|
"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
|
62
|
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
|
63
|
"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
|
64
|
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
|
65
|
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
|
66
|
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
|
67
|
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
|
68
|
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
|
69
|
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
|
70
|
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
|
71
|
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
|
72
|
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
|
73
|
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
|
74
|
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
|
75
|
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
|
76
|
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
|
77
|
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
|
78
|
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
|
79
|
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
|
80
|
};
|
81
|
|
82
|
private static boolean mRunning = false;
|
83
|
private static int mMode = IDLE;
|
84
|
private static Receiver mReceiver;
|
85
|
private static String mUserName, mVersion;
|
86
|
private static int mNumRuns;
|
87
|
|
88
|
private static int mTotal = RubikObjectList.getTotal();
|
89
|
private static String mScores = "";
|
90
|
private static String[][][] mCountry = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
|
91
|
private static String[][][] mName = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
|
92
|
private static float[][][] mTime = new float[mTotal][MAX_SCRAMBLE][MAX_PLACES];
|
93
|
|
94
|
private static int[][] mPlaces = new int[mTotal][MAX_SCRAMBLE];
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
private void fillValues()
|
99
|
{
|
100
|
int begin=-1 ,end, len = mScores.length();
|
101
|
|
102
|
for(int i=0; i<mTotal; i++)
|
103
|
for(int j=0; j<MAX_SCRAMBLE; j++)
|
104
|
{
|
105
|
mPlaces[i][j] = 0;
|
106
|
}
|
107
|
|
108
|
while( begin<len )
|
109
|
{
|
110
|
end = mScores.indexOf('\n', begin+1);
|
111
|
if( end<0 ) end = len;
|
112
|
|
113
|
try
|
114
|
{
|
115
|
fillRow(mScores.substring(begin+1,end));
|
116
|
}
|
117
|
catch(Exception ex)
|
118
|
{
|
119
|
// faulty row - ignore
|
120
|
}
|
121
|
|
122
|
begin = end;
|
123
|
}
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
private void fillRow(String row)
|
129
|
{
|
130
|
int s1 = row.indexOf(' ');
|
131
|
int s2 = row.indexOf(' ',s1+1);
|
132
|
int s3 = row.indexOf(' ',s2+1);
|
133
|
int s4 = row.indexOf(' ',s3+1);
|
134
|
int s5 = row.length();
|
135
|
|
136
|
if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
|
137
|
{
|
138
|
int object = RubikObjectList.unpackObjectFromString( row.substring(0,s1) );
|
139
|
|
140
|
if( object>=0 && object<mTotal )
|
141
|
{
|
142
|
int level = Integer.parseInt( row.substring(s1+1,s2) );
|
143
|
String name = row.substring(s2+1, s3);
|
144
|
int time = Integer.parseInt( row.substring(s3+1,s4) );
|
145
|
String country = row.substring(s4+1, s5);
|
146
|
|
147
|
if(level>0 && level<=MAX_SCRAMBLE)
|
148
|
{
|
149
|
int p = mPlaces[object][level];
|
150
|
mPlaces[object][level]++;
|
151
|
|
152
|
mCountry[object][level-1][p] = country;
|
153
|
mName [object][level-1][p] = name;
|
154
|
mTime [object][level-1][p] = ((float)(time/100))/10.0f;
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
}
|
159
|
|
160
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
161
|
|
162
|
private String URLencode(String s)
|
163
|
{
|
164
|
StringBuilder sbuf = new StringBuilder();
|
165
|
int len = s.length();
|
166
|
|
167
|
for (int i = 0; i < len; i++)
|
168
|
{
|
169
|
int ch = s.charAt(i);
|
170
|
|
171
|
if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
|
172
|
else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
|
173
|
else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
|
174
|
else if (ch == ' ' ) sbuf.append('+');
|
175
|
else if (ch == '-' || ch == '_'
|
176
|
|| ch == '.' || ch == '!'
|
177
|
|| ch == '~' || ch == '*'
|
178
|
|| ch == '\'' || ch == '('
|
179
|
|| ch == ')' ) sbuf.append((char)ch);
|
180
|
else if (ch <= 0x007f) sbuf.append(hex[ch]);
|
181
|
else if (ch <= 0x07FF)
|
182
|
{
|
183
|
sbuf.append(hex[0xc0 | (ch >> 6)]);
|
184
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
185
|
}
|
186
|
else
|
187
|
{
|
188
|
sbuf.append(hex[0xe0 | (ch >> 12)]);
|
189
|
sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
|
190
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
191
|
}
|
192
|
}
|
193
|
|
194
|
return sbuf.toString();
|
195
|
}
|
196
|
|
197
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
198
|
|
199
|
private boolean doDownload()
|
200
|
{
|
201
|
String message="https://distorted.org/magic/cgi-bin/download.cgi";
|
202
|
message += "?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+mVersion+"d";
|
203
|
message += "&o="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&l="+MAX_PLACES;
|
204
|
|
205
|
try
|
206
|
{
|
207
|
java.net.URL connectURL = new URL(message);
|
208
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
209
|
|
210
|
conn.setDoInput(true);
|
211
|
conn.setDoOutput(true);
|
212
|
conn.setUseCaches(false);
|
213
|
conn.setRequestMethod("GET");
|
214
|
conn.connect();
|
215
|
conn.getOutputStream().flush();
|
216
|
|
217
|
try( InputStream is = conn.getInputStream() )
|
218
|
{
|
219
|
int ch;
|
220
|
StringBuilder sb = new StringBuilder();
|
221
|
while( ( ch = is.read() ) != -1 )
|
222
|
{
|
223
|
sb.append( (char)ch );
|
224
|
}
|
225
|
mScores = sb.toString();
|
226
|
}
|
227
|
catch( final Exception e)
|
228
|
{
|
229
|
mReceiver.exception("Failed to get an answer from the High Scores server");
|
230
|
return false;
|
231
|
}
|
232
|
}
|
233
|
catch( final UnknownHostException e )
|
234
|
{
|
235
|
mReceiver.exception("No access to Internet");
|
236
|
return false;
|
237
|
}
|
238
|
catch( final SecurityException e )
|
239
|
{
|
240
|
mReceiver.exception("Application not authorized to connect to the Internet");
|
241
|
return false;
|
242
|
}
|
243
|
catch( final Exception e )
|
244
|
{
|
245
|
mReceiver.exception(e.getMessage());
|
246
|
return false;
|
247
|
}
|
248
|
|
249
|
if( mScores.length()==0 )
|
250
|
{
|
251
|
mReceiver.exception("Failed to download scores");
|
252
|
return false;
|
253
|
}
|
254
|
|
255
|
return true;
|
256
|
}
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
|
260
|
private boolean gottaDownload()
|
261
|
{
|
262
|
return ((mScores.length()==0) && !mRunning);
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
@Override
|
268
|
public void run()
|
269
|
{
|
270
|
boolean success=true;
|
271
|
|
272
|
try
|
273
|
{
|
274
|
if( gottaDownload() )
|
275
|
{
|
276
|
mRunning = true;
|
277
|
success = doDownload();
|
278
|
}
|
279
|
}
|
280
|
catch( Exception e )
|
281
|
{
|
282
|
mReceiver.exception("Exception downloading records: "+e.getMessage() );
|
283
|
}
|
284
|
|
285
|
fillValues();
|
286
|
|
287
|
mRunning = false;
|
288
|
|
289
|
if( success )
|
290
|
{
|
291
|
mReceiver.receive(mCountry, mName, mTime);
|
292
|
}
|
293
|
}
|
294
|
|
295
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
296
|
// PUBLIC API
|
297
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
298
|
|
299
|
public static void onPause()
|
300
|
{
|
301
|
mRunning = false;
|
302
|
}
|
303
|
|
304
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
305
|
|
306
|
public void download(Receiver receiver, String userName, String version, int numRuns)
|
307
|
{
|
308
|
mReceiver = receiver;
|
309
|
mMode = DOWNLOAD;
|
310
|
mUserName = userName;
|
311
|
mVersion = version;
|
312
|
mNumRuns = numRuns;
|
313
|
|
314
|
Thread networkThrd = new Thread(this);
|
315
|
networkThrd.start();
|
316
|
}
|
317
|
}
|