Repository navigation
Expand file tree
/
Copy pathSequence.java
More file actions
217 lines (161 loc) · 6.8 KB
/
Copy pathSequence.java
File metadata and controls
217 lines (161 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.abilityapi.sequenceapi;
import com.abilityapi.sequenceapi.action.Action;
import com.abilityapi.sequenceapi.action.type.observe.ObserverAction;
import com.abilityapi.sequenceapi.action.type.schedule.ScheduleAction;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Represents an order of {@link Action}s
* and a means of manipulating them to produce
* a result.
*
* @param <T> the event type
*/
public class Sequence<T> {
private final SequenceContext sequenceContext;
private final SequenceBlueprint<T> sequenceBlueprint;
private final Map<ScheduleAction, Integer> scheduleActions = new HashMap<>();
private final Map<ObserverAction<T>, Integer> observerActions = new HashMap<>();
private int index = 0;
private long ticks = 0;
private long lastExecutionTime = System.currentTimeMillis();
private State state = State.INACTIVE;
public Sequence(final SequenceContext sequenceContext,
final SequenceBlueprint<T> sequenceBlueprint,
final Map<ScheduleAction, Integer> scheduleActions,
final Map<ObserverAction<T>, Integer> observerActions) {
this.sequenceContext = sequenceContext;
this.sequenceBlueprint = sequenceBlueprint;
this.scheduleActions.putAll(scheduleActions);
this.observerActions.putAll(observerActions);
}
/**
* Applies the next {@link ObserverAction} in the {@link Sequence}
* with an appropriate {@link T} and {@link SequenceContext}.
*
* @param event the event
* @param sequenceContext the sequence context
* @return true if the action was successful and false if it was not
*/
public boolean applyObserve(final T event, final SequenceContext sequenceContext) {
final Iterator<ObserverAction<T>> iterator = this.observerActions.keySet().iterator();
if (this.state.equals(State.INACTIVE)) this.state = State.ACTIVE;
if (iterator.hasNext()) {
final ObserverAction<T> action = iterator.next();
if (this.observerActions.get(action) != this.index) return false;
this.index++;
final long current = System.currentTimeMillis();
// 1. Check that the event is the correct one for this action.
if (!action.getEventClass().equals(event.getClass())) return this.fail(action, sequenceContext);
// 2. Fail the action if it is being executed before the delay.
if (this.lastExecutionTime + ((action.getDelay() / 20) * 1000) > current) {
return this.fail(action, sequenceContext);
}
// 3. Fail the action if it being executed after the expire.
if (this.lastExecutionTime + ((action.getExpire() / 20) * 1000) < current) {
return this.fail(action, sequenceContext);
}
// 4. Run the action conditions and fail if they do not pass.
if (!action.apply(sequenceContext)) {
return this.fail(action, sequenceContext);
}
// 5. Succeed the action, remove it and set finish if there are no more actions left.
iterator.remove();
return this.succeed(action, sequenceContext);
}
return true;
}
/**
* Applies the next {@link ScheduleAction} in the {@link Sequence}
* with an appropriate {@link SequenceContext}.
*
* @param sequenceContext the sequence context
* @return true if the action was successful and false if it was not
*/
public boolean applySchedule(final SequenceContext sequenceContext) {
final Iterator<ScheduleAction> iterator = this.scheduleActions.keySet().iterator();
if (this.state.equals(State.INACTIVE)) this.state = State.ACTIVE;
this.ticks++;
if (iterator.hasNext()) {
final ScheduleAction action = iterator.next();
if (this.scheduleActions.get(action) != this.index) return false;
this.index++;
final long current = System.currentTimeMillis();
// 1. Check that the tick is being executed in the period wanted.
if (this.ticks % action.getPeriod() != 0) return false;
// 2. Fail the action if it is being executed before the delay.
if (this.lastExecutionTime + ((action.getDelay() / 20) * 1000) > current)
return this.fail(action, sequenceContext);
// 3. Fail the action if it being executed after the expire.
if (this.lastExecutionTime + ((action.getExpire() / 20) * 1000) < current) {
if (action.getRepeats() != 0) {
iterator.remove();
return this.succeed(action, sequenceContext);
}
return this.fail(action, sequenceContext);
}
// 4. Run the action conditions and fail if they do not pass.
if (!action.apply(sequenceContext)) return this.fail(action, sequenceContext);
// 5. Succeed the action, remove it and set finish if there are no more actions left.
iterator.remove();
return this.succeed(action, sequenceContext);
}
return true;
}
public boolean succeed(final Action action, final SequenceContext sequenceContext) {
action.success(sequenceContext);
this.lastExecutionTime = System.currentTimeMillis();
if (this.index >= this.observerActions.size() + this.scheduleActions.size()) this.state = State.FINISHED;
return true;
}
public boolean fail(final Action action, final SequenceContext sequenceContext) {
this.state = action.failure(sequenceContext) ? State.CANCELLED : this.state;
return false;
}
/**
* Returns the {@link SequenceBlueprint}.
*
* @return the sequence blueprint
*/
public final SequenceBlueprint<T> getBlueprint() {
return this.sequenceBlueprint;
}
/**
* Returns the {@link SequenceContext}.
*
* @return the sequence context
*/
public final SequenceContext getSequenceContext() {
return this.sequenceContext;
}
/**
* Returns the trigger {@link T} class.
*
* @return the trigger class
*/
public final Class<? extends T> getTrigger() {
return this.sequenceBlueprint.getTrigger();
}
/**
* Returns the {@link Sequence.State}.
* @return the sequence state
*/
public final State getState() {
return this.state;
}
public enum State {
ACTIVE(true),
INACTIVE(true),
CANCELLED(false),
EXPIRED(false),
FINISHED(true);
private final boolean safe;
State(boolean safe) {
this.safe = safe;
}
public final boolean isSafe() {
return this.safe;
}
}
}