-
Notifications
You must be signed in to change notification settings - Fork 0
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
The head ref may contain hidden characters: "20-\uC720\uC800\uAC00-\uC791\uC131\uD55C-\uCF54\uB4DC-\uACB0\uACFC-\uC11C\uBC84\uB85C-\uBCF4\uB0B4\uAE30"
Changes from 9 commits
9455f46
2b85acb
075a21b
0150917
6c3b200
c8aec36
e673300
c6b3678
9a97ac2
1331070
d44198f
bb6b31e
2f17ea0
8665426
4900e23
e861bd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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> | ||
); | ||
} |
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')) { | ||
socket.on('message', handleMessage); | ||
const totalSubmissionResult = 10; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기는 임시로 값을 넣어둔거에요. 나중에 소켓에서 데이터를 받게되면 대체할 예정입니다. |
||
setScoreResults( | ||
range(0, totalSubmissionResult).map((_, index) => ({ | ||
problemId: -1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 따로 변수 안 만들고 -1 넣어서 로딩 걸어주고 진짜 문제 번호 들어오면 점수 넣는 거 너무 좋네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { SubmissionResult } from './SubmissionResult'; |
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; | ||
}; |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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) 하는 행위가 이벤트 등록이네요 ! 👍🏻
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와우
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와우.