Skip to content

Commit

Permalink
Added more validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kkobarii committed Jan 30, 2024
1 parent 0b0fbfc commit 53a7890
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class ValidationController {

@PostMapping("/validate/part")
public ResponseEntity<RestResponse> validatePart(@RequestBody Part part) {
log.info("Validating part " + part.getTag());
log.debug("Validating part " + part.getTag());
List<String> errors = validationService.validatePart(part);

if (errors.isEmpty()) {
log.warn("Part is valid!");
log.debug("Part is valid!");
return new ResponseEntity<>(new RestResponse(HttpStatus.OK, "Part is valid!"), HttpStatus.OK);
}

log.warn("Part is not valid!");
log.debug("Part is not valid!");
RestError error = new RestError(HttpStatus.NOT_ACCEPTABLE, "Part is not valid!");
for (var e : errors) {
log.debug(" > " + e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class ValidationService {
// 3. Part is maximum 8 hexes wide and 8 hexes tall
// 4. All hexes must be connected
public List<String> validatePart(Part part) {
log.info("2 Validating part " + part.getTag());
System.out.println("2 Validating part " + part.getTag());
List<String> errors = new ArrayList<>();

int minHexes = 5;
Expand All @@ -48,17 +46,20 @@ public List<String> validatePart(Part part) {
}

// no hexes can be on the same position
int duplicates = 0;
for (Hex hex1 : part.getHexes()) {
for (Hex hex2 : part.getHexes()) {
if (hex1 == hex2)
continue;

if (hex1.getQ() == hex2.getQ() && hex1.getR() == hex2.getR() && hex1.getS() == hex2.getS()) {
errors.add("No hexes can be on the same position!");
duplicates++;
break;
}
}
}
if (duplicates > 0)
errors.add("Part must not have duplicate hexes!");

// every hex has to have correct coordinates
for (Hex hex : part.getHexes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Set;
import java.util.List;

@Data
@Entity
Expand All @@ -23,7 +23,7 @@ public class Part {
private String tag;

@OneToMany(mappedBy = "key.idPart", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Hex> hexes;
private List<Hex> hexes;

// @OneToMany(mappedBy = "key.fromPart", cascade = CascadeType.ALL)
// private Set<LocationDoor> doors;
Expand Down

0 comments on commit 53a7890

Please sign in to comment.