Project

General

Profile

« Previous | Next » 

Revision 6142069a

Added by Leszek Koltunski about 2 years ago

Progress with marking objects as free.

View differences:

src/main/java/org/distorted/external/RubikScores.java
21 21
import org.distorted.main.BuildConfig;
22 22
import org.distorted.objects.RubikObject;
23 23
import org.distorted.objects.RubikObjectList;
24
import org.distorted.screens.RubikScreenPlay;
24 25

  
25 26
import static org.distorted.objectlib.main.ObjectType.MAX_SCRAMBLES;
26 27

  
......
431 432
    if( thereWasError ) recordDBError(errorStr);
432 433
    }
433 434

  
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

  
437
  public int numberOfSolvedMAXes()
438
    {
439
    int numObjects = RubikObjectList.getNumObjects();
440
    int ret=0, level = RubikScreenPlay.LEVELS_SHOWN;
441

  
442
    for(int obj=0; obj<numObjects; obj++)
443
      {
444
      if( isSolved(obj,level) ) ret++;
445
      }
446

  
447
    return ret;
448
    }
449

  
434 450
///////////////////////////////////////////////////////////////////////////////////////////////////
435 451

  
436 452
  public void recordDBError(String message)
src/main/java/org/distorted/main/RubikActivity.java
61 61
public class RubikActivity extends AppCompatActivity
62 62
{
63 63
    public static final boolean SHOW_DOWNLOADED_DEBUG = false;
64
    public static final boolean SHOW_SOLVED_DEBUG     = true;
64 65

  
65 66
    public static final float PADDING             = 0.01f;
66 67
    public static final float SMALL_MARGIN        = 0.004f;
......
253 254
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
254 255
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
255 256
      view.onResume();
256
      restorePreferences(mJustStarted);
257 257

  
258
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
259
      restorePreferences(preferences,mJustStarted);
258 260
      ScreenList.setScreen(this);
259 261
      unblockEverything();
260

  
261
      restoreMoves();
262
      restoreMoves(preferences);
262 263

  
263 264
      if( mJustStarted )
264 265
        {
......
266 267
        RubikScores scores = RubikScores.getInstance();
267 268
        scores.incrementNumRuns();
268 269
        scores.setCountry(this);
269
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
270 270
        RubikObjectList.restoreMeshState(preferences);
271 271
        }
272 272

  
......
345 345

  
346 346
///////////////////////////////////////////////////////////////////////////////////////////////////
347 347

  
348
    private void restorePreferences(boolean justStarted)
348
    private void restorePreferences(SharedPreferences preferences, boolean justStarted)
349 349
      {
350
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
351

  
352 350
      mPolicyAccepted = preferences.getBoolean("policyAccepted", false);
353 351
      String oldVersion = preferences.getString("appVersion","");
354 352

  
......
364 362
        ScreenList.getScreen(i).getScreenClass().restorePreferences(preferences);
365 363
        }
366 364

  
365
      // this needs to be after the above ScreenList.restore as that restores the Records which we need here
366
      if( justStarted ) RubikObjectList.setObjectFreeState();
367

  
367 368
      ScreenList.restorePreferences(preferences);
368 369
      RubikSurfaceView view = findViewById(R.id.rubikSurfaceView);
369 370

  
......
392 393

  
393 394
///////////////////////////////////////////////////////////////////////////////////////////////////
394 395

  
395
    private void restoreMoves()
396
    private void restoreMoves(SharedPreferences preferences)
396 397
      {
397
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
398 398
      ScreenList curr = ScreenList.getCurrentScreen();
399 399

  
400 400
      if( curr==ScreenList.PLAY )
src/main/java/org/distorted/objects/RubikObjectList.java
16 16
import android.content.SharedPreferences;
17 17

  
18 18
import org.distorted.external.RubikFiles;
19
import org.distorted.external.RubikScores;
19 20
import org.distorted.main.RubikActivity;
20 21
import org.distorted.objectlib.main.ObjectSignatures;
21 22
import org.distorted.objectlib.main.ObjectType;
23
import org.distorted.screens.RubikScreenPlay;
22 24

  
23 25
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
24 26
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
25 27
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
28
import static org.distorted.main.RubikActivity.SHOW_SOLVED_DEBUG;
26 29

  
27 30
///////////////////////////////////////////////////////////////////////////////////////////////////
28 31

  
......
59 62
  private static ArrayList<DownloadedObject> mDownloadedObjects;
60 63
  private static String mFreeSolvedObjects;
61 64
  private static String mFreeBoughtObjects;
65
  private static int mNumFreeSolved;
62 66

  
63 67
///////////////////////////////////////////////////////////////////////////////////////////////////
64 68

  
65 69
  private RubikObjectList()
66 70
    {
67
    mNumObjects= 0;
68
    mNumExtras = 0;
71
    mNumObjects    = 0;
72
    mNumExtras     = 0;
73
    mNumFreeSolved = 0;
69 74

  
70 75
    mObjects           = new ArrayList<>();
71 76
    mDownloadedObjects = new ArrayList<>();
......
116 121
    mObjects.add(obj);
117 122
    mNumObjects++;
118 123

  
124
    int numUnclaimed = getNumUnclaimedSolves();
125

  
126
    if( numUnclaimed>0 && !obj.isFree() )
127
      {
128
      String objname = obj.getUpperName();
129
      if( SHOW_SOLVED_DEBUG ) android.util.Log.e("D", "downloadedObject: "+objname+" making it solved. Unclaimed: "+numUnclaimed);
130
      solveObject(obj,objname);
131
      }
132

  
119 133
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating downloaded object "+obj.getUpperName() );
120 134

  
121 135
    if( obj.hasExtras() )
......
133 147
    return true;
134 148
    }
135 149

  
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

  
152
  private static void restoreFreedObjects(SharedPreferences preferences)
153
    {
154
    mFreeSolvedObjects = preferences.getString("rol_freeSolved", "");
155
    mFreeBoughtObjects = preferences.getString("rol_freeBought", "");
156

  
157
    if( SHOW_SOLVED_DEBUG )
158
      {
159
      android.util.Log.e("D", "freeSolved: "+mFreeSolvedObjects);
160
      android.util.Log.e("D", "freeBought: "+mFreeBoughtObjects);
161
      }
162

  
163
    if( mFreeBoughtObjects.length()>0 )
164
      {
165
      if( mFreeBoughtObjects.charAt(0)=='*' )
166
        {
167
        for(int i=0; i<mNumObjects; i++)
168
          {
169
          RubikObject o = mObjects.get(i);
170
          o.markFree();
171
          }
172
        }
173
      else
174
        {
175
        String[] objs = mFreeBoughtObjects.split(",");
176

  
177
        for( String obj : objs )
178
          {
179
          RubikObject o = getObject(obj);
180
          if( o!=null ) o.markFree();
181
          }
182
        }
183
      }
184

  
185
    if( mFreeSolvedObjects.length()>0 )
186
      {
187
      String[] objs = mFreeSolvedObjects.split(",");
188
      mNumFreeSolved = objs.length;
189

  
190
      for( String obj : objs )
191
        {
192
        RubikObject o = getObject(obj);
193
        if( o!=null ) o.markFree();
194
        }
195
      }
196
    }
197

  
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

  
200
  private static int getNumUnclaimedSolves()
201
    {
202
    RubikScores scores = RubikScores.getInstance();
203
    int numMAXes = scores.numberOfSolvedMAXes();
204
    return numMAXes-mNumFreeSolved;
205
    }
206

  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

  
209
  private static int markAllSolvedAsFree()
210
    {
211
    RubikScores scores = RubikScores.getInstance();
212
    int numObjects = RubikObjectList.getNumObjects();
213
    int level = RubikScreenPlay.LEVELS_SHOWN;
214
    int ret = 0;
215

  
216
    for(int obj=0; obj<numObjects; obj++)
217
      {
218
      RubikObject object = getObject(obj);
219

  
220
      if( object!=null && !object.isFree() && scores.isSolved(obj,level) )
221
        {
222
        solveObject(object.getUpperName());
223
        ret++;
224
        }
225
      }
226

  
227
    return ret;
228
    }
229

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

  
232
  private static void markAsFree(int numToBeMarked)
233
    {
234
    int numObjects = RubikObjectList.getNumObjects();
235

  
236
    for(int obj=0; obj<numObjects && numToBeMarked>0; obj++)
237
      {
238
      RubikObject object = getObject(obj);
239

  
240
      if( object!=null && !object.isFree() )
241
        {
242
        solveObject(object.getUpperName());
243
        numToBeMarked--;
244
        }
245
      }
246
    }
247

  
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

  
250
  public static boolean solveObject(RubikObject object, String shortName)
251
    {
252
    if( object!=null && !object.isFree() )
253
      {
254
      if( SHOW_SOLVED_DEBUG ) android.util.Log.e("D", "object "+shortName+" marked as solved");
255
      object.markFree();
256
      String add = mFreeSolvedObjects.length()==0 ? shortName : (","+shortName);
257
      mFreeSolvedObjects += add;
258
      mNumFreeSolved++;
259
      return true;
260
      }
261

  
262
    return false;
263
    }
264

  
136 265
///////////////////////////////////////////////////////////////////////////////////////////////////
137 266
// PUBLIC API
138 267

  
......
242 371
  public static void buyAll()
243 372
    {
244 373
    mFreeBoughtObjects = "*";
374
    if( SHOW_SOLVED_DEBUG ) android.util.Log.e("D", "all objects marked as bought");
245 375

  
246 376
    for(int i=0; i<mNumObjects; i++)
247 377
      {
......
258 388

  
259 389
    if( o!=null && !o.isFree() )
260 390
      {
391
      if( SHOW_SOLVED_DEBUG ) android.util.Log.e("D", "object "+shortName+" marked as bought");
261 392
      o.markFree();
262 393
      String add = mFreeBoughtObjects.length()==0 ? shortName : (","+shortName);
263 394
      mFreeBoughtObjects += add;
......
271 402

  
272 403
  public static boolean solveObject(String shortName)
273 404
    {
274
    RubikObject o = getObject(shortName);
275

  
276
    if( o!=null && !o.isFree() )
277
      {
278
      o.markFree();
279
      String add = mFreeSolvedObjects.length()==0 ? shortName : (","+shortName);
280
      mFreeSolvedObjects += add;
281
      return true;
282
      }
283

  
284
    return false;
405
    RubikObject object = getObject(shortName);
406
    return solveObject(object,shortName);
285 407
    }
286 408

  
287 409
///////////////////////////////////////////////////////////////////////////////////////////////////
......
330 452

  
331 453
    editor.putString("rol_freeSolved", mFreeSolvedObjects);
332 454
    editor.putString("rol_freeBought", mFreeBoughtObjects);
455

  
456
    if( SHOW_SOLVED_DEBUG )
457
      {
458
      android.util.Log.e("D", "saving solved objects: "+mFreeSolvedObjects);
459
      android.util.Log.e("D", "saving bought objects: "+mFreeBoughtObjects);
460
      }
333 461
    }
334 462

  
335 463
///////////////////////////////////////////////////////////////////////////////////////////////////
......
381 509
    mObject = getOrdinal(objName);
382 510
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
383 511

  
384
    if( justStarted )
385
      {
386
      mFreeSolvedObjects = preferences.getString("rol_freeSolved", "");
387
      mFreeBoughtObjects = preferences.getString("rol_freeBought", "");
388

  
389
      if( mFreeBoughtObjects.length()>0 )
390
        {
391
        if( mFreeBoughtObjects.charAt(0)=='*' )
392
          {
393
          for(int i=0; i<mNumObjects; i++)
394
            {
395
            RubikObject o = mObjects.get(i);
396
            o.markFree();
397
            }
398
          }
399
        else
400
          {
401
          String[] objs = mFreeBoughtObjects.split(",");
402

  
403
          for( String obj : objs )
404
            {
405
            RubikObject o = getObject(obj);
406
            if( o!=null ) o.markFree();
407
            }
408
          }
409
        }
410

  
411
      if( mFreeSolvedObjects.length()>0 )
412
        {
413
        String[] objs = mFreeSolvedObjects.split(",");
414

  
415
        for( String obj : objs )
416
          {
417
          RubikObject o = getObject(obj);
418
          if( o!=null ) o.markFree();
419
          }
420
        }
421
      }
512
    if( justStarted) restoreFreedObjects(preferences);
422 513
    }
423 514

  
424 515
///////////////////////////////////////////////////////////////////////////////////////////////////
......
438 529
      }
439 530
    }
440 531

  
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

  
534
  public static void setObjectFreeState()
535
    {
536
    int numUnclaimed = getNumUnclaimedSolves();
537

  
538
    if( numUnclaimed>0 )
539
      {
540
      int marked = markAllSolvedAsFree();
541
      int stillUnclaimed = numUnclaimed-marked;
542
      if( stillUnclaimed>0) markAsFree(stillUnclaimed);
543
      }
544
    }
545

  
441 546
///////////////////////////////////////////////////////////////////////////////////////////////////
442 547

  
443 548
  public static boolean setCurrObject(int ordinal)

Also available in: Unified diff