@@ -14,10 +14,11 @@ import it.polimi.ingsw.gc14.Model.Orders.Order4;
|
|||||||
import it.polimi.ingsw.gc14.Model.Orders.Order5;
|
import it.polimi.ingsw.gc14.Model.Orders.Order5;
|
||||||
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Game {
|
public class Game implements Serializable {
|
||||||
|
|
||||||
private ArrayList<Player> playersList;
|
private ArrayList<Player> playersList;
|
||||||
private CurrentState currentState;
|
private CurrentState currentState;
|
||||||
|
|||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
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 DrawLowerBuildingCard extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public DrawLowerBuildingCard(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.DRAW_LOWER_BUILD;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.drawLowerBuildingCard(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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 DrawLowerTribeCard extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public DrawLowerTribeCard(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.DRAW_LOWER_TRIBE;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.drawLowerTribeCard(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
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 DrawUpperBuildingCard extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public DrawUpperBuildingCard(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.DRAW_UPPER_BUILD;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.drawUpperBuildingCard(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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 DrawUpperTribeCard extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public DrawUpperTribeCard(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.DRAW_UPPER_TRIBE;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.drawUpperTribeCard(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
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 PickOptionalBuildingCard extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public PickOptionalBuildingCard(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.PICK_OPTIONAL_BUILD;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.pickOptionalBuildingCard(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
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 PickOptionalTribeCard extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public PickOptionalTribeCard(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.PICK_OPTIONAL_TRIBE;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.pickOptionalTribeCard(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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 SlotChoice extends NetworkEvent implements Serializable{
|
||||||
|
private String username;
|
||||||
|
private EventType eventType;
|
||||||
|
private int pos;
|
||||||
|
|
||||||
|
public SlotChoice(String username, int pos){
|
||||||
|
this.username = username;
|
||||||
|
this.eventType = EventType.SLOT_CHOICE;
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean apply(GameController gameController){
|
||||||
|
return gameController.slotChoice(username, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String apply(IView gameController){
|
||||||
|
return gameController.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.EventType;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@@ -8,42 +10,67 @@ import java.util.List;
|
|||||||
|
|
||||||
public class ClientHandler implements Runnable {
|
public class ClientHandler implements Runnable {
|
||||||
private Socket clientSocket;
|
private Socket clientSocket;
|
||||||
BufferedReader in = null;
|
private TCPServer server;
|
||||||
PrintWriter out = null;
|
public ObjectInputStream in = null;
|
||||||
|
public ObjectOutputStream out = null;
|
||||||
List<ClientHandler> clientHandlers;
|
List<ClientHandler> clientHandlers;
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
|
private EventType eventType;
|
||||||
|
|
||||||
|
public Socket getClientSocket() {
|
||||||
|
return clientSocket;
|
||||||
|
}
|
||||||
|
|
||||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, GameController gameController) {
|
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, GameController gameController) {
|
||||||
this.clientSocket = clientSocket;
|
this.clientSocket = clientSocket;
|
||||||
this.clientHandlers = clientHandlers;
|
this.clientHandlers = clientHandlers;
|
||||||
this.gameController = gameController;
|
this.gameController = gameController;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run(){
|
||||||
clientLoop();
|
clientLoop();
|
||||||
}
|
}
|
||||||
private void clientLoop() {
|
|
||||||
try {
|
private void clientLoop(){
|
||||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
try{
|
||||||
synchronized (out) {
|
NetworkEvent input = null;
|
||||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
synchronized(in){
|
||||||
|
in = new ObjectInputStream(clientSocket.getInputStream());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
while(true){
|
||||||
|
try{
|
||||||
|
input = (NetworkEvent)(in.readObject());
|
||||||
|
if(input.apply(gameController)){
|
||||||
|
server.broadcastUpdate(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(java.io.IOException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
String s = "";
|
catch (ClassNotFoundException e){
|
||||||
try{
|
throw new RuntimeException(e);
|
||||||
while ((s = in.readLine()) != null) {
|
}
|
||||||
System.out.println(s);
|
|
||||||
out.println(s.toUpperCase());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void notifyEvent(Object event) {
|
|
||||||
synchronized (out) {
|
public void notifyEvent(NetworkEvent event){
|
||||||
out.println(event.toString());
|
synchronized(out){
|
||||||
|
try{
|
||||||
|
out = new ObjectOutputStream(clientSocket.getOutputStream());
|
||||||
|
out.writeObject(gameController.getModel());
|
||||||
|
}
|
||||||
|
catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,27 @@
|
|||||||
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
package it.polimi.ingsw.gc14.Network.TCP.Server;
|
||||||
|
|
||||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||||
|
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TCPServer {
|
public class TCPServer {
|
||||||
int port = -1;
|
int port = -1;
|
||||||
|
int ConnectedPlayers = 0;
|
||||||
ServerSocket serverTCP = null;
|
ServerSocket serverTCP = null;
|
||||||
GameController gameController;
|
GameController gameController;
|
||||||
|
|
||||||
private List<ClientHandler> clientHandlers;
|
private List<ClientHandler> clientHandlers;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private int getConnectedPlayers(){
|
||||||
|
return ConnectedPlayers;
|
||||||
|
}
|
||||||
|
|
||||||
public void start(String args[]){
|
public void start(String args[]){
|
||||||
clientHandlers = new ArrayList<>();
|
clientHandlers = new ArrayList<>();
|
||||||
|
|
||||||
@@ -25,27 +33,55 @@ public class TCPServer {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Listening on port " + port);
|
System.out.println("Listening on port: " + port);
|
||||||
|
|
||||||
while(true){
|
while(true){
|
||||||
Socket clientSocket = null;
|
Socket clientSocket = null;
|
||||||
|
|
||||||
try {
|
try{
|
||||||
clientSocket = serverTCP.accept();
|
clientSocket = serverTCP.accept();
|
||||||
} catch (IOException e) {
|
if(!gameController.addPlayer(clientSocket.getInputStream().toString()) || ConnectedPlayers > gameController.getModel().getNPlayers()){
|
||||||
|
clientSocket.close();
|
||||||
|
System.out.println("Invalid parameters. Connection terminated.\n");
|
||||||
|
}
|
||||||
|
// gestione di ADD_PLAYER
|
||||||
|
}
|
||||||
|
catch (IOException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Accepted");
|
System.out.println("Accepted player: " + gameController.getModel().getPlayerByUsername(clientSocket.getInetAddress().toString()));
|
||||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, this.gameController);
|
|
||||||
|
ConnectedPlayers++;
|
||||||
|
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController);
|
||||||
clientHandlers.add(clientHandler);
|
clientHandlers.add(clientHandler);
|
||||||
|
|
||||||
|
//Sending model to clients
|
||||||
|
if(ConnectedPlayers == gameController.getModel().getNPlayers()){
|
||||||
|
for (ClientHandler handler : clientHandlers) {
|
||||||
|
try {
|
||||||
|
synchronized(handler.out){
|
||||||
|
ObjectOutputStream socketTx = new ObjectOutputStream(handler.getClientSocket().getOutputStream());
|
||||||
|
socketTx.writeObject(gameController.getModel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(IOException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Thread t = new Thread(clientHandler);
|
Thread t = new Thread(clientHandler);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TCPServer(GameController gameController, int port) {
|
private TCPServer(GameController gameController, int port){
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.gameController = gameController;
|
this.gameController = gameController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void broadcastUpdate(NetworkEvent event){
|
||||||
|
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user