Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ 4c0cd600

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.Context;
23
import android.content.SharedPreferences;
24
import android.telephony.TelephonyManager;
25

    
26
import org.distorted.object.RubikObjectList;
27

    
28
import java.util.UUID;
29

    
30
import static org.distorted.object.RubikObjectList.MAX_SIZE;
31
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
32
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
// hold my own scores, and some other statistics.
36

    
37
public class RubikScores
38
  {
39
  public static final long NO_RECORD = Long.MAX_VALUE;
40
  private static RubikScores mThis;
41

    
42
  private long[][][] mRecords;
43
  private int [][][] mSubmitted;
44

    
45
  private String mName, mCountry;
46
  private boolean mNameIsVerified;
47
  private int mNumRuns;
48
  private int mNumPlays;
49
  private int mDeviceID;
50

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

    
53
  private RubikScores()
54
    {
55
    mRecords   = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
56
    mSubmitted = new int [NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
57

    
58
    for(int i=0; i<NUM_OBJECTS; i++)
59
      for(int j=0; j<MAX_SIZE; j++)
60
        for(int k=0; k<MAX_SCRAMBLE; k++)
61
          {
62
          mRecords[i][j][k]   = NO_RECORD;
63
          mSubmitted[i][j][k] = 0;
64
          }
65

    
66
    mName = "";
67
    mCountry = "un";
68

    
69
    mNameIsVerified = false;
70

    
71
    mNumPlays= -1;
72
    mNumRuns = -1;
73
    mDeviceID= -1;
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  private int privateGetDeviceID()
79
    {
80
    int id;
81

    
82
    try
83
      {
84
      String s = UUID.randomUUID().toString();
85
      id = (s!=null ? s.hashCode():0);
86
      }
87
    catch(Exception ex)
88
      {
89
      id = 0;
90
      android.util.Log.e("scores", "Exception in getDeviceID()");
91
      }
92

    
93
    return id<0 ? -id : id;
94
    }
95

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

    
98
  private String getUnsubmittedList(int mode)
99
    {
100
    RubikObjectList list;
101
    StringBuilder builder = new StringBuilder();
102
    boolean first = true;
103
    int[] sizes;
104
    int length;
105

    
106
    for(int object=0; object<NUM_OBJECTS; object++)
107
      {
108
      list = RubikObjectList.getObject(object);
109
      sizes = list.getSizes();
110
      length = sizes.length;
111

    
112
      for(int size=0; size<length; size++)
113
        {
114
        for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
115
          {
116
          if( mSubmitted[object][size][scramble]==0 && mRecords[object][size][scramble]<NO_RECORD )
117
            {
118
            if( !first ) builder.append(',');
119
            else         first=false;
120

    
121
            switch(mode)
122
              {
123
              case 0: builder.append(list.name());
124
                      builder.append("_");
125
                      builder.append(sizes[size]);
126
                      break;
127
              case 1: builder.append(scramble);
128
                      break;
129
              case 2: builder.append(mRecords[object][size][scramble]);
130
                      break;
131
              }
132
            }
133
          }
134
        }
135
      }
136

    
137
    return builder.toString();
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// PUBLIC API
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  public static RubikScores getInstance()
145
    {
146
    if( mThis==null )
147
      {
148
      mThis = new RubikScores();
149
      }
150

    
151
    return mThis;
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public void savePreferences(SharedPreferences.Editor editor)
157
    {
158
    StringBuilder builder = new StringBuilder();
159
    RubikObjectList list;
160
    int[] sizes;
161
    int length;
162

    
163
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
164
      {
165
      builder.setLength(0);
166

    
167
      for(int object=0; object<NUM_OBJECTS; object++)
168
        {
169
        list = RubikObjectList.getObject(object);
170
        sizes = list.getSizes();
171
        length = sizes.length;
172

    
173
        for(int size=0; size<length; size++)
174
          {
175
          builder.append(list.name());
176
          builder.append("_");
177
          builder.append(sizes[size]);
178
          builder.append("=");
179
          builder.append(mRecords[object][size][scramble]);
180
          builder.append(",");
181
          builder.append(mSubmitted[object][size][scramble]);
182
          builder.append(" ");
183
          }
184
        }
185

    
186
      editor.putString("scores_record"+scramble, builder.toString());
187
      }
188

    
189
    editor.putString("scores_name"  , mName  );
190
    editor.putBoolean("scores_isVerified", mNameIsVerified);
191
    editor.putInt("scores_numPlays", mNumPlays);
192
    editor.putInt("scores_numRuns" , mNumRuns);
193
    editor.putInt("scores_deviceid", mDeviceID);
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  public void restorePreferences(SharedPreferences preferences)
199
    {
200
    String recordStr, subStr, nameStr, sizeStr, timeStr, submStr;
201
    int start, end, equals, underscore, comma;
202
    int object, size, subm;
203
    long time;
204

    
205
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
206
      {
207
      start = end = 0;
208
      recordStr = preferences.getString("scores_record"+scramble, "");
209

    
210
      while( end!=-1 )
211
        {
212
        end = recordStr.indexOf(" ", start);
213

    
214
        if( end==-1 ) subStr = recordStr.substring(start);
215
        else          subStr = recordStr.substring(start,end);
216

    
217
        start = end+1;
218

    
219
        underscore = subStr.indexOf("_");
220
        equals = subStr.indexOf("=");
221
        comma = subStr.indexOf(",");
222

    
223
        if( underscore>=0 && equals>=0 && comma>=0 )
224
          {
225
          nameStr = subStr.substring(0,underscore);
226
          sizeStr = subStr.substring(underscore+1, equals);
227
          timeStr = subStr.substring(equals+1,comma);
228
          submStr = subStr.substring(comma+1);
229

    
230
          object = RubikObjectList.getOrdinal(nameStr);
231

    
232
          if( object>=0 && object< NUM_OBJECTS )
233
            {
234
            size = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
235
            time = Long.parseLong(timeStr);
236
            subm = Integer.parseInt(submStr);
237

    
238
            if( size>=0 && size<MAX_SIZE && subm>=0 && subm<=1 )
239
              {
240
              mRecords  [object][size][scramble] = time;
241
              mSubmitted[object][size][scramble] = subm;
242
              }
243
            else
244
              {
245
              android.util.Log.e("solv", "error: size="+size+" subm="+subm);
246
              }
247
            }
248
          else
249
            {
250
            android.util.Log.e("solv", "error: object="+object);
251
            }
252
          }
253
        }
254
      }
255

    
256
    mName           = preferences.getString("scores_name"  , "" );
257
    mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
258
    mNumPlays       = preferences.getInt("scores_numPlays", 0);
259
    mNumRuns        = preferences.getInt("scores_numRuns" , 0);
260
    mDeviceID       = preferences.getInt("scores_deviceid",-1);
261

    
262
    if( mDeviceID==-1 ) mDeviceID = privateGetDeviceID();
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public boolean setRecord(int object, int size, int scramble, long timeTaken)
268
    {
269
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
270

    
271
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
272
      {
273
      if( mRecords[object][size][scramble-1]> timeTaken )
274
        {
275
        mRecords  [object][size][scramble-1] = timeTaken;
276
        mSubmitted[object][size][scramble-1] = 0;
277
        return true;
278
        }
279
      }
280

    
281
    return false;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  public void incrementNumPlays()
287
    {
288
    mNumPlays++;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public void incrementNumRuns()
294
    {
295
    mNumRuns++;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299
// TODO
300

    
301
  public void setName(String newName)
302
    {
303
    mName = newName;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  void successfulSubmit()
309
    {
310
    mNameIsVerified = true;
311

    
312
    for(int i=0; i<NUM_OBJECTS; i++)
313
      for(int j=0; j<MAX_SIZE   ; j++)
314
        for(int k=0; k<MAX_SCRAMBLE; k++)
315
          {
316
          mSubmitted[i][j][k]=1;
317
          }
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public void setCountry(Context context)
323
    {
324
    TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
325

    
326
    if( tM!=null )
327
      {
328
      mCountry = tM.getSimCountryIso();
329

    
330
      if( mCountry==null || mCountry.length()<=1 )
331
        {
332
        mCountry = tM.getNetworkCountryIso();
333
        }
334
      }
335

    
336
    // Special case: Dominicana. Its ISO-3166-alpha-2 country code is 'do' which we can't have here
337
    // because we later on map this to a resource name (the flag) and 'do' is a resevred Java keyword
338
    // and can't be a resource name.
339

    
340
    if( mCountry.equals("do") ) mCountry = "dm";
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  public long getRecord(int object, int size, int scramble)
346
    {
347
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
348

    
349
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
350
      {
351
      return mRecords[object][size][scramble];
352
      }
353

    
354
    return -1;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  public boolean isSubmitted(int object, int size, int scramble)
360
    {
361
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
362

    
363
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
364
      {
365
      return mSubmitted[object][size][scramble]==1;
366
      }
367

    
368
    return false;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  int getDeviceID()
374
    {
375
    return mDeviceID;
376
    }
377

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

    
380
  boolean isVerified()
381
    {
382
    return mNameIsVerified;
383
    }
384

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  int getNumPlays()
388
    {
389
    return mNumPlays;
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  int getNumRuns()
395
    {
396
    return mNumRuns;
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  public String getName()
402
    {
403
    return mName;
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  public String getCountry()
409
    {
410
    return mCountry;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  boolean thereAreUnsubmittedRecords()
416
    {
417
    RubikObjectList list;
418
    int length;
419

    
420
    for(int object=0; object<NUM_OBJECTS; object++)
421
      {
422
      list = RubikObjectList.getObject(object);
423
      length = list.getSizes().length;
424

    
425
      for(int size=0; size<length; size++)
426
        for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
427
          {
428
          if( mSubmitted[object][size][scramble]==0 && mRecords[object][size][scramble]<NO_RECORD )
429
            {
430
            return true;
431
            }
432
          }
433
      }
434

    
435
    return false;
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
  String getUnsubmittedObjlist()
441
    {
442
    return getUnsubmittedList(0);
443
    }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

    
447
  String getUnsubmittedLevellist()
448
    {
449
    return getUnsubmittedList(1);
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  String getUnsubmittedTimelist()
455
    {
456
    return getUnsubmittedList(2);
457
    }
458
  }
(1-1/2)