@@ -38,6 +38,16 @@ private void saveState(
38
38
actionsO .setValue (newActions );
39
39
}
40
40
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
+
41
51
private TileDecks getShuffledTileDecks (Long seed ) {
42
52
List <Tile > tiles = new ArrayList <>(Tiles .TILES );
43
53
if (seed != null ) {
@@ -68,16 +78,13 @@ public void start(Stage primaryStage) {
68
78
String gameName = "androzGame" ;
69
79
String mySuperName = "Androz" + new Random ().nextInt (1000 );
70
80
WSClient wsClient = new WSClient (
71
- gameName ,
72
- mySuperName
81
+ gameName ,
82
+ mySuperName
73
83
);
74
84
75
-
76
85
Parameters parameters = getParameters ();
77
- Map <String , String > namedParameters = parameters .getNamed ();
78
86
79
- Long seed = namedParameters .containsKey ("seed" ) ? Long .parseUnsignedLong (namedParameters .get ("seed" )) : null ;
80
- TileDecks tileDecks = getShuffledTileDecks (seed );
87
+ TileDecks tileDecks = getShuffledTileDecks ((long ) gameName .hashCode ());
81
88
82
89
List <String > players = parameters .getUnnamed ();
83
90
int playersSize = players .size ();
@@ -91,6 +98,7 @@ public void start(Stage primaryStage) {
91
98
92
99
GameState gameState = GameState .initial (playerColorsO .getValue (), tileDecks , textMaker );
93
100
SimpleObjectProperty <GameState > gameStateO = new SimpleObjectProperty <>(gameState );
101
+ SimpleObjectProperty <List <String >> actionsO = new SimpleObjectProperty <>(List .of ());
94
102
95
103
wsClient .setOnGamePlayerJoin (newPlayerNames -> {
96
104
playerNamesO .setValue (getPlayersMap (newPlayerNames ));
@@ -100,6 +108,15 @@ public void start(Stage primaryStage) {
100
108
playerNamesO .setValue (getPlayersMap (newPlayerNames ));
101
109
gameStateO .setValue (gameStateO .getValue ().withPlayers (playerColorsO .getValue ()));
102
110
});
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
+ });
103
120
104
121
ObservableValue <List <MessageBoard .Message >> observableMessagesO = gameStateO .map (
105
122
gState -> gState .messageBoard ().messages ()
@@ -118,48 +135,47 @@ public void start(Stage primaryStage) {
118
135
}
119
136
);
120
137
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
+ );
122
152
123
153
Consumer <Occupant > onOccupantClick = occupant -> {
154
+ if (!isOwnerCurrentPlayerO .getValue ()) return ;
124
155
GameState currentGameState = gameStateO .getValue ();
125
156
Board board = currentGameState .board ();
126
157
int tileId = Zone .tileId (occupant .zoneId ());
127
158
switch (currentGameState .nextAction ()) {
128
159
case OCCUPY_TILE -> {
129
160
assert board .lastPlacedTile () != null ;
130
161
if (tileId != board .lastPlacedTile ().id ()) return ;
131
- saveState (ActionEncoder .withNewOccupant (currentGameState , occupant ), gameStateO , actionsO );
162
+ saveStateAndDispatch (ActionEncoder .withNewOccupant (currentGameState , occupant ), gameStateO , actionsO , wsClient );
132
163
}
133
164
case RETAKE_PAWN -> {
134
165
if (
135
166
(occupant .kind () != Occupant .Kind .PAWN )
136
167
|| (currentGameState .currentPlayer () != board .tileWithId (tileId ).placer ())
137
168
) return ;
138
- saveState (ActionEncoder .withOccupantRemoved (currentGameState , occupant ), gameStateO , actionsO );
169
+ saveStateAndDispatch (ActionEncoder .withOccupantRemoved (currentGameState , occupant ), gameStateO , actionsO , wsClient );
139
170
}
140
171
}
141
172
};
142
173
143
174
Consumer <String > onEnteredAction = action -> {
144
175
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 );
146
177
};
147
178
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
-
163
179
Node playersNode = PlayersUI .create (gameStateO , new TextMakerFr (playerNamesO ));
164
180
Node messagesNode = MessageBoardUI .create (observableMessagesO , highlightedTilesO );
165
181
Node decksNode = DecksUI .create (tileToPlaceO , leftNormalTilesO , leftMenhirTilesO , textToDisplayO , onOccupantClick );
@@ -171,6 +187,7 @@ public void start(Stage primaryStage) {
171
187
};
172
188
173
189
Consumer <Pos > onPosClick = pos -> {
190
+ if (!isOwnerCurrentPlayerO .getValue ()) return ;
174
191
GameState currentGameState = gameStateO .getValue ();
175
192
if (currentGameState .nextAction () != GameState .Action .PLACE_TILE ) return ;
176
193
Tile tileToPlace = currentGameState .tileToPlace ();
@@ -179,7 +196,7 @@ public void start(Stage primaryStage) {
179
196
nextRotationO .getValue (), pos
180
197
);
181
198
if (!currentGameState .board ().canAddTile (placedTile )) return ;
182
- saveState (ActionEncoder .withPlacedTile (currentGameState , placedTile ), gameStateO , actionsO );
199
+ saveStateAndDispatch (ActionEncoder .withPlacedTile (currentGameState , placedTile ), gameStateO , actionsO , wsClient );
183
200
nextRotationO .setValue (Rotation .NONE );
184
201
};
185
202
0 commit comments