Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ ae77f661

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.library.main.DistortedFramebuffer;
35
import org.distorted.objectlib.helpers.ObjectLibInterface;
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
import java.lang.ref.WeakReference;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
public class RubikObjectLibInterface implements ObjectLibInterface
53
{
54
  WeakReference<RubikActivity> mAct;
55
  private boolean mIsNewRecord;
56
  private long mNewRecord;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  RubikObjectLibInterface(RubikActivity act)
61
    {
62
    mAct = new WeakReference<>(act);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  private void analyticsReport(RubikActivity act, String message, String name, long timeBegin)
68
    {
69
    long elapsed = System.currentTimeMillis() - timeBegin;
70
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
71

    
72
    if( BuildConfig.DEBUG )
73
       {
74
       android.util.Log.d("pre", msg);
75
       }
76
    else
77
      {
78
      FirebaseAnalytics analytics = act.getAnalytics();
79

    
80
      if( analytics!=null )
81
        {
82
        Bundle bundle = new Bundle();
83
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
84
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
85
        }
86
      }
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private void reportRecord(RubikActivity act, String debug, int scrambleNum)
92
    {
93
    RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
94
    RubikScores scores  = RubikScores.getInstance();
95
    ObjectType object   = play.getObject();
96
    int level           = play.getLevel();
97
    String name         = scores.getName();
98

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

    
101
    if( BuildConfig.DEBUG )
102
       {
103
       android.util.Log.e("pre", debug);
104
       android.util.Log.e("pre", name);
105
       android.util.Log.e("pre", record);
106
       }
107
    else
108
      {
109
      FirebaseAnalytics analytics = act.getAnalytics();
110

    
111
      if( analytics!=null )
112
        {
113
        Bundle bundle = new Bundle();
114
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
115
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
116
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
117
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
118
        }
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void requestReview(RubikActivity act)
125
    {
126
    final RubikScores scores = RubikScores.getInstance();
127
    int numWins = scores.incrementNumWins();
128

    
129
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
130
      {
131
      final long timeBegin = System.currentTimeMillis();
132
      final ReviewManager manager = ReviewManagerFactory.create(act);
133
      Task<ReviewInfo> request = manager.requestReviewFlow();
134

    
135
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
136
        {
137
        @Override
138
        public void onComplete (@NonNull Task<ReviewInfo> task)
139
          {
140
          if (task.isSuccessful())
141
            {
142
            final String name = scores.getName();
143
            ReviewInfo reviewInfo = task.getResult();
144
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
145

    
146
            flow.addOnFailureListener(new OnFailureListener()
147
              {
148
              @Override
149
              public void onFailure(Exception e)
150
                {
151
                analyticsReport(act,"Failed", name, timeBegin);
152
                }
153
              });
154

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public void onWinEffectFinished(String debug, int scrambleNum)
177
    {
178
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
179
      {
180
      RubikActivity act = mAct.get();
181
      Bundle bundle = new Bundle();
182
      bundle.putLong("time", mNewRecord );
183

    
184
      reportRecord(act,debug,scrambleNum);
185
      requestReview(act);
186

    
187
      if( mIsNewRecord )
188
        {
189
        RubikDialogNewRecord dialog = new RubikDialogNewRecord();
190
        dialog.setArguments(bundle);
191
        dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
192
        }
193
      else
194
        {
195
        RubikDialogSolved dialog = new RubikDialogSolved();
196
        dialog.setArguments(bundle);
197
        dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
198
        }
199

    
200
      act.runOnUiThread(new Runnable()
201
        {
202
        @Override
203
        public void run()
204
          {
205
          ScreenList.switchScreen( act, ScreenList.DONE);
206
          }
207
        });
208
      }
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public void onScrambleEffectFinished()
214
    {
215
    RubikActivity act = mAct.get();
216
    RubikScores.getInstance().incrementNumPlays();
217

    
218
    act.runOnUiThread(new Runnable()
219
      {
220
      @Override
221
      public void run()
222
        {
223
        ScreenList.switchScreen( act, ScreenList.READ);
224
        }
225
      });
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public void onFinishRotation(int axis, int row, int angle)
231
    {
232
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
233
      {
234
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
235
      solv.addMove(mAct.get(), axis, row, angle);
236
      }
237
    if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
238
      {
239
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
240
      play.addMove(mAct.get(), axis, row, angle);
241
      }
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  public void onBeginRotation()
247
    {
248
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
249
      {
250
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
251
      solving.resetElapsed();
252
      RubikActivity act = mAct.get();
253

    
254
      act.runOnUiThread(new Runnable()
255
        {
256
        @Override
257
        public void run()
258
          {
259
          ScreenList.switchScreen( act, ScreenList.SOLV);
260
          }
261
        });
262
      }
263
    }
264

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

    
267
  public void failedToDrag()
268
    {
269
    ScreenList curr = ScreenList.getCurrentScreen();
270

    
271
    if( curr==ScreenList.PLAY )
272
      {
273
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
274
      play.reddenLock(mAct.get());
275
      }
276
    else if( curr==ScreenList.READ )
277
      {
278
      RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
279
      read.reddenLock(mAct.get());
280
      }
281
    else if( curr==ScreenList.SOLV )
282
      {
283
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
284
      solv.reddenLock(mAct.get());
285
      }
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public void onSolved()
291
    {
292
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
293
      {
294
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
295
      mNewRecord = solving.getRecord();
296

    
297
      if( mNewRecord< 0 )
298
        {
299
        mNewRecord = -mNewRecord;
300
        mIsNewRecord = false;
301
        }
302
      else
303
        {
304
        mIsNewRecord = true;
305
        RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
306
        play.adjustSolvedIcons();
307
        }
308
      }
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public int getCurrentColor()
314
    {
315
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
316
    return solver.getCurrentColor();
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  public int cubitIsLocked(ObjectType type, int cubit)
322
    {
323
    return SolverMain.cubitIsLocked(type,cubit);
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  public DistortedFramebuffer getFramebuffer()
329
    {
330
    RubikActivity act = mAct.get();
331
    return act.getScreen();
332
    }
333
}
(2-2/4)