Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ 82ce8e64

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 = "YOU";
67
    mCountry = "un";
68

    
69
    mNameIsVerified = false;
70

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/*
78
  private static int computeHash()
79
    {
80
    final int MODULO = 227;
81
    int ret = 0;
82

    
83
    for(int i=0; i<MAX_CUBE-1; i++)
84
      ret += (2*i+3)*passedlevel[i];
85

    
86
    for(int i=0; i<MAX_CUBE-1; i++)
87
      for(int j=0; j<MAX_LEVELS; j++)
88
        ret += (i*i+3*j)*records[i][j];
89

    
90
    int length = veriname==null ? 0 : veriname.length();
91

    
92
    for(int i=0;i<length;i++)
93
      ret += i*veriname.charAt(i);
94

    
95
    return (ret%=MODULO);
96
    }
97
*/
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public static RubikScores getInstance()
101
    {
102
    if( mThis==null )
103
      {
104
      mThis = new RubikScores();
105
      }
106

    
107
    return mThis;
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  public void savePreferences(SharedPreferences.Editor editor)
113
    {
114
    StringBuilder builder = new StringBuilder();
115
    RubikObjectList list;
116
    int[] sizes;
117
    int length;
118

    
119
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
120
      {
121
      builder.setLength(0);
122

    
123
      for(int object=0; object<NUM_OBJECTS; object++)
124
        {
125
        list = RubikObjectList.getObject(object);
126
        sizes = list.getSizes();
127
        length = sizes.length;
128

    
129
        for(int size=0; size<length; size++)
130
          {
131
          builder.append(list.name());
132
          builder.append("_");
133
          builder.append(sizes[size]);
134
          builder.append("=");
135
          builder.append(mRecords[object][size][scramble]);
136
          builder.append(",");
137
          builder.append(mSubmitted[object][size][scramble]);
138
          builder.append(" ");
139
          }
140
        }
141

    
142
      editor.putString("scores_record"+scramble, builder.toString());
143
      }
144

    
145
    editor.putString("scores_name"  , mName  );
146
    editor.putBoolean("scores_isVerified", mNameIsVerified);
147
    editor.putInt("scores_numPlays", mNumPlays);
148
    editor.putInt("scores_numRuns" , mNumRuns);
149
    editor.putInt("scores_deviceid", mDeviceID);
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public void restorePreferences(SharedPreferences preferences)
155
    {
156
    String recordStr, subStr, nameStr, sizeStr, timeStr, submStr;
157
    int start, end, equals, underscore, comma;
158
    int object, size, subm;
159
    long time;
160

    
161
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
162
      {
163
      start = end = 0;
164
      recordStr = preferences.getString("scores_record"+scramble, "");
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
        comma = subStr.indexOf(",");
178

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

    
186
          object = RubikObjectList.getOrdinal(nameStr);
187
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
188
          time   = Long.parseLong(timeStr);
189
          subm   = Integer.parseInt(submStr);
190

    
191
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE && subm>=0 && subm<=1 )
192
            {
193
            mRecords  [object][size][scramble] = time;
194
            mSubmitted[object][size][scramble] = subm;
195

    
196
            if( time<NO_RECORD )
197
              {
198
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time+" submitted: "+subm);
199
              }
200
            }
201
          else
202
            {
203
            android.util.Log.e("solv", "error: object="+object+" size="+size);
204
            }
205
          }
206
        }
207
      }
208

    
209
    mName           = preferences.getString("scores_name"  , "YOU" );
210
    mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
211
    mNumPlays       = preferences.getInt("scores_numPlays", 0);
212
    mNumRuns        = preferences.getInt("scores_numRuns" , 0);
213
    mDeviceID       = preferences.getInt("scores_deviceid",-1);
214

    
215
    if( mDeviceID==-1 ) mDeviceID = getDeviceID();
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public void setRecord(int object, int size, int scramble, long timeTaken)
221
    {
222
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
223

    
224
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
225
      {
226
      if( mRecords[object][size][scramble-1]> timeTaken )
227
        {
228
        mRecords[object][size][scramble-1] = timeTaken;
229
        android.util.Log.e("RubikScores","new record: ("+object+","+size+","+scramble+") ="+timeTaken);
230
        }
231
      }
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void incrementNumPlays()
237
    {
238
    mNumPlays++;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public void incrementNumRuns()
244
    {
245
    mNumRuns++;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
// TODO
250

    
251
  public void setName(String newName)
252
    {
253
    mName = newName;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257
// TODO
258

    
259
  public void verifyName()
260
    {
261
    mNameIsVerified = true;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
// TODO
266

    
267
  public void setSubmitted(int object, int size, int scramble)
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
      mSubmitted[object][size][scramble] = 1;
274
      }
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  public void setCountry(Context context)
280
    {
281
    TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
282

    
283
    if( tM!=null )
284
      {
285
      mCountry = tM.getSimCountryIso();
286

    
287
      if( mCountry==null || mCountry.length()<=1 )
288
        {
289
        mCountry = tM.getNetworkCountryIso();
290
        }
291
      }
292

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

    
297
    if( mCountry.equals("do") ) mCountry = "dm";
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  private int getDeviceID()
303
    {
304
    int id;
305

    
306
    try
307
      {
308
      String s = UUID.randomUUID().toString();
309
      id = (s!=null ? s.hashCode():0);
310
      }
311
    catch(Exception ex)
312
      {
313
      id = 0;
314
      android.util.Log.e("scores", "Exception in getDeviceID()");
315
      }
316

    
317
    return id<0 ? -id : id;
318
    }
319

    
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  public long getRecord(int object, int size, int scramble)
324
    {
325
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
326

    
327
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
328
      {
329
      return mRecords[object][size][scramble];
330
      }
331

    
332
    return -1;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public boolean isSubmitted(int object, int size, int scramble)
338
    {
339
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
340

    
341
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
342
      {
343
      return mSubmitted[object][size][scramble]==1;
344
      }
345

    
346
    return false;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
// TODO
351

    
352
  public boolean isVerified()
353
    {
354
    return mNameIsVerified;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
// TODO
359

    
360
  public int getNumPlays()
361
    {
362
    return mNumPlays;
363
    }
364

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
  public int getNumRuns()
368
    {
369
    return mNumRuns;
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  public String getName()
375
    {
376
    return mName;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  public String getCountry()
382
    {
383
    return mCountry;
384
    }
385
  }
(1-1/2)