-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher White <[email protected]>
- Loading branch information
1 parent
2708649
commit 46343b3
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/src/main/java/tc/oc/pgm/variables/types/TimeLimitVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package tc.oc.pgm.variables.types; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import tc.oc.pgm.api.match.Match; | ||
import tc.oc.pgm.timelimit.TimeLimit; | ||
import tc.oc.pgm.timelimit.TimeLimitMatchModule; | ||
import tc.oc.pgm.variables.VariableDefinition; | ||
|
||
public class TimeLimitVariable extends AbstractVariable<Match> { | ||
|
||
private TimeLimit oldTimeLimit; | ||
private TimeLimitMatchModule tlmm; | ||
|
||
public TimeLimitVariable(VariableDefinition<Match> definition) { | ||
super(definition); | ||
oldTimeLimit = | ||
new TimeLimit(null, Duration.of(0, ChronoUnit.SECONDS), null, null, null, null, true); | ||
} | ||
|
||
@Override | ||
public void postLoad(Match match) { | ||
tlmm = match.moduleRequire(TimeLimitMatchModule.class); | ||
} | ||
|
||
@Override | ||
protected double getValueImpl(Match obj) { | ||
Duration remaining = tlmm.getFinalRemaining(); | ||
return remaining == null ? -1 : remaining.getSeconds(); | ||
} | ||
|
||
@Override | ||
protected void setValueImpl(Match obj, double value) { | ||
TimeLimit existingTimeLimit = tlmm.getTimeLimit(); | ||
if (value < 0) { | ||
if (existingTimeLimit != null) { | ||
oldTimeLimit = existingTimeLimit; | ||
} | ||
|
||
tlmm.cancel(); | ||
return; | ||
} | ||
|
||
TimeLimit newTimeLimit = | ||
new TimeLimit( | ||
existingTimeLimit != null ? existingTimeLimit : oldTimeLimit, | ||
Duration.of((long) value, ChronoUnit.SECONDS)); | ||
tlmm.setTimeLimit(newTimeLimit); | ||
tlmm.start(); | ||
} | ||
} |