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 org.distorted.library.message.EffectMessageSender;
|
16
|
import org.distorted.objectlib.main.ObjectPreRender;
|
17
|
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
public class BlockController
|
21
|
{
|
22
|
public static final int TYPE_SCRAMBLING = 0;
|
23
|
public static final int TYPE_ROTATION = 1;
|
24
|
public static final int TYPE_THREAD = 2;
|
25
|
|
26
|
public static final int PLACE_0 =0;
|
27
|
public static final int PLACE_1 =1;
|
28
|
public static final int PLACE_2 =2;
|
29
|
public static final int PLACE_3 =3;
|
30
|
public static final int PLACE_4 =4;
|
31
|
public static final int PLACE_5 =5;
|
32
|
public static final int CONTROL_PLACE_0 =20;
|
33
|
public static final int CONTROL_PLACE_1 =21;
|
34
|
|
35
|
private static final long THRESHHOLD_0 = 6000;
|
36
|
private static final long THRESHHOLD_1 = 50000;
|
37
|
private static final long THRESHHOLD_2 = 10000;
|
38
|
private static final long THRESHHOLD_3 = 90000;
|
39
|
|
40
|
private static long mPauseTime, mResumeTime;
|
41
|
|
42
|
private long mRotationBlockTime, mScramblingAndSolvingBlockTime;
|
43
|
private int mLastRotationPlace, mLastScramblingAndSolvingPlace;
|
44
|
|
45
|
private final ObjectPreRender mPre;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
public static void onPause()
|
50
|
{
|
51
|
mPauseTime = System.currentTimeMillis();
|
52
|
}
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
|
56
|
public static void onResume()
|
57
|
{
|
58
|
mResumeTime = System.currentTimeMillis();
|
59
|
}
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
public BlockController(ObjectPreRender pre)
|
64
|
{
|
65
|
mPre = pre;
|
66
|
|
67
|
Timer timer = new Timer();
|
68
|
|
69
|
timer.scheduleAtFixedRate(new TimerTask()
|
70
|
{
|
71
|
@Override
|
72
|
public void run()
|
73
|
{
|
74
|
checkingThread();
|
75
|
}
|
76
|
}, 0, 1000);
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
|
81
|
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
|
82
|
// CONTROL_PLACE_* are the visual tutorials, could take up to 45 seconds.
|
83
|
|
84
|
private long getThreshhold(int last)
|
85
|
{
|
86
|
switch(last)
|
87
|
{
|
88
|
case PLACE_3 : return THRESHHOLD_1;
|
89
|
case PLACE_4 : return THRESHHOLD_2;
|
90
|
case CONTROL_PLACE_0 :
|
91
|
case CONTROL_PLACE_1 : return THRESHHOLD_3;
|
92
|
default : return THRESHHOLD_0;
|
93
|
}
|
94
|
}
|
95
|
|
96
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
97
|
|
98
|
private void checkingThread()
|
99
|
{
|
100
|
long now = System.currentTimeMillis();
|
101
|
long rotationThreshhold = getThreshhold(mLastRotationPlace);
|
102
|
long rotationBlocked = now- mRotationBlockTime;
|
103
|
|
104
|
if( mRotationBlockTime >mPauseTime && rotationBlocked>rotationThreshhold )
|
105
|
{
|
106
|
boolean running = EffectMessageSender.isRunning();
|
107
|
ObjectLibInterface libInterface = mPre.getInterface();
|
108
|
|
109
|
if( !running )
|
110
|
{
|
111
|
libInterface.reportBlockProblem(TYPE_THREAD,0,mPauseTime,mResumeTime,0);
|
112
|
EffectMessageSender.restartThread();
|
113
|
}
|
114
|
else
|
115
|
{
|
116
|
libInterface.reportBlockProblem(TYPE_ROTATION, mLastRotationPlace,mPauseTime,mResumeTime, mRotationBlockTime);
|
117
|
mPre.unblockRotation();
|
118
|
}
|
119
|
}
|
120
|
|
121
|
long uiThreshhold = getThreshhold(mLastScramblingAndSolvingPlace);
|
122
|
long scramblingAndSolvingBlocked = now- mScramblingAndSolvingBlockTime;
|
123
|
|
124
|
if( mScramblingAndSolvingBlockTime >mPauseTime && scramblingAndSolvingBlocked>uiThreshhold )
|
125
|
{
|
126
|
boolean running = EffectMessageSender.isRunning();
|
127
|
ObjectLibInterface libInterface = mPre.getInterface();
|
128
|
|
129
|
if( !running )
|
130
|
{
|
131
|
libInterface.reportBlockProblem(TYPE_THREAD,0,mPauseTime,mResumeTime,0);
|
132
|
EffectMessageSender.restartThread();
|
133
|
}
|
134
|
else
|
135
|
{
|
136
|
libInterface.reportBlockProblem(TYPE_SCRAMBLING, mLastScramblingAndSolvingPlace,mPauseTime,mResumeTime, mScramblingAndSolvingBlockTime);
|
137
|
mPre.unblockScramblingAndSolving();
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
|
142
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
143
|
|
144
|
public void rotationBlocked(int place)
|
145
|
{
|
146
|
mRotationBlockTime = System.currentTimeMillis();
|
147
|
mLastRotationPlace = place;
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
|
152
|
public void scramblingAndSolvingBlocked(int place)
|
153
|
{
|
154
|
mScramblingAndSolvingBlockTime = System.currentTimeMillis();
|
155
|
mLastScramblingAndSolvingPlace = place;
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void rotationUnblocked()
|
161
|
{
|
162
|
mRotationBlockTime = 0;
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public void scramblingAndSolvingUnblocked()
|
168
|
{
|
169
|
mScramblingAndSolvingBlockTime = 0;
|
170
|
}
|
171
|
}
|