1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.external;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import android.content.Context;
|
16
|
import android.content.SharedPreferences;
|
17
|
import android.telephony.TelephonyManager;
|
18
|
|
19
|
import com.google.firebase.crashlytics.FirebaseCrashlytics;
|
20
|
|
21
|
import org.distorted.main.BuildConfig;
|
22
|
import org.distorted.objects.RubikObject;
|
23
|
import org.distorted.objects.RubikObjectList;
|
24
|
import org.distorted.screens.RubikScreenPlay;
|
25
|
|
26
|
import static org.distorted.objectlib.main.ObjectType.MAX_SCRAMBLES;
|
27
|
import static org.distorted.screens.RubikScreenPlay.LEVELS_SHOWN;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
// hold my own scores, and some other statistics.
|
31
|
|
32
|
public class RubikScores
|
33
|
{
|
34
|
public static final int RECORD_FIRST = 0;
|
35
|
public static final int RECORD_NEW = 1;
|
36
|
public static final int RECORD_NOT_NEW = 2;
|
37
|
|
38
|
public static final int MAX_RECORD = 10;
|
39
|
public static final int MULT = 1000000;
|
40
|
public static final long NO_RECORD = Long.MAX_VALUE;
|
41
|
private static RubikScores mThis;
|
42
|
|
43
|
private String mName, mCountry;
|
44
|
private boolean mNameIsVerified;
|
45
|
private int mNumRuns;
|
46
|
private int mNumPlays;
|
47
|
private int mNumWins;
|
48
|
private int mDeviceID;
|
49
|
private int mNumStars;
|
50
|
|
51
|
private static class MapValue
|
52
|
{
|
53
|
long record;
|
54
|
boolean submitted;
|
55
|
|
56
|
MapValue(long rec,int sub)
|
57
|
{
|
58
|
record = rec;
|
59
|
submitted = sub!=0;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
private final HashMap<Integer,MapValue> mMap;
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
private RubikScores()
|
68
|
{
|
69
|
mMap = new HashMap<>();
|
70
|
|
71
|
mName = "";
|
72
|
mCountry = "un";
|
73
|
|
74
|
mNameIsVerified = false;
|
75
|
|
76
|
mNumPlays= -1;
|
77
|
mNumRuns = -1;
|
78
|
mDeviceID= -1;
|
79
|
mNumWins = 0;
|
80
|
mNumStars= 0;
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
private int mapKey(int object,int level)
|
86
|
{
|
87
|
return object*MULT + level;
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
private int privateGetDeviceID()
|
93
|
{
|
94
|
int id;
|
95
|
|
96
|
try
|
97
|
{
|
98
|
String s = UUID.randomUUID().toString();
|
99
|
id = s.hashCode();
|
100
|
}
|
101
|
catch(Exception ex)
|
102
|
{
|
103
|
id = 0;
|
104
|
android.util.Log.e("scores", "Exception in getDeviceID()");
|
105
|
}
|
106
|
|
107
|
return id<0 ? -id : id;
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
synchronized void successfulSubmit()
|
113
|
{
|
114
|
mNameIsVerified = true;
|
115
|
|
116
|
for(int key: mMap.keySet())
|
117
|
{
|
118
|
MapValue value = mMap.get(key);
|
119
|
if( value!=null ) value.submitted = true;
|
120
|
}
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
int getDeviceID()
|
126
|
{
|
127
|
return mDeviceID;
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
synchronized boolean thereAreUnsubmittedRecords()
|
133
|
{
|
134
|
for(int key: mMap.keySet())
|
135
|
{
|
136
|
MapValue value = mMap.get(key);
|
137
|
if( value!=null && !value.submitted && value.record<NO_RECORD) return true;
|
138
|
}
|
139
|
|
140
|
return false;
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
synchronized String getRecordList(String strObj, String strLvl, String strTim)
|
146
|
{
|
147
|
StringBuilder builderObj = new StringBuilder();
|
148
|
StringBuilder builderLvl = new StringBuilder();
|
149
|
StringBuilder builderTim = new StringBuilder();
|
150
|
boolean first = true;
|
151
|
|
152
|
for(int key: mMap.keySet())
|
153
|
{
|
154
|
MapValue value = mMap.get(key);
|
155
|
|
156
|
if( value!=null && !value.submitted && value.record<NO_RECORD)
|
157
|
{
|
158
|
if( !first )
|
159
|
{
|
160
|
builderObj.append(',');
|
161
|
builderLvl.append(',');
|
162
|
builderTim.append(',');
|
163
|
}
|
164
|
first=false;
|
165
|
|
166
|
RubikObject object = RubikObjectList.getObject(key/MULT);
|
167
|
|
168
|
if( object!=null )
|
169
|
{
|
170
|
builderObj.append(object.getUpperName());
|
171
|
builderLvl.append(key%MULT);
|
172
|
builderTim.append(value.record);
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
|
177
|
return strObj+builderObj.toString()+strLvl+builderLvl.toString()+strTim+builderTim.toString();
|
178
|
}
|
179
|
|
180
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
181
|
// Public API
|
182
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
183
|
|
184
|
public boolean isVerified()
|
185
|
{
|
186
|
return mNameIsVerified;
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
public int getNumPlays()
|
192
|
{
|
193
|
return mNumPlays;
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
|
198
|
public int getNumRuns()
|
199
|
{
|
200
|
return mNumRuns;
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
public String getName()
|
206
|
{
|
207
|
return mName;
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
public String getCountry()
|
213
|
{
|
214
|
return mCountry;
|
215
|
}
|
216
|
|
217
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
218
|
|
219
|
public void incrementNumPlays()
|
220
|
{
|
221
|
mNumPlays++;
|
222
|
}
|
223
|
|
224
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
225
|
|
226
|
public void incrementNumRuns()
|
227
|
{
|
228
|
mNumRuns++;
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
|
233
|
public int incrementNumWins()
|
234
|
{
|
235
|
mNumWins++;
|
236
|
return mNumWins;
|
237
|
}
|
238
|
|
239
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240
|
|
241
|
public void changeNumStars(int stars)
|
242
|
{
|
243
|
mNumStars += stars;
|
244
|
if( mNumStars<0 ) mNumStars = 0;
|
245
|
}
|
246
|
|
247
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
248
|
|
249
|
public int getNumStars()
|
250
|
{
|
251
|
return mNumStars;
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
public void correctNumStars()
|
257
|
{
|
258
|
int numObjects = RubikObjectList.getNumObjects();
|
259
|
|
260
|
for(int obj=0; obj<numObjects; obj++)
|
261
|
{
|
262
|
for(int level=0; level<=RubikScreenPlay.LEVELS_SHOWN; level++)
|
263
|
{
|
264
|
if( isSolved(obj,level) )
|
265
|
{
|
266
|
int numStars = computeNumStars(level+1);
|
267
|
mNumStars += numStars;
|
268
|
}
|
269
|
}
|
270
|
}
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
|
275
|
public int computeNumStars(int level)
|
276
|
{
|
277
|
return level>LEVELS_SHOWN ? 50 : level;
|
278
|
}
|
279
|
|
280
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281
|
|
282
|
public void setName(String newName)
|
283
|
{
|
284
|
mName = newName;
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
public synchronized int setRecord(int object, int level, long record)
|
290
|
{
|
291
|
int key = mapKey(object,level);
|
292
|
MapValue oldValue = mMap.get(key);
|
293
|
|
294
|
if( oldValue==null )
|
295
|
{
|
296
|
MapValue value = new MapValue(record,0);
|
297
|
mMap.put(key,value);
|
298
|
return RECORD_FIRST;
|
299
|
}
|
300
|
|
301
|
long oldRecord = oldValue.record;
|
302
|
|
303
|
if( oldRecord>record)
|
304
|
{
|
305
|
MapValue value = new MapValue(record,0);
|
306
|
mMap.put(key,value);
|
307
|
return RECORD_NEW;
|
308
|
}
|
309
|
|
310
|
return RECORD_NOT_NEW;
|
311
|
}
|
312
|
|
313
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314
|
|
315
|
public synchronized long getRecord(int object, int level)
|
316
|
{
|
317
|
int key = mapKey(object,level);
|
318
|
MapValue value = mMap.get(key);
|
319
|
return value!=null ? value.record : NO_RECORD;
|
320
|
}
|
321
|
|
322
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
323
|
|
324
|
public synchronized boolean isSolved(int object, int level)
|
325
|
{
|
326
|
int key = mapKey(object,level);
|
327
|
MapValue value = mMap.get(key);
|
328
|
return value!=null && value.record<NO_RECORD;
|
329
|
}
|
330
|
|
331
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332
|
|
333
|
public void setCountry(Context context)
|
334
|
{
|
335
|
TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
|
336
|
|
337
|
if( tM!=null )
|
338
|
{
|
339
|
mCountry = tM.getSimCountryIso();
|
340
|
|
341
|
if( mCountry==null || mCountry.length()<=1 )
|
342
|
{
|
343
|
mCountry = tM.getNetworkCountryIso();
|
344
|
}
|
345
|
}
|
346
|
|
347
|
// Special case: Dominicana. Its ISO-3166-alpha-2 country code is 'do' which we can't have here
|
348
|
// because we later on map this to a resource name (the flag) and 'do' is a reserved Java keyword
|
349
|
// and can't be a resource name.
|
350
|
|
351
|
if( mCountry.equals("do") ) mCountry = "dm";
|
352
|
}
|
353
|
|
354
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
355
|
|
356
|
public void setCountry(String country)
|
357
|
{
|
358
|
mCountry = country;
|
359
|
|
360
|
if( mCountry.equals("do") ) mCountry = "dm"; // see above
|
361
|
}
|
362
|
|
363
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
364
|
|
365
|
public static RubikScores getInstance()
|
366
|
{
|
367
|
if( mThis==null ) mThis = new RubikScores();
|
368
|
return mThis;
|
369
|
}
|
370
|
|
371
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
372
|
|
373
|
public synchronized void savePreferences(SharedPreferences.Editor editor)
|
374
|
{
|
375
|
int numObjects = RubikObjectList.getNumObjects();
|
376
|
StringBuilder builder = new StringBuilder();
|
377
|
|
378
|
for(int level=0; level<=MAX_RECORD; level++)
|
379
|
{
|
380
|
builder.setLength(0);
|
381
|
|
382
|
for(int object=0; object<numObjects; object++)
|
383
|
{
|
384
|
int key = mapKey(object,level);
|
385
|
RubikObject obj = RubikObjectList.getObject(object);
|
386
|
MapValue value = mMap.get(key);
|
387
|
|
388
|
if( obj!=null && value!=null && value.record<NO_RECORD )
|
389
|
{
|
390
|
builder.append(obj.getUpperName());
|
391
|
builder.append("=");
|
392
|
builder.append(value.record);
|
393
|
builder.append(",");
|
394
|
builder.append(value.submitted ? 1:0 );
|
395
|
builder.append(" ");
|
396
|
}
|
397
|
}
|
398
|
|
399
|
editor.putString("scores_record"+level, builder.toString());
|
400
|
}
|
401
|
|
402
|
editor.putString("scores_name" , mName );
|
403
|
editor.putBoolean("scores_isVerified", mNameIsVerified);
|
404
|
editor.putInt("scores_numPlays", mNumPlays);
|
405
|
editor.putInt("scores_numRuns" , mNumRuns );
|
406
|
editor.putInt("scores_deviceid", mDeviceID);
|
407
|
editor.putInt("scores_review" , mNumWins ); // legacy name
|
408
|
editor.putInt("scores_numStars", mNumStars );
|
409
|
}
|
410
|
|
411
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
412
|
|
413
|
public synchronized void savePreferencesMinimal(SharedPreferences.Editor editor)
|
414
|
{
|
415
|
editor.putInt("scores_numStars", mNumStars );
|
416
|
}
|
417
|
|
418
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
419
|
|
420
|
public synchronized void restorePreferences(SharedPreferences preferences)
|
421
|
{
|
422
|
String recordStr, subStr, nameStr, timeStr, submStr, errorStr="";
|
423
|
int start, end, equals, comma, ordinal, subm;
|
424
|
long time;
|
425
|
boolean thereWasError = false;
|
426
|
int numObjects = RubikObjectList.getNumObjects();
|
427
|
|
428
|
for(int level=0; level<=MAX_SCRAMBLES; level++)
|
429
|
{
|
430
|
recordStr = preferences.getString("scores_record"+level, null);
|
431
|
if( recordStr==null ) continue;
|
432
|
start = end = 0;
|
433
|
|
434
|
while( end!=-1 )
|
435
|
{
|
436
|
end = recordStr.indexOf(" ", start);
|
437
|
|
438
|
if( end==-1 ) subStr = recordStr.substring(start);
|
439
|
else subStr = recordStr.substring(start,end);
|
440
|
|
441
|
start = end+1;
|
442
|
|
443
|
equals = subStr.indexOf("=");
|
444
|
comma = subStr.indexOf(",");
|
445
|
|
446
|
if( equals>=0 && comma>=0 )
|
447
|
{
|
448
|
nameStr = subStr.substring(0,equals);
|
449
|
timeStr = subStr.substring(equals+1,comma);
|
450
|
submStr = subStr.substring(comma+1);
|
451
|
|
452
|
ordinal = RubikObjectList.getOrdinal(nameStr);
|
453
|
|
454
|
if( ordinal>=0 && ordinal<numObjects )
|
455
|
{
|
456
|
time = Long.parseLong(timeStr);
|
457
|
subm = Integer.parseInt(submStr);
|
458
|
|
459
|
if( subm>=0 && subm<=1 )
|
460
|
{
|
461
|
MapValue value = new MapValue(time,subm);
|
462
|
int key = mapKey(ordinal,level);
|
463
|
mMap.put(key,value);
|
464
|
}
|
465
|
else
|
466
|
{
|
467
|
errorStr += ("error1: subm="+subm+" obj: "+nameStr+"\n");
|
468
|
thereWasError= true;
|
469
|
}
|
470
|
}
|
471
|
else
|
472
|
{
|
473
|
errorStr += ("error2: object="+ordinal+" obj: "+nameStr+"\n");
|
474
|
thereWasError = true;
|
475
|
}
|
476
|
}
|
477
|
}
|
478
|
}
|
479
|
|
480
|
mName = preferences.getString("scores_name" , "" );
|
481
|
mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
|
482
|
mNumPlays = preferences.getInt("scores_numPlays", 0);
|
483
|
mNumRuns = preferences.getInt("scores_numRuns" , 0);
|
484
|
mDeviceID = preferences.getInt("scores_deviceid",-1);
|
485
|
mNumWins = preferences.getInt("scores_review" , 0);
|
486
|
mNumStars = preferences.getInt("scores_numStars", 0);
|
487
|
|
488
|
if( mDeviceID==-1 ) mDeviceID = privateGetDeviceID();
|
489
|
|
490
|
if( thereWasError ) recordDBError(errorStr);
|
491
|
}
|
492
|
|
493
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
494
|
|
495
|
public int numberOfSolvedMAXes()
|
496
|
{
|
497
|
int numObjects = RubikObjectList.getNumObjects();
|
498
|
int ret=0, level = RubikScreenPlay.LEVELS_SHOWN;
|
499
|
|
500
|
for(int obj=0; obj<numObjects; obj++)
|
501
|
{
|
502
|
if( isSolved(obj,level) ) ret++;
|
503
|
}
|
504
|
|
505
|
return ret;
|
506
|
}
|
507
|
|
508
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
509
|
|
510
|
public void recordDBError(String message)
|
511
|
{
|
512
|
if( BuildConfig.DEBUG )
|
513
|
{
|
514
|
android.util.Log.e("scores", message);
|
515
|
}
|
516
|
else
|
517
|
{
|
518
|
Exception ex = new Exception(message);
|
519
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
520
|
crashlytics.setCustomKey("scores" , message);
|
521
|
crashlytics.recordException(ex);
|
522
|
}
|
523
|
}
|
524
|
}
|