259 lines
7.4 KiB
Java
259 lines
7.4 KiB
Java
package it.polimi.ingsw.gc14.Model;
|
|
|
|
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.EventCard;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.EventType;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.ShamanicRitual;
|
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance;
|
|
import it.polimi.ingsw.gc14.Model.GamePackage.Board;
|
|
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
|
|
import it.polimi.ingsw.gc14.Model.Orders.Order2;
|
|
import it.polimi.ingsw.gc14.Model.Orders.Order3;
|
|
import it.polimi.ingsw.gc14.Model.Orders.Order4;
|
|
import it.polimi.ingsw.gc14.Model.Orders.Order5;
|
|
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
|
import javafx.event.Event;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class Game {
|
|
|
|
private ArrayList<Player> playersList;
|
|
private CurrentState currentState;
|
|
private HashMap<Slot,Player> slotMap;
|
|
private int nPlayers;
|
|
private OrderLogicCard orderLogicCard;
|
|
private Board board;
|
|
public Game(int nPlayers) {
|
|
this.nPlayers = nPlayers;
|
|
board=new Board(nPlayers);
|
|
slotMap = new LinkedHashMap<>();
|
|
for(Slot s :board.getSlotList())
|
|
{
|
|
slotMap.put(s,null);
|
|
}
|
|
currentState= new CurrentState();
|
|
playersList = new ArrayList<>();
|
|
}
|
|
public void init() {
|
|
|
|
switch (nPlayers) {
|
|
case 2:
|
|
orderLogicCard=new Order2(playersList);
|
|
break;
|
|
case 3:
|
|
orderLogicCard=new Order3(playersList);
|
|
break;
|
|
case 4:
|
|
orderLogicCard=new Order4(playersList);
|
|
break;
|
|
case 5:
|
|
orderLogicCard=new Order5(playersList);
|
|
break;
|
|
}
|
|
currentState.PlayerUpdate(orderLogicCard.pull(),null);
|
|
currentState.GameStageUpdate(GameStages.SLOT_CHOICE);
|
|
}
|
|
|
|
public boolean SlotChoice(Player player, Slot slot) {
|
|
if(currentState.getGameStage()!= GameStages.SLOT_CHOICE)
|
|
{
|
|
return false;
|
|
}
|
|
if(player.equals(currentState.getCurrentPlayer()))
|
|
{
|
|
if(slotMap.get(slot)!=null)
|
|
{
|
|
return false;
|
|
}
|
|
if(!board.getSlotList().contains(slot))
|
|
{
|
|
return false;
|
|
}
|
|
slotMap.put(slot,player);
|
|
currentState.PlayerUpdate(orderLogicCard.pull(),null);
|
|
if(currentState.getCurrentPlayer()==null)
|
|
{
|
|
currentState.GameStageUpdate(GameStages.RESOLVING_ACTIONS);
|
|
for(Slot s : board.getSlotList())
|
|
{
|
|
if(slotMap.get(s)!=null)
|
|
{
|
|
currentState.PlayerUpdate(slotMap.get(s),s);
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//region Drawing Methods
|
|
public boolean DrawUpperTribeCard(Player player,TribeCard tribeCard) {
|
|
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
|
|
{
|
|
return false;
|
|
}
|
|
if(player.equals(currentState.getCurrentPlayer()))
|
|
{
|
|
|
|
}
|
|
return false;
|
|
|
|
}
|
|
public boolean DrawLowerTribeCard(Player player,TribeCard tribeCard) {
|
|
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
|
|
{
|
|
return false;
|
|
}
|
|
if(player.equals(currentState.getCurrentPlayer()))
|
|
{
|
|
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
public boolean DrawUpperBuildingCard(Player player,BuildingCard buildingCard) {
|
|
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
|
|
{
|
|
return false;
|
|
}
|
|
if(!player.equals(currentState.getCurrentPlayer()))
|
|
{
|
|
return false;
|
|
}
|
|
if(!board.containsUpperBuildingCard(buildingCard))
|
|
{
|
|
return false;
|
|
}
|
|
if(buildingCard.buy(player))
|
|
{
|
|
currentState.UpperDrawn();
|
|
board.removeUpperBuildingCard(buildingCard);
|
|
}
|
|
else
|
|
return false;
|
|
if(currentState.getNUpper()==currentState.getSlot().getNUpper()&& currentState.getNLower()==currentState.getSlot().getNLower())
|
|
{
|
|
|
|
}
|
|
return false;
|
|
}
|
|
public boolean DrawLowerBuildingCard(Player player,BuildingCard buildingCard) {
|
|
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
|
|
{
|
|
return false;
|
|
}
|
|
if(!player.equals(currentState.getCurrentPlayer()))
|
|
{
|
|
return false;
|
|
}
|
|
if(!board.containsLowerBuildingCard(buildingCard))
|
|
{
|
|
return false;
|
|
}
|
|
if(buildingCard.buy(player))
|
|
{
|
|
currentState.UpperDrawn();
|
|
board.removeLowerBuildingCard(buildingCard);
|
|
}
|
|
else
|
|
return false;
|
|
if(currentState.getNUpper()==currentState.getSlot().getNUpper()&& currentState.getNLower()==currentState.getSlot().getNLower())
|
|
{
|
|
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//endregion
|
|
|
|
private void EraUpdate(){}
|
|
private void nextPlayerSetup() {
|
|
if(GameStages.RESOLVING_ACTIONS==currentState.getGameStage()) {
|
|
orderLogicCard.push(currentState.getCurrentPlayer());
|
|
slotMap.put(currentState.getSlot(), null);
|
|
for (Slot s : board.getSlotList()) {
|
|
if (slotMap.get(s) != null) {
|
|
currentState.PlayerUpdate(slotMap.get(s), s);
|
|
}
|
|
}
|
|
if(slotMap.values().stream().allMatch(v -> v == null))
|
|
{
|
|
currentState.GameStageUpdate(GameStages.BUILDING_EFFECT_12);
|
|
BuildingEffect12( playersList.stream().filter(x->x.buildingCards.stream().anyMatch(y->y.getEffectId()==12)).toList());
|
|
//currentState.PlayerUpdate(orderLogicCard.pull(),null);
|
|
|
|
}
|
|
}
|
|
}
|
|
private void BuildingEffect12(List<Player> playerList){
|
|
}
|
|
|
|
private void EventResolution() {
|
|
|
|
if(currentState.getGameStage()!= GameStages.RESOLVING_EVENT)
|
|
{
|
|
return;
|
|
}
|
|
Queue<EventCard> events;
|
|
events=board.getPendingEvents();
|
|
ArrayList<EventCard>sustenance=events.stream().filter(x->x.getType().equals(EventType.SUSTENANCE)).collect(Collectors.toCollection(ArrayList::new));
|
|
events.removeAll(sustenance);
|
|
for(EventCard event:events)
|
|
{
|
|
event.activateEvent(playersList);
|
|
}
|
|
|
|
for(EventCard e : sustenance)
|
|
{
|
|
e.activateEvent(playersList);
|
|
}
|
|
|
|
|
|
}
|
|
private void nextRound() {
|
|
|
|
EventResolution();
|
|
if(currentState.getRound()==10)
|
|
{
|
|
currentState.GameStageUpdate(GameStages.ENDING);
|
|
endGame();
|
|
}
|
|
if(currentState.getEra()!= board.nextRound())
|
|
currentState.EraUpdate();
|
|
currentState.RoundUpdate();
|
|
|
|
}
|
|
private void endGame() {
|
|
//DO SOMETHING THEN
|
|
currentState.GameStageUpdate(GameStages.ENDED);
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean addPlayer(Player player) {
|
|
if(currentState.getGameStage()!= GameStages.WAITING)
|
|
return false;
|
|
playersList.add(player);
|
|
|
|
if(playersList.size()>=nPlayers)
|
|
{
|
|
init();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|