Skip to content

Commit

Permalink
Add match state filter
Browse files Browse the repository at this point in the history
Signed-off-by: KingSimon <[email protected]>
  • Loading branch information
KingOfSquares authored Jan 8, 2021
1 parent 2f7747f commit 217c044
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/src/main/java/tc/oc/pgm/filters/FilterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,44 @@ public ScoreFilter parseScoreFilter(Element el) throws InvalidXMLException {
return new ScoreFilter(XMLUtils.parseNumericRange(new Node(el), Integer.class));
}

@MethodParser("match-phase")
public Filter parseMatchPhase(Element el) throws InvalidXMLException {
return parseMatchPhaseFilter(el.getValue(), el);
}

@MethodParser("match-started")
public Filter parseMatchStarted(Element el) throws InvalidXMLException {
return parseMatchPhaseFilter("started", el);
}

@MethodParser("match-running")
public Filter parseMatchRunning(Element el) throws InvalidXMLException {
return parseMatchPhaseFilter("running", el);
}

@MethodParser("match-finished")
public Filter parseMatchFinished(Element el) throws InvalidXMLException {
return parseMatchPhaseFilter("finished", el);
}

private Filter parseMatchPhaseFilter(String matchState, Element el) throws InvalidXMLException {

switch (matchState) {
case "running":
return MatchPhaseFilter.RUNNING;
case "finished":
return MatchPhaseFilter.FINISHED;
case "starting":
return MatchPhaseFilter.STARTING;
case "idle":
return MatchPhaseFilter.IDLE;
case "started":
return MatchPhaseFilter.STARTED;
}

throw new InvalidXMLException("Invalid or no match state found", el);
}

// Methods for parsing QueryModifiers

@MethodParser("offset")
Expand Down
33 changes: 33 additions & 0 deletions core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tc.oc.pgm.filters;

import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.filter.query.MatchQuery;
import tc.oc.pgm.api.match.MatchPhase;

public class MatchPhaseFilter extends TypedFilter<MatchQuery> {

public static final MatchPhaseFilter RUNNING = new MatchPhaseFilter(MatchPhase.RUNNING);
public static final MatchPhaseFilter FINISHED = new MatchPhaseFilter(MatchPhase.FINISHED);
public static final MatchPhaseFilter STARTING = new MatchPhaseFilter(MatchPhase.STARTING);
public static final MatchPhaseFilter IDLE = new MatchPhaseFilter(MatchPhase.IDLE);
public static final Filter STARTED =
AnyFilter.of(
new MatchPhaseFilter(MatchPhase.RUNNING), new MatchPhaseFilter(MatchPhase.FINISHED));

private final MatchPhase matchPhase;

public MatchPhaseFilter(MatchPhase matchPhase) {
this.matchPhase = matchPhase;
}

@Override
public Class<? extends MatchQuery> getQueryType() {
return MatchQuery.class;
}

@Override
protected QueryResponse queryTyped(MatchQuery query) {
MatchPhase current = query.getMatch().getPhase();
return QueryResponse.fromBoolean(matchPhase == current);
}
}

0 comments on commit 217c044

Please sign in to comment.