Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ c3ffcf58

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
import org.distorted.object.RubikObjectList;
24

    
25
import static org.distorted.object.RubikObjectList.MAX_SIZE;
26
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
27
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
// hold my own scores, some other statistics.
31

    
32
public class RubikScores
33
  {
34
  private static final long NO_RECORD = Integer.MAX_VALUE;
35
  private static RubikScores mThis;
36

    
37
  private long[][][] mRecords;
38
  private String mName;
39
  private boolean mNameIsVerified;
40
  private int mNumRuns;
41
  private int mNumPlays;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  private RubikScores()
46
    {
47
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
48

    
49
    for(int i=0; i<NUM_OBJECTS; i++)
50
      for(int j=0; j<MAX_SIZE; j++)
51
        for(int k=0; k<MAX_SCRAMBLE; k++)
52
          {
53
          mRecords[i][j][k] = NO_RECORD;
54
          }
55

    
56
    mName = "";
57
    mNameIsVerified = false;
58
    mNumPlays= -1;
59
    mNumRuns = -1;
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public static RubikScores getInstance()
65
    {
66
    if( mThis==null )
67
      {
68
      mThis = new RubikScores();
69
      }
70

    
71
    return mThis;
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  public void savePreferences(SharedPreferences.Editor editor)
77
    {
78
    StringBuilder builder = new StringBuilder();
79
    RubikObjectList list;
80
    int[] sizes;
81
    int length;
82

    
83
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
84
      {
85
      builder.setLength(0);
86

    
87
      for(int object=0; object<NUM_OBJECTS; object++)
88
        {
89
        list = RubikObjectList.getObject(object);
90
        sizes = list.getSizes();
91
        length = sizes.length;
92

    
93
        for(int size=0; size<length; size++)
94
          {
95
          builder.append(list.name());
96
          builder.append("_");
97
          builder.append(sizes[size]);
98
          builder.append("=");
99
          builder.append(mRecords[object][size][scramble]);
100
          builder.append(" ");
101
          }
102
        }
103

    
104
      editor.putString("scores_record"+scramble, builder.toString());
105
      }
106

    
107
    editor.putString("scores_name"  , mName  );
108
    editor.putBoolean("scores_isVerified", mNameIsVerified);
109
    editor.putInt("scores_numPlays", mNumPlays);
110
    editor.putInt("scores_numRuns" , mNumRuns);
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public void restorePreferences(SharedPreferences preferences)
116
    {
117
    String recordStr, subStr, nameStr, sizeStr, timeStr;
118
    int start, end, equals, underscore;
119
    int object, size, time;
120

    
121
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
122
      {
123
      start = end = 0;
124
      recordStr = preferences.getString("scores_record"+scramble, "");
125

    
126
      while( end!=-1 )
127
        {
128
        end = recordStr.indexOf(" ", start);
129

    
130
        if( end==-1 ) subStr = recordStr.substring(start);
131
        else          subStr = recordStr.substring(start,end);
132

    
133
        start = end+1;
134

    
135
        underscore = subStr.indexOf("_");
136
        equals = subStr.indexOf("=");
137

    
138
        if( underscore>=0 && equals>=0 )
139
          {
140
          nameStr = subStr.substring(0,underscore);
141
          sizeStr = subStr.substring(underscore+1, equals);
142
          timeStr = subStr.substring(equals+1);
143

    
144
          object = RubikObjectList.getOrdinal(nameStr);
145
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
146
          time   = Integer.parseInt(timeStr);
147

    
148
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
149
            {
150
            mRecords[object][size][scramble] = time;
151

    
152
            if( time<Integer.MAX_VALUE )
153
              {
154
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
155
              }
156
            }
157
          else
158
            {
159
            android.util.Log.e("solv", "error: object="+object+" size="+size);
160
            }
161
          }
162
        }
163
      }
164

    
165
    mName           = preferences.getString("scores_name"  , "");
166
    mNameIsVerified = preferences.getBoolean("scores_isVerified", false);
167
    mNumPlays       = preferences.getInt("scores_numPlays", 0);
168
    mNumRuns        = preferences.getInt("scores_numRuns" , 0);
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  public void setRecord(int object, int size, int scramble, long timeTaken)
174
    {
175
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
176

    
177
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
178
      {
179
      if( mRecords[object][size][scramble-1]> timeTaken )
180
        {
181
        mRecords[object][size][scramble-1] = timeTaken;
182
        android.util.Log.e("RubikScores","new record: ("+object+","+size+","+scramble+") ="+timeTaken);
183
        }
184
      }
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public void incrementNumPlays()
190
    {
191
    mNumPlays++;
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public void incrementNumRuns()
197
    {
198
    mNumRuns++;
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  public void setName(String newName)
204
    {
205
    mName = newName;
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  public void verifyName()
211
    {
212
    mNameIsVerified = true;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public long getRecord(int object, int size, int scramble)
218
    {
219
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
220

    
221
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
222
      {
223
      return mRecords[object][size][scramble];
224
      }
225

    
226
    return -1;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public int getNumPlays()
232
    {
233
    return mNumPlays;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public int getNumRuns()
239
    {
240
    return mNumRuns;
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  public String getName()
246
    {
247
    return mName;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  public boolean isVerified()
253
    {
254
    return mNameIsVerified;
255
    }
256
  }
(1-1/2)