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

[#20] 유저가 작성한 코드 결과 서버로 보내기 #106

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"remark-gfm": "^4.0.0",
"remark-parse": "^11.0.0",
"remark-react": "^9.0.1",
"socket.io-client": "^4.7.2",
"unified": "^11.0.4"
},
"devDependencies": {
Expand Down
68 changes: 68 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Outlet } from 'react-router-dom';

import worker from './__mocks__';

if (process.env.NODE_ENV === 'development') {
worker.start();
}

function App() {
return <Outlet></Outlet>;
}
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/apis/problems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ export async function fetchProblemList(): Promise<ProblemInfo[]> {
return [];
}
}

export async function fetchCompetitionProblemList(competitionId: number): Promise<ProblemInfo[]> {
try {
const { data } = await api.get<FetchProblemListResponse>(
`/competitions/${competitionId}/problems`,
);

return data;
} catch (err) {
console.error(err);

return [];
}
}
21 changes: 21 additions & 0 deletions frontend/src/components/Submission/Score.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Loading from './Loading';
import type { ScoreResult } from './types';

interface Props {
score: ScoreResult;
}

export default function Score({ score }: Props) {
const { problemId, result, stdOut } = score;

if (problemId < 0) {
return <Loading color="#e15b64" size="2rem" />;
}

return (
<div>
<p>결과: {result}</p>
<p>출력: {stdOut}</p>
</div>
);
}
71 changes: 71 additions & 0 deletions frontend/src/components/Submission/SubmissionResult.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { css } from '@style/css';

import { useEffect, useState } from 'react';

import { range } from '@/utils/array';
import type { Socket } from '@/utils/socket';

import Score from './Score';
import type { Message, ScoreResult } from './types';

interface Props {
socket: Socket;
}

export function SubmissionResult({ socket }: Props) {
const [scoreResults, setScoreResults] = useState<ScoreResult[]>([]);

const handleScoreResult = (rawData: string) => {
const newResult = JSON.parse(rawData) as ScoreResult;

setScoreResults((results) => {
return results.map((result, index) => {
if (index === newResult.testcaseId) {
return newResult;
}
return result;
});
});
};

const handleMessage = (rawData: string) => {
const { message } = JSON.parse(rawData) as Message;
console.log(message);
};

useEffect(() => {
if (!socket.hasListeners('message')) {
Copy link
Collaborator

@mahwin mahwin Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 message라는 이벤트가 소켓 객체를 만들면 항상 리슨하고 있다고 생각했는데,
scoket.on('message',cb) 하는 행위가 이벤트 등록이네요 ! 👍🏻

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 소켓에 hasListeners API가 있습니다. 정확히는 Socket은 Emitter를 상속하는데 그래서 Emitter에 있는 hasListeners를 쓸 수 있는 거에요

아래 링크 참고
https://socket.io/docs/v3/client-api/#socketoneventname-callback

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와우

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와우.

socket.on('message', handleMessage);
const totalSubmissionResult = 10;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기는 임시로 값을 넣어둔거에요. 나중에 소켓에서 데이터를 받게되면 대체할 예정입니다.

setScoreResults(
range(0, totalSubmissionResult).map((_, index) => ({
problemId: -1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따로 변수 안 만들고 -1 넣어서 로딩 걸어주고 진짜 문제 번호 들어오면 점수 넣는 거 너무 좋네요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동작 안시킨 케이스를 빼먹었네요 감사합니다

result: '',
stdOut: '',
testcaseId: index,
})),
);
}
if (!socket.hasListeners('scoreResult')) {
socket.on('scoreResult', handleScoreResult);
}
}, [socket]);

return (
<>
<section className={resultWrapperStyle}>
{scoreResults.map((result) => (
<Score key={result.testcaseId} score={result} />
))}
</section>
</>
);
}

const resultWrapperStyle = css({
padding: '24px',
minHeight: '40vh',
width: '80vw',
backgroundColor: 'darkgray',
margin: '0 auto',
});
1 change: 1 addition & 0 deletions frontend/src/components/Submission/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SubmissionResult } from './SubmissionResult';
21 changes: 21 additions & 0 deletions frontend/src/components/Submission/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ProblemId } from '@/apis/problems';

export interface SubmitResult {
contestId: number;
problemId: number;
testcaseId: number;
resultStatus: number;
elapsedTime: number;
memoryUsage: number;
}

export type Message = {
message: string;
};

export type ScoreResult = {
problemId: ProblemId;
result: string;
stdOut: string;
testcaseId: number;
};
56 changes: 0 additions & 56 deletions frontend/src/components/submissionResult/ResultInfo.tsx

This file was deleted.

38 changes: 0 additions & 38 deletions frontend/src/components/submissionResult/ResultInfoWrapper.tsx

This file was deleted.

Loading