@@ -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.GamePackage.GameStages;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Game {
|
||||
public class Game implements Serializable {
|
||||
|
||||
private ArrayList<Player> playersList;
|
||||
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;
|
||||
|
||||
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.net.*;
|
||||
@@ -8,42 +10,67 @@ import java.util.List;
|
||||
|
||||
public class ClientHandler implements Runnable {
|
||||
private Socket clientSocket;
|
||||
BufferedReader in = null;
|
||||
PrintWriter out = null;
|
||||
private TCPServer server;
|
||||
public ObjectInputStream in = null;
|
||||
public ObjectOutputStream out = null;
|
||||
List<ClientHandler> clientHandlers;
|
||||
GameController gameController;
|
||||
private EventType eventType;
|
||||
|
||||
public Socket getClientSocket() {
|
||||
return clientSocket;
|
||||
}
|
||||
|
||||
public ClientHandler(Socket clientSocket, List<ClientHandler> clientHandlers, GameController gameController) {
|
||||
this.clientSocket = clientSocket;
|
||||
this.clientHandlers = clientHandlers;
|
||||
this.gameController = gameController;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
clientLoop();
|
||||
}
|
||||
|
||||
private void clientLoop(){
|
||||
try{
|
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
synchronized (out) {
|
||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||
NetworkEvent input = null;
|
||||
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();
|
||||
}
|
||||
String s = "";
|
||||
try{
|
||||
while ((s = in.readLine()) != null) {
|
||||
System.out.println(s);
|
||||
out.println(s.toUpperCase());
|
||||
catch (ClassNotFoundException e){
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
private void notifyEvent(Object event) {
|
||||
|
||||
public void notifyEvent(NetworkEvent event){
|
||||
synchronized(out){
|
||||
out.println(event.toString());
|
||||
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;
|
||||
|
||||
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.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TCPServer {
|
||||
int port = -1;
|
||||
int ConnectedPlayers = 0;
|
||||
ServerSocket serverTCP = null;
|
||||
GameController gameController;
|
||||
|
||||
private List<ClientHandler> clientHandlers;
|
||||
|
||||
|
||||
|
||||
private int getConnectedPlayers(){
|
||||
return ConnectedPlayers;
|
||||
}
|
||||
|
||||
public void start(String args[]){
|
||||
clientHandlers = new ArrayList<>();
|
||||
|
||||
@@ -25,20 +33,44 @@ public class TCPServer {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
System.out.println("Listening on port " + port);
|
||||
System.out.println("Listening on port: " + port);
|
||||
|
||||
while(true){
|
||||
Socket clientSocket = null;
|
||||
|
||||
try{
|
||||
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();
|
||||
}
|
||||
|
||||
System.out.println("Accepted");
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, this.gameController);
|
||||
System.out.println("Accepted player: " + gameController.getModel().getPlayerByUsername(clientSocket.getInetAddress().toString()));
|
||||
|
||||
ConnectedPlayers++;
|
||||
ClientHandler clientHandler = new ClientHandler(clientSocket, clientHandlers, gameController);
|
||||
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);
|
||||
t.start();
|
||||
}
|
||||
@@ -48,4 +80,8 @@ public class TCPServer {
|
||||
this.port = port;
|
||||
this.gameController = gameController;
|
||||
}
|
||||
|
||||
public void broadcastUpdate(NetworkEvent event){
|
||||
clientHandlers.forEach((x) -> x.notifyEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user