Skip to content

Ergebnis message #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src/main/client/public/stone1.wav
Binary file not shown.
9 changes: 8 additions & 1 deletion src/main/client/src/component/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ export const Chat = ({chatId, className}) => {
</>}
bottomElement={<>
{messages.map(message => (
<p key={message.n}>{message.user + ": " + message.message}</p>
<p key={message.n}>
{
message.user ? (
<span>{message.user + ": " + message.message}</span>
) : (
<span className="italic text-gray-400">{message.message}</span>
)}
</p>
))}
</>}
/>
Expand Down
29 changes: 2 additions & 27 deletions src/main/client/src/feature/game/GamePanel.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
useCallback,
useContext,
useEffect,
} from "react"
import {
useParams,
Expand All @@ -18,10 +19,6 @@ import {
} from "react-icons"
import {
StompContext,
BLACK,
WHITE,
TERRITORY_B,
TERRITORY_W,
base,
} from "src/util.js"
import {
Expand All @@ -48,7 +45,6 @@ import {
moveForward,
countingAgreed,
gameHasEnded,
isReviewing,
} from "./state.js"

export const GamePanel = ({gameState, setGameState}) => {
Expand All @@ -70,6 +66,7 @@ function Panel({gameState, setGameState}) {
let {black, white, viewPos, counting, board} = gameState
let movesLength = gameState.moves.length
let navigate = useNavigate()

let onExit = useCallback(() => {
navigate(base + "/lobby")
}, [navigate])
Expand Down Expand Up @@ -103,7 +100,6 @@ function Panel({gameState, setGameState}) {
if (!board.length) {
return <span>Loading...</span>
}
let result = gameHasEnded(gameState) ? getScore(board) : undefined
return (
<>
<div className="flex-none flex w-full gap-x-2">
Expand Down Expand Up @@ -192,28 +188,7 @@ function Panel({gameState, setGameState}) {
</Button>
</div>
</>}
{result && !isReviewing(gameState) && (
<div className="flex-none">
{(result.w > result.b ? "W+" : "B+") + Math.abs(result.b - result.w)}
</div>
)}
<Chat className="mt-1" chatId={gameId}/>
</>
)
}

function getScore(board) {
let w = 0, b = 0
for (let y = 0; y < board.length; y++) {
for (let x = 0; x < board[y].length; x++) {
let { color } = board[y][x]
if ((color & WHITE) || (color & TERRITORY_W)) {
w++
}
if ((color & BLACK) || (color & TERRITORY_B)) {
b++
}
}
}
return {w, b}
}
10 changes: 10 additions & 0 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import com.bernd.game.MoveList;
import com.bernd.model.AcceptRequest;
import com.bernd.model.ActiveGame;
import com.bernd.model.Chat;
import com.bernd.model.ChatMessage;
import com.bernd.model.ChatRequest;
import com.bernd.model.Game;
import com.bernd.model.Move;
import com.bernd.model.OpenGame;
import com.bernd.model.ViewGame;
import com.bernd.util.Auth;
import com.bernd.util.RandomString;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -82,6 +86,12 @@ public void action(Move move, Principal p) {
Game updated = game.update(updatedMove);
games.put(updated);
Move lastMove = game.getLastMove();
if (lastMove.end()) {
Chat chat = chats.get(game.id());
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), game.getScore(), null);
chat.messages().add(message);
operations.convertAndSend("/topic/chat/" + chat.id(), message);
}
operations.convertAndSend("/topic/move/" + game.id(), lastMove.removeColor());
}

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/bernd/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bernd.game.Direction;
import com.bernd.game.MoveList;
import com.bernd.game.Toggle;
import com.bernd.game.Board;
import com.bernd.util.BoardUpdateImpl;
import com.bernd.util.Util;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -190,4 +191,21 @@ public int remainingHandicap() {
public boolean isForbidden(Move move) {
return move.x() == forbidden[0] && move.y() == forbidden[1];
}

public String getScore() {
int w = 0;
int b = 0;
for (int y = 0; y < board().length; y++){
for (int x = 0; x < board[y].length; x++){
int color = board[y][x];
if ((color & (Board.W | Board.TERRITORY_W)) != 0) {
w++;
}
if ((color & (Board.B | Board.TERRITORY_B)) != 0) {
b++;
}
}
}
return w > b ? "W+" + w : "B+" + b;
}
}
Loading