Skip to content

Commit

Permalink
server-side color
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 6, 2024
1 parent 7234baf commit 4972489
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
3 changes: 0 additions & 3 deletions src/main/client/src/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import {
StompContext,
BLACK,
WHITE,
TERRITORY,
TERRITORY_B,
REMOVED_B,
Expand Down Expand Up @@ -44,7 +43,6 @@ export const Game = () => {
let {gameId} = useParams()
let stompClient = useContext(StompContext)
let auth = useAuthStore(state => state.auth)
let black = useGameStore(state => state.black)
let setGameState = useGameStore(state => state.setGameState)
let queueStatus = useGameStore(state => state.queueStatus)
let queueLength = useGameStore(state => state.queueLength)
Expand Down Expand Up @@ -169,7 +167,6 @@ export const Game = () => {
body: JSON.stringify({
id: gameId,
n: queueLength(),
color: black.name === auth.name ? BLACK : WHITE,
x: cursor_x,
y: cursor_y,
}),
Expand Down
9 changes: 2 additions & 7 deletions src/main/client/src/feature/GamePanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ function Panel({zoom, setZoom}) {
let queueLength = useGameStore(state => state.queueLength)
let counting = useGameStore(state => state.counting)
let countingComplete = useGameStore(state => state.countingComplete)
let currentColor = useGameStore(state => state.currentColor)
let currentPlayer = useGameStore(state => state.currentPlayer)
let { board, gameHasEnded } = useGameStore(state => state.gameState)
let navigate = useNavigate()
let myColor = black.name === auth.name ? BLACK : WHITE
let onExit = useCallback(() => {
navigate(base + "/lobby")
}, [navigate])
Expand All @@ -64,7 +62,6 @@ function Panel({zoom, setZoom}) {
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
color: myColor,
n: queueLength(),
pass: true,
}),
Expand All @@ -75,23 +72,21 @@ function Panel({zoom, setZoom}) {
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
color: myColor,
n: queueLength(),
resetCounting: true,
}),
})
}, [stompClient, gameId, queueLength, currentColor])
}, [stompClient, gameId, queueLength])
let onCountingAgree = useCallback(() => {
stompClient.publish({
destination: "/app/game/move",
body: JSON.stringify({
id: gameId,
color: myColor,
n: queueLength(),
agreeCounting: true,
}),
})
}, [stompClient, gameId, queueLength, currentColor])
}, [stompClient, gameId, queueLength])
if (!board.length) {
return <span>Loading...</span>
}
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.bernd;

import com.bernd.game.Board;
import com.bernd.game.MoveList;
import com.bernd.model.AcceptRequest;
import com.bernd.model.ActiveGame;
import com.bernd.model.Game;
import com.bernd.model.GameMove;
import com.bernd.model.Move;
import com.bernd.model.OpenGame;
import com.bernd.model.ViewGame;
Expand Down Expand Up @@ -51,25 +53,41 @@ public ViewGame getGame(@PathVariable String id) {
}

@MessageMapping("/game/move")
public void action(Move move, Principal principal) {
public void action(Move move, Principal p) {
Game game = games.get(move.id());
if (game == null) {
return;
}
int moveNumber = game.moves().size();
String currentPlayer = move.color() == Board.B ? game.black().name() : game.white().name();
if (!principal.getName().equals(currentPlayer)) {
return; // discard
}
if (move.n() != moveNumber) {
String principal = p.getName();
MoveList moves = game.moves();
int moveNumber = moves.size();
int color = principal.equals(game.black().name()) ? Board.B :
principal.equals(game.white().name()) ? Board.W : 0;
if (color == 0) {
return;
}
Game updated = game.update(move);
if (!game.counting()) {
if (moves.isEmpty()) {
if (color == Board.W) {
return;
}
} else {
GameMove lastMove = moves.get(moves.size() - 1);
if (lastMove.color() == color) {
return;
}
}
if (move.n() != moveNumber) {
return;
}
}
Move updatedMove = move.withColor(color);
Game updated = game.update(updatedMove);
games.put(updated);
if (updated.gameHasEnded()) {
operations.convertAndSend("/topic/move/" + game.id(), move.gameEnd(updated.counting()));
operations.convertAndSend("/topic/move/" + game.id(), updatedMove.gameEnd(updated.counting()));
} else if (!move.agreeCounting()) {
operations.convertAndSend("/topic/move/" + game.id(), move.toView(updated.counting()));
operations.convertAndSend("/topic/move/" + game.id(), updatedMove.toView(updated.counting()));
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/bernd/game/MoveList.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public GameMove get(int i) {
}
}

public boolean isEmpty() {
return pos == 0;
}

private void set(int ptId) {
int i = pos / 2;
if (pos % 2 == 0) {
Expand Down
18 changes: 2 additions & 16 deletions src/main/java/com/bernd/model/GameBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public final class GameBuilder {
private int countingAgreed;
private boolean opponentPassed;
private int[][] board;
private int dim;
private int handicap;
private int[] forbidden;

private GameBuilder(Game game) {
Expand All @@ -35,16 +33,6 @@ public GameBuilder withBoard(int[][] board) {
return this;
}

public GameBuilder withDim(int dim) {
this.dim = dim;
return this;
}

public GameBuilder withHandicap(int handicap) {
this.handicap = handicap;
return this;
}

public GameBuilder withForbidden(int[] forbidden) {
this.forbidden = forbidden;
return this;
Expand All @@ -60,8 +48,6 @@ static GameBuilder builder(Game game) {
builder.countingAgreed = game.countingAgreed();
builder.opponentPassed = game.opponentPassed();
builder.board = game.board();
builder.dim = game.dim();
builder.handicap = game.handicap();
builder.forbidden = game.forbidden();
return builder;
}
Expand All @@ -75,8 +61,8 @@ public Game build() {
countingAgreed,
opponentPassed,
board,
dim,
handicap,
game.dim(),
game.handicap(),
forbidden,
game.moves()
);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/bernd/model/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public GameMove toView(boolean counting) {
public GameMove gameEnd(boolean counting) {
return new GameMove(n, color, pass, x, y, counting, resetCounting, true);
}

public Move withColor(int color) {
return new Move(id, color, n, pass, resetCounting, agreeCounting, x, y);
}
}

0 comments on commit 4972489

Please sign in to comment.