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

[CT-2-2-3] 차트게임 기능 - 실행 테스트 및 버그 수정 #35

Conversation

nosorae
Copy link
Collaborator

@nosorae nosorae commented Jun 6, 2024

#2

PR 순서: [CT-2] <- [CT-2-2-2] <- [CT-2-2-3] <- [CT-5] <- [CT-5-1] <- [CT-37] <- [CT-5-2] <- [CT-5-3-1] <- [CT-5-3-2] <- [CT-5-3-3]

Overview

깨닳은 점

  • 코드를 작성하고 PR을 올리기 전에 테스트 코드를 작성해야겠다는 점입니다.
  • 테스트 코드를 작성하지 않으면 domain 만 있을 때는 실행해서 테스트하기 어렵고 어떻게든 테스트한다 한들 테스트를 쓰기위해서 작성한 코드들을 1회성으로 버려야하니 가성비도 안나오고 실수로 지우지 않으면 리뷰어가 리뷰하기 힘듭니다.
  • 테스트 코드를 작성했다면 현재 PR의 fix: 태그가 붙은 거의 모든 커밋이 사라졌을 것입니다.
  • 테스트 코드를 작성하면 작성한 실행코드를 수정하지 않고 지속 가능한 테스트가 가능하겠다는 생각이 들었습니다.

nosorae added 24 commits June 6, 2024 16:52
}

private suspend fun fetchNewChartRandomlyWithRetry(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

설마 S&P500에 1년미만인 종목이 있을까 하며 처리 안하고 넘어갔던 부분인데, TotalTurn 보다 틱수가 낮으면 게임이 불가하여 TotalTurn 보다는 큰 게 나올 때까지 시도하되 무한 루프를 돌면 안되니 최대 횟수를 두었습니다.

@nosorae nosorae marked this pull request as draft June 8, 2024 17:53
@nosorae nosorae marked this pull request as ready for review June 9, 2024 01:35
}

private suspend fun fetchNewChartRandomlyWithRetry(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q; 이 역할을 가진 함수를 분리한 이유가 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fetchNewChartRandomly 함수를 사용하는 입장에서 알 필요 없는 currentRetryCount 같은 파라미터를 노출하지 않고 withContext 로 디스패칭을 바꿔주는 것을 한 번만 할 수 있어서 분리했습니다.

)

// 서버에서 가져온 차트가 totalTurn 보다 작으면 다시 요청
if ((dto.ticks?.size ?: 0) < totalTurn) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit; 옵셔널을 바로 해제해서 푸는 것보다 위에서 ticks가 널일 때 핸들링 하는 코드를 넣는게 어떨까요?
nit2; dto의 ticks를 널 대신 빈 리스트로 초기화 하는 것은 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 두 번째 방법이 좋다고 생각합니다. 불필요한 null 핸들링이라고 생각합니다. 감사합니다!


val totalCommission: Money = Money(trades.sumOf { trade -> trade.commission.value })

// 현재 턴의까지의 차트 데이터
val visibleTicks: List<Tick> =
chart.ticks
.sortedBy { it.startTimestamp }
.subList(0, chart.ticks.size - totalTurn + currentTurn - 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Q; subList의 인덱스가 항상 범위 내에서 동작할까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

결론부터 말씀드리자면, 여기서도 범위 핸들링을하는 게 좋겠습니다. (보여줄 수 있는 만큼만 보여주기)

물론 현재 브랜치부터는 Tick 개수가 totalTurn 보다 작으면 에러를 내리게 해서 동작합니다.
따라서 현재 코드상으로는 코멘트 주신 코드로 에러가 발생할 일은 없습니다.

하지만 생각해보니 애초에 N번 재시도 하고 그래도 Tick 개수가 totalTurn 보다 적으면 에러 내리는 방식이 좋은 UX는 아닌 것 같네요

그래서 해결방법을 두 가지 생각해봤는데요

  1. 운영으로 푼다. 상장한지 3년 이상인 종목만 랜덤풀에 두고 차트 데이터를 가져온다.
  2. 부족한 대로 그 만큼만 보여준다. (예를들어 유저가 설정한 totalTurn이 100인데 봉이 100개가 아닌 78개라면 78개만 보여준다.)

결론은 두 가지 다 적용하자 입니다.

운영으로 풀고 혹여나 실수가 일어나도 무작정 에러를 내려주는 일은 막자는 생각입니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

운영적인 것은 천천히 적용하고 2번을 먼저 적용했습니다.

if (trade.type.isBuy()) {
trade.totalTradeMoney.value
} else {
-(trade.totalTradeMoney.value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit; 불필요한 소괄호인 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

앗 제거 하겠습니다. 감사합니다.

screenEvent.collectLatest { event ->
when (event) {
is ChartGameEvent.InputBuyingStockCount -> {
context.showToast(context.getString(R.string.chart_game_toast_trade_buy))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit; 코드의 간결함을 위해 반복되는 코드는 확장함수등으로 만들어 보는 것은 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 그게 좋겠습니다. 감사합니다!

}
)
private fun updateGameData(data: ChartGame) =
with(data) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit; data의 복사함수를 따로 정의해서 재사용하는 것도 좋을 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

넵 동의합니다. 만들어보겠습니다,

@nosorae
Copy link
Collaborator Author

nosorae commented Jun 10, 2024

👀 이모지를 붙은 4개의 리뷰를 반영했습니다.

Copy link

@f-lab-nathan f-lab-nathan merged commit 5298aeb into feature/ct-2-2-2-chart-game-presentation-screen Jun 11, 2024
2 checks passed
@nosorae nosorae deleted the feature/ct-2-2-3-chart-game-bug-fix branch June 11, 2024 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants