Project

General

Profile

« Previous | Next » 

Revision f0e87514

Added by Leszek Koltunski over 4 years ago

Separate scores from StateSolving

View differences:

src/main/java/org/distorted/dialog/RubikDialogScoresPagerAdapter.java
27 27
import android.view.ViewGroup;
28 28
import android.widget.LinearLayout;
29 29

  
30
import org.distorted.network.RubikScoresDownloader;
30
import org.distorted.scores.RubikScoresDownloader;
31 31
import org.distorted.object.RubikObjectList;
32 32

  
33 33
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
src/main/java/org/distorted/dialog/RubikDialogScoresView.java
30 30
import android.widget.TextView;
31 31

  
32 32
import org.distorted.magic.R;
33
import static org.distorted.network.RubikScoresDownloader.MAX_PLACES;
33
import static org.distorted.scores.RubikScoresDownloader.MAX_PLACES;
34 34

  
35 35
///////////////////////////////////////////////////////////////////////////////////////////////////
36 36

  
src/main/java/org/distorted/magic/RubikActivity.java
31 31
import org.distorted.effect.BaseEffect;
32 32
import org.distorted.library.main.DistortedLibrary;
33 33

  
34
import org.distorted.network.RubikScoresDownloader;
34
import org.distorted.scores.RubikScoresDownloader;
35 35
import org.distorted.object.RubikObjectList;
36 36
import org.distorted.uistate.RubikState;
37 37
import org.distorted.uistate.RubikStateAbstract;
src/main/java/org/distorted/network/RubikScoresDownloader.java
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 org.distorted.object.RubikObjectList;
23

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

  
29
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
30

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

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

  
41
  public static final int MAX_PLACES = 12;
42

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

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

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

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

  
90
  private static int mTotal = RubikObjectList.getTotal();
91
  private static String mScores = "";
92
  private static String[][][] mCountry = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
93
  private static String[][][] mName    = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
94
  private static String[][][] mTime    = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
95

  
96
  private static int[][] mPlaces = new int[mTotal][MAX_SCRAMBLE];
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

  
104
    for(int i=0; i<mTotal; i++)
105
      for(int j=0; j<MAX_SCRAMBLE; j++)
106
        {
107
        mPlaces[i][j] = 0;
108
        }
109

  
110
    while( begin<len )
111
      {
112
      end = mScores.indexOf('\n', begin+1);
113
      if( end<0 ) end = len;
114
      fillRow(mScores.substring(begin+1,end));
115
      begin = end;
116
      }
117
    }
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

  
130
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
131
      {
132
      int size = Integer.parseInt( row.substring(0,s1) );
133

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

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

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

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

  
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

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

  
192
    return sbuf.toString();
193
    }
194

  
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

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

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

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

  
251
    return true;
252
    }
253

  
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

  
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

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

  
282
    mRunning = false;
283

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

  
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
// PUBLIC API
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

  
294
  public static void onPause()
295
    {
296
    mRunning = false;
297
    }
298

  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

  
309
    Thread networkThrd = new Thread(this);
310
    networkThrd.start();
311
    }
312
}
src/main/java/org/distorted/scores/RubikScores.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.SharedPreferences;
23
import org.distorted.object.RubikObjectList;
24

  
25
import static org.distorted.object.RubikObjectList.MAX_SIZE;
26
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
27
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
28

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
// hold my own scores
31

  
32
public class RubikScores
33
  {
34
  private static final long NO_RECORD = Integer.MAX_VALUE;
35
  private long[][][] mRecords;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
  public RubikScores()
40
    {
41
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
42

  
43
    for(int i=0; i<NUM_OBJECTS; i++)
44
      for(int j=0; j<MAX_SIZE; j++)
45
        for(int k=0; k<MAX_SCRAMBLE; k++)
46
          {
47
          mRecords[i][j][k] = NO_RECORD;
48
          }
49
    }
50

  
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

  
53
  public void savePreferences(SharedPreferences.Editor editor)
54
    {
55
    StringBuilder builder = new StringBuilder();
56
    RubikObjectList list;
57
    int[] sizes;
58
    int length;
59

  
60
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
61
      {
62
      builder.setLength(0);
63

  
64
      for(int object=0; object<NUM_OBJECTS; object++)
65
        {
66
        list = RubikObjectList.getObject(object);
67
        sizes = list.getSizes();
68
        length = sizes.length;
69

  
70
        for(int size=0; size<length; size++)
71
          {
72
          builder.append(list.name());
73
          builder.append("_");
74
          builder.append(sizes[size]);
75
          builder.append("=");
76
          builder.append(mRecords[object][size][scramble]);
77
          builder.append(" ");
78
          }
79
        }
80

  
81
      editor.putString("record"+scramble, builder.toString());
82
      }
83
    }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

  
87
  public void restorePreferences(SharedPreferences preferences)
88
    {
89
    String recordStr, subStr, nameStr, sizeStr, timeStr;
90
    int start, end, equals, underscore;
91
    int object, size, time;
92

  
93
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
94
      {
95
      start = end = 0;
96
      recordStr = preferences.getString("record"+scramble, "");
97

  
98
      //android.util.Log.e("solving", scramble+" record string: "+recordStr);
99

  
100
      while( end!=-1 )
101
        {
102
        end = recordStr.indexOf(" ", start);
103

  
104
        if( end==-1 ) subStr = recordStr.substring(start);
105
        else          subStr = recordStr.substring(start,end);
106

  
107
        start = end+1;
108

  
109
        underscore = subStr.indexOf("_");
110
        equals = subStr.indexOf("=");
111

  
112
        if( underscore>=0 && equals>=0 )
113
          {
114
          nameStr = subStr.substring(0,underscore);
115
          sizeStr = subStr.substring(underscore+1, equals);
116
          timeStr = subStr.substring(equals+1);
117

  
118
          object = RubikObjectList.getOrdinal(nameStr);
119
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
120
          time   = Integer.parseInt(timeStr);
121

  
122
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
123
            {
124
            mRecords[object][size][scramble] = time;
125

  
126
            if( time<Integer.MAX_VALUE )
127
              {
128
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
129
              }
130
            }
131
          else
132
            {
133
            android.util.Log.e("solv", "error: object="+object+" size="+size);
134
            }
135
          }
136
        }
137
      }
138
    }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

  
142
  public void newRecord(int object, int size, int scramble, long timeTaken)
143
    {
144
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
145

  
146
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
147
      {
148
      if( mRecords[object][size][scramble-1]> timeTaken )
149
        {
150
        mRecords[object][size][scramble-1] = timeTaken;
151
        android.util.Log.e("solv","new record!");
152
        }
153
      }
154
    }
155
  }
src/main/java/org/distorted/scores/RubikScoresDownloader.java
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 org.distorted.object.RubikObjectList;
23

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

  
29
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
30

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

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

  
41
  public static final int MAX_PLACES = 12;
42

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

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

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

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

  
90
  private static int mTotal = RubikObjectList.getTotal();
91
  private static String mScores = "";
92
  private static String[][][] mCountry = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
93
  private static String[][][] mName    = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
94
  private static String[][][] mTime    = new String[mTotal][MAX_SCRAMBLE][MAX_PLACES];
95

  
96
  private static int[][] mPlaces = new int[mTotal][MAX_SCRAMBLE];
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

  
104
    for(int i=0; i<mTotal; i++)
105
      for(int j=0; j<MAX_SCRAMBLE; j++)
106
        {
107
        mPlaces[i][j] = 0;
108
        }
109

  
110
    while( begin<len )
111
      {
112
      end = mScores.indexOf('\n', begin+1);
113
      if( end<0 ) end = len;
114
      fillRow(mScores.substring(begin+1,end));
115
      begin = end;
116
      }
117
    }
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

  
130
    if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 )
131
      {
132
      int size = Integer.parseInt( row.substring(0,s1) );
133

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

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

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

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

  
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

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

  
192
    return sbuf.toString();
193
    }
194

  
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

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

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

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

  
251
    return true;
252
    }
253

  
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

  
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

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

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

  
282
    mRunning = false;
283

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

  
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
// PUBLIC API
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

  
294
  public static void onPause()
295
    {
296
    mRunning = false;
297
    }
298

  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

  
309
    Thread networkThrd = new Thread(this);
310
    networkThrd.start();
311
    }
312
}
src/main/java/org/distorted/uistate/RubikStateSolving.java
28 28

  
29 29
import org.distorted.magic.R;
30 30
import org.distorted.magic.RubikActivity;
31
import org.distorted.object.RubikObjectList;
31
import org.distorted.scores.RubikScores;
32 32

  
33 33
import java.util.Timer;
34 34
import java.util.TimerTask;
35 35

  
36
import static android.view.View.INVISIBLE;
37
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
38
import static org.distorted.object.RubikObjectList.MAX_SIZE;
39
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
40

  
41 36
///////////////////////////////////////////////////////////////////////////////////////////////////
42 37

  
43 38
public class RubikStateSolving extends RubikStateAbstract
44 39
  {
45
  private static final long NO_RECORD = Integer.MAX_VALUE;
46

  
47 40
  private TextView mTime;
48 41
  private Timer mTimer;
49 42
  private long mStartTime;
50 43
  private boolean mRunning;
51

  
52
  private long[][][] mRecords;
44
  private RubikScores mScores;
53 45

  
54 46
///////////////////////////////////////////////////////////////////////////////////////////////////
55 47

  
56 48
  RubikStateSolving()
57 49
    {
58
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
59

  
60
    for(int i=0; i<NUM_OBJECTS; i++)
61
      for(int j=0; j<MAX_SIZE; j++)
62
        for(int k=0; k<MAX_SCRAMBLE; k++)
63
          {
64
          mRecords[i][j][k] = NO_RECORD;
65
          }
50
    mScores = new RubikScores();
66 51
    }
67 52

  
68 53
///////////////////////////////////////////////////////////////////////////////////////////////////
......
111 96
    buttonR.setOnClickListener(act);
112 97
    layoutBot.addView(buttonR);
113 98

  
114
    buttonL.setVisibility(INVISIBLE);
99
    buttonL.setVisibility(android.view.View.INVISIBLE);
115 100
    }
116 101

  
117 102
///////////////////////////////////////////////////////////////////////////////////////////////////
118 103

  
119 104
  public void savePreferences(SharedPreferences.Editor editor)
120 105
    {
121
    StringBuilder builder = new StringBuilder();
122
    RubikObjectList list;
123
    int[] sizes;
124
    int length;
125

  
126
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
127
      {
128
      builder.setLength(0);
129

  
130
      for(int object=0; object<NUM_OBJECTS; object++)
131
        {
132
        list = RubikObjectList.getObject(object);
133
        sizes = list.getSizes();
134
        length = sizes.length;
135

  
136
        for(int size=0; size<length; size++)
137
          {
138
          builder.append(list.name());
139
          builder.append("_");
140
          builder.append(sizes[size]);
141
          builder.append("=");
142
          builder.append(mRecords[object][size][scramble]);
143
          builder.append(" ");
144
          }
145
        }
146

  
147
      editor.putString("record"+scramble, builder.toString());
148
      }
106
    mScores.savePreferences(editor);
149 107
    }
150 108

  
151 109
///////////////////////////////////////////////////////////////////////////////////////////////////
152 110

  
153 111
  public void restorePreferences(SharedPreferences preferences)
154 112
    {
155
    String recordStr, subStr, nameStr, sizeStr, timeStr;
156
    int start, end, equals, underscore;
157
    int object, size, time;
158

  
159
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
160
      {
161
      start = end = 0;
162
      recordStr = preferences.getString("record"+scramble, "");
163

  
164
      //android.util.Log.e("solving", scramble+" record string: "+recordStr);
165

  
166
      while( end!=-1 )
167
        {
168
        end = recordStr.indexOf(" ", start);
169

  
170
        if( end==-1 ) subStr = recordStr.substring(start);
171
        else          subStr = recordStr.substring(start,end);
172

  
173
        start = end+1;
174

  
175
        underscore = subStr.indexOf("_");
176
        equals = subStr.indexOf("=");
177

  
178
        if( underscore>=0 && equals>=0 )
179
          {
180
          nameStr = subStr.substring(0,underscore);
181
          sizeStr = subStr.substring(underscore+1, equals);
182
          timeStr = subStr.substring(equals+1);
183

  
184
          object = RubikObjectList.getOrdinal(nameStr);
185
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
186
          time   = Integer.parseInt(timeStr);
187

  
188
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
189
            {
190
            mRecords[object][size][scramble] = time;
191

  
192
            if( time<Integer.MAX_VALUE )
193
              {
194
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
195
              }
196
            }
197
          else
198
            {
199
            android.util.Log.e("solv", "error: object="+object+" size="+size);
200
            }
201
          }
202
        }
203
      }
113
    mScores.restorePreferences(preferences);
204 114
    }
205 115

  
206 116
///////////////////////////////////////////////////////////////////////////////////////////////////
......
251 161
      int object  = play.getObject();
252 162
      int size    = play.getSize();
253 163
      int scramble= play.getPicker();
254
      int maxsize = RubikObjectList.getObject(object).getSizes().length;
255

  
256
      if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
257
        {
258
        if( mRecords[object][size][scramble-1]> timeTaken )
259
          {
260
          mRecords[object][size][scramble-1] = timeTaken;
261
          android.util.Log.e("solv","new record!");
262
          }
263
        }
264 164

  
165
      mScores.newRecord(object, size, scramble, timeTaken);
265 166
      return timeTaken;
266 167
      }
267 168

  

Also available in: Unified diff