|
11 | 11 | import com.bernd.model.OpenGame;
|
12 | 12 | import com.bernd.model.ViewGame;
|
13 | 13 | import com.bernd.util.RandomString;
|
| 14 | +import java.io.ByteArrayInputStream; |
| 15 | +import java.io.IOException; |
| 16 | +import java.security.Principal; |
| 17 | +import java.text.SimpleDateFormat; |
| 18 | +import java.util.Date; |
| 19 | +import org.springframework.core.io.InputStreamResource; |
| 20 | +import org.springframework.core.io.Resource; |
14 | 21 | import org.springframework.http.HttpStatus;
|
| 22 | +import org.springframework.http.MediaType; |
15 | 23 | import org.springframework.http.ResponseEntity;
|
16 | 24 | import org.springframework.messaging.core.MessageSendingOperations;
|
17 | 25 | import org.springframework.messaging.handler.annotation.MessageMapping;
|
|
23 | 31 | import org.springframework.web.bind.annotation.ResponseBody;
|
24 | 32 | import org.springframework.web.server.ResponseStatusException;
|
25 | 33 |
|
26 |
| -import java.security.Principal; |
27 |
| - |
28 | 34 | import static com.bernd.util.Auth.getPrincipal;
|
29 | 35 | import static com.bernd.util.Util.COLORS;
|
30 | 36 |
|
@@ -144,4 +150,47 @@ public ResponseEntity<?> accept(@RequestBody AcceptRequest acceptRequest) {
|
144 | 150 | operations.convertAndSend("/topic/lobby/active_games", activeGames.games());
|
145 | 151 | return ResponseEntity.ok().build();
|
146 | 152 | }
|
| 153 | + |
| 154 | + @GetMapping("/api/sgf/{id}/{black}_vs_{white}.sgf") |
| 155 | + public ResponseEntity<Resource> getSgf( |
| 156 | + @PathVariable String id, @PathVariable String black, @PathVariable String white) throws IOException { |
| 157 | + int asciiAddOn = 97; |
| 158 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
| 159 | + Game game = games.get(id); |
| 160 | + StringBuilder stringBuilder = new StringBuilder(); |
| 161 | + if (game.moves() != null) { |
| 162 | + stringBuilder.append("(;"); |
| 163 | + stringBuilder |
| 164 | + .append("FF[4]\n") |
| 165 | + .append("CA[UTF-8]\n") |
| 166 | + .append("GM[1]\n") |
| 167 | + .append("DT[" + formatter.format(new Date()) + "]\n") |
| 168 | + .append("PC[localhost]\n") |
| 169 | + .append("GN[" + game.id() + "]\n") |
| 170 | + .append("PB[" + black + "]\n") |
| 171 | + .append("PW[" + white + "]\n") |
| 172 | + .append("RE[" + (game.gameHasEnded() ? game.getScore() : "?") + "]\n") |
| 173 | + .append("SZ[" + game.dim() + "]\n"); |
| 174 | + for (Move move : game.moves().moves()) { |
| 175 | + stringBuilder |
| 176 | + .append(";") |
| 177 | + .append(Board.getColorFromValue(move.color())) |
| 178 | + .append("[") |
| 179 | + .append((char) (move.x() + asciiAddOn)) |
| 180 | + .append((char) (move.y() + asciiAddOn)); |
| 181 | + if (move == game.getLastMove()) { |
| 182 | + stringBuilder |
| 183 | + .append("]"); |
| 184 | + } else { |
| 185 | + stringBuilder |
| 186 | + .append("]\n"); |
| 187 | + } |
| 188 | + } |
| 189 | + stringBuilder.append(")"); |
| 190 | + } |
| 191 | + ByteArrayInputStream bais = new ByteArrayInputStream(stringBuilder.toString().getBytes()); |
| 192 | + InputStreamResource resource = new InputStreamResource(bais); |
| 193 | + |
| 194 | + return ResponseEntity.ok().contentLength(stringBuilder.length()).contentType(MediaType.TEXT_PLAIN).body(resource); |
| 195 | + } |
147 | 196 | }
|
0 commit comments