-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for variables modifying blitz lives
Signed-off-by: Christopher White <[email protected]>
- Loading branch information
1 parent
c9c4b3e
commit bca3408
Showing
8 changed files
with
201 additions
and
57 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
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,38 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.blitz.BlitzMatchModule; | ||
import tc.oc.pgm.filters.Filterable; | ||
|
||
public class BlitzVariable implements Variable<MatchPlayer> { | ||
|
||
private final VariableDefinition<MatchPlayer> definition; | ||
|
||
public BlitzVariable(VariableDefinition<MatchPlayer> definition) { | ||
this.definition = definition; | ||
} | ||
|
||
@Override | ||
public VariableDefinition<MatchPlayer> getDefinition() { | ||
return definition; | ||
} | ||
|
||
@Override | ||
public double getValue(Filterable<?> context) { | ||
MatchPlayer matchPlayer = (MatchPlayer) context; | ||
BlitzMatchModule blitzMatchModule = matchPlayer.getMatch().needModule(BlitzMatchModule.class); | ||
return blitzMatchModule.getNumOfLives(matchPlayer.getId()); | ||
} | ||
|
||
@Override | ||
public void setValue(Filterable<?> context, double value) { | ||
MatchPlayer matchPlayer = (MatchPlayer) context; | ||
BlitzMatchModule blitzMatchModule = matchPlayer.getMatch().needModule(BlitzMatchModule.class); | ||
blitzMatchModule.setLives(matchPlayer, Math.max((int) value, 0)); | ||
} | ||
|
||
@Override | ||
public VariableType getType() { | ||
return VariableType.LIVES; | ||
} | ||
} |
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,53 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import tc.oc.pgm.filters.FilterMatchModule; | ||
import tc.oc.pgm.filters.Filterable; | ||
|
||
public class DummyVariable<T extends Filterable<?>> implements Variable<T> { | ||
|
||
private final VariableDefinition<T> definition; | ||
private final Map<T, Double> values; | ||
|
||
public DummyVariable(VariableDefinition<T> definition) { | ||
this.definition = definition; | ||
this.values = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public VariableDefinition<T> getDefinition() { | ||
return definition; | ||
} | ||
|
||
@Override | ||
public double getValue(Filterable<?> context) { | ||
return values.computeIfAbsent(getAncestor(context), k -> definition.getDefault()); | ||
} | ||
|
||
@Override | ||
public void setValue(Filterable<?> context, double value) { | ||
T ctx = getAncestor(context); | ||
values.put(ctx, value); | ||
// For performance reasons, let's avoid launching an event for every variable change | ||
context.getMatch().needModule(FilterMatchModule.class).invalidate(ctx); | ||
} | ||
|
||
@Override | ||
public VariableType getType() { | ||
return VariableType.DUMMY; | ||
} | ||
|
||
private T getAncestor(Filterable<?> context) { | ||
T filterable = context.getFilterableAncestor(definition.getScope()); | ||
if (filterable != null) return filterable; | ||
|
||
throw new IllegalStateException( | ||
"Wrong variable scope for '" | ||
+ getId() | ||
+ "', expected " | ||
+ definition.getScope().getSimpleName() | ||
+ " which cannot be found in " | ||
+ context.getClass().getSimpleName()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,52 +1,18 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import tc.oc.pgm.api.feature.Feature; | ||
import tc.oc.pgm.filters.FilterMatchModule; | ||
import tc.oc.pgm.filters.Filterable; | ||
|
||
public class Variable<T extends Filterable<?>> implements Feature<VariableDefinition<T>> { | ||
|
||
private final VariableDefinition<T> definition; | ||
private final Map<T, Double> values; | ||
|
||
public Variable(VariableDefinition<T> definition) { | ||
this.definition = definition; | ||
this.values = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return definition.getId(); | ||
} | ||
public interface Variable<T extends Filterable<?>> extends Feature<VariableDefinition<T>> { | ||
|
||
@Override | ||
public VariableDefinition<T> getDefinition() { | ||
return definition; | ||
default String getId() { | ||
return getDefinition().getId(); | ||
} | ||
|
||
public double getValue(Filterable<?> context) { | ||
return values.computeIfAbsent(getAncestor(context), k -> definition.getDefault()); | ||
} | ||
double getValue(Filterable<?> context); | ||
|
||
public void setValue(Filterable<?> context, double value) { | ||
T ctx = getAncestor(context); | ||
values.put(ctx, value); | ||
// For performance reasons, let's avoid launching an event for every variable change | ||
context.getMatch().needModule(FilterMatchModule.class).invalidate(ctx); | ||
} | ||
void setValue(Filterable<?> context, double value); | ||
|
||
private T getAncestor(Filterable<?> context) { | ||
T filterable = context.getFilterableAncestor(definition.getScope()); | ||
if (filterable != null) return filterable; | ||
|
||
throw new IllegalStateException( | ||
"Wrong variable scope for '" | ||
+ getId() | ||
+ "', expected " | ||
+ definition.getScope().getSimpleName() | ||
+ " which cannot be found in " | ||
+ context.getClass().getSimpleName()); | ||
} | ||
VariableType getType(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.filters.Filterable; | ||
import tc.oc.pgm.util.xml.InvalidXMLException; | ||
import tc.oc.pgm.util.xml.Node; | ||
|
||
public enum VariableType { | ||
DUMMY(Filterable.class), | ||
LIVES(MatchPlayer.class); | ||
|
||
private final Class<?>[] supportedScopes; | ||
|
||
VariableType(Class<?>... supportedScopes) { | ||
this.supportedScopes = supportedScopes; | ||
} | ||
|
||
public boolean supports(Class<?> cls) { | ||
for (Class<?> supportedScope : supportedScopes) { | ||
if (supportedScope.isAssignableFrom(cls)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static final Map<String, VariableType> stringTypeMap = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
for (VariableType value : VariableType.values()) { | ||
stringTypeMap.put(value.name().toLowerCase(Locale.ROOT), value); | ||
} | ||
} | ||
|
||
static VariableType parse(Node variableTypeNode, VariableType defaultType) | ||
throws InvalidXMLException { | ||
if (variableTypeNode == null) return defaultType; | ||
VariableType variableType = stringTypeMap.get(variableTypeNode.getValue()); | ||
if (variableType != null) return variableType; | ||
|
||
throw new InvalidXMLException( | ||
"Unknown Variable Type: " + variableTypeNode.getValue(), variableTypeNode); | ||
} | ||
} |
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