Skip to content

Commit fd80951

Browse files
committed
websocket fix
1 parent a0fb10f commit fd80951

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

src/ch/epfl/chacun/gui/Main.java

+43-26
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ private void saveState(
3838
actionsO.setValue(newActions);
3939
}
4040

41+
private void saveStateAndDispatch(
42+
ActionEncoder.StateAction stateAction,
43+
SimpleObjectProperty<GameState> gameStateO,
44+
SimpleObjectProperty<List<String>> actionsO,
45+
WSClient wsClient
46+
) {
47+
saveState(stateAction, gameStateO, actionsO);
48+
wsClient.sendAction(stateAction.action());
49+
}
50+
4151
private TileDecks getShuffledTileDecks(Long seed) {
4252
List<Tile> tiles = new ArrayList<>(Tiles.TILES);
4353
if (seed != null) {
@@ -68,16 +78,13 @@ public void start(Stage primaryStage) {
6878
String gameName = "androzGame";
6979
String mySuperName = "Androz" + new Random().nextInt(1000);
7080
WSClient wsClient = new WSClient(
71-
gameName,
72-
mySuperName
81+
gameName,
82+
mySuperName
7383
);
7484

75-
7685
Parameters parameters = getParameters();
77-
Map<String, String> namedParameters = parameters.getNamed();
7886

79-
Long seed = namedParameters.containsKey("seed") ? Long.parseUnsignedLong(namedParameters.get("seed")) : null;
80-
TileDecks tileDecks = getShuffledTileDecks(seed);
87+
TileDecks tileDecks = getShuffledTileDecks((long) gameName.hashCode());
8188

8289
List<String> players = parameters.getUnnamed();
8390
int playersSize = players.size();
@@ -91,6 +98,7 @@ public void start(Stage primaryStage) {
9198

9299
GameState gameState = GameState.initial(playerColorsO.getValue(), tileDecks, textMaker);
93100
SimpleObjectProperty<GameState> gameStateO = new SimpleObjectProperty<>(gameState);
101+
SimpleObjectProperty<List<String>> actionsO = new SimpleObjectProperty<>(List.of());
94102

95103
wsClient.setOnGamePlayerJoin(newPlayerNames -> {
96104
playerNamesO.setValue(getPlayersMap(newPlayerNames));
@@ -100,6 +108,15 @@ public void start(Stage primaryStage) {
100108
playerNamesO.setValue(getPlayersMap(newPlayerNames));
101109
gameStateO.setValue(gameStateO.getValue().withPlayers(playerColorsO.getValue()));
102110
});
111+
wsClient.setOnPlayerAction(action -> {
112+
System.out.println("Received" + action);
113+
ActionEncoder.StateAction stateAction = ActionEncoder.decodeAndApply(gameStateO.getValue(), action);
114+
if (stateAction == null) {
115+
// todo server what have you done bro?
116+
return;
117+
}
118+
saveState(stateAction, gameStateO, actionsO);
119+
});
103120

104121
ObservableValue<List<MessageBoard.Message>> observableMessagesO = gameStateO.map(
105122
gState -> gState.messageBoard().messages()
@@ -118,48 +135,47 @@ public void start(Stage primaryStage) {
118135
}
119136
);
120137

121-
SimpleObjectProperty<List<String>> actionsO = new SimpleObjectProperty<>(List.of());
138+
ObservableValue<String> ownerPlayerColorO = playerNamesO.map(playerName -> {
139+
PlayerColor owner = null;
140+
for (Map.Entry<PlayerColor, String> entry : playerName.entrySet()) {
141+
if (entry.getValue().equals(mySuperName)) {
142+
owner = entry.getKey();
143+
break;
144+
}
145+
}
146+
return owner.toString();
147+
});
148+
149+
ObservableValue<Boolean> isOwnerCurrentPlayerO = gameStateO.map(
150+
gState -> gState.currentPlayer() == PlayerColor.valueOf(ownerPlayerColorO.getValue())
151+
);
122152

123153
Consumer<Occupant> onOccupantClick = occupant -> {
154+
if (!isOwnerCurrentPlayerO.getValue()) return;
124155
GameState currentGameState = gameStateO.getValue();
125156
Board board = currentGameState.board();
126157
int tileId = Zone.tileId(occupant.zoneId());
127158
switch (currentGameState.nextAction()) {
128159
case OCCUPY_TILE -> {
129160
assert board.lastPlacedTile() != null;
130161
if (tileId != board.lastPlacedTile().id()) return;
131-
saveState(ActionEncoder.withNewOccupant(currentGameState, occupant), gameStateO, actionsO);
162+
saveStateAndDispatch(ActionEncoder.withNewOccupant(currentGameState, occupant), gameStateO, actionsO, wsClient);
132163
}
133164
case RETAKE_PAWN -> {
134165
if (
135166
(occupant.kind() != Occupant.Kind.PAWN)
136167
|| (currentGameState.currentPlayer() != board.tileWithId(tileId).placer())
137168
) return;
138-
saveState(ActionEncoder.withOccupantRemoved(currentGameState, occupant), gameStateO, actionsO);
169+
saveStateAndDispatch(ActionEncoder.withOccupantRemoved(currentGameState, occupant), gameStateO, actionsO, wsClient);
139170
}
140171
}
141172
};
142173

143174
Consumer<String> onEnteredAction = action -> {
144175
ActionEncoder.StateAction newSt = ActionEncoder.decodeAndApply(gameStateO.getValue(), action);
145-
if (newSt != null) saveState(newSt, gameStateO, actionsO);
176+
if (newSt != null) saveStateAndDispatch(newSt, gameStateO, actionsO, wsClient);
146177
};
147178

148-
ObservableValue<String> ownerPlayerColorO = playerNamesO.map(playerName -> {
149-
PlayerColor owner = null;
150-
for (Map.Entry<PlayerColor, String> entry : playerName.entrySet()) {
151-
if (entry.getValue().equals(mySuperName)) {
152-
owner = entry.getKey();
153-
break;
154-
}
155-
}
156-
return owner.toString();
157-
});
158-
159-
ObservableValue<Boolean> isOwnerCurrentPlayerO = gameStateO.map(
160-
gState -> gState.currentPlayer() == PlayerColor.valueOf(ownerPlayerColorO.getValue())
161-
);
162-
163179
Node playersNode = PlayersUI.create(gameStateO, new TextMakerFr(playerNamesO));
164180
Node messagesNode = MessageBoardUI.create(observableMessagesO, highlightedTilesO);
165181
Node decksNode = DecksUI.create(tileToPlaceO, leftNormalTilesO, leftMenhirTilesO, textToDisplayO, onOccupantClick);
@@ -171,6 +187,7 @@ public void start(Stage primaryStage) {
171187
};
172188

173189
Consumer<Pos> onPosClick = pos -> {
190+
if (!isOwnerCurrentPlayerO.getValue()) return;
174191
GameState currentGameState = gameStateO.getValue();
175192
if (currentGameState.nextAction() != GameState.Action.PLACE_TILE) return;
176193
Tile tileToPlace = currentGameState.tileToPlace();
@@ -179,7 +196,7 @@ public void start(Stage primaryStage) {
179196
nextRotationO.getValue(), pos
180197
);
181198
if (!currentGameState.board().canAddTile(placedTile)) return;
182-
saveState(ActionEncoder.withPlacedTile(currentGameState, placedTile), gameStateO, actionsO);
199+
saveStateAndDispatch(ActionEncoder.withPlacedTile(currentGameState, placedTile), gameStateO, actionsO, wsClient);
183200
nextRotationO.setValue(Rotation.NONE);
184201
};
185202

src/ch/epfl/chacun/gui/PlayersUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.List;
1616
import java.util.Map;
1717
import java.util.function.Function;
18+
import java.util.stream.Collectors;
1819

1920
/**
2021
* This class represents the graphical representation
@@ -59,6 +60,8 @@ public static Node create(
5960
ObservableValue<List<PlayerColor>> players = gameStateO.map(GameState::players);
6061

6162
Function<List<PlayerColor>, Boolean> addPlayersNodes = (newPlayers) -> {
63+
System.out.println("rerender players");
64+
System.out.println(newPlayers.stream().map(e -> textMaker.playerName(e)).collect(Collectors.joining()));
6265
newPlayers
6366
.stream()
6467
.skip(vBox.getChildren().size())

src/ch/epfl/chacun/net/WSClient.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ public final class WSClient implements WebSocket.Listener {
1919

2020
private Consumer<String> onGameJoinAccept;
2121
private Consumer<String> onGamePlayerJoin;
22+
private Consumer<String> onPlayerAction;
2223

2324
public WSClient(String gameName, String username) {
2425
this.gameName = gameName;
2526
this.username = username;
2627

2728
this.onGameJoinAccept = (data) -> {};
2829
this.onGamePlayerJoin = (data) -> {};
30+
this.onPlayerAction = (data) -> {};
2931
}
3032

3133
private static String connectURI(String gameName, String username) {
@@ -38,6 +40,7 @@ private void acknowledgePing() {
3840
}
3941

4042
private void handleMessage(String message) {
43+
System.out.println(message);
4144
String[] messageParts = message.split("\\.");
4245
String action = messageParts[0];
4346
String data = messageParts[1];
@@ -48,6 +51,8 @@ private void handleMessage(String message) {
4851
case "GAMEJOIN_NEWCOMER":
4952
onGamePlayerJoin.accept(data);
5053
break;
54+
case "GAMEACTION":
55+
onPlayerAction.accept(data);
5156
case "PING":
5257
acknowledgePing();
5358
break;
@@ -62,6 +67,10 @@ public void setOnGamePlayerJoin(Consumer<String> onGamePlayerJoin) {
6267
this.onGamePlayerJoin = onGamePlayerJoin;
6368
}
6469

70+
public void setOnPlayerAction(Consumer<String> onPlayerAction) {
71+
this.onPlayerAction = onPlayerAction;
72+
}
73+
6574
public void connect() {
6675
Preconditions.checkArgument(!connected);
6776
ws = HttpClient
@@ -90,7 +99,7 @@ public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String re
9099
}
91100

92101
public void sendAction(String message) {
93-
ws.sendText(message, true);
102+
ws.sendText(STR."GAMEACTION.\{message}", true);
94103
}
95104

96105
public void dispatchGameStarted() {

0 commit comments

Comments
 (0)