Skip to content

Commit

Permalink
stop transmitting move color
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Aug 10, 2024
1 parent a3c99c2 commit ec6b7b1
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
https://www.jessym.com/articles/bundling-react-vite-with-spring-boot
This is a go / baduk server.

### Hacking

First, start the backend server.

```
make run
```

http://localhost:8080/app/
Now, in a new terminal window or tab, start vite:

```
make dev
```

The app is running at [http://localhost:3006](http://localhost:3006/app).

Try editing a file in `src/main/client/src`.
The change should immediately be visible in the browser.
22 changes: 17 additions & 5 deletions src/main/client/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "zustand/middleware"
import {
BLACK,
WHITE,
COLORS,
} from "./util.js"
import {
rehydrate,
Expand Down Expand Up @@ -98,7 +98,7 @@ export const useGameStore = create((set, get) => ({
if (!moves.length) {
return BLACK
}
return moves[moves.length - 1].color ^ (BLACK | WHITE)
return moves[moves.length - 1].color ^ COLORS
},
counting: false,
gameState: {
Expand Down Expand Up @@ -127,7 +127,7 @@ export const useGameStore = create((set, get) => ({
if (!counting) {
state.queueLength = get().queueLength + 1
}
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting)
let [storedMove, updated, forbidden] = createMoveData(baseBoard, moves, move, counting, get().handicap)
state.moves.push(storedMove)
state.lastMove = move.pass ? undefined : move
state.baseBoard = updated
Expand Down Expand Up @@ -167,7 +167,7 @@ export const useGameStore = create((set, get) => ({
} else {
passes = 0
}
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move, counting)
let [storedMove, updated, newForbidden] = createMoveData(baseBoard, moves, move, counting, game.handicap)
moves.push(storedMove)
forbidden = newForbidden
baseBoard = updated
Expand All @@ -189,7 +189,8 @@ export const useGameStore = create((set, get) => ({
},
}))

function createMoveData(baseBoard, moves, move, counting) {
function createMoveData(baseBoard, moves, colorlessMove, counting, handicap) {
let move = {...colorlessMove, color: nextMoveColor(moves, handicap)}
if (move.pass && moves.length && moves[moves.length - 1].pass) {
return [move, count(baseBoard), [-1, -1]]
}
Expand All @@ -204,3 +205,14 @@ function createMoveData(baseBoard, moves, move, counting) {
let forbidden = getForbidden(baseBoard, updated, storedMove)
return [storedMove, updated, forbidden]
}

function nextMoveColor(moves, handicap) {
let remainingHandicap = Math.max(0, handicap - moves.length)
if (remainingHandicap) {
return BLACK
}
if (!moves.length) {
return BLACK
}
return moves[moves.length - 1].color ^ COLORS
}
2 changes: 1 addition & 1 deletion src/main/client/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const TOGGLE_STUFF = TOGGLE_B | TOGGLE_W
export const TERRITORY = TERRITORY_W | TERRITORY_B
export const ANY_REMOVED = REMOVED_W | REMOVED_B

export const COLORS = BLACK | WHITE;
export const COLORS = BLACK | WHITE

export async function tfetch(url, options) {
let response
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.bernd.model.OpenGame;
import com.bernd.model.ViewGame;
import com.bernd.util.RandomString;
import java.security.Principal;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.core.MessageSendingOperations;
Expand All @@ -22,8 +23,6 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ResponseStatusException;

import java.security.Principal;

import static com.bernd.util.Auth.getPrincipal;
import static com.bernd.util.Util.COLORS;

Expand Down Expand Up @@ -82,7 +81,7 @@ public void action(Move move, Principal p) {
Game updated = game.update(updatedMove);
games.put(updated);
GameMove lastMove = game.getLastMove();
operations.convertAndSend("/topic/move/" + game.id(), lastMove);
operations.convertAndSend("/topic/move/" + game.id(), lastMove.removeColor());
}

private int getColorFromGameState(Game game) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/bernd/game/MoveList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.bernd.model.GameMove;
import com.bernd.model.Move;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public final class MoveList {

Expand Down Expand Up @@ -42,6 +42,10 @@ public List<GameMove> asList() {
return List.copyOf(moves);
}

public Stream<GameMove> asStream() {
return moves.stream();
}

@Override
public String toString() {
return moves.toString();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/bernd/model/ColorlessMove.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bernd.model;

public record ColorlessMove(
int n,
boolean pass,
int x,
int y,
boolean resetCounting,
boolean agreeCounting,
boolean end) {
}
5 changes: 5 additions & 0 deletions src/main/java/com/bernd/model/GameMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public record GameMove(
boolean resetCounting,
boolean agreeCounting,
boolean end) {

public ColorlessMove removeColor() {
return new ColorlessMove(
n, pass, x, y, resetCounting, agreeCounting, end);
}
}
4 changes: 0 additions & 4 deletions src/main/java/com/bernd/model/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public GameMove toGameMove() {
return new GameMove(n, color, pass, x, y, resetCounting, agreeCounting, false);
}

public GameMove gameEnd() {
return new GameMove(n, color, pass, x, y, resetCounting, agreeCounting, true);
}

public Move withColor(int color) {
return new Move(color, n, pass, resetCounting, agreeCounting, x, y);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/bernd/model/ViewGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record ViewGame(
int dim,
int handicap,
int remainingHandicap,
List<GameMove> moves
List<ColorlessMove> moves
) {

static ViewGame fromGame(Game game) {
Expand All @@ -20,6 +20,6 @@ static ViewGame fromGame(Game game) {
game.dim(),
game.handicap(),
game.remainingHandicap(),
game.moves().asList());
game.moves().asStream().map(GameMove::removeColor).toList());
}
}

0 comments on commit ec6b7b1

Please sign in to comment.