Skip to content

Commit 9b87f71

Browse files
author
Christopher Strauss
committed
Ergebnis message
1 parent bf7b93b commit 9b87f71

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

src/main/client/public/stone1.wav

-42.4 KB
Binary file not shown.

src/main/client/src/component/Chat.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const Chat = ({chatId, className}) => {
108108
</>}
109109
bottomElement={<>
110110
{messages.map(message => (
111-
<p key={message.n}>{message.user + ": " + message.message}</p>
111+
<p key={message.n}>{message.user === null ? <text className="italic text-gray-400">{message.message}</text> : message.user + ": " + message.message}</p>
112112
))}
113113
</>}
114114
/>

src/main/client/src/feature/game/GamePanel.jsx

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
useCallback,
33
useContext,
4+
useEffect,
45
} from "react"
56
import {
67
useParams,
@@ -18,10 +19,6 @@ import {
1819
} from "react-icons"
1920
import {
2021
StompContext,
21-
BLACK,
22-
WHITE,
23-
TERRITORY_B,
24-
TERRITORY_W,
2522
base,
2623
} from "src/util.js"
2724
import {
@@ -48,7 +45,6 @@ import {
4845
moveForward,
4946
countingAgreed,
5047
gameHasEnded,
51-
isReviewing,
5248
} from "./state.js"
5349

5450
export const GamePanel = ({gameState, setGameState}) => {
@@ -70,6 +66,20 @@ function Panel({gameState, setGameState}) {
7066
let {black, white, viewPos, counting, board} = gameState
7167
let movesLength = gameState.moves.length
7268
let navigate = useNavigate()
69+
70+
useEffect(() => {
71+
if (gameHasEnded(gameState)) {
72+
stompClient.publish({
73+
destination: "/app/game/move",
74+
body:
75+
JSON.stringify({
76+
n: movesLength,
77+
action: "agreeCounting",
78+
}),
79+
})
80+
}
81+
}, [stompClient, gameState, movesLength])
82+
7383
let onExit = useCallback(() => {
7484
navigate(base + "/lobby")
7585
}, [navigate])
@@ -103,7 +113,6 @@ function Panel({gameState, setGameState}) {
103113
if (!board.length) {
104114
return <span>Loading...</span>
105115
}
106-
let result = gameHasEnded(gameState) ? getScore(board) : undefined
107116
return (
108117
<>
109118
<div className="flex-none flex w-full gap-x-2">
@@ -192,28 +201,7 @@ function Panel({gameState, setGameState}) {
192201
</Button>
193202
</div>
194203
</>}
195-
{result && !isReviewing(gameState) && (
196-
<div className="flex-none">
197-
{(result.w > result.b ? "W+" : "B+") + Math.abs(result.b - result.w)}
198-
</div>
199-
)}
200204
<Chat className="mt-1" chatId={gameId}/>
201205
</>
202206
)
203207
}
204-
205-
function getScore(board) {
206-
let w = 0, b = 0
207-
for (let y = 0; y < board.length; y++) {
208-
for (let x = 0; x < board[y].length; x++) {
209-
let { color } = board[y][x]
210-
if ((color & WHITE) || (color & TERRITORY_W)) {
211-
w++
212-
}
213-
if ((color & BLACK) || (color & TERRITORY_B)) {
214-
b++
215-
}
216-
}
217-
}
218-
return {w, b}
219-
}

src/main/client/src/feature/game/state.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ export function gameHasEnded({moves}) {
104104
return move.action === "end"
105105
}
106106

107-
export function isReviewing(baseState) {
108-
return baseState.viewPos < baseState.queueLength
109-
}
110-
111107
export function moveBack(baseState) {
112108
return produce(baseState, (draft) => {
113109
let moves = baseState.moves

src/main/java/com/bernd/GameController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
import com.bernd.game.MoveList;
55
import com.bernd.model.AcceptRequest;
66
import com.bernd.model.ActiveGame;
7+
import com.bernd.model.Chat;
8+
import com.bernd.model.ChatMessage;
9+
import com.bernd.model.ChatRequest;
710
import com.bernd.model.Game;
811
import com.bernd.model.Move;
912
import com.bernd.model.OpenGame;
1013
import com.bernd.model.ViewGame;
14+
import com.bernd.util.Auth;
1115
import com.bernd.util.RandomString;
1216
import org.springframework.http.HttpStatus;
1317
import org.springframework.http.ResponseEntity;
@@ -82,6 +86,12 @@ public void action(Move move, Principal p) {
8286
Game updated = game.update(updatedMove);
8387
games.put(updated);
8488
Move lastMove = game.getLastMove();
89+
if (lastMove.end()) {
90+
Chat chat = chats.get(game.id());
91+
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), game.getScore(), null);
92+
chat.messages().add(message);
93+
operations.convertAndSend("/topic/chat/" + chat.id(), message);
94+
}
8595
operations.convertAndSend("/topic/move/" + game.id(), lastMove.removeColor());
8696
}
8797

src/main/java/com/bernd/model/Game.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public record Game(
2626

2727
private static final Logger logger = LogManager.getLogger(Game.class);
2828
public static final int[] NOT_FORBIDDEN = {-1, -1};
29+
public static final int BLACK = 32;
30+
public static final int WHITE = 64;
31+
public static final int TERRITORY_B = 2;
32+
public static final int TERRITORY_W = 4;
2933

3034
public Game update(Move move) {
3135
try {
@@ -190,4 +194,21 @@ public int remainingHandicap() {
190194
public boolean isForbidden(Move move) {
191195
return move.x() == forbidden[0] && move.y() == forbidden[1];
192196
}
197+
198+
public String getScore() {
199+
int w = 0;
200+
int b = 0;
201+
for (int y = 0; y < board().length; y++){
202+
for (int x = 0; x < board[y].length; x++){
203+
int color = board[y][x];
204+
if ((color & (WHITE | TERRITORY_W)) != 0) {
205+
w++;
206+
}
207+
if ((color & (BLACK | TERRITORY_B)) != 0) {
208+
b++;
209+
}
210+
}
211+
}
212+
return w > b ? "W+" + w : "B+" + b;
213+
}
193214
}

0 commit comments

Comments
 (0)