Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ 1c90c64a

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.SharedPreferences;
23

    
24
import org.distorted.magic.R;
25
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
// hold my own scores, some other statistics.
33

    
34
public class RubikScores
35
  {
36
  public static final long NO_RECORD = Long.MAX_VALUE;
37
  private static RubikScores mThis;
38

    
39
  private long[][][] mRecords;
40
  private String mName;
41
  private boolean mNameIsVerified;
42
  private int mNumRuns;
43
  private int mNumPlays;
44
  private int mCountryID;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  private RubikScores()
49
    {
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

    
59
    mName = "YOU";
60
    mNameIsVerified = false;
61
    mNumPlays= -1;
62
    mNumRuns = -1;
63
    mCountryID = R.drawable.unk;
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  public static RubikScores getInstance()
69
    {
70
    if( mThis==null )
71
      {
72
      mThis = new RubikScores();
73
      }
74

    
75
    return mThis;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      editor.putString("scores_record"+scramble, builder.toString());
109
      }
110

    
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
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void restorePreferences(SharedPreferences preferences)
120
    {
121
    String recordStr, subStr, nameStr, sizeStr, timeStr;
122
    int start, end, equals, underscore;
123
    int object, size;
124
    long time;
125

    
126
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
127
      {
128
      start = end = 0;
129
      recordStr = preferences.getString("scores_record"+scramble, "");
130

    
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
          time   = Long.parseLong(timeStr);
152

    
153
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
154
            {
155
            mRecords[object][size][scramble] = time;
156

    
157
            if( time<NO_RECORD )
158
              {
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

    
170
    mName           = preferences.getString("scores_name"  , "YOU" );
171
    mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
172
    mNumPlays       = preferences.getInt("scores_numPlays", 0);
173
    mNumRuns        = preferences.getInt("scores_numRuns" , 0);
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public void setRecord(int object, int size, int scramble, long timeTaken)
179
    {
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
        android.util.Log.e("RubikScores","new record: ("+object+","+size+","+scramble+") ="+timeTaken);
188
        }
189
      }
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public void incrementNumPlays()
195
    {
196
    mNumPlays++;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public void incrementNumRuns()
202
    {
203
    mNumRuns++;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
// TODO
208

    
209
  public void setName(String newName)
210
    {
211
    mName = newName;
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
// TODO
216

    
217
  public void verifyName()
218
    {
219
    mNameIsVerified = true;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
// TODO
224

    
225
  public void setCountry(int country)
226
    {
227
    mCountryID = country;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  public long getRecord(int object, int size, int scramble)
233
    {
234
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
235

    
236
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=0 && scramble<MAX_SCRAMBLE )
237
      {
238
      return mRecords[object][size][scramble];
239
      }
240

    
241
    return -1;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
// TODO
246

    
247
  public boolean isVerified()
248
    {
249
    return mNameIsVerified;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
// TODO
254

    
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
  public int getCountryID()
277
    {
278
    return mCountryID;
279
    }
280
  }
(1-1/2)