Project

General

Profile

Download (6.04 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / BlockController.java @ 2e44531e

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.helpers;
11

    
12
import java.util.Timer;
13
import java.util.TimerTask;
14

    
15
import android.app.Activity;
16

    
17
import org.distorted.library.message.EffectMessageSender;
18
import org.distorted.objectlib.main.ObjectPreRender;
19

    
20
///////////////////////////////////////////////////////////////////////////////////////////////////
21

    
22
public class BlockController
23
  {
24
  public static final int TYPE_SCRAMBLING = 0;
25
  public static final int TYPE_ROTATION   = 1;
26
  public static final int TYPE_THREAD     = 2;
27

    
28
  public static final int PLACE_0 =0;
29
  public static final int PLACE_1 =1;
30
  public static final int PLACE_2 =2;
31
  public static final int PLACE_3 =3;
32
  public static final int PLACE_4 =4;
33
  public static final int PLACE_5 =5;
34
  public static final int CONTROL_PLACE_0 =20;
35
  public static final int CONTROL_PLACE_1 =21;
36

    
37
  private static final long THRESHHOLD_0 =  6000;
38
  private static final long THRESHHOLD_1 = 50000;
39
  private static final long THRESHHOLD_2 = 10000;
40
  private static final long THRESHHOLD_3 = 90000;
41

    
42
  private static long mPauseTime, mResumeTime;
43

    
44
  private long mRotationBlockTime, mScramblingAndSolvingBlockTime;
45
  private int mLastRotationPlace, mLastScramblingAndSolvingPlace;
46

    
47
  private final ObjectPreRender mPre;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  public static void onPause()
52
    {
53
    mPauseTime = System.currentTimeMillis();
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  public static void onResume()
59
    {
60
    mResumeTime = System.currentTimeMillis();
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public BlockController(Activity act, ObjectPreRender pre)
66
    {
67
    mPre = pre;
68

    
69
    Timer timer = new Timer();
70

    
71
    timer.scheduleAtFixedRate(new TimerTask()
72
      {
73
      @Override
74
      public void run()
75
        {
76
        act.runOnUiThread(new Runnable()
77
          {
78
          @Override
79
          public void run()
80
            {
81
            checkingThread();
82
            }
83
          });
84
        }
85
      }, 0, 1000);
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
90
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
91
// CONTROL_PLACE_* are the visual tutorials, could take up to 45 seconds.
92

    
93
  private long getThreshhold(int last)
94
    {
95
    switch(last)
96
      {
97
      case PLACE_3         : return THRESHHOLD_1;
98
      case PLACE_4         : return THRESHHOLD_2;
99
      case CONTROL_PLACE_0 :
100
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
101
      default              : return THRESHHOLD_0;
102
      }
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  private void checkingThread()
108
    {
109
    long now = System.currentTimeMillis();
110
    long rotationThreshhold = getThreshhold(mLastRotationPlace);
111
    long rotationBlocked = now- mRotationBlockTime;
112

    
113
    if( mRotationBlockTime >mPauseTime && rotationBlocked>rotationThreshhold )
114
      {
115
      boolean running = EffectMessageSender.isRunning();
116
      ObjectLibInterface libInterface = mPre.getInterface();
117

    
118
      if( !running )
119
        {
120
        libInterface.reportBlockProblem(TYPE_THREAD,0,mPauseTime,mResumeTime,0);
121
        EffectMessageSender.restartThread();
122
        }
123
      else
124
        {
125
        libInterface.reportBlockProblem(TYPE_ROTATION, mLastRotationPlace,mPauseTime,mResumeTime, mRotationBlockTime);
126
        mPre.unblockRotation();
127
        }
128
      }
129

    
130
    long uiThreshhold = getThreshhold(mLastScramblingAndSolvingPlace);
131
    long scramblingAndSolvingBlocked = now- mScramblingAndSolvingBlockTime;
132

    
133
    if( mScramblingAndSolvingBlockTime >mPauseTime && scramblingAndSolvingBlocked>uiThreshhold )
134
      {
135
      boolean running = EffectMessageSender.isRunning();
136
      ObjectLibInterface libInterface = mPre.getInterface();
137

    
138
      if( !running )
139
        {
140
        libInterface.reportBlockProblem(TYPE_THREAD,0,mPauseTime,mResumeTime,0);
141
        EffectMessageSender.restartThread();
142
        }
143
      else
144
        {
145
        libInterface.reportBlockProblem(TYPE_SCRAMBLING, mLastScramblingAndSolvingPlace,mPauseTime,mResumeTime, mScramblingAndSolvingBlockTime);
146
        mPre.unblockScramblingAndSolving();
147
        }
148
      }
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  public void rotationBlocked(int place)
154
    {
155
    mRotationBlockTime = System.currentTimeMillis();
156
    mLastRotationPlace = place;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public void scramblingAndSolvingBlocked(int place)
162
    {
163
    mScramblingAndSolvingBlockTime = System.currentTimeMillis();
164
    mLastScramblingAndSolvingPlace = place;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public void rotationUnblocked()
170
    {
171
    mRotationBlockTime = 0;
172
    }
173

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

    
176
  public void scramblingAndSolvingUnblocked()
177
    {
178
    mScramblingAndSolvingBlockTime = 0;
179
    }
180
  }
(1-1/13)