From 46343b3f588e254b6f5781e096d05ba5514032cf Mon Sep 17 00:00:00 2001 From: Christopher White <18whitechristop@gmail.com> Date: Fri, 8 Mar 2024 16:22:09 -0800 Subject: [PATCH] Add timelimit variable Signed-off-by: Christopher White <18whitechristop@gmail.com> --- .../java/tc/oc/pgm/timelimit/TimeLimit.java | 10 ++++ .../tc/oc/pgm/variables/VariableParser.java | 7 +++ .../variables/types/TimeLimitVariable.java | 51 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 core/src/main/java/tc/oc/pgm/variables/types/TimeLimitVariable.java diff --git a/core/src/main/java/tc/oc/pgm/timelimit/TimeLimit.java b/core/src/main/java/tc/oc/pgm/timelimit/TimeLimit.java index b77aab2651..1182ac5b52 100644 --- a/core/src/main/java/tc/oc/pgm/timelimit/TimeLimit.java +++ b/core/src/main/java/tc/oc/pgm/timelimit/TimeLimit.java @@ -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; } diff --git a/core/src/main/java/tc/oc/pgm/variables/VariableParser.java b/core/src/main/java/tc/oc/pgm/variables/VariableParser.java index fd1dc78cb8..8140149a3b 100644 --- a/core/src/main/java/tc/oc/pgm/variables/VariableParser.java +++ b/core/src/main/java/tc/oc/pgm/variables/VariableParser.java @@ -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. @@ -89,6 +90,12 @@ public VariableDefinition parseScore(Element el, String id) throws Invali return VariableDefinition.ofStatic(id, Party.class, ScoreVariable::new); } + @MethodParser("timelimit") + public VariableDefinition parseTimeLimit(Element el, String id) + throws InvalidXMLException { + return VariableDefinition.ofStatic(id, Match.class, TimeLimitVariable::new); + } + @MethodParser("with-team") public VariableDefinition parseTeamAdapter(Element el, String id) throws InvalidXMLException { diff --git a/core/src/main/java/tc/oc/pgm/variables/types/TimeLimitVariable.java b/core/src/main/java/tc/oc/pgm/variables/types/TimeLimitVariable.java new file mode 100644 index 0000000000..195f6113bd --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/variables/types/TimeLimitVariable.java @@ -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 { + + private TimeLimit oldTimeLimit; + private TimeLimitMatchModule tlmm; + + public TimeLimitVariable(VariableDefinition 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(); + } +}