Project

General

Profile

« Previous | Next » 

Revision 1cd323dd

Added by Leszek Koltunski over 2 years ago

Move yet more code to objectlib.
It's almost possible to move the PreRender to objectlib now.

View differences:

src/main/java/org/distorted/main/RubikPreRender.java
22 22
import android.content.Context;
23 23
import android.content.SharedPreferences;
24 24
import android.content.res.Resources;
25
import android.os.Bundle;
26

  
27
import androidx.annotation.NonNull;
28

  
29
import com.google.android.play.core.review.ReviewInfo;
30
import com.google.android.play.core.review.ReviewManager;
31
import com.google.android.play.core.review.ReviewManagerFactory;
32
import com.google.android.play.core.tasks.OnCompleteListener;
33
import com.google.android.play.core.tasks.OnFailureListener;
34
import com.google.android.play.core.tasks.Task;
35
import com.google.firebase.analytics.FirebaseAnalytics;
36 25

  
26
import org.distorted.objectlib.helpers.ObjectStateActioner;
37 27
import org.distorted.objectlib.main.TwistyObject;
38 28
import org.distorted.objectlib.main.ObjectType;
39

  
40
import org.distorted.dialogs.RubikDialogNewRecord;
41
import org.distorted.dialogs.RubikDialogSolved;
42 29
import org.distorted.objectlib.effects.BaseEffect;
43 30
import org.distorted.objectlib.effects.EffectController;
44 31
import org.distorted.objectlib.effects.scramble.ScrambleEffect;
45 32
import org.distorted.objectlib.helpers.BlockController;
46 33
import org.distorted.objectlib.helpers.MovesFinished;
47 34
import org.distorted.objectlib.helpers.TwistyPreRender;
48
import org.distorted.network.RubikScores;
49
import org.distorted.screens.RubikScreenPlay;
50
import org.distorted.screens.ScreenList;
51
import org.distorted.screens.RubikScreenSolving;
52 35

  
53 36
///////////////////////////////////////////////////////////////////////////////////////////////////
54 37

  
......
63 46
  private ObjectType mNextObject;
64 47
  private long mRotationFinishedID;
65 48
  private final long[] mEffectID;
66
  private boolean mIsNewRecord;
67
  private long mNewRecord;
68 49
  private int mScreenWidth;
69 50
  private SharedPreferences mPreferences;
70 51
  private int[][] mNextMoves;
......
76 57
  private long mAddRotationID, mRemoveRotationID;
77 58
  private int mCubit, mFace, mNewColor;
78 59
  private int mNearestAngle;
79
  private String mDebug;
80 60
  private long mDebugStartTime;
81 61
  private final BlockController mBlockController;
62
  private final ObjectStateActioner mActioner;
63
  private String mDebug;
82 64

  
83 65
///////////////////////////////////////////////////////////////////////////////////////////////////
84 66

  
85
  RubikPreRender(RubikSurfaceView view)
67
  RubikPreRender(RubikSurfaceView view, ObjectStateActioner actioner)
86 68
    {
87 69
    mView = view;
70
    mActioner = actioner;
88 71

  
89 72
    mFinishRotation       = false;
90 73
    mRemoveRotation       = false;
......
100 83
    mOldObject = null;
101 84
    mNewObject = null;
102 85

  
86
    mDebug = "";
87

  
103 88
    mScreenWidth = 0;
104 89
    mScrambleObjectNum = 0;
105 90

  
106 91
    mEffectID = new long[BaseEffect.Type.LENGTH];
107 92

  
108
    mDebug = "";
109

  
110 93
    RubikActivity act = (RubikActivity)mView.getContext();
111 94
    mBlockController = new BlockController(act);
112 95
    unblockEverything();
......
162 145

  
163 146
    if( solved && !mIsSolved )
164 147
      {
165
      if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
166
        {
167
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
168
        mNewRecord = solving.getRecord();
169

  
170
        if( mNewRecord< 0 )
171
          {
172
          mNewRecord = -mNewRecord;
173
          mIsNewRecord = false;
174
          }
175
        else
176
          {
177
          mIsNewRecord = true;
178
          }
179
        }
180

  
148
      mActioner.onSolved();
181 149
      unblockEverything();
182 150
      doEffectNow( BaseEffect.Type.WIN );
183 151
      }
......
279 247
    mScrambleObject = false;
280 248
    mIsSolved       = false;
281 249
    blockEverything(BlockController.RUBIK_PLACE_3);
282
    RubikScores.getInstance().incrementNumPlays();
283 250
    doEffectNow( BaseEffect.Type.SCRAMBLE );
284 251
    }
285 252

  
......
333 300
    mView.setQuat();
334 301
    }
335 302

  
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

  
338
  private void reportRecord()
