Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikScoresDownloader.java @ cc5ec229

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 android.content.res.Resources;
23

    
24
import java.io.InputStream;
25
import java.net.HttpURLConnection;
26
import java.net.URL;
27
import java.net.UnknownHostException;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

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

    
39
  static final int MAX_PLACES = 12;
40

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

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

    
47
  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
  private static boolean mRunning = false;
83
  private static int mMode = IDLE;
84
  private static Receiver mReceiver;
85
  private static Resources mResources;
86
  private static String mPackageName;
87

    
88
  private static String mScores = "";
89
  private static int[][][] mCountry = new int   [RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES];
90
  private static String[][][] mName = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES];
91
  private static String[][][] mTime = new String[RubikSize.LENGTH][RubikActivity.MAX_SCRAMBLE][MAX_PLACES];
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  private void fillValues()
96
    {
97
    int begin=-1 ,end, len = mScores.length();
98

    
99
    while( begin<len )
100
      {
101
      end = mScores.indexOf('\n', begin+1);
102
      if( end<0 ) end = len;
103
      fillRow(mScores.substring(begin+1,end));
104
      begin = end;
105
      }
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  private void fillRow(String row)
111
    {
112
    int s1 = row.indexOf(' ');
113
    int s2 = row.indexOf(' ',s1+1);
114
    int s3 = row.indexOf(' ',s2+1);
115
    int s4 = row.indexOf(' ',s3+1);
116
    int s5 = row.indexOf(' ',s4+1);
117
    int s6 = row.length();
118

    
119
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
120
      {
121
      int size = Integer.valueOf( row.substring(0,s1) );
122

    
123
      if( size>=0 && size<RubikSize.LENGTH )
124
        {
125
        int level      = Integer.valueOf( row.substring(s1+1,s2) );
126
        int place      = Integer.valueOf( row.substring(s2+1,s3) );
127
        String name    = row.substring(s3+1, s4);
128
        int time       = Integer.valueOf( row.substring(s4+1,s5) );
129
        String country = row.substring(s5+1, s6);
130
        String realTime= String.valueOf(time/10.0f);
131

    
132
        if(level>=0 && level<RubikActivity.MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
133
          {
134
          int resID = mResources.getIdentifier( country, "drawable", mPackageName);
135
          mCountry[size][level][place] = resID!=0 ? resID:R.drawable.unk;
136
          mName[size][level][place]    = name;
137
          mTime[size][level][place]    = realTime;
138
          }
139
        }
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private String URLencode(String s)
146
    {
147
    StringBuilder sbuf = new StringBuilder();
148
    int len = s.length();
149

    
150
    for (int i = 0; i < len; i++)
151
      {
152
      int ch = s.charAt(i);
153

    
154
           if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch);
155
      else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch);
156
      else if ('0' <= ch && ch <= '9') sbuf.append((char)ch);
157
      else if (ch == ' '             ) sbuf.append('+');
158
      else if (ch == '-' || ch == '_'
159
            || ch == '.' || ch == '!'
160
            || ch == '~' || ch == '*'
161
            || ch == '\'' || ch == '('
162
            || ch == ')'             ) sbuf.append((char)ch);
163
      else if (ch <= 0x007f)           sbuf.append(hex[ch]);
164
      else if (ch <= 0x07FF)
165
        {
166
        sbuf.append(hex[0xc0 | (ch >> 6)]);
167
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
168
        }
169
      else
170
        {
171
        sbuf.append(hex[0xe0 | (ch >> 12)]);
172
        sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]);
173
        sbuf.append(hex[0x80 | (ch & 0x3F)]);
174
        }
175
      }
176

    
177
    return sbuf.toString();
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  private boolean doDownload()
183
    {
184
    String veri = "distored";
185
    String name = URLencode(veri);
186
    int numRuns = 1;
187
    String version = R.string.app_version+"d";
188
    String message=URL+"/download.cgi?n="+name+"&r="+numRuns+"&e="+version;
189

    
190
    try
191
      {
192
      java.net.URL connectURL = new URL(message);
193
      HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
194

    
195
      conn.setDoInput(true);
196
      conn.setDoOutput(true);
197
      conn.setUseCaches(false);
198
      conn.setRequestMethod("GET");
199
      conn.connect();
200
      conn.getOutputStream().flush();
201

    
202
      try( InputStream is = conn.getInputStream() )
203
        {
204
        int ch;
205
        StringBuilder sb = new StringBuilder();
206
        while( ( ch = is.read() ) != -1 )
207
          {
208
          sb.append( (char)ch );
209
          }
210
        mScores = sb.toString();
211
        }
212
      catch( final Exception e)
213
        {
214
        mReceiver.exception("biffed it getting HTTPResponse");
215
        return false;
216
        }
217
      }
218
    catch( final UnknownHostException e )
219
      {
220
      mReceiver.exception("No access to Internet");
221
      return false;
222
      }
223
    catch( final SecurityException e )
224
      {
225
      mReceiver.exception("Application not authorized to connect to the Internet");
226
      return false;
227
      }
228
    catch( final Exception e )
229
      {
230
      mReceiver.exception(e.getMessage());
231
      return false;
232
      }
233

    
234
    return true;
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  private boolean gottaDownload()
240
    {
241
    return ((mScores.length()==0) && !mRunning);
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  static void onPause()
247
    {
248
    mRunning = false;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  void download(Receiver receiver, Resources resources, String packageName)
254
    {
255
    mReceiver = receiver;
256
    mResources= resources;
257
    mPackageName = packageName;
258
    mMode = DOWNLOAD;
259

    
260
    Thread networkThrd = new Thread(this);
261
    networkThrd.start();
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public void run()
267
    {
268
    boolean success=true;
269

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

    
284
    mRunning = false;
285

    
286
    if( success )
287
      {
288
      mReceiver.receive(mCountry, mName, mTime);
289
      }
290
    }
291
}
(6-6/12)