Skip to content

Commit

Permalink
Use card position as parameter for PlayPokemonToBench
Browse files Browse the repository at this point in the history
Before, we were using the card ID as a parameter for PlayPokemonToBench. We were first checking if the card is in the player's hand.
Remember, it's /PlayPokemonToBench?game=[id]&card[position]

Now, instead of passing the card ID as the parameter, we will pass the position of the card in the player's hand.
We use a zero-based index. If your hand is, for example, [2, 5, 9, 12, 27] (these are the actual card IDs):
    - If you choose card 0, you get the first card, 2.
    - If you choose card 2, you get the third card, 9.
    - If you choose card 10, you should get a fail message.
Of course, if you don't have any cards in your hand to begin with, you get a fail message.

Also updated README.md.

Signed-off-by: vartanbeno <vartanbeno@gmail.com>
  • Loading branch information
vartanbeno committed Nov 9, 2018
1 parent b2343c7 commit 2adf71e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 125 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,17 @@ To view your hand, visit `/ViewHand?game=[id]`, with the appropriate game ID par

### Playing Pokemon to the Bench

To play a Pokemon card to the bench, visit `/PlayPokemonToBench`, where you will see a list of Pokemon type cards that are currently in your hand. You can freely choose whichever to send to the bench.
To play a Pokemon card to the bench, visit `/PlayPokemonToBench?game=[id]&card=[position]`, where the `game` parameter is the ID of a game that you are a part of, and the `card` parameter is the position of the card in your hand that you want to send to the bench, using a zero-based index (e.g. position 2 corresponds to the 3rd card in your hand).

The maximum amount of Pokemon on your bench at any given time is 5, so you might get a fail message.

There are more features left to be implemented. Will update soon.
You can only send Pokemon to the bench. If you attempt to send a card that isn't a Pokemon to the bench, you will get a fail message.

### Retiring From a Game

To retire from a game, visit `/Retire?game=[id]`, where the `game` parameter is the ID of a game that you are a part of. Once you retire from a game, you can no longer draw cards or play Pokemon to the bench. You can still view its board and the hand you had right before retiring.

When you retire from a game, your status is declared retired, and your opponent is declared the winner.

## Acknowledgement

