Project

General

Profile

Download (15.4 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / scores / RubikScoresDownloader.java @ 6570171b

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 android.content.pm.PackageInfo;
23
import android.content.pm.PackageManager;
24

    
25
import androidx.fragment.app.FragmentActivity;
26

    
27
import org.distorted.objects.ObjectList;
28

    
29
import java.io.InputStream;
30
import java.net.HttpURLConnection;
31
import java.net.URL;
32
import java.net.UnknownHostException;
33
import java.security.MessageDigest;
34
import java.security.NoSuchAlgorithmException;
35

    
36
import static org.distorted.objects.ObjectList.MAX_LEVEL;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikScoresDownloader implements Runnable
41
  {
42
  public interface Receiver
43
    {
44
    void receive(String[][][] country, String[][][] name, float[][][] time);
45
    void message(String mess);
46
    void error(String error);
47
    }
48

    
49
  public static final int MAX_PLACES = 10;
50

    
51
  private static final int DOWNLOAD   = 0;
52
  private static final int SUBMIT     = 1;
53
  private static final int IDLE       = 2;
54

    
55
  private final String[] hex = {
56
    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
57
    "%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f",
58
    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
59
    "%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f",
60
    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
61
    "%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f",
62
    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
63
    "%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f",
64
    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
65
    "%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f",
66
    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
67
    "%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f",
68
    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
69
    "%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f",
70
    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
71
    "%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f",
72
    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
73
    "%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f",
74
    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
75
    "%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f",
76
    "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7",
77
    "%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af",
78
    "%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7",
79
    "%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf",
80
    "%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7",
81
    "%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf",
82
    "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
83
    "%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df",
84
    "%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7",
85
    "%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef",
86
    "%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7",
87
    "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff"
88
    };
89

    
90
  private static boolean mRunning = false;
91
  private static int mMode = IDLE;
92
  private static Receiver mReceiver;
93
  private static String mVersion;
94

    
95
  private static int mTotal = ObjectList.getTotal();
96
  private static String mScores = "";
97
  private static String[][][] mCountry = new String[mTotal][MAX_LEVEL][MAX_PLACES];
98
  private static String[][][] mName    = new String[mTotal][MAX_LEVEL][MAX_PLACES];
99
  private static  float[][][] mTime    = new  float[mTotal][MAX_LEVEL][MAX_PLACES];
100

    
101
  private static int[][] mPlaces = new int[mTotal][MAX_LEVEL];
102
  private static RubikScoresDownloader mThis;
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  private static String computeHash(String stringToHash, byte[] salt)
107
    {
108
    String generatedPassword;
109

    
110
    try
111
      {
112
      MessageDigest md = MessageDigest.getInstance("MD5");
113
      md.update(salt);
114
      byte[] bytes = md.digest(stringToHash.getBytes());
115
      StringBuilder sb = new StringBuilder();
116

    
117
      for (byte aByte : bytes)
118
        {
119
        sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
120
        }
121

    
122
      generatedPassword = sb.toString();
123
      }
124
    catch (NoSuchAlgorithmException e)
125
      {
126
      return "NoSuchAlgorithm";
127
      }
128

    
129
    return generatedPassword;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private boolean fillValues()
135
    {
136
    int begin=-1 ,end, len = mScores.length();
137
    String row;
138

    
139
    if( len==0 )
140
      {
141
      mReceiver.error("1");
142
      return false;
143
      }
144
    else if( len<=2 )
145
      {
146
      mReceiver.error(mScores);
147
      return false;
148
      }
149

    
150
    for(int i=0; i<mTotal; i++)
151
      for(int j=0; j<MAX_LEVEL; j++)
152
        {
153
        mPlaces[i][j] = 0;
154
        }
155

    
156
    while( begin<len )
157
      {
158
      end = mScores.indexOf('\n', begin+1);
159
      if( end<0 ) end = len;
160

    
161
      try
162
        {
163
        row = mScores.substring(begin+1,end);
164
        fillRow(row);
165
        }
166
      catch(Exception ex)
167
        {
168
        // faulty row - ignore
169
        }
170

    
171
      begin = end;
172
      }
173

    
174
    return true;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void fillRow(String row)
180
    {
181
    int s1 = row.indexOf(' ');
182
    int s2 = row.indexOf(' ',s1+1);
183
    int s3 = row.indexOf(' ',s2+1);
184
    int s4 = row.indexOf(' ',s3+1);
185
    int s5 = row.length();
186

    
187
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
188
      {
189
      int object = ObjectList.unpackObjectFromString( row.substring(0,s1) );
190

    
191
      if( object>=0 && object<mTotal )
192
        {
193
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
194
        String name    = row.substring(s2+1, s3);
195
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
196
        String country = row.substring(s4+1, s5);
197

    
198
        if(level>=0 && level<MAX_LEVEL)
199
          {
200
          int p = mPlaces[object][level];
201
          mPlaces[object][level]++;
202

    
203
          mCountry[object][level][p] = country;
204
          mName   [object][level][p] = name;
205
          mTime   [object][level][p] = ((float)(time/10))/100.0f;
206
          }
207
        }
208
      }
209
    else
210
      {
211
      tryDoCommand(row);
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  private void tryDoCommand(String row)
218
    {
219
    if( row.startsWith("comm") )
220
      {
221
      int colon = row.indexOf(':');
222

    
223
      if( colon>0 )
224
        {
225
        String commandNumber = row.substring(4,colon);
226
        int number;
227

    
228
        try
229
          {
230
          number = Integer.parseInt(commandNumber);
231
          }
232
        catch(NumberFormatException ex)
233
          {
234
          number=0;
235
          }
236

    
237
        if(number==1)
238
          {
239
          String country = row.substring(colon+1);
240
          RubikScores scores = RubikScores.getInstance();
241
          scores.setCountry(country);
242
          }
243
        }
244
      }
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  private String URLencode(String s)
250
    {
251
    StringBuilder sbuf = new StringBuilder();
252
    int len = s.length();
253

    
254
    for (int i = 0; i < len; i++)
255
      {
256
      int ch = s.charAt(i);
257

    
258
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
259
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
260
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
261
      else if (ch == ' '             ) sbuf.append('+');
262
      else if (ch == '-' || ch == '_'
263
            || ch == '.' || ch == '!'
264
            || ch == '~' || ch == '*'
265
            || ch == '\'' || ch == '('
266
            || ch == ')'             ) sbuf.append((char)ch);
267
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
268
      else if (ch <= 0x07FF)
269
        {
270
        sbuf.append(hex[0xc0 | (ch >> 6)]);
271
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
272
        }
273
      else
274
        {
275
        sbuf.append(hex[0xe0 | (ch >> 12)]);
276
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
277
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
278
        }
279
      }
280

    
281
    return sbuf.toString();
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  private boolean network(String url)
287
    {
288
    try
289
      {
290
      java.net.URL connectURL = new URL(url);
291
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
292

    
293
      conn.setDoInput(true);
294
      conn.setDoOutput(true);
295
      conn.setUseCaches(false);
296
      conn.setRequestMethod("GET");
297
      conn.connect();
298
      conn.getOutputStream().flush();
299

    
300
      try( InputStream is = conn.getInputStream() )
301
        {
302
        int ch;
303
        StringBuilder sb = new StringBuilder();
304
        while( ( ch = is.read() ) != -1 )
305
          {
306
          sb.append( (char)ch );
307
          }
308
        mScores = sb.toString();
309
        }
310
      catch( final Exception e)
311
        {
312
        mReceiver.message("Failed to get an answer from the High Scores server");
313
        return false;
314
        }
315
      }
316
    catch( final UnknownHostException e )
317
      {
318
      mReceiver.message("No access to Internet");
319
      return false;
320
      }
321
    catch( final SecurityException e )
322
      {
323
      mReceiver.message("Application not authorized to connect to the Internet");
324
      return false;
325
      }
326
    catch( final Exception e )
327
      {
328
      mReceiver.message(e.getMessage());
329
      return false;
330
      }
331

    
332
    if( mScores.length()==0 )
333
      {
334
      mReceiver.message("Failed to download scores");
335
      return false;
336
      }
337

    
338
    return true;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  private String constructDownloadURL()
344
    {
345
    RubikScores scores = RubikScores.getInstance();
346
    String name = URLencode(scores.getName());
347
    String veri = scores.isVerified() ? name : "";
348
    int numRuns = scores.getNumRuns();
349
    int numPlay = scores.getNumPlays();
350
    String country = scores.getCountry();
351

    
352
    String url="https://distorted.org/magic/cgi-bin/download.cgi";
353
    url += "?n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d";
354
    url += "&o="+ ObjectList.getObjectList()+"&min=0&max="+MAX_LEVEL+"&l="+MAX_PLACES;
355

    
356
    return url;
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  private String constructSubmitURL()
362
    {
363
    RubikScores scores = RubikScores.getInstance();
364
    String name = URLencode(scores.getName());
365
    String veri = scores.isVerified() ? name : "";
366
    int numRuns = scores.getNumRuns();
367
    int numPlay = scores.getNumPlays();
368
    int deviceID= scores.getDeviceID();
369
    String reclist = scores.getRecordList("&o=","&l=","&t=");
370
    String country = scores.getCountry();
371
    long epoch = System.currentTimeMillis();
372
    String salt = "cuboid";
373

    
374
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
375
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
376
    url2 += reclist+"&c="+country+"&f="+epoch+"&oo="+ ObjectList.getObjectList();
377
    url2 += "&min=0&max="+MAX_LEVEL+"&lo="+MAX_PLACES+"&h="+computeHash( url2, salt.getBytes() );
378

    
379
    return url1 + "?" + url2;
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  private boolean gottaDownload()
385
    {
386
    return ((mScores.length()==0) && !mRunning);
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  @Override
392
  public void run()
393
    {
394
    boolean success=true;
395

    
396
    try
397
      {
398
      if( mMode==DOWNLOAD && gottaDownload() )
399
        {
400
        mRunning = true;
401
        success = network(constructDownloadURL());
402
        }
403
      if( mMode==SUBMIT )
404
        {
405
        mRunning = true;
406

    
407
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
408
          {
409
          success = network(constructSubmitURL());
410
          }
411
        }
412
      }
413
    catch( Exception e )
414
      {
415
      mReceiver.message("Exception downloading records: "+e.getMessage() );
416
      }
417

    
418
    if( mRunning )
419
      {
420
      success = fillValues();
421
      mRunning = false;
422
      }
423

    
424
    if( success )
425
      {
426
      mReceiver.receive(mCountry, mName, mTime);
427

    
428
      if( mMode==SUBMIT )
429
        {
430
        RubikScores.getInstance().successfulSubmit();
431
        }
432
      }
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  private RubikScoresDownloader()
438
    {
439

    
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
// PUBLIC API
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public static void onPause()
447
    {
448
    mRunning = false;
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public static RubikScoresDownloader getInstance()
454
    {
455
    if( mThis==null )
456
      {
457
      mThis = new RubikScoresDownloader();
458
      }
459

    
460
    return mThis;
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  private void start(Receiver receiver, FragmentActivity act, int mode)
466
    {
467
    mReceiver = receiver;
468
    mMode     = mode;
469

    
470
    try
471
      {
472
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
473
      mVersion = pInfo.versionName;
474
      }
475
    catch (PackageManager.NameNotFoundException e)
476
      {
477
      mVersion = "0.9.2";
478
      }
479

    
480
    Thread networkThrd = new Thread(this);
481
    networkThrd.start();
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  public void download(Receiver receiver, FragmentActivity act)
487
    {
488
    start(receiver, act, DOWNLOAD);
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  public void submit(Receiver receiver, FragmentActivity act)
494
    {
495
    start(receiver, act, SUBMIT);
496
    }
497
}
(2-2/2)