Commit 2e512655 authored by Yechang's avatar Yechang
Browse files

fix(submission): fix penalty

parent 8c264cc8
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import type { SubmissionFeedback } from "@prisma/client";
import { contentSchema } from "$lib/shared/feedback";


const getFeedbackScore = async (feedback: SubmissionFeedback): Promise<number> => {
export const getFeedbackScore = async (feedback: SubmissionFeedback): Promise<number> => {
    const contentResult = contentSchema.safeParse(feedback.content)
    if (!contentResult.success) {
        return 0
@@ -47,7 +47,7 @@ export const fixAndCalculatePenalty = async (currentSubmissionId: number): Promi
            problemId: problem.id
        },
        orderBy: {
            id: 'asc'
            createdAt: 'asc'
        },
        include: {
            feedback: true
+69 −0
Original line number Diff line number Diff line
import { error, json, redirect } from '@sveltejs/kit';
import type { PageParentData, PageServerLoad } from './$types';
import _ from 'lodash';
import { db } from '$lib/server/db';
import { log } from "$lib/server/log"
import { getFeedbackScore } from '$lib/server/db/feedback';

export const load = (async ({ parent }) => {
    const { currentClass } = await parent()

    const problems = await db.problem.findMany({
        where: {
            penalty: { gt: 0 }
        }
    })
    const students = await db.classesOnUsers.findMany({
        where: { classId: currentClass.id, role: 'STUDENT' }
    })

    const res = await Promise.all(problems.map(async p => {
        const penalty = p.penalty || 0
        const penaltyAfter = p.penaltyAfter || 0
        const results = await Promise.all(students.map(async student => {
            const submissions = await db.submission.findMany({
                where: {
                    submitterId: student.userId,
                    problemId: p.id
                },
                orderBy: { createdAt: 'asc' },
                include: { feedback: true }
            })

            let num = 0;
            const cal = (n: number) => (_.max([n - penaltyAfter, 0]) || 0) * penalty;
            let res = []
            for (const s of submissions) {
                if (_.isNil(s.feedback)) {
                    // no feedback
                    return { student: student.userId, reason: 'no feedback', submission: s.id }
                }
                const { feedback } = s
                num += feedback.isValid ? 1 : 0;
                if (feedback.score >= 0) {
                    // already had score
                    continue
                }
                const penaltyScore = cal(num)
                const originalScore = await getFeedbackScore(s.feedback)
                const finalScore = _.max([originalScore - penaltyScore, 0]) || 0;

                if (finalScore != feedback.score) {
                    res.push({
                        feedback: feedback.id,
                        originScore: feedback.score,
                        newScore: finalScore
                    })
                }
            }
            return { student: student.userId, result: res }
        }))
        return {
            problemId: p.id,
            results
        }
    }))

    return json(res)
}) satisfies PageServerLoad;
+7 −0
Original line number Diff line number Diff line
<script lang="ts">
	import type { PageData } from './$types';

	export let data: PageData;
</script>

{JSON.stringify(data)}