Skip to content

Commit

Permalink
Add support for simple predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
igoudt committed Oct 26, 2021
1 parent 8ce8cde commit 73e5d73
Show file tree
Hide file tree
Showing 37 changed files with 337 additions and 201 deletions.
68 changes: 34 additions & 34 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
- name: Build with Maven
run: mvn -B package --file pom.xml
- uses: actions/checkout@v2
- name: Set up JDK 8
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
- name: Build with Maven
run: mvn -B package --file pom.xml

test:
needs: build
runs-on: ubuntu-latest
Expand All @@ -39,31 +39,31 @@ jobs:
with:
name: codecov


sonar:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Cache SonarCloud packages
uses: actions/[email protected]
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/[email protected]
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=ingmargoudt_referee
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Cache SonarCloud packages
uses: actions/[email protected]
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Maven packages
uses: actions/[email protected]
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=ingmargoudt_referee
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# referee

[![codecov](https://codecov.io/gh/ingmargoudt/referee/branch/main/graph/badge.svg?token=VYZIZE8PYX)](https://codecov.io/gh/ingmargoudt/referee)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ingmargoudt_referee&metric=alert_status)](https://sonarcloud.io/dashboard?id=ingmargoudt_referee)
5 changes: 2 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -101,7 +101,6 @@
</dependency>



</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.ingmargoudt.referee.cards.e;

import io.github.ingmargoudt.referee.game.CardType;
import io.github.ingmargoudt.referee.game.effects.CounterEffect;
import io.github.ingmargoudt.referee.game.objects.Card;
import io.github.ingmargoudt.referee.game.targets.Filter;
import io.github.ingmargoudt.referee.game.targets.TargetSpell;

public class EssenceScatter extends Card {

public EssenceScatter() {
super("Essence Scatter");
cardtypes.add(CardType.INSTANT);
getSpellEffects().add(new CounterEffect(new TargetSpell(Filter.by(CardType.CREATURE.getPredicate()))));
}
}
11 changes: 9 additions & 2 deletions src/main/java/io/github/ingmargoudt/referee/game/CardType.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package io.github.ingmargoudt.referee.game;

public enum CardType {
import io.github.ingmargoudt.referee.game.targets.CardTypePredicate;
import io.github.ingmargoudt.referee.game.targets.Predicate;

public enum CardType implements Predicatable {
CREATURE,
ENCHANTMENT,
INSTANT,
LAND
LAND;

public Predicate getPredicate() {
return new CardTypePredicate(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public boolean isLand() {
public void clear() {
cardtypesList.clear();
}

public boolean has(CardType cardType) {
return cardtypesList.contains(cardType);
}
}
3 changes: 1 addition & 2 deletions src/main/java/io/github/ingmargoudt/referee/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ public void start() {
public void assignActivePlayer() {
if (activePlayer == null) {
activePlayer = players[0].getId();
}
else {
} else {
for (int i = 0; i < players.length; i++) {
if (players[i].getId().equals(activePlayer)) {
setActive(players[(i + 1) % players.length].getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.ingmargoudt.referee.game;

import io.github.ingmargoudt.referee.game.targets.Predicate;

public interface Predicatable {

Predicate getPredicate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ private Indestructible() {
super();
}

public static Indestructible getInstance(){
public static Indestructible getInstance() {
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class StaticAbility extends Ability {

List<ContinuousEffect> effects = new ArrayList<>();

public StaticAbility(){
public StaticAbility() {
super();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.github.ingmargoudt.referee.game.objects.MagicObject;
import io.github.ingmargoudt.referee.game.properties.Damageable;
import io.github.ingmargoudt.referee.game.targets.Target;
import io.github.ingmargoudt.referee.game.targets.TargetAny;

public class DamageTargetEffect extends OneShotEffect implements TargetEffect {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import io.github.ingmargoudt.referee.game.abilities.Ability;
import io.github.ingmargoudt.referee.game.effects.OneShotEffect;
import io.github.ingmargoudt.referee.game.effects.ReplacementEffect;
import io.github.ingmargoudt.referee.players.Player;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;


Expand All @@ -27,7 +27,7 @@ public class MagicObject extends BaseObject {

protected String name;
protected ManaCost manaCost;
protected Color color;
protected Set<Color> color;
protected Color colorIndicator;
protected CardTypes cardtypes;
protected SubTypes subTypes;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void addAbility(Ability ability) {
abilities.add(ability);
}

public void removeAbility(Class<? extends Ability> abilityClass){
public void removeAbility(Class<? extends Ability> abilityClass) {
abilities.remove(abilityClass);
}

Expand All @@ -85,4 +85,8 @@ public boolean isBasic() {
public boolean isControlledBy(UUID thePlayer) {
return getController().equals(thePlayer);
}

public boolean hasColor(Color color) {
return this.color.contains(color);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ public void damage(int amount) {
public int getReceivedDamage() {
return damageReceived;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
public class Spell extends MagicObject implements Stackable, Counterable, Targetable {

@Getter
Card card;
private Card card;


public Spell(Card card) {
super(card.getName());
this.card = card;
this.cardtypes = card.getCardtypes();
this.spellEffects = card.getSpellEffects();
}

public void resolve(Game game) {
if (card.isPermanent()) {
game.moveToBattlefield(card);
} else {
card.getSpellEffects().forEach(effect -> effect.apply(this, game));
getSpellEffects().forEach(effect -> effect.apply(this, game));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.ingmargoudt.referee.game.targets;

import io.github.ingmargoudt.referee.game.CardType;
import io.github.ingmargoudt.referee.game.Game;
import io.github.ingmargoudt.referee.game.objects.Spell;
import io.github.ingmargoudt.referee.game.properties.Targetable;

public class CardTypePredicate implements Predicate {

CardType theCardType;

public CardTypePredicate(CardType cardType) {
this.theCardType = cardType;
}

@Override
public boolean evaluate(Targetable target, Game game) {
if (target instanceof Spell) {
return ((Spell) target).getCard().getCardtypes().has(theCardType);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.ingmargoudt.referee.game.targets;

import io.github.ingmargoudt.referee.game.Game;
import io.github.ingmargoudt.referee.game.properties.Targetable;

import java.util.ArrayList;
import java.util.List;

public class Filter {

List<Predicate> predicates = new ArrayList<>();

private Filter() {

}

public static Filter by(Predicate predicate) {
return new Filter().addPredicate(predicate);
}

public static Filter empty() {
return new Filter();
}

public Filter addPredicate(Predicate predicate) {
predicates.add(predicate);
return this;
}

public boolean evaluate(Targetable target, Game game) {
return predicates.stream().allMatch(predicate -> predicate.evaluate(target, game));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.ingmargoudt.referee.game.targets;

import io.github.ingmargoudt.referee.game.Game;
import io.github.ingmargoudt.referee.game.properties.Targetable;

@FunctionalInterface
public interface Predicate {

boolean evaluate(Targetable target, Game game);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public abstract class Target {

Filter filter = Filter.empty();


public abstract List<Targetable> validTargets(Game game);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TargetAny extends Target {

@Override
public List<Targetable> validTargets(Game game) {
return Stream.concat(game.getPlayers().stream().map(Targetable.class::cast),
return Stream.concat(game.getPlayers().stream().map(Targetable.class::cast).filter(p -> filter.evaluate(p, game)),
game.getBattlefield().getAll().stream().map(Targetable.class::cast)).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@
import io.github.ingmargoudt.referee.game.objects.Spell;
import io.github.ingmargoudt.referee.game.properties.Stackable;
import io.github.ingmargoudt.referee.game.properties.Targetable;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@NoArgsConstructor
public class TargetSpell extends Target {

Targetable theTarget;


public TargetSpell(Filter filter) {
super();
this.filter = filter;
}

@Override
public List<Targetable> validTargets(Game game) {
return game.getStack().show().stream().filter(Spell.class::isInstance).collect(Collectors.toList());
return game.getStack().show().stream().filter(Spell.class::isInstance).filter(spell -> filter.evaluate(spell, game)).collect(Collectors.toList());
}

@Override
public Optional<Targetable> resolve(Game game) {
return game.getStack().show().stream().filter(Spell.class::isInstance).filter(spell -> spell.getId().equals(theTarget.getId())).findFirst();
return game.getStack().show().stream().filter(Spell.class::isInstance).filter(spell -> spell.getId().equals(theTarget.getId())).filter(spell -> filter.evaluate(spell, game)).findFirst();
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Set root logger level to INFO and its only appender to stdout.
log4j.rootLogger=INFO, stdout

# Define the stdout appender to output logs to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
Expand Down
Loading

0 comments on commit 73e5d73

Please sign in to comment.