-
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.
Signed-off-by: Christopher White <[email protected]>
- Loading branch information
1 parent
c9c4b3e
commit de13461
Showing
5 changed files
with
161 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tc.oc.pgm.action; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public enum Operation { | ||
ADD("add", "addition", "plus"), | ||
SUBTRACT("sub", "subtract", "minus"), | ||
SET("set"); | ||
|
||
String[] aliases; | ||
|
||
Operation(String... aliases) { | ||
this.aliases = aliases; | ||
} | ||
|
||
static final Map<String, Operation> operationMap = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
for (Operation operation : Operation.values()) { | ||
for (String alias : operation.aliases) { | ||
operationMap.put(alias, operation); | ||
} | ||
} | ||
} | ||
|
||
public static Operation parseOperation(String operationString) { | ||
return operationMap.get(operationString); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
core/src/main/java/tc/oc/pgm/action/actions/BlitzLivesAction.java
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,87 @@ | ||
package tc.oc.pgm.action.actions; | ||
|
||
import java.util.function.Consumer; | ||
import tc.oc.pgm.action.Operation; | ||
import tc.oc.pgm.api.match.Match; | ||
import tc.oc.pgm.api.party.Party; | ||
import tc.oc.pgm.api.player.MatchPlayer; | ||
import tc.oc.pgm.blitz.BlitzMatchModule; | ||
import tc.oc.pgm.filters.Filterable; | ||
import tc.oc.pgm.util.math.Formula; | ||
|
||
public class BlitzLivesAction<T extends Filterable<?>> extends AbstractAction<T> { | ||
|
||
final Consumer<T> modifyLivesConsumer; | ||
final Operation operation; | ||
final Formula<T> formula; | ||
final boolean showTitle; | ||
|
||
public BlitzLivesAction( | ||
Class<T> scope, Operation operation, Formula<T> formula, boolean showTitle) { | ||
super(scope); | ||
this.operation = operation; | ||
this.formula = formula; | ||
this.showTitle = showTitle; | ||
this.modifyLivesConsumer = chooseScopeConsumer(scope); | ||
} | ||
|
||
public void changeLives(MatchPlayer player, int amount) { | ||
BlitzMatchModule blitzMatchModule = player.getMatch().getModule(BlitzMatchModule.class); | ||
if (blitzMatchModule == null) return; | ||
|
||
switch (operation) { | ||
case ADD: | ||
blitzMatchModule.addLives(player.getId(), amount); | ||
break; | ||
case SUBTRACT: | ||
blitzMatchModule.addLives(player.getId(), amount * -1); | ||
break; | ||
case SET: | ||
blitzMatchModule.setLives(player.getId(), amount); | ||
break; | ||
} | ||
|
||
if (showTitle) { | ||
blitzMatchModule.showLivesTitle(player); | ||
} | ||
} | ||
|
||
@Override | ||
public void trigger(T t) { | ||
modifyLivesConsumer.accept(t); | ||
} | ||
|
||
private Consumer<T> chooseScopeConsumer(Class<T> cls) { | ||
if (MatchPlayer.class.isAssignableFrom(cls)) { | ||
return this::triggerPlayer; | ||
} else if (Party.class.isAssignableFrom(cls)) { | ||
return this::triggerParty; | ||
} else if (Match.class.isAssignableFrom(cls)) { | ||
return this::triggerMatch; | ||
} else { | ||
throw new UnsupportedOperationException("Unknown Scope: " + cls); | ||
} | ||
} | ||
|
||
public void triggerPlayer(T t) { | ||
MatchPlayer player = (MatchPlayer) t; | ||
int amount = (int) formula.applyAsDouble(t); | ||
changeLives(player, amount); | ||
} | ||
|
||
public void triggerParty(T t) { | ||
Party party = (Party) t; | ||
int amount = (int) formula.applyAsDouble(t); | ||
for (MatchPlayer player : party.getPlayers()) { | ||
changeLives(player, amount); | ||
} | ||
} | ||
|
||
public void triggerMatch(T t) { | ||
Match match = (Match) t; | ||
int amount = (int) formula.applyAsDouble(t); | ||
for (MatchPlayer player : match.getParticipants()) { | ||
changeLives(player, amount); | ||
} | ||
} | ||
} |
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