use of com.faendir.zachtronics.bot.validation.ValidationResult in project zachtronics-leaderboard-bot by F43nd1r.
the class ScController method submit.
@PostMapping(path = "/submit", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Map<String, Object>> submit(@NotNull @ModelAttribute ScSubmissionDTO submissionDTO) throws IOException {
if (submissionDTO.getVideo() != null && !UtilsKt.isValidLink(submissionDTO.getVideo()))
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid video link");
String export = new String(submissionDTO.getExport().getBytes());
Collection<ValidationResult<ScSubmission>> submissions = SChem.validateMultiExport(export, false, submissionDTO.getAuthor());
return repository.submitAll(submissions).stream().map(r -> Map.of("result", SubmitResultTypeKt.toType(r), "data", r)).toList();
}
use of com.faendir.zachtronics.bot.validation.ValidationResult in project zachtronics-leaderboard-bot by F43nd1r.
the class SChem method validationResultFrom.
@NotNull
static ValidationResult<ScSubmission> validationResultFrom(@NotNull SChemResult result, boolean bypassValidation, String author) throws SChemException {
if (result.getLevelName() == null || result.getAuthor() == null) {
assert result.getError() != null;
return new ValidationResult.Unparseable<>(result.getError());
}
if (result.getCycles() == null) {
if (result.getError() != null)
return new ValidationResult.Unparseable<>(result.getError());
else
// there is no associated error on the schem side
return new ValidationResult.Unparseable<>("Missing expected cycles for \"" + result.getSolutionName() + "\"");
}
assert result.getExport() != null;
// puzzle
SingleParseResult<ScPuzzle> puzzleParseResult = ScPuzzle.parsePuzzle(result.getLevelName());
if (puzzleParseResult instanceof SingleParseResult.Ambiguous && result.getResnetId() != null) {
puzzleParseResult = ScPuzzle.parsePuzzle(result.getLevelName() + Arrays.stream(result.getResnetId()).mapToObj(Integer::toString).collect(Collectors.joining("-", " (", ")")));
}
ScPuzzle puzzle = puzzleParseResult.orElseThrow();
// author
if (author == null)
author = result.getAuthor();
// score
// we pull flags from the input
boolean declaresBugged = false;
boolean declaresPrecog = false;
if (result.getSolutionName() != null) {
Matcher m = ScSolutionMetadata.SOLUTION_NAME_REGEX.matcher(result.getSolutionName());
if (!m.matches()) {
return new ValidationResult.Unparseable<>("Invalid solution name: \"" + result.getSolutionName() + "\"");
}
declaresBugged = m.group("Bflag") != null;
declaresPrecog = m.group("Pflag") != null;
}
ScScore score = new ScScore(result.getCycles(), result.getReactors(), result.getSymbols(), declaresBugged, declaresPrecog);
// build submission
ScSubmission submission = new ScSolutionMetadata(puzzle, author, score, result.getSolutionName()).extendToSubmission(null, result.getExport());
if (!bypassValidation) {
if (result.getError() != null)
return new ValidationResult.Invalid<>(submission, result.getError());
// check if the user is lying part 1, we know the score isn't bugged because SChem ran it
if (declaresBugged) {
return new ValidationResult.Invalid<>(submission, "Submission was declared bugged, but SChem ran it successfully");
}
// check if the user is lying part 2, we can check SChem's precog opinion
if (result.getPrecog() != null && declaresPrecog != result.getPrecog()) {
return new ValidationResult.Invalid<>(submission, "Incoherent precognition flag, given " + "\"" + score.sepFlags("/") + "\"" + " but SChem wanted \"" + ScScore.sepFlags("/", false, result.getPrecog()) + "\"\n" + result.getPrecogExplanation());
}
} else {
if (puzzle.getType() == ScType.BOSS_RANDOM) {
return new ValidationResult.Invalid<>(submission, "Boss levels with true randomness are not supported");
}
if (declaresPrecog && puzzle.isDeterministic()) {
return new ValidationResult.Invalid<>(submission, "Submission was declared precognitive, but the level is not random");
}
}
return new ValidationResult.Valid<>(submission);
}
use of com.faendir.zachtronics.bot.validation.ValidationResult in project zachtronics-leaderboard-bot by F43nd1r.
the class ScSubmitCommand method parseSubmissions.
@NotNull
@Override
public Collection<ValidationResult<ScSubmission>> parseSubmissions(@NotNull ScSubmitCommand.SubmitData parameters) {
if (parameters.getExport().equals(parameters.video))
throw new IllegalArgumentException("Export link and video link cannot be the same link");
boolean bypassValidation = parameters.bypassValidation != null && parameters.bypassValidation;
Collection<ValidationResult<ScSubmission>> results = ScSubmission.fromExportLink(parameters.export, bypassValidation, parameters.author);
if (parameters.video != null) {
if (results.size() != 1)
throw new IllegalArgumentException("Only one solution can be paired with a video");
ValidationResult<ScSubmission> result = results.iterator().next();
if (result instanceof ValidationResult.Valid<ScSubmission>) {
ScSubmission submission = result.getSubmission();
ScSubmission videoSubmission = submission.withDisplayLink(parameters.video);
return Collections.singleton(new ValidationResult.Valid<>(videoSubmission));
} else {
throw new IllegalArgumentException(result.getMessage());
}
} else
return results;
}
Aggregations