Skip to content

Commit

Permalink
スコア算出リファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Nov 15, 2024
1 parent cec5028 commit 4bbf800
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/src/app/api/minio/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function POST(req: NextRequest) {
userId: user?.id || 0,
};

const score = await scoreRegister(scoreData);
const score = await scoreRegister(scoreData, assignmentId);

const response: ScoreResponse = {
text: caption || "",
Expand Down
14 changes: 10 additions & 4 deletions app/src/functions/scoreRegister.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { prisma } from "@/lib/prisma";
import type { Score, ScoreData } from "@/types";

const assignmentDate = async function GET() {
const assignmentDate = async function GET(assignmentId: number) {
const assignment = await prisma.assignment.findFirst({
where: { id: assignmentId },
select: { date: true },
});
const date = assignment?.date ?? null;
Expand All @@ -13,7 +14,10 @@ const assignmentDate = async function GET() {
});
};

export const scoreRegister = async (scoreData: ScoreData) => {
export const scoreRegister = async (
scoreData: ScoreData,
assignmentId: number,
) => {
// 必須データの存在確認
if (
scoreData.similarity == null ||
Expand All @@ -22,23 +26,25 @@ export const scoreRegister = async (scoreData: ScoreData) => {
scoreData.assignmentId == null ||
scoreData.userId == null
) {
return
return;
}

// ポイントの計算
const minTime = 60;
const maxTime = 3600;

const response = await assignmentDate();
const response = await assignmentDate(assignmentId);
const assignmentDateValue = await response.json();

const answerIntervalTime =
new Date(scoreData.answerTime).getTime() -
new Date(assignmentDateValue.date).getTime();

const normalizedTime = Math.max(
0,
Math.min(1, (answerIntervalTime / 1000 - minTime) / (maxTime - minTime)),
);

const point = scoreData.similarity * 70 + (1 - normalizedTime) * 30;

const score: Score = await prisma.score.create({
Expand Down

0 comments on commit 4bbf800

Please sign in to comment.