-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
85cada9
3e0e04c
0573512
012f3cd
c9f5535
c0c9c88
a922675
568e9ac
d3cadd8
d2f060d
17809f7
068198d
d3ccf97
afd02be
c949dc7
c123aab
af50e7c
c6d1c4d
8568076
95391ad
2c69b2a
5fa51fd
4310b54
c47db0b
9019b5a
7ee037b
59d2c0f
c1231e7
8a07b8e
3b02a4f
d610fd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import com.yessorae.data.source.network.polygon.model.chart.asDomainModel | |
import com.yessorae.domain.common.ChartRequestArgumentHelper | ||
import com.yessorae.domain.entity.Chart | ||
import com.yessorae.domain.entity.tick.TickUnit | ||
import com.yessorae.domain.exception.ChartGameException | ||
import com.yessorae.domain.repository.ChartRepository | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
|
@@ -23,19 +24,48 @@ class ChartRepositoryImpl @Inject constructor( | |
@Dispatcher(ChartTrainerDispatcher.IO) | ||
private val dispatcher: CoroutineDispatcher | ||
) : ChartRepository { | ||
override suspend fun fetchNewChartRandomly(): Chart = | ||
override suspend fun fetchNewChartRandomly(totalTurn: Int): Chart = | ||
withContext(dispatcher) { | ||
val chart = networkDataSource | ||
.getChart( | ||
ticker = chartRequestArgumentHelper.getRandomTicker(), | ||
tickUnit = appPreferences.getTickUnit(), | ||
from = chartRequestArgumentHelper.getFromDate(), | ||
to = chartRequestArgumentHelper.getToDate() | ||
) | ||
.asDomainModel(TickUnit.DAY) | ||
|
||
val chartId = localDBDataSource.insertChart(chart.asEntity()) | ||
localDBDataSource.insertTicks(chart.ticks.map { it.asEntity(chartId = chartId) }) | ||
chart.copy(id = chartId) | ||
fetchNewChartRandomlyWithRetry( | ||
currentRetryCount = 0, | ||
totalTurn = totalTurn | ||
) | ||
} | ||
|
||
private suspend fun fetchNewChartRandomlyWithRetry( | ||
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. Q; 이 역할을 가진 함수를 분리한 이유가 있나요? 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. fetchNewChartRandomly 함수를 사용하는 입장에서 알 필요 없는 currentRetryCount 같은 파라미터를 노출하지 않고 withContext 로 디스패칭을 바꿔주는 것을 한 번만 할 수 있어서 분리했습니다. |
||
currentRetryCount: Int, | ||
totalTurn: Int | ||
): Chart { | ||
// RETRY_COUNT 만큼 시도했는데도 실패하면 IllegalStateException 발생. | ||
// 거의 발생하지 않아도 안전망 역할 | ||
if (currentRetryCount > RETRY_COUNT) { | ||
throw ChartGameException.HardToFetchTradeException | ||
} | ||
|
||
val dto = networkDataSource | ||
.getChart( | ||
ticker = chartRequestArgumentHelper.getRandomTicker(), | ||
tickUnit = appPreferences.getTickUnit(), | ||
from = chartRequestArgumentHelper.getFromDate(), | ||
to = chartRequestArgumentHelper.getToDate() | ||
) | ||
|
||
// 서버에서 가져온 차트가 totalTurn 보다 작으면 다시 요청 | ||
if (dto.ticks.size < totalTurn) { | ||
return fetchNewChartRandomlyWithRetry( | ||
currentRetryCount = currentRetryCount + 1, | ||
totalTurn = totalTurn | ||
) | ||
} | ||
|
||
val chart = dto.asDomainModel(TickUnit.DAY) | ||
|
||
val chartId = localDBDataSource.insertChart(chart.asEntity()) | ||
localDBDataSource.insertTicks(chart.ticks.map { it.asEntity(chartId = chartId) }) | ||
return chart.copy(id = chartId) | ||
} | ||
|
||
companion object { | ||
private const val RETRY_COUNT = 3 | ||
} | ||
} |
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.
설마 S&P500에 1년미만인 종목이 있을까 하며 처리 안하고 넘어갔던 부분인데, TotalTurn 보다 틱수가 낮으면 게임이 불가하여 TotalTurn 보다는 큰 게 나올 때까지 시도하되 무한 루프를 돌면 안되니 최대 횟수를 두었습니다.