Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ 286d73ae

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