Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectStateActioner.java @ 8ab435b9

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.main;
21

    
22
import android.os.Bundle;
23

    
24
import androidx.annotation.NonNull;
25

    
26
import com.google.android.play.core.review.ReviewInfo;
27
import com.google.android.play.core.review.ReviewManager;
28
import com.google.android.play.core.review.ReviewManagerFactory;
29
import com.google.android.play.core.tasks.OnCompleteListener;
30
import com.google.android.play.core.tasks.OnFailureListener;
31
import com.google.android.play.core.tasks.Task;
32
import com.google.firebase.analytics.FirebaseAnalytics;
33

    
34
import org.distorted.objectlib.helpers.ObjectStateActioner;
35
import org.distorted.objectlib.helpers.TwistyActivity;
36
import org.distorted.objectlib.main.ObjectType;
37

    
38
import org.distorted.dialogs.RubikDialogNewRecord;
39
import org.distorted.dialogs.RubikDialogSolved;
40
import org.distorted.network.RubikScores;
41
import org.distorted.screens.RubikScreenPlay;
42
import org.distorted.screens.RubikScreenReady;
43
import org.distorted.screens.RubikScreenSolver;
44
import org.distorted.screens.RubikScreenSolving;
45
import org.distorted.screens.ScreenList;
46
import org.distorted.solvers.SolverMain;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
public class RubikObjectStateActioner implements ObjectStateActioner
51
{
52
  private boolean mIsNewRecord;
53
  private long mNewRecord;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  private void analyticsReport(TwistyActivity act, String message, String name, long timeBegin)
58
    {
59
    long elapsed = System.currentTimeMillis() - timeBegin;
60
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
61

    
62
    if( BuildConfig.DEBUG )
63
       {
64
       android.util.Log.d("pre", msg);
65
       }
66
    else
67
      {
68
      RubikActivity ract = (RubikActivity)act;
69
      FirebaseAnalytics analytics = ract.getAnalytics();
70

    
71
      if( analytics!=null )
72
        {
73
        Bundle bundle = new Bundle();
74
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
75
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
76
        }
77
      }
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private void reportRecord(TwistyActivity act, String debug, int scrambleNum)
83
    {
84
    RubikActivity ract  = (RubikActivity)act;
85
    RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
86
    RubikScores scores  = RubikScores.getInstance();
87
    ObjectType object   = play.getObject();
88
    int level           = play.getLevel();
89
    String name         = scores.getName();
90

    
91
    String record = object.name()+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
92

    
93
    if( BuildConfig.DEBUG )
94
       {
95
       android.util.Log.e("pre", debug);
96
       android.util.Log.e("pre", name);
97
       android.util.Log.e("pre", record);
98
       }
99
    else
100
      {
101
      FirebaseAnalytics analytics = ract.getAnalytics();
102

    
103
      if( analytics!=null )
104
        {
105
        Bundle bundle = new Bundle();
106
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
107
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
108
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
109
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
110
        }
111
      }
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private void requestReview(TwistyActivity act)
117
    {
118
    final RubikScores scores = RubikScores.getInstance();
119
    int numWins = scores.incrementNumWins();
120

    
121
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
122
      {
123
      final long timeBegin = System.currentTimeMillis();
124
      final ReviewManager manager = ReviewManagerFactory.create(act);
125
      Task<ReviewInfo> request = manager.requestReviewFlow();
126

    
127
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
128
        {
129
        @Override
130
        public void onComplete (@NonNull Task<ReviewInfo> task)
131
          {
132
          if (task.isSuccessful())
133
            {
134
            final String name = scores.getName();
135
            ReviewInfo reviewInfo = task.getResult();
136
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
137

    
138
            flow.addOnFailureListener(new OnFailureListener()
139
              {
140
              @Override
141
              public void onFailure(Exception e)
142
                {
143
                analyticsReport(act,"Failed", name, timeBegin);
144
                }
145
              });
146

    
147
            flow.addOnCompleteListener(new OnCompleteListener<Void>()
148
              {
149
              @Override
150
              public void onComplete(@NonNull Task<Void> task)
151
                {
152
                analyticsReport(act,"Complete", name, timeBegin);
153
                }
154
              });
155
            }
156
          else
157
            {
158
            String name = scores.getName();
159
            analyticsReport(act,"Not Successful", name, timeBegin);
160
            }
161
          }
162
        });
163
      }
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
   public void onWinEffectFinished(TwistyActivity act, String debug, int scrambleNum)
169
     {
170
     if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
171
       {
172
       RubikActivity ract = (RubikActivity)act;
173
       Bundle bundle = new Bundle();
174
       bundle.putLong("time", mNewRecord );
175

    
176
       reportRecord(act,debug,scrambleNum);
177
       requestReview(act);
178

    
179
       if( mIsNewRecord )
180
         {
181
         RubikDialogNewRecord dialog = new RubikDialogNewRecord();
182
         dialog.setArguments(bundle);
183
         dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
184
         }
185
       else
186
         {
187
         RubikDialogSolved dialog = new RubikDialogSolved();
188
         dialog.setArguments(bundle);
189
         dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
190
         }
191

    
192
       act.runOnUiThread(new Runnable()
193
         {
194
         @Override
195
         public void run()
196
           {
197
           ScreenList.switchScreen( ract, ScreenList.DONE);
198
           }
199
         });
200
       }
201
     }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
   public void onScrambleEffectFinished(TwistyActivity act)
206
     {
207
     RubikActivity ract = (RubikActivity)act;
208

    
209
     RubikScores.getInstance().incrementNumPlays();
210

    
211
     act.runOnUiThread(new Runnable()
212
       {
213
       @Override
214
       public void run()
215
         {
216
         ScreenList.switchScreen( ract, ScreenList.READ);
217
         }
218
       });
219
     }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
   public void onFinishRotation(TwistyActivity act, int axis, int row, int angle)
224
     {
225
     if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
226
       {
227
       RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
228
       solv.addMove(act, axis, row, angle);
229
       }
230
     if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
231
       {
232
       RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
233
       play.addMove(act, axis, row, angle);
234
       }
235
     }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
   public void onBeginRotation(TwistyActivity act)
240
     {
241
     if( ScreenList.getCurrentScreen()== ScreenList.READ )
242
       {
243
       RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
244
       solving.resetElapsed();
245
       RubikActivity ract = (RubikActivity)act;
246

    
247
       ract.runOnUiThread(new Runnable()
248
         {
249
         @Override
250
         public void run()
251
           {
252
           ScreenList.switchScreen( ract, ScreenList.SOLV);
253
           }
254
         });
255
       }
256
     }
257

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

    
260
    public void failedToDrag(TwistyActivity act)
261
      {
262
      ScreenList curr = ScreenList.getCurrentScreen();
263

    
264
      if( curr==ScreenList.PLAY )
265
        {
266
        RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
267
        play.reddenLock(act);
268
        }
269
      else if( curr==ScreenList.READ )
270
        {
271
        RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
272
        read.reddenLock(act);
273
        }
274
      else if( curr==ScreenList.SOLV )
275
        {
276
        RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
277
        solv.reddenLock(act);
278
        }
279
      }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
   public void onSolved()
284
     {
285
     if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
286
        {
287
        RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
288
        mNewRecord = solving.getRecord();
289

    
290
        if( mNewRecord< 0 )
291
          {
292
          mNewRecord = -mNewRecord;
293
          mIsNewRecord = false;
294
          }
295
        else
296
          {
297
          mIsNewRecord = true;
298
          }
299
        }
300
     }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
   public int getCurrentColor()
305
     {
306
     RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
307
     return solver.getCurrentColor();
308
     }
309

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
   public int cubitIsLocked(ObjectType type, int cubit)
313
     {
314
     return SolverMain.cubitIsLocked(type,cubit);
315
     }
316
}
(2-2/4)