-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkControllerFacade.java
139 lines (128 loc) · 4.74 KB
/
NetworkControllerFacade.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package frontend.util;
import frontend.controllers.AbstractController;
import frontend.controllers.Game;
import frontend.controllers.Pregame;
import model.exceptions.*;
import model.player.Piece;
import model.player.PiecePosition;
import model.player.Player;
/***
* An additional layer between network and ui.
*/
public class NetworkControllerFacade {
private AbstractController controller;
/***
* Starts up this facade with a controller.
* @param controller Controller.
*/
public NetworkControllerFacade(AbstractController controller) {
this.controller = controller;
}
/***
* Loads the map.
* @param map An array quite similar to that used in {@link model.board.BasicBoard#loadBoard(String[][])}.
*/
public void loadMap(String[][] map) {
try {
AbstractController.getGame().getBoardMovementInterface().getBoard().loadBoard(map);
} catch (CorruptedFileException e) {
controller.showAlert(e.getMessage());
e.printStackTrace();
}
}
/***
* Make a move on the board.
* @param oldPos Starting position of a piece, [x, y].
* @param newPos Ending position of a piece, [x, y].
*/
public void makeMove(int[] oldPos, int[] newPos) {
if (controller.getClass() == Game.class) {
Piece piece;
try {
piece = AbstractController.getGame().getPlayerById(AbstractController.getGame().getTurn()).getArmy().getPieceByPosition(new PiecePosition(oldPos[0], oldPos[1]));
} catch (NoSuchPlayerException e) {
controller.showAlert(e.getMessage());
return;
} catch (NoSuchPieceException e) {
//controller.showAlert(e.getMessage());
return;
}
try {
AbstractController.getGame().getBoardMovementInterface().makeMove(piece, new PiecePosition(newPos[0], newPos[1]));
((Game) controller).renderFields();
} catch (MoveNotAllowedException e) {
controller.showAlert(e.getMessage());
}
}
}
/***
* Switches a turn.
* @param id Id of a player that turn we switch to.
*/
public void nextTurn(int id) {
AbstractController.getGame().setTurn(id);
if (controller.getClass() == Game.class) {
((Game) controller).renderFields();
}
}
/***
* Pre-game method. Adds a player to the game.
* @param nick The nickname of a player.
* @param id Player's id.
*/
public void addPlayer(String nick, int id) {
System.out.println("Adding a new player: " + nick + " id: " + id);
if (controller.getClass() == Pregame.class) {
System.out.println("Us: " + AbstractController.getThisPlayer().getName());
if (nick.equals(AbstractController.getThisPlayer().getName())) {
AbstractController.getThisPlayer().setId(id);
System.out.println("Id set to: " + AbstractController.getThisPlayer().getId());
AbstractController.getGame().createArmy(AbstractController.getThisPlayer());
try {
AbstractController.getGame().addPlayerWithId(AbstractController.getThisPlayer());
} catch (CannotAddPlayerException e) {
controller.showAlert(e.getMessage());
e.printStackTrace();
}
((Pregame) controller).addPlayer(nick, id);
return;
}
Player player = new Player(nick);
player.setId(id);
AbstractController.getGame().createArmy(player);
try {
AbstractController.getGame().addPlayerWithId(player);
} catch (CannotAddPlayerException e) {
controller.showAlert(e.getMessage());
e.printStackTrace();
}
((Pregame) controller).addPlayer(nick, id);
} else {
System.out.println("Wrong controller state");
}
}
public void ready(int id) {
if (controller.getClass() == Pregame.class) {
((Pregame) controller).readyPlayer(id);
} else {
System.out.println("Wrong controller state");
}
}
/***
* Method used to switch between pre-game and game.
*/
public void startGame() { //pass new game controller here
if (controller.getClass() == Pregame.class) {
((Pregame) controller).startGame();
} else {
System.out.println("Wrong controller state");
}
}
/***
* Shows an alert.
* @param message Message show be the alert.
*/
public void alert(String message) {
controller.showAlert(message);
}
}