Bind: TUI->Launcher

This commit is contained in:
rubenpirreram
2026-04-30 18:01:35 +02:00
parent 239b6687fb
commit 7e3623d3ae
5 changed files with 34 additions and 24 deletions
@@ -3,12 +3,15 @@ import it.polimi.ingsw.gc14.Controller.ClientController;
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient; import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient; import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.util.Scanner; import java.util.Scanner;
public class ClientLauncherTUI { public class ClientLauncherTUI {
private IView view;
public void main() throws InterruptedException { public void main() throws InterruptedException {
ClientController controller = new ClientController(); view=new TUI(null);
ClientController controller = new ClientController(view);
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
@@ -29,11 +32,12 @@ public class ClientLauncherTUI {
System.out.println("Succesfully connected to RMI server\n\n"); System.out.println("Succesfully connected to RMI server\n\n");
} else { } else {
System.out.println("RMI connection refused\n\n"); System.out.println("RMI connection refused\n\n");
return;
} }
while(true) { while(true) {
System.out.flush(); System.out.flush();
if (controller.localModel!=null) { if (controller.localController.getModel()!=null) {
break; break;
} }
Thread.sleep(500); Thread.sleep(500);
@@ -43,7 +47,7 @@ public class ClientLauncherTUI {
System.out.print("\033[H\033[2J"); System.out.print("\033[H\033[2J");
System.out.flush(); System.out.flush();
System.out.println(controller.localModel); System.out.println(controller.localController.getModel());
} else if (networkType == 1) { } else if (networkType == 1) {
@@ -56,7 +60,7 @@ public class ClientLauncherTUI {
while(true) { while(true) {
System.out.flush(); System.out.flush();
if (controller.localModel!=null) { if (controller.localController.getModel()!=null) {
break; break;
} }
Thread.sleep(500); Thread.sleep(500);
@@ -66,7 +70,7 @@ public class ClientLauncherTUI {
System.out.print("\033[H\033[2J"); System.out.print("\033[H\033[2J");
System.out.flush(); System.out.flush();
System.out.println(controller.localModel); System.out.println(controller.localController.getModel());
} }
} }
} }
@@ -6,28 +6,19 @@ import it.polimi.ingsw.gc14.View.IView;
public class ClientController { public class ClientController {
public Game localModel;
public GameController localController; public GameController localController;
public IView view=null; public IView view=null;
public ClientController(IView view,Game localModel) { public ClientController(IView view) {
this.view = view; this.view = view;
this.localModel = localModel;
this.localController = new GameController(localModel);
}
public ClientController() {
this.localController = new GameController(); this.localController = new GameController();
} }
public void setModel(Game model) { public void setModel(Game model) {
this.localModel = model;
localController.setModel(model); localController.setModel(model);
// localModel.addObserver((Observer) view); // registra la view come observer view.update(localController.getModel());
} }
public void onError(String message) { public void onError(String message) {
view.showError(message); view.showError(message);
} }
@@ -39,6 +39,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
@Override @Override
public void onGameInit(Game model) throws RemoteException { public void onGameInit(Game model) throws RemoteException {
clientController.setModel(model); clientController.setModel(model);
clientController.view.render();
} }
@@ -55,7 +56,7 @@ public class ClientCallbackImpl extends UnicastRemoteObject implements IClientCa
System.out.println(event.toString()); System.out.println(event.toString());
} else { } else {
event.apply(clientController.localController); event.apply(clientController.localController);
//clientController.view.update(); TODO clientController.view.render();
} }
} }
} }
@@ -4,8 +4,8 @@ import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
public interface IView { public interface IView {
public void update(Game game);
public void render(Game model); public void render();
public void showMessage(String message); public void showMessage(String message);
public void showError(String message); public void showError(String message);
@@ -1,18 +1,31 @@
package it.polimi.ingsw.gc14.View.TUI; package it.polimi.ingsw.gc14.View.TUI;
import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Network.Observer;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
public class TUI implements IView { public class TUI implements IView, Observer {
// ── dati di stato ─────────────────────────────────────────── // ── dati di stato ───────────────────────────────────────────
private BorderStyle style = BorderStyle.UNICODE; private BorderStyle style = BorderStyle.UNICODE;
private Game model; private Game model;
private String username;
public TUI(Game model) { public TUI(Game model) {
this.model = model; this.model = model;
this.username="";
} }
public void setUsername(String username) {
this.username = username;
}
@Override
public void update(Game model) {
this.model = model;
}
// ── punto di ingresso ─────────────────────────────────────── // ── punto di ingresso ───────────────────────────────────────
public void render(Game model) public void render()
{ {
this.model=model;
try{ try{
String os = System.getProperty("os.name").toLowerCase(); String os = System.getProperty("os.name").toLowerCase();
ProcessBuilder pb; ProcessBuilder pb;
@@ -28,7 +41,7 @@ public class TUI implements IView {
System.out.println(model.toString()); System.out.println(model.toString());
} }
public void renderBoard(Game model) public void renderBoard()
{ {
try{ try{
String os = System.getProperty("os.name").toLowerCase(); String os = System.getProperty("os.name").toLowerCase();
@@ -44,7 +57,7 @@ public class TUI implements IView {
} }
System.out.println(model.BoardStamp()); System.out.println(model.BoardStamp());
} }
public void renderPlayer(Game model) public void renderPlayer()
{ {
try{ try{
String os = System.getProperty("os.name").toLowerCase(); String os = System.getProperty("os.name").toLowerCase();
@@ -60,7 +73,7 @@ public class TUI implements IView {
} }
System.out.println(model.BoardStamp()); System.out.println(model.BoardStamp());
} }
public void renderMyHand(Game model,String username) public void renderMyHand()
{ {
try{ try{
String os = System.getProperty("os.name").toLowerCase(); String os = System.getProperty("os.name").toLowerCase();
@@ -77,6 +90,7 @@ public class TUI implements IView {
System.out.println(model.getPlayerByUsername(username)); System.out.println(model.getPlayerByUsername(username));
} }
public void showMessage(String message) public void showMessage(String message)
{ {
System.out.println(message); System.out.println(message);