-
Notifications
You must be signed in to change notification settings - Fork 91
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 d8b533c
Showing
3 changed files
with
71 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
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; | ||
if (existingTimeLimit == null) { | ||
newTimeLimit = new TimeLimit(oldTimeLimit, Duration.of((long) value, ChronoUnit.SECONDS)); | ||
} else { | ||
newTimeLimit = | ||
new TimeLimit(existingTimeLimit, Duration.of((long) value, ChronoUnit.SECONDS)); | ||
} | ||
tlmm.setTimeLimit(newTimeLimit); | ||
tlmm.start(); | ||
} | ||
} |