Project

General

Profile

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

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

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f0e87514 Leszek Koltunski
package org.distorted.scores;
21 fdec60a3 Leszek Koltunski
22 4888e97c Leszek Koltunski
import org.distorted.object.RubikObjectList;
23
24 36e2cbdd Leszek Koltunski
import java.io.InputStream;
25
import java.net.HttpURLConnection;
26
import java.net.URL;
27 cc5ec229 Leszek Koltunski
import java.net.UnknownHostException;
28 36e2cbdd Leszek Koltunski
29 7aff6aa7 Leszek Koltunski
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
30 211b48f2 Leszek Koltunski
31 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 211b48f2 Leszek Koltunski
public class RubikScoresDownloader implements Runnable
34 fdec60a3 Leszek Koltunski
  {
35 211b48f2 Leszek Koltunski
  public interface Receiver
36 b8b38548 Leszek Koltunski
    {
37 1c90c64a Leszek Koltunski
    void receive(String[][][] country, String[][][] name, float[][][] time);
38 a675474f Leszek Koltunski
    void exception(String exception);
39 b8b38548 Leszek Koltunski
    }
40
41 286d73ae Leszek Koltunski
  public static final int MAX_PLACES = 10;
42 d8aa4ba8 Leszek Koltunski
43 b8b38548 Leszek Koltunski
  private static final int DOWNLOAD   = 0;
44
  private static final int SUBMIT     = 1;
45
  private static final int IDLE       = 2;
46
47 36e2cbdd Leszek Koltunski
  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 b8b38548 Leszek Koltunski
  private static boolean mRunning = false;
83
  private static int mMode = IDLE;
84
  private static Receiver mReceiver;
85 329c0aeb Leszek Koltunski
  private static String mUserName, mVersion;
86 6f8e5dfc Leszek Koltunski
  private static int mNumRuns;
87 b8b38548 Leszek Koltunski
88 4888e97c Leszek Koltunski
  private static int mTotal = RubikObjectList.getTotal();
89 f3e12931 Leszek Koltunski
  private static String mScores = "";
90 4888e97c Leszek Koltunski
  private static String[][][] mCountry = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
91
  private static String[][][] mName    = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
92 1c90c64a Leszek Koltunski
  private static  float[][][] mTime    = new  float[mTotal][MAX_SCRAMBLE][MAX_PLACES];
93 f3e12931 Leszek Koltunski
94 4888e97c Leszek Koltunski
  private static int[][] mPlaces = new int[mTotal][MAX_SCRAMBLE];
95 bc511ea5 Leszek Koltunski
  private static RubikScoresDownloader mThis;
96 329c0aeb Leszek Koltunski
97 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 d8aa4ba8 Leszek Koltunski
  private void fillValues()
100 f3e12931 Leszek Koltunski
    {
101 d8aa4ba8 Leszek Koltunski
    int begin=-1 ,end, len = mScores.length();
102 f3e12931 Leszek Koltunski
103 4888e97c Leszek Koltunski
    for(int i=0; i<mTotal; i++)
104 329c0aeb Leszek Koltunski
      for(int j=0; j<MAX_SCRAMBLE; j++)
105
        {
106
        mPlaces[i][j] = 0;
107
        }
108
109 d8aa4ba8 Leszek Koltunski
    while( begin<len )
110
      {
111
      end = mScores.indexOf('\n', begin+1);
112
      if( end<0 ) end = len;
113 286d73ae Leszek Koltunski
114
      try
115
        {
116
        fillRow(mScores.substring(begin+1,end));
117
        }
118
      catch(Exception ex)
119
        {
120
        // faulty row - ignore
121
        }
122
123 d8aa4ba8 Leszek Koltunski
      begin = end;
124
      }
125 f3e12931 Leszek Koltunski
    }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129 d8aa4ba8 Leszek Koltunski
  private void fillRow(String row)
130 f3e12931 Leszek Koltunski
    {
131 d8aa4ba8 Leszek Koltunski
    int s1 = row.indexOf(' ');
132
    int s2 = row.indexOf(' ',s1+1);
133
    int s3 = row.indexOf(' ',s2+1);
134
    int s4 = row.indexOf(' ',s3+1);
135 286d73ae Leszek Koltunski
    int s5 = row.length();
136 d8aa4ba8 Leszek Koltunski
137
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
138
      {
139 286d73ae Leszek Koltunski
      int object = RubikObjectList.unpackObjectFromString( row.substring(0,s1) );
140 b8b38548 Leszek Koltunski
141 286d73ae Leszek Koltunski
      if( object>=0 && object<mTotal )
142 d8aa4ba8 Leszek Koltunski
        {
143 4888e97c Leszek Koltunski
        int level      = Integer.parseInt( row.substring(s1+1,s2) );
144 286d73ae Leszek Koltunski
        String name    = row.substring(s2+1, s3);
145
        int time       = Integer.parseInt( row.substring(s3+1,s4) );
146
        String country = row.substring(s4+1, s5);
147 d8aa4ba8 Leszek Koltunski
148 286d73ae Leszek Koltunski
        if(level>0 && level<=MAX_SCRAMBLE)
149 d8aa4ba8 Leszek Koltunski
          {
150 286d73ae Leszek Koltunski
          int p = mPlaces[object][level];
151
          mPlaces[object][level]++;
152 329c0aeb Leszek Koltunski
153 286d73ae Leszek Koltunski
          mCountry[object][level-1][p] = country;
154
          mName   [object][level-1][p] = name;
155
          mTime   [object][level-1][p] = ((float)(time/100))/10.0f;
156 d8aa4ba8 Leszek Koltunski
          }
157
        }
158
      }
159 f3e12931 Leszek Koltunski
    }
160
161 36e2cbdd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163
  private String URLencode(String s)
164
    {
165
    StringBuilder sbuf = new StringBuilder();
166
    int len = s.length();
167
168
    for (int i = 0; i < len; i++)
169
      {
170
      int ch = s.charAt(i);
171
172
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
173
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
174
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
175
      else if (ch == ' '             ) sbuf.append('+');
176
      else if (ch == '-' || ch == '_'
177
            || ch == '.' || ch == '!'
178
            || ch == '~' || ch == '*'
179
            || ch == '\'' || ch == '('
180
            || ch == ')'             ) sbuf.append((char)ch);
181
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
182
      else if (ch <= 0x07FF)
183
        {
184
        sbuf.append(hex[0xc0 | (ch >> 6)]);
185
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
186
        }
187
      else
188
        {
189
        sbuf.append(hex[0xe0 | (ch >> 12)]);
190
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
191
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
192
        }
193
      }
194
195
    return sbuf.toString();
196
    }
197
198 f3e12931 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200 a675474f Leszek Koltunski
  private boolean doDownload()
201 f3e12931 Leszek Koltunski
    {
202 286d73ae Leszek Koltunski
    String message="https://distorted.org/magic/cgi-bin/download.cgi";
203
    message += "?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+mVersion+"d";
204
    message += "&o="+RubikObjectList.getObjectList()+"&min=0&max="+MAX_SCRAMBLE+"&l="+MAX_PLACES;
205 36e2cbdd Leszek Koltunski
206
    try
207
      {
208
      java.net.URL connectURL = new URL(message);
209
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
210
211
      conn.setDoInput(true);
212
      conn.setDoOutput(true);
213
      conn.setUseCaches(false);
214
      conn.setRequestMethod("GET");
215
      conn.connect();
216
      conn.getOutputStream().flush();
217
218 a675474f Leszek Koltunski
      try( InputStream is = conn.getInputStream() )
219 36e2cbdd Leszek Koltunski
        {
220
        int ch;
221
        StringBuilder sb = new StringBuilder();
222
        while( ( ch = is.read() ) != -1 )
223
          {
224
          sb.append( (char)ch );
225
          }
226
        mScores = sb.toString();
227
        }
228 cc5ec229 Leszek Koltunski
      catch( final Exception e)
229 36e2cbdd Leszek Koltunski
        {
230 beb325a0 Leszek Koltunski
        mReceiver.exception("Failed to get an answer from the High Scores server");
231 a675474f Leszek Koltunski
        return false;
232 36e2cbdd Leszek Koltunski
        }
233
      }
234 cc5ec229 Leszek Koltunski
    catch( final UnknownHostException e )
235
      {
236
      mReceiver.exception("No access to Internet");
237
      return false;
238
      }
239
    catch( final SecurityException e )
240
      {
241
      mReceiver.exception("Application not authorized to connect to the Internet");
242
      return false;
243
      }
244
    catch( final Exception e )
245 36e2cbdd Leszek Koltunski
      {
246 a675474f Leszek Koltunski
      mReceiver.exception(e.getMessage());
247
      return false;
248 36e2cbdd Leszek Koltunski
      }
249
250 329c0aeb Leszek Koltunski
    if( mScores.length()==0 )
251
      {
252
      mReceiver.exception("Failed to download scores");
253
      return false;
254
      }
255
256 a675474f Leszek Koltunski
    return true;
257 b8b38548 Leszek Koltunski
    }
258 d8aa4ba8 Leszek Koltunski
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
  private boolean gottaDownload()
262 b8b38548 Leszek Koltunski
    {
263 d8aa4ba8 Leszek Koltunski
    return ((mScores.length()==0) && !mRunning);
264 f3e12931 Leszek Koltunski
    }
265 b8b38548 Leszek Koltunski
266 d8aa4ba8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 4f9f99a2 Leszek Koltunski
  @Override
269 d8aa4ba8 Leszek Koltunski
  public void run()
270
    {
271 a675474f Leszek Koltunski
    boolean success=true;
272
273 d8aa4ba8 Leszek Koltunski
    try
274
      {
275
      if( gottaDownload() )
276
        {
277
        mRunning = true;
278 a675474f Leszek Koltunski
        success = doDownload();
279 d8aa4ba8 Leszek Koltunski
        }
280
      }
281
    catch( Exception e )
282
      {
283 cc5ec229 Leszek Koltunski
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
284 d8aa4ba8 Leszek Koltunski
      }
285
286 286d73ae Leszek Koltunski
    fillValues();
287
288 d8aa4ba8 Leszek Koltunski
    mRunning = false;
289 a675474f Leszek Koltunski
290
    if( success )
291
      {
292 cc5ec229 Leszek Koltunski
      mReceiver.receive(mCountry, mName, mTime);
293 a675474f Leszek Koltunski
      }
294 d8aa4ba8 Leszek Koltunski
    }
295 4f9f99a2 Leszek Koltunski
296 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298
  private RubikScoresDownloader()
299
    {
300
301
    }
302
303 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
304
// PUBLIC API
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
  public static void onPause()
308
    {
309
    mRunning = false;
310
    }
311
312 bc511ea5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
313
314
  public static RubikScoresDownloader getInstance()
315
    {
316
    if( mThis==null )
317
      {
318
      mThis = new RubikScoresDownloader();
319
      }
320
321
    return mThis;
322
    }
323
324 4f9f99a2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326 329c0aeb Leszek Koltunski
  public void download(Receiver receiver, String userName, String version, int numRuns)
327 4f9f99a2 Leszek Koltunski
    {
328
    mReceiver = receiver;
329 6f8e5dfc Leszek Koltunski
    mMode     = DOWNLOAD;
330
    mUserName = userName;
331 329c0aeb Leszek Koltunski
    mVersion  = version;
332 6f8e5dfc Leszek Koltunski
    mNumRuns  = numRuns;
333
334 4f9f99a2 Leszek Koltunski
    Thread networkThrd = new Thread(this);
335
    networkThrd.start();
336
    }
337 bc511ea5 Leszek Koltunski
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339
340
  public void submit()
341
    {
342
    mMode = SUBMIT;
343
344
    android.util.Log.e("downloader", "submitting!");
345
    }
346 b8b38548 Leszek Koltunski
}