1 |
f0e87514
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
1c90c64a
|
Leszek Koltunski
|
|
24 |
|
|
import org.distorted.magic.R;
|
25 |
f0e87514
|
Leszek Koltunski
|
import org.distorted.object.RubikObjectList;
|
26 |
|
|
|
27 |
|
|
import static org.distorted.object.RubikObjectList.MAX_SIZE;
|
28 |
|
|
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
|
29 |
|
|
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
|
30 |
|
|
|
31 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32 |
c3ffcf58
|
Leszek Koltunski
|
// hold my own scores, some other statistics.
|
33 |
f0e87514
|
Leszek Koltunski
|
|
34 |
|
|
public class RubikScores
|
35 |
|
|
{
|
36 |
1c90c64a
|
Leszek Koltunski
|
public static final long NO_RECORD = Long.MAX_VALUE;
|
37 |
714292f1
|
Leszek Koltunski
|
private static RubikScores mThis;
|
38 |
|
|
|
39 |
f0e87514
|
Leszek Koltunski
|
private long[][][] mRecords;
|
40 |
c3ffcf58
|
Leszek Koltunski
|
private String mName;
|
41 |
|
|
private boolean mNameIsVerified;
|
42 |
|
|
private int mNumRuns;
|
43 |
|
|
private int mNumPlays;
|
44 |
1c90c64a
|
Leszek Koltunski
|
private int mCountryID;
|
45 |
f0e87514
|
Leszek Koltunski
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
714292f1
|
Leszek Koltunski
|
private RubikScores()
|
49 |
f0e87514
|
Leszek Koltunski
|
{
|
50 |
|
|
mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
|
51 |
|
|
|
52 |
|
|
for(int i=0; i<NUM_OBJECTS; i++)
|
53 |
|
|
for(int j=0; j<MAX_SIZE; j++)
|
54 |
|
|
for(int k=0; k<MAX_SCRAMBLE; k++)
|
55 |
|
|
{
|
56 |
|
|
mRecords[i][j][k] = NO_RECORD;
|
57 |
|
|
}
|
58 |
c3ffcf58
|
Leszek Koltunski
|
|
59 |
1c90c64a
|
Leszek Koltunski
|
mName = "YOU";
|
60 |
c3ffcf58
|
Leszek Koltunski
|
mNameIsVerified = false;
|
61 |
|
|
mNumPlays= -1;
|
62 |
|
|
mNumRuns = -1;
|
63 |
1c90c64a
|
Leszek Koltunski
|
mCountryID = R.drawable.unk;
|
64 |
f0e87514
|
Leszek Koltunski
|
}
|
65 |
|
|
|
66 |
714292f1
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67 |
|
|
|
68 |
|
|
public static RubikScores getInstance()
|
69 |
|
|
{
|
70 |
|
|
if( mThis==null )
|
71 |
|
|
{
|
72 |
|
|
mThis = new RubikScores();
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
return mThis;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
f0e87514
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79 |
|
|
|
80 |
|
|
public void savePreferences(SharedPreferences.Editor editor)
|
81 |
|
|
{
|
82 |
|
|
StringBuilder builder = new StringBuilder();
|
83 |
|
|
RubikObjectList list;
|
84 |
|
|
int[] sizes;
|
85 |
|
|
int length;
|
86 |
|
|
|
87 |
|
|
for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
|
88 |
|
|
{
|
89 |
|
|
builder.setLength(0);
|
90 |
|
|
|
91 |
|
|
for(int object=0; object<NUM_OBJECTS; object++)
|
92 |
|
|
{
|
93 |
|
|
list = RubikObjectList.getObject(object);
|
94 |
|
|
sizes = list.getSizes();
|
95 |
|
|
length = sizes.length;
|
96 |
|
|
|
97 |
|
|
for(int size=0; size<length; size++)
|
98 |
|
|
{
|
99 |
|
|
builder.append(list.name());
|
100 |
|
|
builder.append("_");
|
101 |
|
|
builder.append(sizes[size]);
|
102 |
|
|
builder.append("=");
|
103 |
|
|
builder.append(mRecords[object][size][scramble]);
|
104 |
|
|
builder.append(" ");
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
c3ffcf58
|
Leszek Koltunski
|
editor.putString("scores_record"+scramble, builder.toString());
|
109 |
f0e87514
|
Leszek Koltunski
|
}
|
110 |
c3ffcf58
|
Leszek Koltunski
|
|
111 |
|
|
editor.putString("scores_name" , mName );
|
112 |
|
|
editor.putBoolean("scores_isVerified", mNameIsVerified);
|
113 |
|
|
editor.putInt("scores_numPlays", mNumPlays);
|
114 |
|
|
editor.putInt("scores_numRuns" , mNumRuns);
|
115 |
f0e87514
|
Leszek Koltunski
|
}
|
116 |
|
|
|
117 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
|
|
119 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
120 |
|
|
{
|
121 |
|
|
String recordStr, subStr, nameStr, sizeStr, timeStr;
|
122 |
|
|
int start, end, equals, underscore;
|
123 |
1c90c64a
|
Leszek Koltunski
|
int object, size;
|
124 |
|
|
long time;
|
125 |
f0e87514
|
Leszek Koltunski
|
|
126 |
|
|
for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
|
127 |
|
|
{
|
128 |
|
|
start = end = 0;
|
129 |
c3ffcf58
|
Leszek Koltunski
|
recordStr = preferences.getString("scores_record"+scramble, "");
|
130 |
f0e87514
|
Leszek Koltunski
|
|
131 |
|
|
while( end!=-1 )
|
132 |
|
|
{
|
133 |
|
|
end = recordStr.indexOf(" ", start);
|
134 |
|
|
|
135 |
|
|
if( end==-1 ) subStr = recordStr.substring(start);
|
136 |
|
|
else subStr = recordStr.substring(start,end);
|
137 |
|
|
|
138 |
|
|
start = end+1;
|
139 |
|
|
|
140 |
|
|
underscore = subStr.indexOf("_");
|
141 |
|
|
equals = subStr.indexOf("=");
|
142 |
|
|
|
143 |
|
|
if( underscore>=0 && equals>=0 )
|
144 |
|
|
{
|
145 |
|
|
nameStr = subStr.substring(0,underscore);
|
146 |
|
|
sizeStr = subStr.substring(underscore+1, equals);
|
147 |
|
|
timeStr = subStr.substring(equals+1);
|
148 |
|
|
|
149 |
|
|
object = RubikObjectList.getOrdinal(nameStr);
|
150 |
|
|
size = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
|
151 |
1c90c64a
|
Leszek Koltunski
|
time = Long.parseLong(timeStr);
|
152 |
f0e87514
|
Leszek Koltunski
|
|
153 |
|
|
if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
|
154 |
|
|
{
|
155 |
|
|
mRecords[object][size][scramble] = time;
|
156 |
|
|
|
157 |
1c90c64a
|
Leszek Koltunski
|
if( time<NO_RECORD )
|
158 |
f0e87514
|
Leszek Koltunski
|
{
|
159 |
|
|
android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
else
|
163 |
|
|
{
|
164 |
|
|
android.util.Log.e("solv", "error: object="+object+" size="+size);
|
165 |
|
|
}
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
c3ffcf58
|
Leszek Koltunski
|
|
170 |
1c90c64a
|
Leszek Koltunski
|
mName = preferences.getString("scores_name" , "YOU" );
|
171 |
c3ffcf58
|
Leszek Koltunski
|
mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
|
172 |
|
|
mNumPlays = preferences.getInt("scores_numPlays", 0);
|
173 |
|
|
mNumRuns = preferences.getInt("scores_numRuns" , 0);
|
174 |
f0e87514
|
Leszek Koltunski
|
}
|
175 |
|
|
|
176 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
177 |
|
|
|
178 |
c3ffcf58
|
Leszek Koltunski
|
public void setRecord(int object, int size, int scramble, long timeTaken)
|
179 |
f0e87514
|
Leszek Koltunski
|
{
|
180 |
|
|
int maxsize = RubikObjectList.getObject(object).getSizes().length;
|
181 |
|
|
|
182 |
|
|
if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
|
183 |
|
|
{
|
184 |
|
|
if( mRecords[object][size][scramble-1]> timeTaken )
|
185 |
|
|
{
|
186 |
|
|
mRecords[object][size][scramble-1] = timeTaken;
|
187 |
714292f1
|
Leszek Koltunski
|
android.util.Log.e("RubikScores","new record: ("+object+","+size+","+scramble+") ="+timeTaken);
|
188 |
f0e87514
|
Leszek Koltunski
|
}
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
714292f1
|
Leszek Koltunski
|
|
192 |
c3ffcf58
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193 |
|
|
|
194 |
|
|
public void incrementNumPlays()
|
195 |
|
|
{
|
196 |
|
|
mNumPlays++;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200 |
|
|
|
201 |
|
|
public void incrementNumRuns()
|
202 |
|
|
{
|
203 |
|
|
mNumRuns++;
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
207 |
1c90c64a
|
Leszek Koltunski
|
// TODO
|
208 |
c3ffcf58
|
Leszek Koltunski
|
|
209 |
|
|
public void setName(String newName)
|
210 |
|
|
{
|
211 |
|
|
mName = newName;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215 |
1c90c64a
|
Leszek Koltunski
|
// TODO
|
216 |
c3ffcf58
|
Leszek Koltunski
|
|
217 |
|
|
public void verifyName()
|
218 |
|
|
{
|
219 |
|
|
mNameIsVerified = true;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
1c90c64a
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223 |
|
|
// TODO
|
224 |
|
|
|
225 |
|
|
public void setCountry(int country)
|
226 |
|
|
{
|
227 |
|
|
mCountryID = country;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
714292f1
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231 |
|
|
|
232 |
|
|
public long getRecord(int object, int size, int scramble)
|
233 |
|
|
{
|
234 |
|
|
int maxsize = RubikObjectList.getObject(object).getSizes().length;
|
235 |
|
|
|
236 |
1c90c64a
|
Leszek Koltunski
|
if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
|
237 |
714292f1
|
Leszek Koltunski
|
{
|
238 |
|
|
return mRecords[object][size][scramble];
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
return -1;
|
242 |
|
|
}
|
243 |
c3ffcf58
|
Leszek Koltunski
|
|
244 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245 |
1c90c64a
|
Leszek Koltunski
|
// TODO
|
246 |
|
|
|
247 |
|
|
public boolean isVerified()
|
248 |
|
|
{
|
249 |
|
|
return mNameIsVerified;
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253 |
|
|
// TODO
|
254 |
c3ffcf58
|
Leszek Koltunski
|
|
255 |
|
|
public int getNumPlays()
|
256 |
|
|
{
|
257 |
|
|
return mNumPlays;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261 |
|
|
|
262 |
|
|
public int getNumRuns()
|
263 |
|
|
{
|
264 |
|
|
return mNumRuns;
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268 |
|
|
|
269 |
|
|
public String getName()
|
270 |
|
|
{
|
271 |
|
|
return mName;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275 |
|
|
|
276 |
1c90c64a
|
Leszek Koltunski
|
public int getCountryID()
|
277 |
c3ffcf58
|
Leszek Koltunski
|
{
|
278 |
1c90c64a
|
Leszek Koltunski
|
return mCountryID;
|
279 |
c3ffcf58
|
Leszek Koltunski
|
}
|
280 |
f0e87514
|
Leszek Koltunski
|
}
|