339
    {
340
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
341
    RubikScores scores = RubikScores.getInstance();
342

  
343
    int object      = play.getObject();
344
    int level       = play.getLevel();
345
    ObjectType list = ObjectType.getObject(object);
346
    String name     = scores.getName();
347

  
348
    String record = list.name()+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+mScrambleObjectNum;
349

  
350
    if( BuildConfig.DEBUG )
351
       {
352
       android.util.Log.e("pre", mDebug);
353
       android.util.Log.e("pre", name);
354
       android.util.Log.e("pre", record);
355
       }
356
    else
357
      {
358
      final RubikActivity act = (RubikActivity)mView.getContext();
359
      FirebaseAnalytics analytics = act.getAnalytics();
360

  
361
      if( analytics!=null )
362
        {
363
        Bundle bundle = new Bundle();
364
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, mDebug);
365
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
366
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
367
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
368
        }
369
      }
370
    }
371

  
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

  
374
  private void requestReview()
375
    {
376
    final RubikScores scores = RubikScores.getInstance();
377
    int numWins = scores.incrementNumWins();
378

  
379
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
380
      {
381
      final long timeBegin = System.currentTimeMillis();
382
      final RubikActivity act = (RubikActivity)mView.getContext();
383
      final ReviewManager manager = ReviewManagerFactory.create(act);
384
      Task<ReviewInfo> request = manager.requestReviewFlow();
385

  
386
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
387
        {
388
        @Override
389
        public void onComplete (@NonNull Task<ReviewInfo> task)
390
          {
391
          if (task.isSuccessful())
392
            {
393
            final String name = scores.getName();
394
            ReviewInfo reviewInfo = task.getResult();
395
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
396

  
397
            flow.addOnFailureListener(new OnFailureListener()
398
              {
399
              @Override
400
              public void onFailure(Exception e)
401
                {
402
                analyticsReport(act,"Failed", name, timeBegin);
403
                }
404
              });
405

  
406
            flow.addOnCompleteListener(new OnCompleteListener<Void>()
407
              {
408
              @Override
409
              public void onComplete(@NonNull Task<Void> task)
410
                {
411
                analyticsReport(act,"Complete", name, timeBegin);
412
                }
413
              });
414
            }
415
          else
416
            {
417
            String name = scores.getName();
418
            analyticsReport(act,"Not Successful", name, timeBegin);
419
            }
420
          }
421
        });
422
      }
423
    }
424

  
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

  
427
  private void analyticsReport(RubikActivity act, String message, String name, long timeBegin)
428
    {
429
    long elapsed = System.currentTimeMillis() - timeBegin;
430
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
431

  
432
    if( BuildConfig.DEBUG )
433
       {
434
       android.util.Log.d("pre", msg);
435
       }
436
    else
437
      {
438
      FirebaseAnalytics analytics = act.getAnalytics();
439

  
440
      if( analytics!=null )
441
        {
442
        Bundle bundle = new Bundle();
443
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
444
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
445
        }
446
      }
447
    }
448

  
449 303
///////////////////////////////////////////////////////////////////////////////////////////////////
450 304
//
451 305
///////////////////////////////////////////////////////////////////////////////////////////////////
......
720 574

  
721 575
          if( i==BaseEffect.Type.SCRAMBLE.ordinal() )
722 576
            {
723
            final RubikActivity act = (RubikActivity)mView.getContext();
724

  
725
            act.runOnUiThread(new Runnable()
726
              {
727
              @Override
728
              public void run()
729
                {
730
                ScreenList.switchScreen( act, ScreenList.READ);
731
                }
732
              });
577
            RubikActivity act = (RubikActivity)mView.getContext();
578
            mActioner.onScrambleEffectFinished(act);
733 579
            }
734 580

  
735 581
          if( i==BaseEffect.Type.WIN.ordinal() )
736 582
            {
737
            if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
738
              {
739
              final RubikActivity act = (RubikActivity)mView.getContext();
740
              Bundle bundle = new Bundle();
741
              bundle.putLong("time", mNewRecord );
742

  
743
              reportRecord();
744
              requestReview();
745

  
746
              if( mIsNewRecord )
747
                {
748
                RubikDialogNewRecord dialog = new RubikDialogNewRecord();
749
                dialog.setArguments(bundle);
750
                dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
751
                }
752
              else
753
                {
754
                RubikDialogSolved dialog = new RubikDialogSolved();
755
                dialog.setArguments(bundle);
756
                dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
757
                }
758

  
759
              act.runOnUiThread(new Runnable()
760
                {
761
                @Override
762
                public void run()
763
                  {
764
                  ScreenList.switchScreen( act, ScreenList.DONE);
765
                  }
766
                });
767
              }
583
            RubikActivity act = (RubikActivity)mView.getContext();
584
            mActioner.onWinEffectFinished(act,mDebug,mScrambleObjectNum);
768 585
            }
769 586

  
770 587
          break;

Also available in: Unified diff