Merge branch 'main' into TCP
This commit is contained in:
@@ -8,6 +8,14 @@ public class GameController {
|
|||||||
public GameController(Game model) {
|
public GameController(Game model) {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
}
|
}
|
||||||
|
public GameController() {
|
||||||
|
}
|
||||||
|
public Game getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
public void setModel(Game model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
public boolean addPlayer(String username) {
|
public boolean addPlayer(String username) {
|
||||||
return model.addPlayer(new Player(username));
|
return model.addPlayer(new Player(username));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,9 @@ public class Game {
|
|||||||
public int getNPlayers() {
|
public int getNPlayers() {
|
||||||
return nPlayers;
|
return nPlayers;
|
||||||
}
|
}
|
||||||
public Game(int nPlayers) {
|
public Game(int nPlayers) throws IllegalArgumentException{
|
||||||
|
if(nPlayers < 0||nPlayers > 5)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
this.nPlayers = nPlayers;
|
this.nPlayers = nPlayers;
|
||||||
board=new Board(nPlayers);
|
board=new Board(nPlayers);
|
||||||
slotMap = new LinkedHashMap<>();
|
slotMap = new LinkedHashMap<>();
|
||||||
@@ -72,8 +74,16 @@ public class Game {
|
|||||||
playersList = new ArrayList<>();
|
playersList = new ArrayList<>();
|
||||||
OptionalCardQueue = new LinkedList<>();
|
OptionalCardQueue = new LinkedList<>();
|
||||||
}
|
}
|
||||||
|
public Game()
|
||||||
|
{
|
||||||
|
this(0);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean addPlayer(Player player) {
|
public boolean addPlayer(Player player) {
|
||||||
|
if(this.nPlayers==0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if(currentState.getGameStage()!= GameStages.WAITING)
|
if(currentState.getGameStage()!= GameStages.WAITING)
|
||||||
return false;
|
return false;
|
||||||
if(playersList.contains(player))
|
if(playersList.contains(player))
|
||||||
@@ -415,7 +425,13 @@ public class Game {
|
|||||||
currentState.GameStageUpdate(GameStages.ENDED);
|
currentState.GameStageUpdate(GameStages.ENDED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean setNPlayer(int nPlayers)
|
||||||
|
{
|
||||||
|
if(this.nPlayers!=0)
|
||||||
|
return false;
|
||||||
|
this.nPlayers=nPlayers;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.Client.TCP;
|
package it.polimi.ingsw.gc14.Network.TCP.Client;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.View.IView;
|
import it.polimi.ingsw.gc14.View.IView;
|
||||||
|
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package it.polimi.ingsw.gc14.Network.TCP;
|
||||||
|
|
||||||
|
public enum EventType {
|
||||||
|
ADD_PLAYER,
|
||||||
|
SLOT_CHOICE,
|
||||||
|
DRAW_UPPER_TRIBE,
|
||||||
|
DRAW_LOWER_TRIBE,
|
||||||
|
DRAW_UPPER_BUILD,
|
||||||
|
DRAW_LOWER_BUILD,
|
||||||
|
PICK_OPTIONAL_TRIBE,
|
||||||
|
PICK_OPTIONAL_BUILD
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package it.polimi.ingsw.gc14.Network.TCP;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public abstract class NetworkEvent implements Serializable {
|
||||||
|
|
||||||
|
public abstract boolean apply(GameController gameController);
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package it.polimi.ingsw.gc14.Network.TCP.NetworkEvents;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.EventType;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||||
|
import it.polimi.ingsw.gc14.View.IView;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class AddPlayer extends NetworkEvent implements Serializable {
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
public AddPlayer(String username) {
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.ADD_PLAYER;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController)
|
||||||
|
{
|
||||||
|
return gameController.addPlayer(username);
|
||||||
|
}
|
||||||
|
public String apply(IView gameController)
|
||||||
|
{
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.Server.TCP;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.Server.TCP;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
|
||||||
@@ -0,0 +1,228 @@
|
|||||||
|
package it.polimi.ingsw.gc14.Controller;
|
||||||
|
|
||||||
|
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
|
||||||
|
import it.polimi.ingsw.gc14.Model.Game;
|
||||||
|
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||||
|
import it.polimi.ingsw.gc14.Model.Player;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class GameControllerTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addPlayer() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giorgio"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
assertNotNull(game.getPlayerByUsername("Giorgio"));
|
||||||
|
assertNotNull(game.getPlayerByUsername("Marco"));
|
||||||
|
assertNotNull(game.getPlayerByUsername("Luca"));
|
||||||
|
|
||||||
|
assertEquals(GameStages.SLOT_CHOICE, game.getCurrentState().getGameStage());
|
||||||
|
assertFalse(controller.addPlayer("Extra"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void allMethodsShouldReturnFalseForUnknownUsername() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertFalse(controller.slotChoice("ghost", 0));
|
||||||
|
assertFalse(controller.drawUpperTribeCard("ghost", 0));
|
||||||
|
assertFalse(controller.drawLowerTribeCard("ghost", 0));
|
||||||
|
assertFalse(controller.drawUpperBuildingCard("ghost", 0));
|
||||||
|
assertFalse(controller.drawLowerBuildingCard("ghost", 0));
|
||||||
|
assertFalse(controller.pickOptionalTribeCard("ghost", 0));
|
||||||
|
assertFalse(controller.pickOptionalBuildingCard("ghost", 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void slotChoice() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giorgio"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
String cur = game.getCurrentState().getCurrentPlayer().getUserName();
|
||||||
|
String other = cur.equals("Giorgio") ? "Marco" : "Giorgio";
|
||||||
|
assertFalse(controller.slotChoice(other, 0));
|
||||||
|
|
||||||
|
Queue<Player> order = new LinkedList<>();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Player p = game.getCurrentState().getCurrentPlayer();
|
||||||
|
order.add(p);
|
||||||
|
assertTrue(controller.slotChoice(p.getUserName(), i));
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
|
||||||
|
assertEquals(order.poll(), game.getCurrentState().getCurrentPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void drawLowerTribeCard() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giorgio"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
Queue<Player> order = new LinkedList<>();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Player p = game.getCurrentState().getCurrentPlayer();
|
||||||
|
order.add(p);
|
||||||
|
assertTrue(controller.slotChoice(p.getUserName(), i));
|
||||||
|
}
|
||||||
|
|
||||||
|
Player first = order.poll();
|
||||||
|
assertEquals(first, game.getCurrentState().getCurrentPlayer());
|
||||||
|
|
||||||
|
List<TribeCard> cards = game.getLowerListTribeCards();
|
||||||
|
int idx = cards.indexOf(
|
||||||
|
cards.stream()
|
||||||
|
.filter(c -> !c.IsEventCard())
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow()
|
||||||
|
);
|
||||||
|
|
||||||
|
Player wrongPlayer = order.peek();
|
||||||
|
assertNotNull(wrongPlayer);
|
||||||
|
|
||||||
|
int before = first.getTotCharacters();
|
||||||
|
assertTrue(controller.drawLowerTribeCard(first.getUserName(), idx));
|
||||||
|
assertEquals(before + 1, first.getTotCharacters());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void drawUpperTribeCard() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giorgio"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Player p = game.getCurrentState().getCurrentPlayer();
|
||||||
|
assertTrue(controller.slotChoice(p.getUserName(), i));
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
|
||||||
|
|
||||||
|
while (game.getCurrentState().getNUpper() == 0) {
|
||||||
|
assertTrue(game.getCurrentState().getNLower() > 0);
|
||||||
|
|
||||||
|
Player current = game.getCurrentState().getCurrentPlayer();
|
||||||
|
List<TribeCard> lower = game.getLowerListTribeCards();
|
||||||
|
int lowerIdx = lower.indexOf(
|
||||||
|
lower.stream()
|
||||||
|
.filter(c -> !c.IsEventCard())
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow()
|
||||||
|
);
|
||||||
|
|
||||||
|
assertTrue(controller.drawLowerTribeCard(current.getUserName(), lowerIdx));
|
||||||
|
}
|
||||||
|
|
||||||
|
Player current = game.getCurrentState().getCurrentPlayer();
|
||||||
|
int beforeTot = current.getTotCharacters();
|
||||||
|
|
||||||
|
List<TribeCard> upper = game.getUpperListTribeCards();
|
||||||
|
int upperIdx = upper.indexOf(
|
||||||
|
upper.stream()
|
||||||
|
.filter(c -> !c.IsEventCard())
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow()
|
||||||
|
);
|
||||||
|
|
||||||
|
assertTrue(controller.drawUpperTribeCard(current.getUserName(), upperIdx));
|
||||||
|
assertEquals(beforeTot + 1, current.getTotCharacters());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void drawUpperBuildingCard() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giacomo"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Player p = game.getCurrentState().getCurrentPlayer();
|
||||||
|
assertTrue(controller.slotChoice(p.getUserName(), i));
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
|
||||||
|
|
||||||
|
while (game.getCurrentState().getNUpper() == 0) {
|
||||||
|
assertTrue(game.getCurrentState().getNLower() > 0);
|
||||||
|
|
||||||
|
Player current = game.getCurrentState().getCurrentPlayer();
|
||||||
|
List<TribeCard> lower = game.getLowerListTribeCards();
|
||||||
|
int lowerIdx = lower.indexOf(
|
||||||
|
lower.stream()
|
||||||
|
.filter(c -> !c.IsEventCard())
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow()
|
||||||
|
);
|
||||||
|
|
||||||
|
assertTrue(controller.drawLowerTribeCard(current.getUserName(), lowerIdx));
|
||||||
|
}
|
||||||
|
|
||||||
|
Player current = game.getCurrentState().getCurrentPlayer();
|
||||||
|
|
||||||
|
assertFalse(controller.drawUpperBuildingCard(current.getUserName(), 0));
|
||||||
|
|
||||||
|
current.addFood(100);
|
||||||
|
assertTrue(controller.drawUpperBuildingCard(current.getUserName(), 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void drawLowerBuildingCard() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giacomo"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Player p = game.getCurrentState().getCurrentPlayer();
|
||||||
|
assertTrue(controller.slotChoice(p.getUserName(), i));
|
||||||
|
}
|
||||||
|
|
||||||
|
String cur = game.getCurrentState().getCurrentPlayer().getUserName();
|
||||||
|
assertFalse(controller.drawLowerBuildingCard(cur, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pickOptionalCards() {
|
||||||
|
Game game = new Game(3);
|
||||||
|
GameController controller = new GameController(game);
|
||||||
|
|
||||||
|
assertTrue(controller.addPlayer("Giacomo"));
|
||||||
|
assertTrue(controller.addPlayer("Marco"));
|
||||||
|
assertTrue(controller.addPlayer("Luca"));
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
Player p = game.getCurrentState().getCurrentPlayer();
|
||||||
|
assertTrue(controller.slotChoice(p.getUserName(), i));
|
||||||
|
}
|
||||||
|
|
||||||
|
String cur = game.getCurrentState().getCurrentPlayer().getUserName();
|
||||||
|
assertFalse(controller.pickOptionalTribeCard(cur, 0));
|
||||||
|
assertFalse(controller.pickOptionalBuildingCard(cur, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,14 @@ class GameTest {
|
|||||||
Game game=new Game(nPlayers);
|
Game game=new Game(nPlayers);
|
||||||
assertEquals(nPlayers,game.getNPlayers());
|
assertEquals(nPlayers,game.getNPlayers());
|
||||||
assertEquals(GameStages.WAITING,game.getCurrentState().getGameStage());
|
assertEquals(GameStages.WAITING,game.getCurrentState().getGameStage());
|
||||||
|
|
||||||
|
assertThrows(IllegalArgumentException.class,()->new Game(6));
|
||||||
|
game=new Game();
|
||||||
|
assertFalse(game.addPlayer(new Player("p1")));
|
||||||
|
game.setNPlayer(nPlayers);
|
||||||
|
assertTrue(game.addPlayer(new Player("p1")));
|
||||||
|
assertEquals(nPlayers,game.getNPlayers());
|
||||||
|
assertEquals(GameStages.WAITING,game.getCurrentState().getGameStage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user