package it.polimi.ingsw.gc14.Model; import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; import it.polimi.ingsw.gc14.Model.Orders.OrderPlayer; import it.polimi.ingsw.gc14.View.TUI.AsciiTable; import it.polimi.ingsw.gc14.View.TUI.BorderStyle; import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; /** * Abstract base class for all order logic cards. * *
An order logic card manages the turn order of the players and defines
* the behavior used to update the order during the game.
*/
public abstract class OrderLogicCard implements Serializable {
/**
* The queue of {@link Player Players} associated with this order logic card.
*/
public Queue Any previous occurrence of the player is removed from both the queue
* and the order list before the player is added again.
*
* @param player the player to be pushed into the queue.
*/
public void pushNoEffect(Player player){
players.removeIf(x->player.getUserName().equals(x.getUserName()));
playerList.removeIf(x->player.getUserName().equals(x.player.getUserName()));
playerList.add(new OrderPlayer(player,false));
players.add(player);
}
/**
* Removes and returns the first player in the queue.
*
* The first order entry that has not yet been marked as played
* is marked as played before removing the player from the queue.
*
* @return the first player in the queue, or {@code null} if the queue is empty.
*/
public Player pull(){
for(OrderPlayer p:playerList){
if(p.played==false)
{
p.played=true;
break;
}
}
return players.poll();
}
/**
* Returns the first player in the queue without removing it.
*
* @return the first player in the queue, or {@code null} if the queue is empty.
*/
public Player getFirst()
{
return players.peek();
}
/**
* Applies the effect associated with the specified player and queue position.
*
* @param player the player to whom the effect is applied.
* @param index the queue position index associated with the effect.
* @throws IndexOutOfBoundsException if the specified index is not valid.
*/
protected abstract void effect(Player player, int index) throws IndexOutOfBoundsException;
/**
* Applies the building-related effect to the specified player.
* For each building card owned by the player with effect id equal to 3,
* the player gains 1 Food.
*
* @param player the player to whom the building effect is applied.
*/
protected void buildingEffect(Player player)
{
for(BuildingCard b : player.getBuildingCards().stream().filter(x->x.getEffectId()==3).toList())
player.addFood(1);
}
/**
* Returns the position of the player associated with the specified username
* within the current order list.
*
* @param username the username of the player whose position is requested.
* @return the player's position, or {@code -1} if no player with the specified
* username is present in the order list.
* @see Player
*/
public int getPosition(String username)
{
int pos = 0;
for (OrderPlayer p : playerList) {
if (p.player.getUserName().equals(username))
return pos;
pos++;
}
return -1;
}
}