Skip to content

Commit

Permalink
feat: add logger and remove prints
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Jul 16, 2024
1 parent d4071ef commit 119b050
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package board.master.controller;

import java.util.logging.Logger;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -16,7 +17,7 @@
@RestController
@RequestMapping("/api")
public class GameController {

private final Logger logger = Logger.getLogger(getClass().getName());
private final GameService gameService;

@Autowired
Expand Down Expand Up @@ -78,16 +79,12 @@ public ResponseEntity<GameResponse> botMove(@RequestBody GameId gameId) {
GameResponse response = gameService.botMove(id);
return ResponseEntity.ok(response);
}
catch (IllegalArgumentException e) {
System.out.println("Bot move request: id:" + gameId + " error:" + e.getMessage());
return ResponseEntity.badRequest().build();
}
catch (IllegalStateException e) {
System.out.println("Bot move request: id:" + gameId + " error:" + e.getMessage());
catch (IllegalArgumentException | IllegalStateException e) {
logger.warning("Bot move request: id:" + gameId + " error:" + e.getMessage());
return ResponseEntity.badRequest().build();
}
catch (Exception e) {
System.out.println("Bot move request: id:" + gameId + " error:" + e.getMessage());
logger.severe("Bot move request: id:" + gameId + " error:" + e.getMessage());
return ResponseEntity.internalServerError().build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package board.master.model.games.chess.Pieces;

import java.util.List;
import java.util.logging.Logger;

import board.master.model.Action;
import board.master.model.games.Board;
Expand All @@ -11,6 +12,8 @@
* Abstract class for a chess piece
*/
public abstract class Piece {
private final Logger logger = Logger.getLogger(getClass().getName());

public final Color color;
public int row;
public int column;
Expand Down Expand Up @@ -78,7 +81,7 @@ private boolean isValidPosition(int row, int col) {
*/
public void move(int row, int column, Board board) throws IllegalArgumentException {
if (!isValidMove(row, column, board)) {
System.err.println("Invalid move: " + String.valueOf(row) + String.valueOf(column) + " for piece: " + getSymbol() + " at position: " + String.valueOf(this.row) + String.valueOf(this.column));
logger.severe("Invalid move: " + String.valueOf(row) + String.valueOf(column) + " for piece: " + getSymbol() + " at position: " + String.valueOf(this.row) + String.valueOf(this.column));
throw new IllegalArgumentException("Invalid move");
}
// Delete old position
Expand Down
8 changes: 1 addition & 7 deletions backend/src/main/java/board/master/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public GameResponse playerMove(MoveRequest request) throws IllegalArgumentExcept
game.setStateHandler(transformedGame);
}
else {
System.out.println("Invalid move");
throw new IllegalArgumentException("Invalid move" + action.toString());
}

Expand Down Expand Up @@ -144,15 +143,10 @@ public GameResponse botMove(String gameId) throws IllegalArgumentException, Ille
// Wait for the bot move to complete or timeout
return future.get(timeToLive, TimeUnit.MINUTES);
} catch (TimeoutException e) {
// Handle timeout, e.g., by updating the game state or setting a flag
System.out.println("Error TimeoutException bot move " + e.getMessage());
// Attempt to cancel the ongoing task
future.cancel(true);
throw new IllegalStateException("Bot move timed out");
} catch (InterruptedException | ExecutionException e) {
// Handle other exceptions
System.out.println("Error InterruptedException bot move " + e.getMessage());
throw new IllegalStateException("Error executing bot move: ", e.getCause());
throw new IllegalStateException("Error executing bot move: " + e.getMessage());
} finally {
// stop clock
throttlingScheduler.shutdownNow();
Expand Down

0 comments on commit 119b050

Please sign in to comment.