Expand All @@ -95,3 +101,4 @@ I made use of the [SoenEA](http://soenea.htmlweb.com/) (Software Engineering Ent
The framework was developed by Stuart Thiel, a professor at Concordia University. Per his thesis, it is used to "aid in the rapid development of dependable Web Enterprise Applications."

The project's file structure follows the recommended file structure from the thesis, which can be found in the Appendix, on page 73.

25 changes: 0 additions & 25 deletions WebContent/WEB-INF/jsp/play-to-bench-form.jsp

This file was deleted.

Binary file modified build/classes/app/Global.class
Binary file not shown.
Binary file modified build/classes/app/PlayPokemonToBench.class
Binary file not shown.
2 changes: 0 additions & 2 deletions src/app/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class Global {
public static final String VIEW_DECK = PATH_TO_JSP + "view-deck.jsp";

public static final String VIEW_BOARD = PATH_TO_JSP + "view-board.jsp";

public static final String VIEW_HAND = PATH_TO_JSP + "view-hand.jsp";
public static final String PLAY_TO_BENCH_FORM = PATH_TO_JSP + "play-to-bench-form.jsp";

}
132 changes: 36 additions & 96 deletions src/app/PlayPokemonToBench.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package app;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dom.model.card.CardHelper;
import dom.model.card.rdg.CardRDG;
import dom.model.cardinplay.CardStatus;
import dom.model.cardinplay.rdg.CardInPlayRDG;
import dom.model.deck.DeckHelper;
import dom.model.deck.rdg.DeckRDG;
import dom.model.game.GameStatus;
import dom.model.game.rdg.GameRDG;
import dom.model.user.UserHelper;
import dom.model.user.rdg.UserRDG;

@WebServlet("/PlayPokemonToBench")
public class PlayPokemonToBench extends PageController {
Expand All @@ -27,10 +21,10 @@ public class PlayPokemonToBench extends PageController {

private static final String CARD_ID_FORMAT = "You must provide a valid format for the card ID (positive integer).";
private static final String BENCH_IS_FULL = "Your bench is full. It already has 5 Pokemon.";
private static final String ALREADY_BENCHED = "That card is already on the bench.";
private static final String NOT_IN_HAND = "That card is not in your hand. You cannot bench it.";
private static final String NOT_A_POKEMON = "You can only bench cards of type 'p', i.e. Pokemon.";
private static final String GAME_STOPPED = "This game is over. You cannot continue playing.";
private static final String EMPTY_HAND = "You do not have any cards in your hand.";
private static final String NOT_LOGGED_IN = "You must be logged in to play.";

private static final String BENCH_SUCCESS = "You have sent %s to the bench!";
Expand All @@ -40,6 +34,10 @@ public PlayPokemonToBench() {
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {

Expand All @@ -56,73 +54,19 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
return;
}

List<CardInPlayRDG> playerBenchRDG = CardInPlayRDG.findByGameAndPlayerAndStatus(
gameRDG.getId(), getUserId(request), CardStatus.benched.ordinal()
);

if (playerBenchRDG.size() >= 5) {
failure(request, response, BENCH_IS_FULL);
return;
}

List<CardInPlayRDG> playerHandRDG = CardInPlayRDG.findByGameAndPlayerAndStatus(
gameRDG.getId(), getUserId(request), CardStatus.hand.ordinal()
);

UserRDG userRDG = UserRDG.findById(getUserId(request));
UserHelper user = new UserHelper(
userRDG.getId(), userRDG.getVersion(), userRDG.getUsername(), ""
);

DeckRDG deckRDG = DeckRDG.findByPlayer(userRDG.getId());
DeckHelper deck = new DeckHelper(deckRDG.getId(), user);

List<CardHelper> cards = new ArrayList<CardHelper>();

for (CardInPlayRDG cardInHand : playerHandRDG) {

CardRDG cardRDG = CardRDG.findById(cardInHand.getCard());

/**
* You can only play Pokemon to the bench.
*/
if (cardRDG.getType().equals("p")) {
CardHelper card = new CardHelper(cardRDG.getId(), deck, cardRDG.getType(), cardRDG.getName());
cards.add(card);
}

}

request.setAttribute("benchSize", playerBenchRDG.size());
request.setAttribute("cards", cards);
request.getRequestDispatcher(Global.PLAY_TO_BENCH_FORM).forward(request, response);

}
catch (Exception e) {
e.printStackTrace();
}
finally {
closeDb();
}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {

if (!loggedIn(request)) {
failure(request, response, NOT_LOGGED_IN);
return;
}

GameRDG gameRDG = getGame(request, response);
if (gameRDG == null) return;

final Long cardId;
/**
* This is the card index.
* Example: let's say your hand is: [2, 5, 9, 12, 27]
* Going off of a zero-based index:
* If you choose card 0, you get 2.
* If you choose card 3, you get 12.
* If you choose card 10, you should get an error.
* etc.
*/
final int cardIndex;

try {
cardId = Long.parseLong(request.getParameter("card"));
cardIndex = Integer.parseInt(request.getParameter("card"));
}
catch (NumberFormatException e) {
failure(request, response, CARD_ID_FORMAT);
Expand All @@ -138,38 +82,34 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
return;
}

boolean alreadyBenched =
playerBenchRDG.stream().filter(cardInHand -> cardInHand.getCard() == cardId).findFirst().isPresent();
List<CardInPlayRDG> playerHandRDG = CardInPlayRDG.findByGameAndPlayerAndStatus(
gameRDG.getId(), getUserId(request), CardStatus.hand.ordinal()
);

if (alreadyBenched) {
failure(request, response, ALREADY_BENCHED);
if (playerHandRDG.size() == 0) {
failure(request, response, EMPTY_HAND);
return;
}

List<CardInPlayRDG> playerHandRDG = CardInPlayRDG.findByGameAndPlayerAndStatus(
gameRDG.getId(), getUserId(request), CardStatus.hand.ordinal()
);
CardInPlayRDG playerCardInHandRDG;
try {
playerCardInHandRDG = playerHandRDG.get(cardIndex);
}
catch (IndexOutOfBoundsException e) {
failure(request, response, NOT_IN_HAND);
return;
}

boolean inHand =
playerHandRDG.stream().filter(cardInHand -> cardInHand.getCard() == cardId).findFirst().isPresent();
CardInPlayRDG cardInHand = CardInPlayRDG.findByCard(playerCardInHandRDG.getCard());
CardRDG card = CardRDG.findById(cardInHand.getCard());

if (inHand) {

CardInPlayRDG cardInHand = CardInPlayRDG.findByCard(cardId);
CardRDG card = CardRDG.findById(cardInHand.getCard());

if (card.getType().equals("p")) {
cardInHand.setStatus(CardStatus.benched.ordinal());
cardInHand.update();
success(request, response, String.format(BENCH_SUCCESS, card.getName()));
}
else {
failure(request, response, NOT_A_POKEMON);
}

if (card.getType().equals("p")) {
cardInHand.setStatus(CardStatus.benched.ordinal());
cardInHand.update();
success(request, response, String.format(BENCH_SUCCESS, card.getName()));
}
else {
failure(request, response, NOT_IN_HAND);
failure(request, response, NOT_A_POKEMON);
}

}
Expand Down

0 comments on commit 2adf71e

Please sign in to comment.