Skip to content

Commit 144f64a

Browse files
author
Christopher Strauss
committed
create sgf
1 parent ac39d63 commit 144f64a

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
FaVolumeUp,
77
FaSearchPlus,
88
FaSearchMinus,
9+
FaDownload,
910
} from "react-icons/fa"
1011
import {
1112
useMuteStore,
@@ -14,11 +15,14 @@ import {
1415
useViewStateStore,
1516
} from "src/layout.js"
1617

17-
export function BoardSettings() {
18+
export function BoardSettings({gameState, setGameState}) {
19+
20+
1821
return (
1922
<div className="absolute pl-2 pt-1 flex flex-col items-center">
2023
<MuteIcon />
2124
<Zoom />
25+
<SaveGameFile gameState={gameState} setGameState={setGameState} />
2226
</div>
2327
)
2428
}
@@ -66,3 +70,19 @@ function Zoom() {
6670
</>
6771
)
6872
}
73+
74+
function SaveGameFile({gameState}) {
75+
return (
76+
<a
77+
href={"/app/api/sgf/" + gameState.id + "/" + gameState.black + "_vs_" + gameState.white + ".sgf"}
78+
target="_blank"
79+
download={gameState.id + "_" + gameState.black + "_vs_" + gameState.white + ".sgf"}
80+
className="mt-[0.25rem]">
81+
<IconContext.Provider value={{
82+
size: "1.5em",
83+
}}>
84+
<FaDownload />
85+
</IconContext.Provider>
86+
</a>
87+
)
88+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function Game() {
6969
<div
7070
style={{ width: vw() - sidebarWidth }}
7171
className="h-full">
72-
<BoardSettings />
72+
<BoardSettings gameState={gameState} setGameState={setGameState} />
7373
<Board gameState={gameState} setGameState={setGameState} />
7474
<GamePanel gameState={gameState} setGameState={setGameState} />
7575
</div>

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
import com.bernd.model.OpenGame;
1212
import com.bernd.model.ViewGame;
1313
import com.bernd.util.RandomString;
14+
import java.io.IOException;
15+
import java.security.Principal;
16+
import java.text.SimpleDateFormat;
17+
import java.util.Date;
1418
import org.springframework.http.HttpStatus;
19+
import org.springframework.http.MediaType;
1520
import org.springframework.http.ResponseEntity;
1621
import org.springframework.messaging.core.MessageSendingOperations;
1722
import org.springframework.messaging.handler.annotation.MessageMapping;
@@ -23,8 +28,6 @@
2328
import org.springframework.web.bind.annotation.ResponseBody;
2429
import org.springframework.web.server.ResponseStatusException;
2530

26-
import java.security.Principal;
27-
2831
import static com.bernd.util.Auth.getPrincipal;
2932
import static com.bernd.util.Util.COLORS;
3033

@@ -144,4 +147,43 @@ public ResponseEntity<?> accept(@RequestBody AcceptRequest acceptRequest) {
144147
operations.convertAndSend("/topic/lobby/active_games", activeGames.games());
145148
return ResponseEntity.ok().build();
146149
}
150+
151+
@GetMapping("/api/sgf/{id}/{black}_vs_{white}.sgf")
152+
public ResponseEntity<String> getSgf(
153+
@PathVariable String id, @PathVariable String black, @PathVariable String white) throws IOException {
154+
int asciiAddOn = 97;
155+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
156+
Game game = games.get(id);
157+
StringBuilder sb = new StringBuilder();
158+
sb.append("(;");
159+
sb
160+
.append("FF[4]\n")
161+
.append("CA[UTF-8]\n")
162+
.append("GM[1]\n")
163+
.append("DT[" + formatter.format(new Date()) + "]\n")
164+
.append("PC[localhost]\n")
165+
.append("GN[" + game.id() + "]\n")
166+
.append("PB[" + black + "]\n")
167+
.append("PW[" + white + "]\n")
168+
.append("RE[" + (game.gameHasEnded() ? game.getScore() : "?") + "]\n")
169+
.append("SZ[" + game.dim() + "]\n");
170+
for (Move move : game.moves().moves()) {
171+
sb
172+
.append(";")
173+
.append(Board.getColorFromValue(move.color()))
174+
.append("[")
175+
.append((char) (move.x() + asciiAddOn))
176+
.append((char) (move.y() + asciiAddOn));
177+
if (move == game.getLastMove()) {
178+
sb
179+
.append("]");
180+
} else {
181+
sb
182+
.append("]\n");
183+
}
184+
}
185+
sb.append(")");
186+
187+
return ResponseEntity.ok().contentLength(sb.length()).contentType(MediaType.TEXT_PLAIN).body(sb.toString());
188+
}
147189
}

src/main/java/com/bernd/game/Board.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,12 @@ public static int[][] removeDeadStonesAround(
111111
}
112112
return result;
113113
}
114+
115+
public static String getColorFromValue(int value) {
116+
if (value == 32) {
117+
return "B";
118+
}
119+
return "W";
120+
}
121+
114122
}

0 commit comments

Comments
 (0)