Project

General

Profile

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

magiccube / src / main / java / org / distorted / network / RubikScoresDownloader.java @ 0333d81e

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 org.distorted.magic.R;
28

    
29
import static org.distorted.object.RubikObject.LENGTH;
30
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

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

    
42
  public static final int MAX_PLACES = 12;
43

    
44
  private static final int DOWNLOAD   = 0;
45
  private static final int SUBMIT     = 1;
46
  private static final int IDLE       = 2;
47

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

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

    
85
  private static boolean mRunning = false;
86
  private static int mMode = IDLE;
87
  private static Receiver mReceiver;
88
  private static String mUserName;
89
  private static int mNumRuns;
90

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  private void fillValues()
99
    {
100
    int begin=-1 ,end, len = mScores.length();
101

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

    
122
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
123
      {
124
      int size = Integer.valueOf( row.substring(0,s1) );
125

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

    
135
        if(level>=0 && level<MAX_SCRAMBLE && place>=0 && place<MAX_PLACES)
136
          {
137
          mCountry[size][level][place] = country;
138
          mName   [size][level][place] = name;
139
          mTime   [size][level][place] = realTime;
140
          }
141
        }
142
      }
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

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

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

    
179
    return sbuf.toString();
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  private boolean doDownload()
185
    {
186
    String version = R.string.app_version+"d";
187
    String message=URL+"/download.cgi?n="+URLencode(mUserName)+"&r="+mNumRuns+"&e="+version;
188

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

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

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

    
233
    return true;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  @Override
246
  public void run()
247
    {
248
    boolean success=true;
249

    
250
    try
251
      {
252
      if( gottaDownload() )
253
        {
254
        mRunning = true;
255
        success = doDownload();
256
        fillValues();
257
        }
258
      }
259
    catch( Exception e )
260
      {
261
      mReceiver.exception("Exception downloading records: "+e.getMessage() );
262
      }
263

    
264
    mRunning = false;
265

    
266
    if( success )
267
      {
268
      mReceiver.receive(mCountry, mName, mTime);
269
      }
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
// PUBLIC API
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public static void onPause()
277
    {
278
    mRunning = false;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  public void download(Receiver receiver, String userName, int numRuns)
284
    {
285
    mReceiver = receiver;
286
    mMode     = DOWNLOAD;
287
    mUserName = userName;
288
    mNumRuns  = numRuns;
289

    
290
    Thread networkThrd = new Thread(this);
291
    networkThrd.start();
292
    }
293
}
    (1-1/1)