Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectStateActioner.java @ dd1a65c1

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

    
88
    int object      = play.getObject();
89
    int level       = play.getLevel();
90
    ObjectType list = ObjectType.getObject(object);
91
    String name     = scores.getName();
92

    
93
    String record = list.name()+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
94

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

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

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

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

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

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

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

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

    
178
       reportRecord(act,debug,scrambleNum);
179
       requestReview(act);
180

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
   public void onScrambleEffectFinished(TwistyActivity act)
208
     {
209
     RubikActivity ract = (RubikActivity)act;
210

    
211
     RubikScores.getInstance().incrementNumPlays();
212

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

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

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

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

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

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

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

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

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