Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ 68191e7d

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.objects;
21

    
22
import java.util.ArrayList;
23
import java.util.Locale;
24

    
25
import android.content.Context;
26
import android.content.SharedPreferences;
27

    
28
import org.distorted.external.RubikFiles;
29
import org.distorted.main.RubikActivity;
30
import org.distorted.objectlib.main.ObjectSignatures;
31
import org.distorted.objectlib.main.ObjectType;
32

    
33
import static org.distorted.objectlib.main.TwistyObject.MESH_NICE;
34
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
35
import static org.distorted.main.RubikActivity.SHOW_DOWNLOADED_DEBUG;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class RubikObjectList
40
{
41
  public static final int DEF_OBJECT= ObjectSignatures.CUBE_3;
42
  public static int MAX_LEVEL;
43

    
44
  private static RubikObjectList mThis;
45
  private static int mNumObjects;
46
  private static int mNumExtras;
47
  private static ArrayList<RubikObject> mObjects;
48
  private static int mObject = DEF_OBJECT;
49

    
50
  public static class DownloadedObject
51
    {
52
    String shortName;
53
    boolean icon,object,extras;
54
    int numScrambles, objectMinor, extrasMinor;
55

    
56
    DownloadedObject(String sName, int scrambles, int oMinor, int eMinor, boolean i, boolean o, boolean e)
57
      {
58
      shortName = sName;
59

    
60
      numScrambles= scrambles;
61
      objectMinor = oMinor;
62
      extrasMinor = eMinor;
63

    
64
      icon   = i;
65
      object = o;
66
      extras = e;
67
      }
68
    }
69

    
70
  private static ArrayList<DownloadedObject> mDownloadedObjects;
71

    
72
  static
73
    {
74
    int max = Integer.MIN_VALUE;
75

    
76
    for (int i=0; i<NUM_OBJECTS; i++)
77
      {
78
      int cur = getDBLevel(i);
79
      if( cur>max ) max = cur;
80
      }
81

    
82
    MAX_LEVEL = max;
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private RubikObjectList()
88
    {
89
    mNumObjects= 0;
90
    mNumExtras = 0;
91

    
92
    mObjects           = new ArrayList<>();
93
    mDownloadedObjects = new ArrayList<>();
94

    
95
    createBuiltinObjects();
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void createBuiltinObjects()
101
    {
102
    for(int i=0; i<NUM_OBJECTS; i++)
103
      {
104
      ObjectType type = ObjectType.getObject(i);
105
      RubikObject obj = new RubikObject(type);
106
      mObjects.add(obj);
107
      mNumObjects++;
108

    
109
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating local object "+type.name() );
110

    
111
      if( obj.hasExtras() )
112
        {
113
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
114

    
115
        obj.setExtrasOrdinal(mNumExtras);
116
        mNumExtras++;
117
        }
118
      else
119
        {
120
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
121
        }
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private static boolean internalAddDownloadedObject(DownloadedObject object)
128
    {
129
    String name = object.shortName;
130

    
131
    for(RubikObject ro : mObjects )
132
      if( ro.getLowerName().equals(name) )
133
        {
134
        return ro.updateObject(object);
135
        }
136

    
137
    RubikObject obj = new RubikObject(object);
138
    mObjects.add(obj);
139
    mNumObjects++;
140

    
141
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "creating downloaded object "+obj.getUpperName() );
142

    
143
    if( obj.hasExtras() )
144
      {
145
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "has extras "+mNumExtras );
146

    
147
      obj.setExtrasOrdinal(mNumExtras);
148
      mNumExtras++;
149
      }
150
    else
151
      {
152
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "no extras");
153
      }
154

    
155
    return true;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// PUBLIC API
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// historically older versions of the app had lower 'maxScrambles' in case of several objects and
162
// those got remembered in the server-side DB already, so we need to keep using them. This function
163
// provides a map between 'maxScramble' of an object and its 'dbLevel'. All new objects will have
164
// those two values the same.
165

    
166
  public static int getDBLevel(int ordinal)
167
    {
168
    if( ordinal== ObjectSignatures.CUBE_3 ) return 16;
169
    if( ordinal== ObjectSignatures.CUBE_4 ) return 20;
170
    if( ordinal== ObjectSignatures.CUBE_5 ) return 24;
171
    if( ordinal== ObjectSignatures.BAN2_3 ) return 16;
172
    if( ordinal== ObjectSignatures.BAN4_3 ) return 16;
173
    if( ordinal== ObjectSignatures.PYRA_4 ) return 15;
174
    if( ordinal== ObjectSignatures.PYRA_5 ) return 20;
175
    if( ordinal== ObjectSignatures.MEGA_5 ) return 35;
176
    if( ordinal== ObjectSignatures.DIAM_2 ) return 10;
177
    if( ordinal== ObjectSignatures.DIAM_3 ) return 18;
178
    if( ordinal== ObjectSignatures.REDI_3 ) return 14;
179
    if( ordinal== ObjectSignatures.HELI_3 ) return 18;
180
    if( ordinal== ObjectSignatures.SKEW_3 ) return 17;
181
    if( ordinal== ObjectSignatures.REX_3  ) return 16;
182
    if( ordinal== ObjectSignatures.MIRR_3 ) return 16;
183
    if( ordinal== ObjectSignatures.IVY_2  ) return  8;
184
    if( ordinal== ObjectSignatures.DIN4_3 ) return  7;
185

    
186
    // in 1.9.6 & 1.9.7 there is a bug with downloadable objects (in this very function!):
187
    // All of those have DBLevel equal to CUBE_3's DBlevel (in 1.9.7!), i.e. 17.
188
    // This will be a problem when we release a new version of the app which has some of the
189
    // previously downloadable objects built-in. Thus: in case of those, we need to keep using
190
    // 17.
191

    
192
    if( ObjectType.wasDownloadableButNowIsBuiltIn(ordinal) )
193
      {
194
      return 17;
195
      }
196

    
197
    return ObjectType.getObject(ordinal).getNumScramble();
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  public static boolean addDownloadedObject(Context context, String shortName, int numScrambles, int objectMinor,
203
                                         int extrasMinor, boolean icon, boolean object, boolean extras)
204
    {
205
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" icon="+icon+" object="+object+" extras="+extras);
206

    
207
    for( DownloadedObject obj : mDownloadedObjects )
208
      {
209
      if( obj.shortName.equals(shortName) )
210
        {
211
        obj.icon  |= icon;
212
        obj.object|= object;
213
        obj.extras|= extras;
214

    
215
        if( !obj.object ) objectMinor=-1;
216
        if( !obj.extras ) extrasMinor=-1;
217

    
218
        obj.objectMinor = objectMinor;
219
        obj.extrasMinor = extrasMinor;
220

    
221
        if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Updating downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
222

    
223
        try
224
          {
225
          RubikActivity ract = (RubikActivity)context;
226
          ract.reloadObject(shortName);
227
          }
228
        catch(Exception ex)
229
          {
230
          android.util.Log.e("D", "exception trying to reload object: "+ex.getMessage() );
231
          }
232

    
233
        return false;
234
        }
235
      }
236

    
237
    if( !object ) objectMinor=-1;
238
    if( !extras ) extrasMinor=-1;
239

    
240
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
241
    if ( internalAddDownloadedObject(obj) )
242
      {
243
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
244
      mDownloadedObjects.add(obj);
245
      return true;
246
      }
247
    else
248
      {
249
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" is already built-in, deleting");
250
      RubikFiles files = RubikFiles.getInstance();
251
      files.deleteIcon(context,shortName);
252
      files.deleteJsonObject(context,shortName);
253
      files.deleteJsonExtras(context,shortName);
254
      return false;
255
      }
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public static void setMeshState(int ordinal, int state)
261
    {
262
    if( ordinal>=0 && ordinal<mNumObjects ) mObjects.get(ordinal).setMeshState(state);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public static int getMeshState(int ordinal)
268
    {
269
    return (ordinal>=0 && ordinal<mNumObjects) ? mObjects.get(ordinal).getMeshState() : MESH_NICE;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  public static void savePreferences(SharedPreferences.Editor editor)
275
    {
276
    RubikObject obj = getObject(mObject);
277
    if( obj!=null ) editor.putString("rol_objName", obj.getUpperName() );
278

    
279
    int numDownloaded = mDownloadedObjects.size();
280

    
281
    if( numDownloaded>0 )
282
      {
283
      StringBuilder downloadedObjects = new StringBuilder();
284

    
285
      for(int i=0; i<numDownloaded; i++)
286
        {
287
        if( i>0 ) downloadedObjects.append(',');
288

    
289
        DownloadedObject object = mDownloadedObjects.get(i);
290
        downloadedObjects.append(object.shortName);
291
        downloadedObjects.append(' ');
292
        downloadedObjects.append(object.numScrambles);
293
        downloadedObjects.append(' ');
294
        downloadedObjects.append(object.objectMinor);
295
        downloadedObjects.append(' ');
296
        downloadedObjects.append(object.extrasMinor);
297
        downloadedObjects.append(' ');
298
        downloadedObjects.append(object.icon   ? "1":"0");
299
        downloadedObjects.append(' ');
300
        downloadedObjects.append(object.object ? "1":"0");
301
        downloadedObjects.append(' ');
302
        downloadedObjects.append(object.extras ? "1":"0");
303
        }
304

    
305
      String objects = downloadedObjects.toString();
306
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "saving: "+objects);
307
      editor.putString("rol_downloaded", objects );
308
      }
309
    else
310
      {
311
      editor.putString("rol_downloaded", "" );
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public static void saveMeshState(SharedPreferences.Editor editor)
318
    {
319
    for(int i=0; i<mNumObjects; i++)
320
      {
321
      RubikObject obj = getObject(i);
322

    
323
      if( obj!=null )
324
        {
325
        String name = obj.getUpperName();
326
        editor.putInt("rol_"+name, obj.getMeshState() );
327
        }
328
      }
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  public static void restorePreferences(Context context, SharedPreferences preferences)
334
    {
335
    if( mThis==null ) mThis = new RubikObjectList();
336

    
337
    String downloaded = preferences.getString("rol_downloaded","");
338

    
339
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
340

    
341
    if( !downloaded.equals(""))
342
      {
343
      String[] dObjects = downloaded.split(",");
344

    
345
      for(String dObj : dObjects)
346
        {
347
        String[] parts = dObj.split(" ");
348

    
349
        if( parts.length==7 )
350
          {
351
          String name = parts[0];
352
          String scra = parts[1];
353
          String objM = parts[2];
354
          String extM = parts[3];
355
          String icon = parts[4];
356
          String obje = parts[5];
357
          String extr = parts[6];
358

    
359
          int scrambles = Integer.parseInt(scra);
360
          int oMinor    = Integer.parseInt(objM);
361
          int eMinor    = Integer.parseInt(extM);
362

    
363
          boolean bIcon = icon.equals("1");
364
          boolean bObje = obje.equals("1");
365
          boolean bExtr = extr.equals("1");
366

    
367
          addDownloadedObject(context,name,scrambles,oMinor,eMinor,bIcon,bObje,bExtr);
368
          }
369
        }
370
      }
371

    
372
    RubikObject object = getObject(DEF_OBJECT);
373
    String defName = object==null ? "CUBE_3" : object.getUpperName();
374
    String objName= preferences.getString("rol_objName",defName);
375
    mObject = getOrdinal(objName);
376
    if( mObject<0 || mObject>=mNumObjects ) mObject = DEF_OBJECT;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  public static void restoreMeshState(SharedPreferences preferences)
382
    {
383
    for(int i=0; i<mNumObjects; i++)
384
      {
385
      RubikObject obj = getObject(i);
386

    
387
      if( obj!=null )
388
        {
389
        String name  = obj.getUpperName();
390
        int meshState= preferences.getInt("rol_"+name,MESH_NICE);
391
        obj.setMeshState(meshState);
392
        }
393
      }
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public static boolean setCurrObject(int ordinal)
399
    {
400
    if( mObject!=ordinal )
401
      {
402
      mObject = ordinal;
403
      return true;
404
      }
405

    
406
    return false;
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public static int getCurrObject()
412
    {
413
    return mObject;
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public static String getCurrentName()
419
    {
420
    RubikObject object = mObjects.get(mObject);
421
    return object==null ? "" : object.getUpperName();
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public static RubikObject getObject(int ordinal)
427
    {
428
    if( mThis==null ) mThis = new RubikObjectList();
429
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
  public static RubikObject getObject(String shortUpperName)
435
    {
436
    if( mThis==null ) mThis = new RubikObjectList();
437

    
438
    for(int i=0; i<mNumObjects; i++)
439
      {
440
      RubikObject object = mObjects.get(i);
441
      if( object.getUpperName().equals(shortUpperName) )
442
        {
443
        return object;
444
        }
445
      }
446

    
447
    return null;
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  public static int getNumObjects()
453
    {
454
    if( mThis==null ) mThis = new RubikObjectList();
455
    return mNumObjects;
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
  public static int getOrdinal(String name)
461
    {
462
    if( mThis==null ) mThis = new RubikObjectList();
463

    
464
    String lowerName = name.toLowerCase(Locale.ENGLISH);
465

    
466
    for(int i=0; i<mNumObjects; i++)
467
      {
468
      RubikObject obj = mObjects.get(i);
469
      if( obj.getLowerName().equals(lowerName) ) return i;
470
      }
471

    
472
    return -1;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  public static int getNumExtrasObjects()
478
    {
479
    return mNumExtras;
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public static int getNumTutorialObjects()
485
    {
486
    return mNumExtras;
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490

    
491
  public static int getObjectOrdinal(int extrasOrdinal)
492
    {
493
    for(int i=extrasOrdinal; i<mNumObjects; i++)
494
      {
495
      RubikObject object = getObject(i);
496
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
497
      if( extOrd==extrasOrdinal ) return i;
498
      }
499

    
500
    return -1;
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
  public static int getExtrasOrdinal(int objectOrdinal)
506
    {
507
    RubikObject object = getObject(objectOrdinal);
508
    return object!=null ? object.getExtrasOrdinal() : -1;
509
    }
510

    
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512

    
513
  public static int getTutorialOrdinal(int objectOrdinal)
514
    {
515
    RubikObject object = getObject(objectOrdinal);
516
    return object!=null ? object.getExtrasOrdinal() : -1;
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  public static int getLocalObjectMinor(int objectOrdinal)
522
    {
523
    RubikObject object = getObject(objectOrdinal);
524
    return object!=null ? object.getObjectMinor() : -1;
525
    }
526

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528

    
529
  public static int getLocalExtrasMinor(int objectOrdinal)
530
    {
531
    RubikObject object = getObject(objectOrdinal);
532
    return object!=null ? object.getExtrasMinor() : -1;
533
    }
534
}
(4-4/4)