-
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 acting on Blitz Lives (#1214)
Signed-off-by: Christopher White <[email protected]>
- Loading branch information
1 parent
4c442f1
commit 87278fe
Showing
10 changed files
with
180 additions
and
58 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
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,33 @@ | ||
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<? extends Filterable<?>> definition) { | ||
this.definition = (VariableDefinition<MatchPlayer>) definition; | ||
} | ||
|
||
@Override | ||
public VariableDefinition<MatchPlayer> getDefinition() { | ||
return definition; | ||
} | ||
|
||
@Override | ||
public double getValue(Filterable<?> context) { | ||
MatchPlayer matchPlayer = context.getFilterableAncestor(MatchPlayer.class); | ||
BlitzMatchModule blitzMatchModule = matchPlayer.moduleRequire(BlitzMatchModule.class); | ||
return blitzMatchModule.getNumOfLives(matchPlayer.getId()); | ||
} | ||
|
||
@Override | ||
public void setValue(Filterable<?> context, double value) { | ||
MatchPlayer matchPlayer = context.getFilterableAncestor(MatchPlayer.class); | ||
BlitzMatchModule blitzMatchModule = matchPlayer.moduleRequire(BlitzMatchModule.class); | ||
blitzMatchModule.setLives(matchPlayer, Math.max((int) value, 0)); | ||
} | ||
} |
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,48 @@ | ||
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); | ||
} | ||
|
||
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,16 @@ | ||
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<>(); | ||
} | ||
public interface Variable<T extends Filterable<?>> extends Feature<VariableDefinition<T>> { | ||
|
||
@Override | ||
public String getId() { | ||
return definition.getId(); | ||
default String getId() { | ||
return getDefinition().getId(); | ||
} | ||
|
||
@Override | ||
public VariableDefinition<T> getDefinition() { | ||
return definition; | ||
} | ||
double getValue(Filterable<?> context); | ||
|
||
public double getValue(Filterable<?> context) { | ||
return values.computeIfAbsent(getAncestor(context), k -> definition.getDefault()); | ||
} | ||
|
||
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); | ||
} | ||
|
||
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()); | ||
} | ||
void setValue(Filterable<?> context, double value); | ||
} |
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,33 @@ | ||
package tc.oc.pgm.variables; | ||
|
||
import java.util.function.Function; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.filters.Filterable; | ||
|
||
public enum VariableType { | ||
DUMMY(DummyVariable::new, Filterable.class), | ||
LIVES(BlitzVariable::new, MatchPlayer.class); | ||
|
||
private final Function<VariableDefinition<?>, Variable<?>> supplierFunction; | ||
private final Class<?>[] supportedScopes; | ||
|
||
VariableType( | ||
Function<VariableDefinition<? extends Filterable>, Variable<?>> supplierFunction, | ||
Class<?>... supportedScopes) { | ||
this.supplierFunction = supplierFunction; | ||
this.supportedScopes = supportedScopes; | ||
} | ||
|
||
public boolean supports(Class<?> cls) { | ||
for (Class<?> supportedScope : supportedScopes) { | ||
if (supportedScope.isAssignableFrom(cls)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public Variable<?> buildInstance(VariableDefinition<?> definition) { | ||
return supplierFunction.apply(definition); | ||
} | ||
} |
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