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 boolean 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
|
return true;
|
230
|
}
|
231
|
}
|
232
|
|
233
|
return false;
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
public void incrementNumPlays()
|
239
|
{
|
240
|
mNumPlays++;
|
241
|
}
|
242
|
|
243
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244
|
|
245
|
public void incrementNumRuns()
|
246
|
{
|
247
|
mNumRuns++;
|
248
|
}
|
249
|
|
250
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251
|
// TODO
|
252
|
|
253
|
public void setName(String newName)
|
254
|
{
|
255
|
mName = newName;
|
256
|
}
|
257
|
|
258
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
259
|
// TODO
|
260
|
|
261
|
public void verifyName()
|
262
|
{
|
263
|
mNameIsVerified = true;
|
264
|
}
|
265
|
|
266
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
267
|
// TODO
|
268
|
|
269
|
public void setSubmitted(int object, int size, int scramble)
|
270
|
{
|
271
|
int maxsize = RubikObjectList.getObject(object).getSizes().length;
|
272
|
|
273
|
if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
|
274
|
{
|
275
|
mSubmitted[object][size][scramble] = 1;
|
276
|
}
|
277
|
}
|
278
|
|
279
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
280
|
|
281
|
public void setCountry(Context context)
|
282
|
{
|
283
|
TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
|
284
|
|
285
|
if( tM!=null )
|
286
|
{
|
287
|
mCountry = tM.getSimCountryIso();
|
288
|
|
289
|
if( mCountry==null || mCountry.length()<=1 )
|
290
|
{
|
291
|
mCountry = tM.getNetworkCountryIso();
|
292
|
}
|
293
|
}
|
294
|
|
295
|
// Special case: Dominicana. Its ISO-3166-alpha-2 country code is 'do' which we can't have here
|
296
|
// because we later on map this to a resource name (the flag) and 'do' is a resevred Java keyword
|
297
|
// and can't be a resource name.
|
298
|
|
299
|
if( mCountry.equals("do") ) mCountry = "dm";
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
|
304
|
private int getDeviceID()
|
305
|
{
|
306
|
int id;
|
307
|
|
308
|
try
|
309
|
{
|
310
|
String s = UUID.randomUUID().toString();
|
311
|
id = (s!=null ? s.hashCode():0);
|
312
|
}
|
313
|
catch(Exception ex)
|
314
|
{
|
315
|
id = 0;
|
316
|
android.util.Log.e("scores", "Exception in getDeviceID()");
|
317
|
}
|
318
|
|
319
|
return id<0 ? -id : id;
|
320
|
}
|
321
|
|
322
|
|
323
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
324
|
|
325
|
public long getRecord(int object, int size, int scramble)
|
326
|
{
|
327
|
int maxsize = RubikObjectList.getObject(object).getSizes().length;
|
328
|
|
329
|
if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
|
330
|
{
|
331
|
return mRecords[object][size][scramble];
|
332
|
}
|
333
|
|
334
|
return -1;
|
335
|
}
|
336
|
|
337
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
338
|
|
339
|
public boolean isSubmitted(int object, int size, int scramble)
|
340
|
{
|
341
|
int maxsize = RubikObjectList.getObject(object).getSizes().length;
|
342
|
|
343
|
if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
|
344
|
{
|
345
|
return mSubmitted[object][size][scramble]==1;
|
346
|
}
|
347
|
|
348
|
return false;
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
// TODO
|
353
|
|
354
|
public boolean isVerified()
|
355
|
{
|
356
|
return mNameIsVerified;
|
357
|
}
|
358
|
|
359
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
360
|
// TODO
|
361
|
|
362
|
public int getNumPlays()
|
363
|
{
|
364
|
return mNumPlays;
|
365
|
}
|
366
|
|
367
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
368
|
|
369
|
public int getNumRuns()
|
370
|
{
|
371
|
return mNumRuns;
|
372
|
}
|
373
|
|
374
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
375
|
|
376
|
public String getName()
|
377
|
{
|
378
|
return mName;
|
379
|
}
|
380
|
|
381
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
382
|
|
383
|
public String getCountry()
|
384
|
{
|
385
|
return mCountry;
|
386
|
}
|
387
|
}
|