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.magic;
|
21
|
|
22
|
import java.io.IOException;
|
23
|
import java.io.InputStream;
|
24
|
import java.net.HttpURLConnection;
|
25
|
import java.net.URL;
|
26
|
|
27
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
28
|
|
29
|
class RubikScoresDownloader implements Runnable
|
30
|
{
|
31
|
interface Receiver
|
32
|
{
|
33
|
void receive(String[][][][] scores);
|
34
|
}
|
35
|
|
36
|
static final int MAX_PLACES = 12;
|
37
|
|
38
|
private static final int DOWNLOAD = 0;
|
39
|
private static final int SUBMIT = 1;
|
40
|
private static final int IDLE = 2;
|
41
|
|
42
|
private static final String URL ="http://koltunski.pl/rubik/cgi-bin";
|
43
|
|
44
|
private final String[] hex = {
|
45
|
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
|
46
|
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
|
47
|
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
|
48
|
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
|
49
|
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
|
50
|
"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
|
51
|
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
|
52
|
"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
|
53
|
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
|
54
|
"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
|
55
|
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
|
56
|
"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
|
57
|
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
|
58
|
"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
|
59
|
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
|
60
|
"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
|
61
|
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
|
62
|
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
|
63
|
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
|
64
|
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
|
65
|
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
|
66
|
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
|
67
|
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
|
68
|
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
|
69
|
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
|
70
|
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
|
71
|
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
|
72
|
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
|
73
|
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
|
74
|
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
|
75
|
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
|
76
|
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
|
77
|
};
|
78
|
|
79
|
private static boolean mRunning = false;
|
80
|
private static int mMode = IDLE;
|
81
|
private static Receiver mReceiver;
|
82
|
|
83
|
private static String mScores = "";
|
84
|
private static String[][][][] mValues = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES][3];
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
|
88
|
private void fillValues()
|
89
|
{
|
90
|
int begin=-1 ,end, len = mScores.length();
|
91
|
|
92
|
while( begin<len )
|
93
|
{
|
94
|
end = mScores.indexOf('\n', begin+1);
|
95
|
if( end<0 ) end = len;
|
96
|
fillRow(mScores.substring(begin+1,end));
|
97
|
begin = end;
|
98
|
}
|
99
|
}
|
100
|
|
101
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102
|
|
103
|
private void fillRow(String row)
|
104
|
{
|
105
|
int s1 = row.indexOf(' ');
|
106
|
int s2 = row.indexOf(' ',s1+1);
|
107
|
int s3 = row.indexOf(' ',s2+1);
|
108
|
int s4 = row.indexOf(' ',s3+1);
|
109
|
int s5 = row.indexOf(' ',s4+1);
|
110
|
int s6 = row.length();
|
111
|
|
112
|
if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
|
113
|
{
|
114
|
int size = Integer.valueOf( row.substring(0,s1) );
|
115
|
|
116
|
if( size>=0 && size<RubikSize.LENGTH )
|
117
|
{
|
118
|
int level = Integer.valueOf( row.substring(s1+1,s2) );
|
119
|
int place = Integer.valueOf( row.substring(s2+1,s3) );
|
120
|
String name = row.substring(s3+1, s4);
|
121
|
int time = Integer.valueOf( row.substring(s4+1,s5) );
|
122
|
String country = row.substring(s5+1, s6);
|
123
|
String realTime= String.valueOf(time/10.0f);
|
124
|
|
125
|
if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
|
126
|
{
|
127
|
mValues[size][level][place][0] = country;
|
128
|
mValues[size][level][place][1] = name;
|
129
|
mValues[size][level][place][2] = realTime;
|
130
|
}
|
131
|
else
|
132
|
{
|
133
|
android.util.Log.e("fillRow", "row invalid: level or place invalid: "+row);
|
134
|
}
|
135
|
}
|
136
|
else
|
137
|
{
|
138
|
android.util.Log.e("fillRow", "row invalid: size invalid: "+row);
|
139
|
}
|
140
|
}
|
141
|
else
|
142
|
{
|
143
|
android.util.Log.e("fillRow", "row invalid: "+row);
|
144
|
}
|
145
|
}
|
146
|
|
147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148
|
|
149
|
private String URLencode(String s)
|
150
|
{
|
151
|
StringBuilder sbuf = new StringBuilder();
|
152
|
int len = s.length();
|
153
|
|
154
|
for (int i = 0; i < len; i++)
|
155
|
{
|
156
|
int ch = s.charAt(i);
|
157
|
|
158
|
if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
|
159
|
else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
|
160
|
else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
|
161
|
else if (ch == ' ' ) sbuf.append('+');
|
162
|
else if (ch == '-' || ch == '_'
|
163
|
|| ch == '.' || ch == '!'
|
164
|
|| ch == '~' || ch == '*'
|
165
|
|| ch == '\'' || ch == '('
|
166
|
|| ch == ')' ) sbuf.append((char)ch);
|
167
|
else if (ch <= 0x007f) sbuf.append(hex[ch]);
|
168
|
else if (ch <= 0x07FF)
|
169
|
{
|
170
|
sbuf.append(hex[0xc0 | (ch >> 6)]);
|
171
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
172
|
}
|
173
|
else
|
174
|
{
|
175
|
sbuf.append(hex[0xe0 | (ch >> 12)]);
|
176
|
sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
|
177
|
sbuf.append(hex[0x80 | (ch & 0x3F)]);
|
178
|
}
|
179
|
}
|
180
|
|
181
|
return sbuf.toString();
|
182
|
}
|
183
|
|
184
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185
|
|
186
|
private void doDownload()
|
187
|
{
|
188
|
String veri = "distored";
|
189
|
String name = URLencode(veri);
|
190
|
int numRuns = 1;
|
191
|
String version = R.string.app_version+"d";
|
192
|
String message=URL+"/download.cgi?n="+name+"&r="+numRuns+"&e="+version;
|
193
|
|
194
|
try
|
195
|
{
|
196
|
java.net.URL connectURL = new URL(message);
|
197
|
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
|
198
|
|
199
|
conn.setDoInput(true);
|
200
|
conn.setDoOutput(true);
|
201
|
conn.setUseCaches(false);
|
202
|
conn.setRequestMethod("GET");
|
203
|
conn.connect();
|
204
|
conn.getOutputStream().flush();
|
205
|
|
206
|
InputStream is = null;
|
207
|
|
208
|
try
|
209
|
{
|
210
|
is = conn.getInputStream();
|
211
|
int ch;
|
212
|
StringBuilder sb = new StringBuilder();
|
213
|
while( ( ch = is.read() ) != -1 )
|
214
|
{
|
215
|
sb.append( (char)ch );
|
216
|
}
|
217
|
mScores = sb.toString();
|
218
|
}
|
219
|
catch(Exception e)
|
220
|
{
|
221
|
android.util.Log.e("downloader", "biffed it getting HTTPResponse");
|
222
|
}
|
223
|
finally
|
224
|
{
|
225
|
try
|
226
|
{
|
227
|
if (is != null)
|
228
|
is.close();
|
229
|
}
|
230
|
catch (Exception e) {}
|
231
|
}
|
232
|
}
|
233
|
catch( IOException ioe )
|
234
|
{
|
235
|
android.util.Log.e("downloader", "IOException: "+ioe.getMessage());
|
236
|
}
|
237
|
catch( SecurityException se )
|
238
|
{
|
239
|
android.util.Log.e("downloader", "SecurityException: "+se.getMessage());
|
240
|
}
|
241
|
|
242
|
/*
|
243
|
mScores =
|
244
|
|
245
|
"0 0 0 INEED7X7X7 1 rus" + "\n" +
|
246
|
"0 0 1 Tilly 1 uk" + "\n" +
|
247
|
"0 0 2 ARNOLD 1 rus" + "\n" +
|
248
|
"0 0 3 ALBERTO 1 mex" + "\n" +
|
249
|
"0 0 4 TO 1 ger" + "\n" +
|
250
|
"0 1 0 INEED7X7X7 1 rus" + "\n" +
|
251
|
"0 1 1 Tilly 1 uk" + "\n" +
|
252
|
"0 1 2 ALBERTO 1 mex" + "\n" +
|
253
|
"0 1 3 RINAT 1 rus" + "\n" +
|
254
|
"0 1 4 BOOW 1 tha" + "\n" +
|
255
|
"0 2 0 Neriel 3 bra" + "\n" +
|
256
|
"0 2 1 NPPN 3 vie" + "\n" +
|
257
|
"0 2 2 FREDRIK 4 den" + "\n" +
|
258
|
"0 2 3 OHIOCUBER 4 usa" + "\n" +
|
259
|
"0 2 4 S13 4 usa" + "\n" +
|
260
|
"0 3 0 NIKITOS 8 rus" + "\n" +
|
261
|
"0 3 1 THISNAVIS 8 usa" + "\n" +
|
262
|
"0 3 2 MDR04 9 ita" + "\n" +
|
263
|
"0 3 3 ROBERT04 9 unk" + "\n" +
|
264
|
"0 3 4 DULCE 10 mex" + "\n" +
|
265
|
"1 0 0 INEED7X7X7 1 rus" + "\n" +
|
266
|
"1 0 1 Tilly 1 uk" + "\n" +
|
267
|
"1 0 2 REBECCA 1 fra" + "\n" +
|
268
|
"1 0 3 PRAKOBKRIT 1 tha" + "\n" +
|
269
|
"1 0 4 E 1 mex" + "\n" +
|
270
|
"1 1 0 BAYLEY 3 usa" + "\n" +
|
271
|
"1 1 1 DEMONAIRE 3 uk" + "\n" +
|
272
|
"1 1 2 UMMWT 3 ina" + "\n" +
|
273
|
"1 1 3 MDR04 4 ita" + "\n" +
|
274
|
"1 1 4 FREDRIK 4 den" + "\n" +
|
275
|
"1 2 0 HAGER 5 egy" + "\n" +
|
276
|
"1 2 1 S13 7 usa" + "\n" +
|
277
|
"1 2 2 UTEK 7 ina" + "\n" +
|
278
|
"1 2 3 UMMWT 7 ina" + "\n" +
|
279
|
"1 2 4 210JITQU 7 ukr" + "\n" +
|
280
|
"1 3 0 UTEK 16 ina" + "\n" +
|
281
|
"1 3 1 LOLO766 21 fra" + "\n" +
|
282
|
"1 3 2 NIKITOS 21 rus" + "\n" +
|
283
|
"1 3 3 PYCUK1707 21 rus" + "\n" +
|
284
|
"1 3 4 KEDAR 22 ind" + "\n" +
|
285
|
"2 0 0 INEED7X7X7 1 rus" + "\n" +
|
286
|
"2 0 1 HOMER0815 1 ger" + "\n" +
|
287
|
"2 0 2 EIP 1 usa" + "\n" +
|
288
|
"2 0 3 THEBIGBOSS 1 fra" + "\n" +
|
289
|
"2 0 4 PHONG 1 vie" + "\n" +
|
290
|
"2 1 0 SATERNSPY9 3 unk" + "\n" +
|
291
|
"2 1 1 S13 4 usa" + "\n" +
|
292
|
"2 1 2 ALSOELAN 4 usa" + "\n" +
|
293
|
"2 1 3 NIKITOS 4 rus" + "\n" +
|
294
|
"2 1 4 SEPIA6MIRA 4 unk" + "\n" +
|
295
|
"2 2 0 STEVE123 9 irl" + "\n" +
|
296
|
"2 2 1 NIKITOS 9 rus" + "\n" +
|
297
|
"2 2 2 SEPIA6MIRA 9 unk" + "\n" +
|
298
|
"2 2 3 INEED7X7X7 10 rus" + "\n" +
|
299
|
"2 2 4 S13 10 usa" + "\n" +
|
300
|
"2 3 0 NIKITOS 22 rus" + "\n" +
|
301
|
"2 3 1 RUBIK123 24 lit" + "\n" +
|
302
|
"2 3 2 NONAME 24 fin" + "\n" +
|
303
|
"2 3 3 ABB0 26 ind" + "\n" +
|
304
|
"2 3 4 NOYS 27 mex" + "\n" +
|
305
|
"3 0 0 JARO 1 ger" + "\n" +
|
306
|
"3 0 1 INEED7X7X7 1 rus" + "\n" +
|
307
|
"3 0 2 MDR04 1 ita" + "\n" +
|
308
|
"3 0 3 SAMW 1 uk" + "\n" +
|
309
|
"3 0 4 MOKAI88 1 den" + "\n" +
|
310
|
"3 1 0 MDR04G 3 ita" + "\n" +
|
311
|
"3 1 1 INEED7X7X7 4 rus" + "\n" +
|
312
|
"3 1 2 S13 4 usa" + "\n" +
|
313
|
"3 1 3 KARAVAN 4 rus" + "\n" +
|
314
|
"3 1 4 AWSOME897 4 usa" + "\n" +
|
315
|
"3 2 0 INEED7X7X7 8 rus" + "\n" +
|
316
|
"3 2 1 KARAVAN 8 rus" + "\n" +
|
317
|
"3 2 2 S13 9 usa" + "\n" +
|
318
|
"3 2 3 ALSOELAN 9 usa" + "\n" +
|
319
|
"3 2 4 ROBERT04 9 unk" + "\n" +
|
320
|
"3 3 0 NIKITOS 23 rus" + "\n" +
|
321
|
"3 3 1 THISNAVIS 23 usa" + "\n" +
|
322
|
"3 3 2 INEED7X7X7 24 rus" + "\n" +
|
323
|
"3 3 3 RUBIK123 30 lit" + "\n" +
|
324
|
"3 3 4 SKY16 31 usa";
|
325
|
|
326
|
*/
|
327
|
}
|
328
|
|
329
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
330
|
|
331
|
private boolean gottaDownload()
|
332
|
{
|
333
|
return ((mScores.length()==0) && !mRunning);
|
334
|
}
|
335
|
|
336
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
337
|
|
338
|
static void onPause()
|
339
|
{
|
340
|
mRunning = false;
|
341
|
mScores = "";
|
342
|
}
|
343
|
|
344
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
345
|
|
346
|
void download(Receiver receiver)
|
347
|
{
|
348
|
mReceiver = receiver;
|
349
|
mMode = DOWNLOAD;
|
350
|
Thread networkThrd = new Thread(this);
|
351
|
networkThrd.start();
|
352
|
}
|
353
|
|
354
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355
|
|
356
|
public void run()
|
357
|
{
|
358
|
try
|
359
|
{
|
360
|
if( gottaDownload() )
|
361
|
{
|
362
|
mRunning = true;
|
363
|
doDownload();
|
364
|
fillValues();
|
365
|
}
|
366
|
}
|
367
|
catch( Exception e )
|
368
|
{
|
369
|
android.util.Log.e("downloader", "Exception downloading records: "+e.getMessage() );
|
370
|
}
|
371
|
|
372
|
mRunning = false;
|
373
|
mReceiver.receive(mValues);
|
374
|
}
|
375
|
}
|