Search in sources :

Example 1 with ValidationResult

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();
}
Also used : PostMapping(org.springframework.web.bind.annotation.PostMapping) java.util(java.util) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Getter(lombok.Getter) SChem(com.faendir.zachtronics.bot.sc.validator.SChem) MediaType(org.springframework.http.MediaType) RequiredArgsConstructor(lombok.RequiredArgsConstructor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) com.faendir.zachtronics.bot.sc.model(com.faendir.zachtronics.bot.sc.model) IOException(java.io.IOException) ScSolutionRepository(com.faendir.zachtronics.bot.sc.repository.ScSolutionRepository) RestController(org.springframework.web.bind.annotation.RestController) HttpStatus(org.springframework.http.HttpStatus) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) CategoryRecord(com.faendir.zachtronics.bot.repository.CategoryRecord) GameRestController(com.faendir.zachtronics.bot.rest.GameRestController) com.faendir.zachtronics.bot.sc.rest.dto(com.faendir.zachtronics.bot.sc.rest.dto) ValidationResult(com.faendir.zachtronics.bot.validation.ValidationResult) NotNull(org.jetbrains.annotations.NotNull) UtilsKt(com.faendir.zachtronics.bot.utils.UtilsKt) SubmitResultTypeKt(com.faendir.zachtronics.bot.rest.dto.SubmitResultTypeKt) ValidationResult(com.faendir.zachtronics.bot.validation.ValidationResult) ResponseStatusException(org.springframework.web.server.ResponseStatusException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with ValidationResult

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);
}
Also used : Matcher(java.util.regex.Matcher) ValidationResult(com.faendir.zachtronics.bot.validation.ValidationResult) SingleParseResult(com.faendir.discord4j.command.parse.SingleParseResult) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ValidationResult

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;
}
Also used : ValidationResult(com.faendir.zachtronics.bot.validation.ValidationResult) ScSubmission(com.faendir.zachtronics.bot.sc.model.ScSubmission) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ValidationResult (com.faendir.zachtronics.bot.validation.ValidationResult)3 NotNull (org.jetbrains.annotations.NotNull)3 SingleParseResult (com.faendir.discord4j.command.parse.SingleParseResult)1 CategoryRecord (com.faendir.zachtronics.bot.repository.CategoryRecord)1 GameRestController (com.faendir.zachtronics.bot.rest.GameRestController)1 SubmitResultTypeKt (com.faendir.zachtronics.bot.rest.dto.SubmitResultTypeKt)1 com.faendir.zachtronics.bot.sc.model (com.faendir.zachtronics.bot.sc.model)1 ScSubmission (com.faendir.zachtronics.bot.sc.model.ScSubmission)1 ScSolutionRepository (com.faendir.zachtronics.bot.sc.repository.ScSolutionRepository)1 com.faendir.zachtronics.bot.sc.rest.dto (com.faendir.zachtronics.bot.sc.rest.dto)1 SChem (com.faendir.zachtronics.bot.sc.validator.SChem)1 UtilsKt (com.faendir.zachtronics.bot.utils.UtilsKt)1 IOException (java.io.IOException)1 java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Getter (lombok.Getter)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)1