Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikScoresDownloader.java @ 329c0aeb

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.network;
21

    
22
import java.io.InputStream;
23
import java.net.HttpURLConnection;
24
import java.net.URL;
25
import java.net.UnknownHostException;
26

    
27
import static org.distorted.object.RubikObject.LENGTH;
28
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public class RubikScoresDownloader implements Runnable
33
  {
34
  public interface Receiver
35
    {
36
    void receive(String[][][] country, String[][][] name, String[][][] time);
37
    void exception(String exception);
38
    }
39

    
40
  public static final int MAX_PLACES = 12;
41

    
42
  private static final int DOWNLOAD   = 0;
43
  private static final int SUBMIT     = 1;
44
  private static final int IDLE       = 2;
45

    
46
  private static final String URL  ="http://koltunski.pl/rubik/cgi-bin";
47

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

    
83
  private static boolean mRunning = false;
84
  private static int mMode = IDLE;
85
  private static Receiver mReceiver;
86
  private static String mUserName, mVersion;
87
  private static int mNumRuns;
88

    
89
  private static String mScores = "";
90
  private static String[][][] mCountry = new String[LENGTH][MAX_SCRAMBLE][MAX_PLACES];
91
  private static String[][][] mName    = new String[LENGTH][MAX_SCRAMBLE][MAX_PLACES];
92
  private static String[][][] mTime    = new String[LENGTH][MAX_SCRAMBLE][MAX_PLACES];
93

    
94
  private static int[][] mPlaces = new int[LENGTH][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<LENGTH; 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
      fillRow(mScores.substring(begin+1,end));
113
      begin = end;
114
      }
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private void fillRow(String row)
120
    {
121
    int s1 = row.indexOf(' ');
122
    int s2 = row.indexOf(' ',s1+1);
123
    int s3 = row.indexOf(' ',s2+1);
124
    int s4 = row.indexOf(' ',s3+1);
125
    int s5 = row.indexOf(' ',s4+1);
126
    int s6 = row.length();
127

    
128
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
129
      {
130
      int size = Integer.valueOf( row.substring(0,s1) );
131

    
132
      if( size>=0 && size<LENGTH )
133
        {
134
        int level      = Integer.valueOf( row.substring(s1+1,s2) );
135
        int place      = Integer.valueOf( row.substring(s2+1,s3) );
136
        String name    = row.substring(s3+1, s4);
137
        int time       = Integer.valueOf( row.substring(s4+1,s5) );
138
        String country = row.substring(s5+1, s6);
139
        String realTime= String.valueOf(time/10.0f);
140

    
141
        if(level>=0 && level<MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
142
          {
143
          int p = mPlaces[size][level];
144
          mPlaces[size][level]++;
145

    
146
          if( p!=place ) android.util.Log.e("downloader", "size="+size+" level="+level+" p="+p+" place="+place);
147

    
148
          mCountry[size][level][place] = country;
149
          mName   [size][level][place] = name;
150
          mTime   [size][level][place] = realTime;
151
          }
152
        }
153
      }
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private String URLencode(String s)
159
    {
160
    StringBuilder sbuf = new StringBuilder();
161
    int len = s.length();
162

    
163
    for (int i = 0; i < len; i++)
164
      {
165
      int ch = s.charAt(i);
166

    
167
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
168
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
169
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
170
      else if (ch == ' '             ) sbuf.append('+');
171
      else if (ch == '-' || ch == '_'
172
            || ch == '.' || ch == '!'
173
            || ch == '~' || ch == '*'
174
            || ch == '\'' || ch == '('
175
            || ch == ')'             ) sbuf.append((char)ch);
176
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
177
      else if (ch <= 0x07FF)
178
        {
179
        sbuf.append(hex[0xc0 | (ch >> 6)]);
180
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
181
        }
182
      else
183
        {
184
        sbuf.append(hex[0xe0 | (ch >> 12)]);
185
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
186
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
187
        }
188
      }
189

    
190
    return sbuf.toString();
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  private boolean doDownload()
196
    {
197
    String message=URL+"/download.cgi?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+mVersion+"d";
198

    
199
    try
200
      {
201
      java.net.URL connectURL = new URL(message);
202
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
203

    
204
      conn.setDoInput(true);
205
      conn.setDoOutput(true);
206
      conn.setUseCaches(false);
207
      conn.setRequestMethod("GET");
208
      conn.connect();
209
      conn.getOutputStream().flush();
210

    
211
      try( InputStream is = conn.getInputStream() )
212
        {
213
        int ch;
214
        StringBuilder sb = new StringBuilder();
215
        while( ( ch = is.read() ) != -1 )
216
          {
217
          sb.append( (char)ch );
218
          }
219
        mScores = sb.toString();
220
        }
221
      catch( final Exception e)
222
        {
223
        mReceiver.exception("Failed to get an answer from the High Scores server");
224
        return false;
225
        }
226
      }
227
    catch( final UnknownHostException e )
228
      {
229
      mReceiver.exception("No access to Internet");
230
      return false;
231
      }
232
    catch( final SecurityException e )
233
      {
234
      mReceiver.exception("Application not authorized to connect to the Internet");
235
      return false;
236
      }
237
    catch( final Exception e )
238
      {
239
      mReceiver.exception(e.getMessage());
240
      return false;
241
      }
242

    
243
    if( mScores.length()==0 )
244
      {
245
      mReceiver.exception("Failed to download scores");
246
      return false;
247
      }
248

    
249
    return true;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private boolean gottaDownload()
255
    {
256
    return ((mScores.length()==0) && !mRunning);
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  @Override
262
  public void run()
263
    {
264
    boolean success=true;
265

    
266
    try
267
      {
268
      if( gottaDownload() )
269
        {
270
        mRunning = true;
271
        success = doDownload();
272
        fillValues();
273
        }
274
      }
275
    catch( Exception e )
276
      {
277
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
278
      }
279

    
280
    mRunning = false;
281

    
282
    if( success )
283
      {
284
      mReceiver.receive(mCountry, mName, mTime);
285
      }
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
// PUBLIC API
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  public static void onPause()
293
    {
294
    mRunning = false;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public void download(Receiver receiver, String userName, String version, int numRuns)
300
    {
301
    mReceiver = receiver;
302
    mMode     = DOWNLOAD;
303
    mUserName = userName;
304
    mVersion  = version;
305
    mNumRuns  = numRuns;
306

    
307
    Thread networkThrd = new Thread(this);
308
    networkThrd.start();
309
    }
310
}
    (1-1/1)