Commit 530aec52 authored by Yechang's avatar Yechang
Browse files

feat: sim

parent 084bf248
Loading
Loading
Loading
Loading
Loading
+127 −0
Original line number Diff line number Diff line
import _ from "lodash";
import type { PageServerLoad } from "./$types";
import { error, redirect, type Actions } from "@sveltejs/kit";
import { db } from "$lib/server/db";

export const load = (async ({ parent, params }) => {
    const data = await parent();
    const { currentClass } = data;
    if (currentClass.role === 'STUDENT') {
        error(403, 'Hey boy, you should not be here');
    }
    const problemId = _.toNumber(params.problemId)
    const problem = await db.problem.findUnique({ where: { id: problemId } })
    if (_.isNil(problem) || _.isNil(problem.gradeGroupId)) {
        error(404);
    }

    // get all students in this class
    const students = await db.classesOnUsers.findMany({
        where: {
            classId: currentClass.id,
            role: 'STUDENT'
        },
        include: {
            user: {
                select: {
                    sustechId: true
                },
            }
        },
        orderBy: {
            user: {
                sustechId: 'asc'
            }
        }
    })
    const studentIds = students.map(student => student.user.sustechId).filter(id => !_.isNil(id))

    // get all entries with groups and records
    const group = await db.gradeGroup.findUnique({
        where: {
            id: problem.gradeGroupId
        },
        include: {
            records: {
                include: {
                    student: {
                        select: { sustechId: true }
                    }
                },
                orderBy: { id: 'asc' }
            }
        }
    });
    if (_.isNil(group)) {
        redirect(302, '/')
    }

    const { strategy, records } = group;
    const strategyFn = {
        MAX: _.max<number>,
        MIN: _.min<number>,
        LAST: _.last<number>
    }[strategy]

    const userRecords = _.groupBy(
        records.filter(record => !_.isNil(record.student.sustechId)),
        record => record.student.sustechId
    )
    const _userScores = _.entries(userRecords).map(([sustechId, records]) => {
        const score = strategyFn(records.map((record) => record.grade)) || 0;
        return {
            [sustechId]: score
        }
    })
    const userScores = _.merge({}, ..._userScores)

    const users: {
        [sustechId: string]: number
    } = _.fromPairs(
        studentIds.map(studentId => [studentId, 0])
    )

    studentIds.forEach(studentId => {
        const score = userScores[studentId] || 0
        users[studentId] = score
    });

    const submissions = await db.submission.findMany({
        where: { problemId },
        include: {
            submitter: true,
            feedback: true
        }
    })

    const involvedSubmissions = submissions.filter(submission => {
        const { feedback, submitter } = submission;
        if (_.isNil(feedback)) {
            return false
        }
        const { sustechId } = submitter
        if (_.isNil(sustechId)) {
            return false
        }
        if (!(sustechId in users)) {
            return false
        }
        const targetScore = users[sustechId]

        const finalScore = feedback.score;

        return finalScore == targetScore
    })

    return {
        problem,
        submissions: involvedSubmissions
    }
}) satisfies PageServerLoad


export const actions = {
    default: async (event) => {
        // TODO log the user in
    }
} satisfies Actions; 
 No newline at end of file
+19 −0
Original line number Diff line number Diff line
<script lang="ts">
	import type { PageData } from './$types';
	export let data: PageData;

	const { problem, submissions } = data;
</script>

<div class="h-full px-4 py-6 lg:px-8">
	<div class="flex items-center justify-between space-y-2">
		<h2 class="text-3xl font-bold tracking-tight">{problem.title}</h2>
	</div>
	<div class="space-y-4 py-6">
		<div class="flex h-full justify-center">
			{#each submissions as submission (submission.id)}
				{submission.id} {submission.submitter.sustechId} {submission.feedback?.score}
			{/each}
		</div>
	</div>
</div>