Skip to content

Commit

Permalink
Add timelimit variable
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 committed Mar 10, 2024
1 parent 2708649 commit 46343b3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/java/tc/oc/pgm/timelimit/TimeLimit.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public TimeLimit(
this.show = show;
}

public TimeLimit(TimeLimit timeLimit, Duration duration) {
super(timeLimit.getId());
this.duration = assertNotNull(duration);
this.overtime = timeLimit.getOvertime();
this.maxOvertime = timeLimit.getMaxOvertime();
this.endOvertime = timeLimit.getEndOvertime();
this.result = timeLimit.getResult();
this.show = timeLimit.getShow();
}

public Duration getDuration() {
return duration;
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/tc/oc/pgm/variables/VariableParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tc.oc.pgm.variables.types.MaxBuildVariable;
import tc.oc.pgm.variables.types.ScoreVariable;
import tc.oc.pgm.variables.types.TeamVariableAdapter;
import tc.oc.pgm.variables.types.TimeLimitVariable;

public class VariableParser {
// The limitation is due to them being used in exp4j formulas for.
Expand Down Expand Up @@ -89,6 +90,12 @@ public VariableDefinition<Party> parseScore(Element el, String id) throws Invali
return VariableDefinition.ofStatic(id, Party.class, ScoreVariable::new);
}

@MethodParser("timelimit")
public VariableDefinition<Match> parseTimeLimit(Element el, String id)
throws InvalidXMLException {
return VariableDefinition.ofStatic(id, Match.class, TimeLimitVariable::new);
}

@MethodParser("with-team")
public VariableDefinition<Match> parseTeamAdapter(Element el, String id)
throws InvalidXMLException {
Expand Down
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();
}
}

0 comments on commit 46343b3

Please sign in to comment.