Search in sources :

Example 1 with SingleParseResult

use of com.faendir.discord4j.command.parse.SingleParseResult 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)

Aggregations

SingleParseResult (com.faendir.discord4j.command.parse.SingleParseResult)1 ValidationResult (com.faendir.zachtronics.bot.validation.ValidationResult)1 Matcher (java.util.regex.Matcher)1 NotNull (org.jetbrains.annotations.NotNull)1