Commit 9b5e3d09 authored by Yechang's avatar Yechang
Browse files

fix: penalty

parent 2e512655
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
	"version": "0.0.1",
	"private": true,
	"scripts": {
		"dev": "vite dev",
		"dev": "ENV=development vite dev",
		"build": "vite build",
		"preview": "vite preview",
		"test": "npm run test:integration && npm run test:unit",
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ export const load = (async ({ parent }) => {

    const problems = await db.problem.findMany({
        where: {
            classId: currentClass.id,
            penalty: { gt: 0 }
        }
    })
+65 −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, params }) => {
    const { currentClass } = await parent()

    const problems = await db.problem.findMany({
        where: {
            classId: currentClass.id,
            penalty: { gt: 0 }
        }
    })
    const studentId = _.toNumber(params.studentId)

    const res = await Promise.all(problems.map(async p => {
        const penalty = p.penalty || 0
        const penaltyAfter = p.penaltyAfter || 0
        const submissions = await db.submission.findMany({
            where: {
                submitterId: studentId,
                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: studentId, 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 {
            problemId: p.id,
            results: res
        }
    }))

    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)}
+0 −6
Original line number Diff line number Diff line
@@ -32,9 +32,6 @@

<script lang="ts">
	import CalendarIcon from 'svelte-radix/Calendar.svelte';
	import CaretSort from 'svelte-radix/CaretSort.svelte';
	import { Textarea } from '$lib/components/ui/textarea';
	import Check from 'svelte-radix/Check.svelte';
	import SuperDebug, {
		type SuperValidated,
		type Infer,
@@ -44,7 +41,6 @@
	import { zodClient } from 'sveltekit-superforms/adapters';
	import * as Form from '$lib/components/ui/form';
	import * as Popover from '$lib/components/ui/popover';
	import * as Command from '$lib/components/ui/command';
	import { Calendar } from '$lib/components/ui/calendar';
	import { Input } from '$lib/components/ui/input';
	import { buttonVariants } from '$lib/components/ui/button';
@@ -53,8 +49,6 @@
	import {
		DateFormatter,
		getLocalTimeZone,
		type DateValue,
		parseDate,
		ZonedDateTime,
		parseZonedDateTime,
		now
Loading