Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

スコア計算の修正とトーストをダイアログに集約した #80

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/app/api/minio/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,21 @@ export async function POST(req: NextRequest) {

const score = await scoreRegister(scoreData, assignmentId);

const japaneseText = await prisma.word.findFirst({
where: {
english: assignment,
},
select: {
japanese: true,
},
});

const response: ScoreResponse = {
text: caption || "",
score: score?.point || 0,
similarity: resSimilarity.similarity,
assignmentId: assignmentId,
japaneseText: japaneseText?.japanese || "",
};

return new Response(JSON.stringify(response), {
Expand Down
13 changes: 7 additions & 6 deletions app/src/app/camera/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Toaster } from "@/components/ui/sonner";
import { LoadingSpinner } from "@/components/view/LoadingSpinner";
import { PointDialog } from "@/components/view/PointDialog";
import { useOpenPointDialog, usePointDialog } from "@/lib/atom";
Expand All @@ -24,7 +23,6 @@ import imageCompression from "browser-image-compression";
import type React from "react";
import { useEffect, useRef, useState } from "react";
import { Camera, type CameraType } from "react-camera-pro";
import { toast } from "sonner";
import AddImageIcon from "../../../public/icons/icon-add-image.svg";
import RotateCameraIcon from "../../../public/icons/icon-rotate-camera.svg";
import ShutterIcon from "../../../public/icons/icon-shutter.svg";
Expand Down Expand Up @@ -92,6 +90,7 @@ const CameraApp = () => {
const [isPointDialogOpen, _] = usePointDialog();
const openDialog = useOpenPointDialog();
const [loginUser, setLoginUser] = useState<User>();
const [message, setMessage] = useState<string>("");

useEffect(() => {
const getDevices = async () => {
Expand Down Expand Up @@ -232,7 +231,9 @@ const CameraApp = () => {

const percentSimilarity = Math.floor(data.similarity * 100);

const message = `${data.text} 類似度 ${percentSimilarity}% スコア: ${data.score} ランキングから順位を確認しましょう!`;
setMessage(
`生成文章: ${data.text} \n日本語: ${data.japaneseText} \n類似度: ${percentSimilarity}% スコア: ${data.score} \nランキングから順位を確認しましょう!`,
);
const newAssignments = assignments.map((assignment) => {
if (assignment.assignmentId === data.assignmentId) {
assignment.isAnswered = true;
Expand All @@ -250,7 +251,6 @@ const CameraApp = () => {
openDialog();
}
setIsUploading(false);
toast(message);
setAssignments(newAssignments);

if (newAssignments.every((assignment) => assignment.isAnswered)) {
Expand Down Expand Up @@ -280,7 +280,9 @@ const CameraApp = () => {

return (
<>
{isPointDialogOpen && <PointDialog type="photo" />}
{isPointDialogOpen && (
<PointDialog type="photo" customSubMessage={message} />
)}
{isActive ? (
<>
<div className="flex items-center justify-center">
Expand Down Expand Up @@ -386,7 +388,6 @@ const CameraApp = () => {
) : (
<Answered assignments={assignments} />
)}
<Toaster />
</>
);
};
Expand Down
10 changes: 8 additions & 2 deletions app/src/components/view/PointDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function PointDialog({
title: customTitle || "撮影に成功しました!",
icon: Camera,
iconColor: "text-green-500",
message: customMessage || "素晴らしい写真です!",
message: customMessage || "ポイントを獲得しました!",
subMessage: customSubMessage || "ポイントを獲得しました!",
},
ranking: {
Expand Down Expand Up @@ -85,7 +85,13 @@ export function PointDialog({
</div>
<div className="space-y-2 text-center">
<p className="text-sm text-muted-foreground">{message}</p>
<p className="text-sm text-muted-foreground">{subMessage}</p>
<div
className="text-xs text-muted-foreground"
// biome-ignore lint/security/noDangerouslySetInnerHtml: <explanation>
dangerouslySetInnerHTML={{
__html: subMessage.replace(/\n/g, "<br>"),
}}
/>
</div>
<Button
className="mt-4 w-full max-w-[200px] rounded-full"
Expand Down
33 changes: 32 additions & 1 deletion app/src/functions/scoreRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ const assignmentDate = async function GET(assignmentId: number) {
});
};

const experiencePointData = async function GET(userId: number) {
const exp = await prisma.experiencePoint.findFirst({
where: { userId: userId },
select: {
speedPoint: true,
similarityPoint: true,
},
});

return new Response(JSON.stringify({ exp }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
};

export const scoreRegister = async (
scoreData: ScoreData,
assignmentId: number,
Expand All @@ -36,6 +51,9 @@ export const scoreRegister = async (
const response = await assignmentDate(assignmentId);
const assignmentDateValue = await response.json();

const resExp = await experiencePointData(scoreData.userId);
const expDataValue = await resExp.json();

const answerIntervalTime =
new Date(scoreData.answerTime).getTime() -
new Date(assignmentDateValue.date).getTime();
Expand All @@ -45,7 +63,20 @@ export const scoreRegister = async (
Math.min(1, (answerIntervalTime / 1000 - minTime) / (maxTime - minTime)),
);

const point = scoreData.similarity * 70 + (1 - normalizedTime) * 30;
// 旧式 なんかあれば戻す
// const point = scoreData.similarity * 70 + (1 - normalizedTime) * 30;

// similarityやnormalizedが低い時に各ステータスに振ったポイントを多少スコアに付与する。最低保証みたいな感じ
const similarityPoint = expDataValue.similarityPoint ?? 0;
const speedPoint = expDataValue.speedPoint ?? 0;

const point = Math.min(
scoreData.similarity * 70 +
(scoreData.similarity < 0.5 ? similarityPoint * 0.1 : 0) +
(1 - normalizedTime) * 30 +
(normalizedTime > 0.5 ? speedPoint * 0.2 : 0),
100,
);

const score: Score = await prisma.score.create({
data: {
Expand Down
1 change: 1 addition & 0 deletions app/src/types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface ScoreResponse {
score: number;
similarity: number;
assignmentId: number;
japaneseText: string;
}

export interface ChangeStatus {
Expand Down
Loading