Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timelimit variable #1260

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading