Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScoresDownloader.java @ fb377dae

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.RubikObjectList;
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.RubikObjectList.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 = RubikObjectList.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 = RubikObjectList.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
    //android.util.Log.e("down", "url: "+url);
289

    
290
    try
291
      {
292
      java.net.URL connectURL = new URL(url);
293
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
294

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

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

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

    
340
    return true;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

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

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

    
358
    return url;
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
  private String constructSubmitURL()
364
    {
365
    RubikScores scores = RubikScores.getInstance();
366
    String name = URLencode(scores.getName());
367
    String veri = scores.isVerified() ? name : "";
368
    int numRuns = scores.getNumRuns();
369
    int numPlay = scores.getNumPlays();
370
    int deviceID= scores.getDeviceID();
371
    String objlist = scores.getUnsubmittedObjlist();
372
    String lvllist = scores.getUnsubmittedLevellist();
373
    String timlist = scores.getUnsubmittedTimelist();
374
    String country = scores.getCountry();
375
    long epoch = System.currentTimeMillis();
376
    String salt = "cuboid";
377

    
378
    String url1="https://distorted.org/magic/cgi-bin/submit.cgi";
379
    String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion+"d";
380
    url2 += "&o="+objlist+"&l="+lvllist+"&t="+timlist+"&c="+country+"&f="+epoch;
381
    url2 += "&oo="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_LEVEL+"&lo="+MAX_PLACES;
382
    url2 += "&h="+computeHash( url2, salt.getBytes() );
383

    
384
    return url1 + "?" + url2;
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  private boolean gottaDownload()
390
    {
391
    return ((mScores.length()==0) && !mRunning);
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

    
396
  @Override
397
  public void run()
398
    {
399
    boolean success=true;
400

    
401
    try
402
      {
403
      if( mMode==DOWNLOAD && gottaDownload() )
404
        {
405
        mRunning = true;
406
        success = network(constructDownloadURL());
407
        }
408
      if( mMode==SUBMIT )
409
        {
410
        mRunning = true;
411

    
412
        if( RubikScores.getInstance().thereAreUnsubmittedRecords() )
413
          {
414
          success = network(constructSubmitURL());
415
          }
416
        }
417
      }
418
    catch( Exception e )
419
      {
420
      mReceiver.message("Exception downloading records: "+e.getMessage() );
421
      }
422

    
423
    if( mRunning )
424
      {
425
      success = fillValues();
426
      mRunning = false;
427
      }
428

    
429
    if( success )
430
      {
431
      mReceiver.receive(mCountry, mName, mTime);
432

    
433
      if( mMode==SUBMIT )
434
        {
435
        RubikScores.getInstance().successfulSubmit();
436
        }
437
      }
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  private RubikScoresDownloader()
443
    {
444

    
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448
// PUBLIC API
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public static void onPause()
452
    {
453
    mRunning = false;
454
    }
455

    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457

    
458
  public static RubikScoresDownloader getInstance()
459
    {
460
    if( mThis==null )
461
      {
462
      mThis = new RubikScoresDownloader();
463
      }
464

    
465
    return mThis;
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  private void start(Receiver receiver, FragmentActivity act, int mode)
471
    {
472
    mReceiver = receiver;
473
    mMode     = mode;
474

    
475
    try
476
      {
477
      PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0);
478
      mVersion = pInfo.versionName;
479
      }
480
    catch (PackageManager.NameNotFoundException e)
481
      {
482
      mVersion = "1.1.2";
483
      }
484

    
485
    Thread networkThrd = new Thread(this);
486
    networkThrd.start();
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
  public void download(Receiver receiver, FragmentActivity act)
492
    {
493
    start(receiver, act, DOWNLOAD);
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public void submit(Receiver receiver, FragmentActivity act)
499
    {
500
    start(receiver, act, SUBMIT);
501
    }
502
}
(2-2/2)