Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis game status #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 7 additions & 36 deletions src/main/java/com/matag/game/cardinstance/CardInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
creatorVisibility = JsonAutoDetect.Visibility.NONE
)
@Component
@Scope("prototype")
public class CardInstance {

private int id;
Expand All @@ -56,17 +54,14 @@ public class CardInstance {
private List<CardInstanceAbility> triggeredAbilities = new ArrayList<>();
private Set<String> acknowledgedBy = new HashSet<>();

private GameStatus gameStatus;
private final AttachmentsService attachmentsService;
private final AbilitiesFromOtherPermanentsService abilitiesFromOtherPermanentsService;
private int attachmentsPower = 0;
private int attachmentsToughness = 0;
private int powerFromOtherPermanents = 0;
private int toughnessFromOtherPermanents = 0;
private List<CardInstanceAbility> attachmentsAbilities = emptyList();
private List<CardInstanceAbility> abilitiesFormOtherPermanents = emptyList();


public CardInstance(
@Autowired(required = false) AttachmentsService attachmentsService,
@Autowired(required = false) AbilitiesFromOtherPermanentsService abilitiesFromOtherPermanentsService
) {
this.attachmentsService = attachmentsService;
this.abilitiesFromOtherPermanentsService = abilitiesFromOtherPermanentsService;
}

@JsonProperty
public int getId() {
Expand Down Expand Up @@ -262,28 +257,4 @@ public void cleanup() {
public void resetAllModifiers() {
modifiers = new CardModifiers();
}

private int getAttachmentsPower() {
return attachmentsService != null ? attachmentsService.getAttachmentsPower(gameStatus, this) : 0;
}

private int getAttachmentsToughness() {
return attachmentsService != null ? attachmentsService.getAttachmentsToughness(gameStatus, this) : 0;
}

private List<CardInstanceAbility> getAttachmentsAbilities() {
return attachmentsService != null ? attachmentsService.getAttachmentsAbilities(gameStatus, this) : emptyList();
}

private int getPowerFromOtherPermanents() {
return abilitiesFromOtherPermanentsService != null ? abilitiesFromOtherPermanentsService.getPowerFromOtherPermanents(gameStatus, this) : 0;
}

private int getToughnessFromOtherPermanents() {
return abilitiesFromOtherPermanentsService != null ? abilitiesFromOtherPermanentsService.getToughnessFromOtherPermanents(gameStatus, this) : 0;
}

private List<CardInstanceAbility> getAbilitiesFormOtherPermanents() {
return abilitiesFromOtherPermanentsService != null ? abilitiesFromOtherPermanentsService.getAbilitiesFormOtherPermanents(gameStatus, this) : emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
this.applicationContext = applicationContext;
}

public CardInstance create(GameStatus gameStatus, int id, Card card, String owner, String controller) {
CardInstance cardInstance = applicationContext.getBean(CardInstance.class);
cardInstance.setGameStatus(gameStatus);
public CardInstance create(int id, Card card, String owner, String controller) {
CardInstance cardInstance = new CardInstance();
cardInstance.setId(id);
cardInstance.setCard(card);
cardInstance.setOwner(owner);
cardInstance.setController(controller);
return cardInstance;
}

public CardInstance create(GameStatus gameStatus, int id, Card card, String owner) {
return create(gameStatus, id, card, owner, null);
public CardInstance create(int id, Card card, String owner) {
return create(id, card, owner, null);
}

public CardInstance mask(CardInstance cardInstance) {
return create(cardInstance.getGameStatus(), cardInstance.getId(), CardUtils.hiddenCard(), cardInstance.getOwner());
return create(cardInstance.getId(), CardUtils.hiddenCard(), cardInstance.getOwner());
}

public List<CardInstance> mask(List<CardInstance> cardInstanceList) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/matag/game/deck/DeckFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public List<CardInstance> create(String playerName, GameStatus gameStatus, DeckI
}

return cards.stream()
.map(card -> cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, playerName))
.map(card -> cardInstanceFactory.create(gameStatus.nextCardId(), card, playerName))
.collect(Collectors.toList());
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/matag/game/init/test/InitTestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@ public void initGameStatusForTest(GameStatus gameStatus) {
public abstract void initGameStatus(GameStatus gameStatus);

protected void addCardToCurrentPlayerLibrary(GameStatus gameStatus, Card card) {
gameStatus.getCurrentPlayer().getLibrary().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, gameStatus.getCurrentPlayer().getName()));
gameStatus.getCurrentPlayer().getLibrary().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, gameStatus.getCurrentPlayer().getName()));
}

protected void addCardToCurrentPlayerHand(GameStatus gameStatus, Card card) {
gameStatus.getCurrentPlayer().getHand().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, gameStatus.getCurrentPlayer().getName()));
gameStatus.getCurrentPlayer().getHand().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, gameStatus.getCurrentPlayer().getName()));
}

protected void addCardToCurrentPlayerBattlefield(GameStatus gameStatus, Card card) {
String owner = gameStatus.getCurrentPlayer().getName();
String controller = gameStatus.getCurrentPlayer().getName();
gameStatus.getCurrentPlayer().getBattlefield().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, owner, controller));
gameStatus.getCurrentPlayer().getBattlefield().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, owner, controller));
}

protected void addCardToCurrentPlayerGraveyard(GameStatus gameStatus, Card card) {
gameStatus.getCurrentPlayer().getGraveyard().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, gameStatus.getCurrentPlayer().getName()));
gameStatus.getCurrentPlayer().getGraveyard().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, gameStatus.getCurrentPlayer().getName()));
}

protected void addCardToNonCurrentPlayerLibrary(GameStatus gameStatus, Card card) {
gameStatus.getNonCurrentPlayer().getLibrary().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, gameStatus.getNonCurrentPlayer().getName()));
gameStatus.getNonCurrentPlayer().getLibrary().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, gameStatus.getNonCurrentPlayer().getName()));
}

protected void addCardToNonCurrentPlayerHand(GameStatus gameStatus, Card card) {
gameStatus.getNonCurrentPlayer().getHand().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, gameStatus.getNonCurrentPlayer().getName()));
gameStatus.getNonCurrentPlayer().getHand().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, gameStatus.getNonCurrentPlayer().getName()));
}

protected void addCardToNonCurrentPlayerBattlefield(GameStatus gameStatus, Card card) {
String owner = gameStatus.getNonCurrentPlayer().getName();
String controller = gameStatus.getNonCurrentPlayer().getName();
gameStatus.getNonCurrentPlayer().getBattlefield().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, owner, controller));
gameStatus.getNonCurrentPlayer().getBattlefield().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, owner, controller));
}

protected void addCardToNonCurrentPlayerGraveyard(GameStatus gameStatus, Card card) {
gameStatus.getNonCurrentPlayer().getGraveyard().addCard(cardInstanceFactory.create(gameStatus, gameStatus.nextCardId(), card, gameStatus.getNonCurrentPlayer().getName()));
gameStatus.getNonCurrentPlayer().getGraveyard().addCard(cardInstanceFactory.create(gameStatus.nextCardId(), card, gameStatus.getNonCurrentPlayer().getName()));
}

public void setCardInstanceFactory(CardInstanceFactory cardInstanceFactory) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/matag/game/stack/SpellStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import java.util.LinkedList;

