Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObjectList.java @ c5fca790

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

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

    
190
    if( ObjectType.wasDownloadableButNowIsBuiltIn(ordinal) )
191
      {
192
      return 17;
193
      }
194

    
195
    return ObjectType.getObject(ordinal).getNumScramble();
196
    }
197

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

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

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

    
213
        if( !obj.object ) objectMinor=-1;
214
        if( !obj.extras ) extrasMinor=-1;
215

    
216
        obj.objectMinor = objectMinor;
217
        obj.extrasMinor = extrasMinor;
218

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

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

    
231
        return;
232
        }
233
      }
234

    
235
    if( !object ) objectMinor=-1;
236
    if( !extras ) extrasMinor=-1;
237

    
238
    DownloadedObject obj = new DownloadedObject(shortName,numScrambles,objectMinor,extrasMinor,icon,object,extras);
239
    if ( internalAddDownloadedObject(obj) )
240
      {
241
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "Adding new downloaded object "+shortName+" icon="+obj.icon+" object="+obj.object+" extras="+obj.extras);
242
      mDownloadedObjects.add(obj);
243
      }
244
    else
245
      {
246
      if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", "New downloaded object "+shortName+" is already built-in, deleting");
247

    
248
      mDownloadedObjects.remove(obj);
249
      RubikFiles files = RubikFiles.getInstance();
250
      files.deleteIcon(context,shortName);
251
      files.deleteJsonObject(context,shortName);
252
      files.deleteJsonExtras(context,shortName);
253
      }
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

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

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

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

    
277
    int numDownloaded = mDownloadedObjects.size();
278

    
279
    if( numDownloaded>0 )
280
      {
281
      StringBuilder downloadedObjects = new StringBuilder();
282

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

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

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

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

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

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

    
335
    String downloaded = preferences.getString("rol_downloaded","");
336

    
337
    if( SHOW_DOWNLOADED_DEBUG ) android.util.Log.e("D", downloaded);
338

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

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

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

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

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

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

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

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

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

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395

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

    
404
    return false;
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

    
409
  public static int getCurrObject()
410
    {
411
    return mObject;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
  public static RubikObject getObject(int ordinal)
417
    {
418
    if( mThis==null ) mThis = new RubikObjectList();
419
    return ordinal>=0 && ordinal<mNumObjects ? mObjects.get(ordinal) : null;
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  public static RubikObject getObject(String shortUpperName)
425
    {
426
    if( mThis==null ) mThis = new RubikObjectList();
427

    
428
    for(int i=0; i<mNumObjects; i++)
429
      {
430
      RubikObject object = mObjects.get(i);
431
      if( object.getUpperName().equals(shortUpperName) )
432
        {
433
        return object;
434
        }
435
      }
436

    
437
    return null;
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  public static int getNumObjects()
443
    {
444
    if( mThis==null ) mThis = new RubikObjectList();
445
    return mNumObjects;
446
    }
447

    
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449

    
450
  public static int getOrdinal(String name)
451
    {
452
    if( mThis==null ) mThis = new RubikObjectList();
453

    
454
    String lowerName = name.toLowerCase(Locale.ENGLISH);
455

    
456
    for(int i=0; i<mNumObjects; i++)
457
      {
458
      RubikObject obj = mObjects.get(i);
459
      if( obj.getLowerName().equals(lowerName) ) return i;
460
      }
461

    
462
    return -1;
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
  public static int getNumExtrasObjects()
468
    {
469
    return mNumExtras;
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public static int getNumTutorialObjects()
475
    {
476
    return mNumExtras;
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
  public static int getObjectOrdinal(int extrasOrdinal)
482
    {
483
    for(int i=extrasOrdinal; i<mNumObjects; i++)
484
      {
485
      RubikObject object = getObject(i);
486
      int extOrd = object!=null ? object.getExtrasOrdinal() : -1;
487
      if( extOrd==extrasOrdinal ) return i;
488
      }
489

    
490
    return -1;
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
  public static int getExtrasOrdinal(int objectOrdinal)
496
    {
497
    RubikObject object = getObject(objectOrdinal);
498
    return object!=null ? object.getExtrasOrdinal() : -1;
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

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

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

    
511
  public static int getLocalObjectMinor(int objectOrdinal)
512
    {
513
    RubikObject object = getObject(objectOrdinal);
514
    return object!=null ? object.getObjectMinor() : -1;
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  public static int getLocalExtrasMinor(int objectOrdinal)
520
    {
521
    RubikObject object = getObject(objectOrdinal);
522
    return object!=null ? object.getExtrasMinor() : -1;
523
    }
524
}
(4-4/4)