Skip to content

create sgf #25

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 30, 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
25 changes: 24 additions & 1 deletion src/main/client/src/feature/game/BoardSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import {
FaVolumeUp,
FaSearchPlus,
FaSearchMinus,
FaDownload,
} from "react-icons/fa"
import {
useMuteStore,
} from "src/store.js"
import {
useViewStateStore,
} from "src/layout.js"
import {
base,
} from "src/util.js"

export function BoardSettings({gameId, black, white}) {


export function BoardSettings() {
return (
<div className="absolute pl-2 pt-1 flex flex-col items-center">
<MuteIcon />
<Zoom />
<SaveGameFile gameId={gameId} black={black} white={white} />
</div>
)
}
Expand Down Expand Up @@ -66,3 +73,19 @@ function Zoom() {
</>
)
}

function SaveGameFile({gameId, black, white}) {
return (
<a
href={base + "/api/sgf/" + gameId + "/" + black + "_vs_" + white + ".sgf"}
target="_blank"
download={gameId + "_" + black + "_vs_" + white + ".sgf"}
className="mt-[0.25rem]">
<IconContext.Provider value={{
size: "1.5em",
}}>
<FaDownload />
</IconContext.Provider>
</a>
)
}
2 changes: 1 addition & 1 deletion src/main/client/src/feature/game/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function Game() {
<div
style={{ width: vw() - sidebarWidth }}
className="h-full">
<BoardSettings />
<BoardSettings gameId={gameState.id} black={gameState.black} white={gameState.white} />
<Board gameState={gameState} setGameState={setGameState} />
<GamePanel gameState={gameState} setGameState={setGameState} />
</div>
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/bernd/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
import com.bernd.model.OpenGame;
import com.bernd.model.ViewGame;
import com.bernd.util.RandomString;
import com.bernd.util.SgfCreator;
import java.security.Principal;
import java.time.LocalDate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.messaging.handler.annotation.MessageMapping;
Expand All @@ -23,8 +27,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 @@ -144,4 +146,15 @@ public ResponseEntity<?> accept(@RequestBody AcceptRequest acceptRequest) {
operations.convertAndSend("/topic/lobby/active_games", activeGames.games());
return ResponseEntity.ok().build();
}

@GetMapping("/api/sgf/{id}/{black}_vs_{white}.sgf")
public ResponseEntity<String> getSgf(
@PathVariable String id) {
Game game = games.get(id);
if (game == null) {
return ResponseEntity.notFound().build();
}
String sgf = SgfCreator.createSgf(game, LocalDate.now());
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(sgf);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/bernd/game/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,5 @@ public static int[][] removeDeadStonesAround(
}
return result;
}

}
55 changes: 55 additions & 0 deletions src/main/java/com/bernd/util/SgfCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.bernd.util;

import com.bernd.model.Game;
import com.bernd.model.Move;
import java.time.LocalDate;

public final class SgfCreator {

public static String createSgf(Game game, LocalDate date) {
int twoPass = 0;
int asciiAddOn = 97;
StringBuilder sb = new StringBuilder();
sb.append("(;");
sb
.append("FF[4]\n")
.append("CA[UTF-8]\n")
.append("GM[1]\n")
.append("DT[" + date.toString() + "]\n")
.append("GN[" + game.id() + "]\n")
.append("PB[" + game.black() + "]\n")
.append("PW[" + game.white() + "]\n")
.append("RE[" + (game.gameHasEnded() ? game.getScore() : "?") + "]\n")
.append("SZ[" + game.dim() + "]\n");
for (Move move : game.moves().moves()) {
sb
.append(";")
.append(getColorFromValue(move.color()))
.append("[");
if (!move.pass()) {
twoPass = 0;
sb
.append((char) (move.x() + asciiAddOn))
.append((char) (move.y() + asciiAddOn));
} else {
twoPass++;
if (twoPass == 2) {
sb.append("]\n");
break;
}
}
sb
.append("]\n");
}
sb.append(")");
return sb.toString();
}

private static String getColorFromValue(int value) {
if (value == 32) {
return "B";
}
return "W";
}

}
26 changes: 26 additions & 0 deletions src/test/java/com/bernd/util/SgfCreatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bernd.util;

import com.bernd.game.Board;
import com.bernd.game.MoveList;
import com.bernd.model.Game;
import com.bernd.model.Move;
import java.time.LocalDate;
import java.time.Month;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class SgfCreatorTest {

@Test
void testCreate() {
MoveList moveList = MoveList.create(20);
moveList.add(new Move(Board.B, 0, null, 3, 4));
moveList.add(new Move(Board.W, 0, null, 6, 4));
Game game = new Game("1234", "B", "W", false, new int[9][], 9, 0, null, moveList);
String sgf = SgfCreator.createSgf(game, LocalDate.of(2024, Month.AUGUST, 30));
assertEquals("(;FF[4]CA[UTF-8]GM[1]DT[2024-08-30]GN[1234]PB[B]PW[W]RE[?]SZ[9];B[de];W[ge])",
sgf.replace("\n", ""));
}

}
Loading