@Component
@Scope("prototype")
public class SpellStack {
private LinkedList<CardInstance> items = new LinkedList<>();

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/matag/game/status/GameStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.concurrent.atomic.AtomicInteger;

@Getter
@Component
@Scope("prototype")
public class GameStatus {
private final AtomicInteger nextCardId = new AtomicInteger();
private String gameId;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/matag/game/status/GameStatusFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.matag.game.status;

import com.matag.game.stack.SpellStack;
import com.matag.game.turn.Turn;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -15,7 +17,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
}

public GameStatus create(String gameId) {
GameStatus gameStatus = applicationContext.getBean(GameStatus.class);
GameStatus gameStatus = new GameStatus(new Turn(), new SpellStack());
gameStatus.setGameId(gameId);
return gameStatus;
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/matag/game/turn/Turn.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

@ToString
@EqualsAndHashCode
@Component
@Scope("prototype")
public class Turn {
private int turnNumber;
private String currentTurnPlayer;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/matag/game/turn/TurnController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.matag.game.status.GameStatusRepository;
import com.matag.game.status.GameStatusUpdaterService;
import com.matag.game.turn.action._continue.ConsolidateStatusService;
import com.matag.game.turn.action.attach.AttachmentsService;
import com.matag.game.turn.action.selection.AbilitiesFromOtherPermanentsService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,6 +32,8 @@ public class TurnController {
private final GameStatusUpdaterService gameStatusUpdaterService;
private final TurnService turnService;
private final ConsolidateStatusService consolidateStatusService;
private final AttachmentsService attachmentsService;
private final AbilitiesFromOtherPermanentsService abilitiesFromOtherPermanentsService;

@MessageMapping("/game/turn")
public void turn(SimpMessageHeaderAccessor headerAccessor, TurnRequest request) {
Expand All @@ -54,6 +58,9 @@ public void turn(SimpMessageHeaderAccessor headerAccessor, TurnRequest request)
turnService.declareBlockers(gameStatus, toMapListInteger(request.getTargetsIdsForCardIds()));
}

consolidateStatusService.consolidate(gameStatus);
abilitiesFromOtherPermanentsService.updateGameStatus(gameStatus);
attachmentsService.updateGameStatus(gameStatus);
consolidateStatusService.consolidate(gameStatus);
gameStatusUpdaterService.sendUpdateGameStatus(gameStatus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import com.matag.cards.ability.AbilityService;
import com.matag.cards.ability.type.AbilityType;
import com.matag.cards.properties.PowerToughness;
import com.matag.game.player.Battlefield;
import com.matag.game.player.Player;
import com.matag.game.status.GameStatus;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

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

import static com.matag.cards.ability.type.AbilityType.ENCHANTED_CREATURE_GETS;
import static com.matag.cards.ability.type.AbilityType.EQUIPPED_CREATURE_GETS;
Expand All @@ -25,6 +28,17 @@ public class AttachmentsService {
private final AbilityService abilityService;
private final CardInstanceAbilityFactory cardInstanceAbilityFactory;

public void updateGameStatus(GameStatus gameStatus) {
Stream.of(gameStatus.getPlayer1(), gameStatus.getPlayer2())
.map(Player::getBattlefield)
.flatMap(battlefield -> battlefield.getCards().stream())
.forEach(cardInstance -> {
cardInstance.setAttachmentsPower(getAttachmentsPower(gameStatus, cardInstance));
cardInstance.setAttachmentsToughness(getAttachmentsToughness(gameStatus, cardInstance));
cardInstance.setAttachmentsAbilities(getAttachmentsAbilities(gameStatus, cardInstance));
});
}

public List<CardInstance> getAttachedCards(GameStatus gameStatus, CardInstance cardInstance) {
return gameStatus.getAllBattlefieldCards().attachedToId(cardInstance.getId()).getCards();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import com.matag.cards.ability.AbilityService;
import com.matag.cards.ability.trigger.TriggerType;
import com.matag.cards.properties.PowerToughness;
import com.matag.game.player.Player;
import com.matag.game.status.GameStatus;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static com.matag.cards.ability.type.AbilityType.SELECTED_PERMANENTS_GET;

Expand All @@ -22,6 +24,17 @@ public class AbilitiesFromOtherPermanentsService {
private final AbilityService abilityService;
private final CardInstanceAbilityFactory cardInstanceAbilityFactory;

public void updateGameStatus(GameStatus gameStatus) {
Stream.of(gameStatus.getPlayer1(), gameStatus.getPlayer2())
.map(Player::getBattlefield)
.flatMap(battlefield -> battlefield.getCards().stream())
.forEach(cardInstance -> {
cardInstance.setPowerFromOtherPermanents(getPowerFromOtherPermanents(gameStatus, cardInstance));
cardInstance.setToughnessFromOtherPermanents(getToughnessFromOtherPermanents(gameStatus, cardInstance));
cardInstance.setAbilitiesFormOtherPermanents(getAbilitiesFormOtherPermanents(gameStatus, cardInstance));
});
}

public int getPowerFromOtherPermanents(GameStatus gameStatus, CardInstance cardInstance) {
return getParametersFromOtherPermanents(gameStatus, cardInstance).stream()
.map(abilityService::powerToughnessFromParameter)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/application/browser/MatagBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ WebElement findElement(By element) {
}

void wait(ExpectedCondition<?> condition) {
new WebDriverWait(webDriver, 5).until(condition);
new WebDriverWait(webDriver, 10).until(condition);
}

public void dumpContent() {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/integration/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public GameStatus testGameStatus() {
PlayerInfo player1 = new PlayerInfo("player-name");
SecurityToken player1SecurityToken = new SecurityToken("player-session", UUID.randomUUID().toString(), "1");
gameStatus.setPlayer1(playerFactory.create(player1SecurityToken, player1));
gameStatus.getPlayer1().getLibrary().addCards(testLibrary(gameStatus, player1.getPlayerName()));
gameStatus.getPlayer1().getLibrary().addCards(testLibrary(player1.getPlayerName()));
gameStatus.getPlayer1().drawHand();

PlayerInfo player2 = new PlayerInfo("opponent-name");
SecurityToken player2SecurityToken = new SecurityToken("opponent-session", UUID.randomUUID().toString(), "1");
gameStatus.setPlayer2(playerFactory.create(player2SecurityToken, player2));
gameStatus.getPlayer2().getLibrary().addCards(testLibrary(gameStatus, player2.getPlayerName()));
gameStatus.getPlayer2().getLibrary().addCards(testLibrary(player2.getPlayerName()));
gameStatus.getPlayer2().drawHand();

gameStatus.getTurn().setCurrentTurnPlayer("player-name");
Expand All @@ -53,10 +53,10 @@ public GameStatus testGameStatus() {
return gameStatus;
}

private List<CardInstance> testLibrary(GameStatus gameStatus, String playerName) {
private List<CardInstance> testLibrary(String playerName) {
return IntStream.rangeClosed(1, 40)
.boxed()
.map(i -> cardInstanceFactory.create(gameStatus, i, cards.get("Plains"), playerName))
.map(i -> cardInstanceFactory.create(i, cards.get("Plains"), playerName))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void shouldCreateACardInstance() {
GameStatus gameStatus = testUtils.testGameStatus();

// When
CardInstance cardInstance = cardInstanceFactory.create(gameStatus, 1, cards.get("Short Sword"), "player-name");
CardInstance cardInstance = cardInstanceFactory.create(1, cards.get("Short Sword"), "player-name");

// Then
assertThat(cardInstance.getId()).isEqualTo(1);
Expand All @@ -46,8 +46,8 @@ public void shouldCreateTwoDifferentCardInstances() {
GameStatus gameStatus = testUtils.testGameStatus();

// When
CardInstance cardInstance1 = cardInstanceFactory.create(gameStatus, 1, cards.get("Short Sword"), "player-name");
CardInstance cardInstance2 = cardInstanceFactory.create(gameStatus, 2, cards.get("Befuddle"), "opponent-name");
CardInstance cardInstance1 = cardInstanceFactory.create(1, cards.get("Short Sword"), "player-name");
CardInstance cardInstance2 = cardInstanceFactory.create(2, cards.get("Befuddle"), "opponent-name");

// Then
assertThat(cardInstance1).isNotSameAs(cardInstance2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void canAffordReturnsFalseIfWrongManaDualLands() {

private CardInstance createCardInstance(GameStatus gameStatus, String cardName) {
Card card = cards.get(cardName);
CardInstance cardInstance = cardInstanceFactory.create(gameStatus, ++cardInstanceId, card, "player-name", "player-name");
CardInstance cardInstance = cardInstanceFactory.create(++cardInstanceId, card, "player-name", "player-name");
gameStatus.getActivePlayer().getBattlefield().addCard(cardInstance);
return cardInstance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void isCastingCostFulfilledCheckpointOfficerTapAbility() {

private CardInstance createCardInstance(GameStatus gameStatus, String cardName) {
Card card = cards.get(cardName);
CardInstance cardInstance = cardInstanceFactory.create(gameStatus, ++cardInstanceId, card, "player-name", "player-name");
CardInstance cardInstance = cardInstanceFactory.create(++cardInstanceId, card, "player-name", "player-name");
gameStatus.getActivePlayer().getBattlefield().addCard(cardInstance);
return cardInstance;
}
Expand Down
Loading