-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
612 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/dev/ecattez/shahmat/domain/board/piece/king/KingMovingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.ecattez.shahmat.domain.board.piece.king; | ||
|
||
import dev.ecattez.shahmat.domain.board.Board; | ||
import dev.ecattez.shahmat.domain.board.Direction; | ||
import dev.ecattez.shahmat.domain.board.Orientation; | ||
import dev.ecattez.shahmat.domain.board.piece.Piece; | ||
import dev.ecattez.shahmat.domain.board.piece.move.AbstractMovingStrategy; | ||
import dev.ecattez.shahmat.domain.board.piece.move.Capture; | ||
import dev.ecattez.shahmat.domain.board.piece.move.MoveOnVacant; | ||
import dev.ecattez.shahmat.domain.board.piece.move.Movement; | ||
import dev.ecattez.shahmat.domain.board.square.Square; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class KingMovingStrategy extends AbstractMovingStrategy { | ||
|
||
public static final Direction[] MOVING_DIRECTIONS = Direction.values(); | ||
|
||
@Override | ||
public List<Movement> getAvailableMovements(Board board, Piece piece, Square from) { | ||
Orientation orientation = piece.orientation(); | ||
return Stream.of(MOVING_DIRECTIONS) | ||
.map(direction -> from.findNeighbour(direction, orientation)) | ||
.flatMap(Optional::stream) | ||
.filter(neighbour -> !board.hasAlly(neighbour, piece)) | ||
.map(neighbour -> toMovement(board, piece, from, neighbour)) | ||
.flatMap(Optional::stream) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private Optional<Movement> toMovement(Board board, Piece piece, Square from, Square location) { | ||
if (board.isVacant(location)) { | ||
return Optional.of(new MoveOnVacant(piece, from, location)); | ||
} | ||
if (board.hasOpponent(location, piece)) { | ||
return Optional.of(new Capture(piece, from, location, board.getPiece(location))); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/dev/ecattez/shahmat/infra/CorsConfigurationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package dev.ecattez.shahmat.infra; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class CorsConfigurationAdapter extends WebSecurityConfigurerAdapter { | ||
|
||
private List<String> allowedOrigins; | ||
|
||
public CorsConfigurationAdapter(@Value("${cors.global.allowed-origins}") List<String> allowedOrigins) { | ||
this.allowedOrigins = allowedOrigins; | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http | ||
.cors(corsConfigurer -> corsConfigurer.configurationSource(corsConfigurationSource())) | ||
.formLogin(); | ||
} | ||
|
||
private CorsConfigurationSource corsConfigurationSource() { | ||
CorsConfiguration configuration = new CorsConfiguration(); | ||
configuration.setAllowedOrigins(allowedOrigins); | ||
configuration.setAllowedMethods(List.of("GET","POST", "PUT")); | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", configuration); | ||
return source; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/dev/ecattez/shahmat/infra/controller/RootResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.ecattez.shahmat.infra.controller; | ||
|
||
import dev.ecattez.shahmat.infra.aggregate.ChessGameId; | ||
import dev.ecattez.shahmat.infra.projection.BoardInfo; | ||
import dev.ecattez.shahmat.infra.store.EventStore; | ||
import org.springframework.hateoas.CollectionModel; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; | ||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; | ||
|
||
@RestController | ||
public class RootResource { | ||
|
||
private final EventStore eventStore; | ||
|
||
public RootResource(EventStore eventStore) { | ||
this.eventStore = eventStore; | ||
} | ||
|
||
@GetMapping(produces = "application/prs.hal-forms+json") | ||
public CollectionModel<BoardInfo> listBoards() { | ||
List<BoardInfo> boardsInfo = eventStore.aggregateIds() | ||
.stream() | ||
.map(this::toBoardInfo) | ||
.collect(Collectors.toList()); | ||
|
||
return new CollectionModel<>( | ||
boardsInfo, | ||
linkTo( | ||
methodOn(RootResource.class).listBoards() | ||
).withSelfRel() | ||
); | ||
} | ||
|
||
private BoardInfo toBoardInfo(ChessGameId id) { | ||
return new BoardInfo( | ||
id.value, | ||
"<not_implemented>", | ||
"<not_implemented>", | ||
"<not_implemented>", | ||
false | ||
); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/dev/ecattez/shahmat/infra/projection/BoardInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dev.ecattez.shahmat.infra.projection; | ||
|
||
import dev.ecattez.shahmat.infra.controller.HalBoardResource; | ||
import org.springframework.hateoas.IanaLinkRelations; | ||
import org.springframework.hateoas.RepresentationModel; | ||
import org.springframework.hateoas.server.core.Relation; | ||
|
||
import java.util.Objects; | ||
|
||
@Relation(collectionRelation = "boards", itemRelation = "board") | ||
public class BoardInfo extends RepresentationModel<BoardInfo> { | ||
|
||
private String id; | ||
private String white; | ||
private String black; | ||
private String turnOf; | ||
private boolean over; | ||
|
||
public BoardInfo(String id, String white, String black, String turnOf, boolean over) { | ||
this.id = id; | ||
this.white = white; | ||
this.black = black; | ||
this.turnOf = turnOf; | ||
this.over = over; | ||
this.add(HalBoardResource.getBoardLink(IanaLinkRelations.SELF, id)); | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getWhite() { | ||
return white; | ||
} | ||
|
||
public String getBlack() { | ||
return black; | ||
} | ||
|
||
public String getTurnOf() { | ||
return turnOf; | ||
} | ||
|
||
public boolean isOver() { | ||
return over; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
if (!super.equals(o)) return false; | ||
BoardInfo boardInfo = (BoardInfo) o; | ||
return over == boardInfo.over && | ||
Objects.equals(id, boardInfo.id) && | ||
Objects.equals(white, boardInfo.white) && | ||
Objects.equals(black, boardInfo.black) && | ||
Objects.equals(turnOf, boardInfo.turnOf); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), id, white, black, turnOf, over); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.