Add: RMI
This commit is contained in:
@@ -5,9 +5,10 @@ import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType;
|
||||
import it.polimi.ingsw.gc14.Model.PlayableCard;
|
||||
import it.polimi.ingsw.gc14.Model.Player;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BuildingCard extends PlayableCard implements Cloneable , BuildingEffect {
|
||||
public class BuildingCard extends PlayableCard implements Cloneable , BuildingEffect, Serializable {
|
||||
private int price;
|
||||
public int getPrice() {
|
||||
return price;
|
||||
@@ -26,7 +27,7 @@ public class BuildingCard extends PlayableCard implements Cloneable , BuildingEf
|
||||
return effectType;
|
||||
}
|
||||
|
||||
public BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{
|
||||
protected BuildingCard(int era,int price,int prestigeValue) throws IllegalArgumentException{
|
||||
super(era);
|
||||
if (price > 0) {
|
||||
this.price = price;
|
||||
|
||||
@@ -3,7 +3,9 @@ package it.polimi.ingsw.gc14.Model.Cards;
|
||||
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Character;
|
||||
import it.polimi.ingsw.gc14.Model.PlayableCard;
|
||||
|
||||
public abstract class TribeCard extends PlayableCard {
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class TribeCard extends PlayableCard implements Serializable {
|
||||
private boolean isEventCard;
|
||||
public boolean IsEventCard() {
|
||||
return isEventCard;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -8,13 +8,14 @@ import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.Sustenance;
|
||||
import it.polimi.ingsw.gc14.Model.DecksCreator;
|
||||
import it.polimi.ingsw.gc14.Model.Slot;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Board manages all the elements during the game such as decks, upper and lower rows, totems, tiles...
|
||||
*/
|
||||
public class Board {
|
||||
public class Board implements Serializable {
|
||||
/**
|
||||
* slotList contains the ordered list of slots (tiles). The slots changes based on the number of players.
|
||||
* Each slot (tile) has special action as drawing from the upper/lower row or taking food.
|
||||
|
||||
@@ -4,7 +4,9 @@ import it.polimi.ingsw.gc14.Model.Player;
|
||||
import it.polimi.ingsw.gc14.Model.Slot;
|
||||
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
|
||||
|
||||
public class CurrentState {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class CurrentState implements Serializable {
|
||||
// region Getters
|
||||
private Player player;
|
||||
public Player getCurrentPlayer(){
|
||||
|
||||
@@ -2,9 +2,10 @@ package it.polimi.ingsw.gc14.Model;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class OrderLogicCard {
|
||||
public abstract class OrderLogicCard implements Serializable {
|
||||
private Queue<Player> players;
|
||||
public OrderLogicCard(ArrayList<Player> players) {
|
||||
Collections.shuffle(players);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package it.polimi.ingsw.gc14.Model;
|
||||
|
||||
public abstract class PlayableCard {
|
||||
import java.io.Serializable;
|
||||
|
||||
public abstract class PlayableCard implements Serializable {
|
||||
private int Era;
|
||||
public int getEra(){
|
||||
return Era;
|
||||
|
||||
@@ -4,13 +4,14 @@ import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
|
||||
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
|
||||
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Default Player class; contains all identifiers and methods needed.
|
||||
*/
|
||||
public class Player {
|
||||
public class Player implements Serializable {
|
||||
/**
|
||||
* The maximum length allowed for the username string.
|
||||
*/
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package it.polimi.ingsw.gc14.Model;
|
||||
|
||||
public class Slot {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Slot implements Serializable {
|
||||
// Getters
|
||||
private char slotId;
|
||||
public char getSlotId() {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
|
||||
import it.polimi.ingsw.gc14.Model.Game;
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
public interface IClientCallback extends Remote {
|
||||
void onGameInit(Game model) throws RemoteException;
|
||||
void onAction(NetworkEvent action) throws RemoteException;
|
||||
void onError(String message) throws RemoteException;
|
||||
void onGameOver(String winner) throws RemoteException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Client;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
public class RMIClient {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
|
||||
public interface IGameServer extends Remote {
|
||||
// Connessione
|
||||
String joinGame(String username, IClientCallback callback) throws RemoteException;
|
||||
|
||||
// Azioni di gioco — specchio dei tuoi metodi GameController
|
||||
boolean drawUpperTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean drawLowerTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean drawUpperBuildingCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean drawLowerBuildingCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean pickOptionalTribeCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException;
|
||||
boolean slotChoice(String playerUsername, int pos) throws RemoteException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
|
||||
public interface IRMIServer {
|
||||
void connect(VirtualView client) throws RemoteException;
|
||||
void add(Integer number) throws RemoteException;
|
||||
void reset() throws RemoteException;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
import it.polimi.ingsw.gc14.Network.RMI.Client.IClientCallback;
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
import java.rmi.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.*;
|
||||
|
||||
public class RMIGameController implements IGameServer {
|
||||
|
||||
private final GameController controller; // delega tutto qui
|
||||
private final Map<String, IClientCallback> clients = new ConcurrentHashMap<>();
|
||||
|
||||
public RMIGameController(GameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String joinGame(String username, IClientCallback callback) throws RemoteException {
|
||||
controller.addPlayer(username);
|
||||
clients.put(username, callback);
|
||||
callback.onGameInit(controller.getModel());
|
||||
return username;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean drawUpperTribeCard(String username, int pos) throws RemoteException {
|
||||
boolean result = controller.drawUpperTribeCard(username, pos);
|
||||
if (!result) {
|
||||
notifyError(username, "Mossa non valida");
|
||||
} else {
|
||||
notifyAll(new NetworkEvent(Action.ActionType.DRAW_UPPER_TRIBE, username, pos));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void notifyAll(Action action) throws RemoteException {
|
||||
for (IClientCallback cb : clients.values()) {
|
||||
cb.onAction(action);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyError(String username, String message) throws RemoteException {
|
||||
IClientCallback cb = clients.get(username);
|
||||
if (cb != null) cb.onError(message);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package it.polimi.ingsw.gc14.Network.RMI.Server;
|
||||
|
||||
import it.polimi.ingsw.gc14.Controller.GameController;
|
||||
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
public class RMIServer {
|
||||
|
||||
private final RMIGameController controller;
|
||||
private Registry registry;
|
||||
|
||||
public RMIServer(RMIGameController controller) {
|
||||
this.controller = controller;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
IGameServer stub = (IGameServer) UnicastRemoteObject.exportObject(controller, 0);
|
||||
registry = LocateRegistry.createRegistry(1099);
|
||||
registry.rebind("GameServer", stub);
|
||||
System.out.println("RMI Server avviato sulla porta 1099");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
registry.unbind("GameServer");
|
||||
UnicastRemoteObject.unexportObject(controller, true);
|
||||
System.out.println("RMI Server fermato");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,21 @@
|
||||
package it.polimi.ingsw.gc14.View;
|
||||
|
||||
import it.polimi.ingsw.gc14.Network.TCP.NetworkEvent;
|
||||
|
||||
public interface IView {
|
||||
// --- Setup ---
|
||||
void showWelcome();
|
||||
String askPlayerName();
|
||||
|
||||
// --- Rendering ---
|
||||
void render(GameState state);
|
||||
void showMessage(String message);
|
||||
void showError(String message);
|
||||
void showWinner(String winner);
|
||||
|
||||
// --- Input giocatore ---
|
||||
NetworkEvent askMove(GameState state);
|
||||
|
||||
// --- Lifecycle ---
|
||||
void close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user