Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,9 @@ open class Snackgame(

fun remove(streakWithFever: StreakWithFever) {
val streak = streakWithFever.streak
val removedSnacks = board.removeSnacksIn(streak)
val removedSnacks = board.removeSnacksIn(streakWithFever.streak)

val serverIsFever = feverTime?.isActive(streakWithFever.occurredAt) == true
val isValid = streakWithFever.clientIsFever && serverIsFever

val multiplier = if (isValid) FEVER_MULTIPLIER else NORMAL_MULTIPLIER
increaseScore(streak.length * multiplier)
increaseScore(streak.length * isFever(streakWithFever))

if (removedSnacks.any(Snack::isGolden)) {
this.board = board.reset()
Expand All @@ -67,13 +63,19 @@ open class Snackgame(
}
}

fun startFeverTime() {
this.feverTime = FeverTime.start()
private fun isFever(streakWithFever: StreakWithFever): Int {
val serverIsFever = feverTime?.isActive(streakWithFever.occurredAt) == true
val isValid = streakWithFever.clientIsFever && serverIsFever
val multiplier = if (isValid) FEVER_MULTIPLIER else NORMAL_MULTIPLIER
return multiplier
}

private fun increaseScore(earn: Int) {
val multiplier = if (feverTime?.isActive() == true) FEVER_MULTIPLIER else NORMAL_MULTIPLIER
this.score += earn * multiplier
this.score += earn
}

fun startFeverTime() {
this.feverTime = FeverTime.start()
}

override val metadata = SNACK_GAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FeverTime(
fun isActive(now: LocalDateTime = LocalDateTime.now()): Boolean {
if (feverStartedAt == null) return false
val effectiveStart = feverPausedAt?.let { feverStartedAt!!.plus(Duration.between(it, now)) } ?: feverStartedAt
return Duration.between(effectiveStart, now) < DURATION
return Duration.between(effectiveStart, now) < FEVER_TIME_PERIOD
}

fun pause() {
Expand All @@ -31,7 +31,7 @@ class FeverTime(
}

companion object {
private val DURATION: Duration = Duration.ofSeconds(30)
private val FEVER_TIME_PERIOD: Duration = Duration.ofSeconds(30)

fun start(now: LocalDateTime = LocalDateTime.now()): FeverTime {
return FeverTime(now)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.snackgame.server.game.snackgame.core.service.dto
import com.fasterxml.jackson.annotation.JsonCreator
import com.snackgame.server.game.snackgame.core.domain.Coordinate
import com.snackgame.server.game.snackgame.core.domain.Streak
import com.snackgame.server.game.snackgame.exception.InvalidStreakTimeException
import java.time.Duration
import java.time.LocalDateTime

data class StreaksRequest @JsonCreator constructor(
Expand All @@ -14,14 +16,24 @@ data class StreaksRequest @JsonCreator constructor(

data class StreakWithMeta(
val coordinates: List<CoordinateRequest>,
val isFever: Boolean
val isFever: Boolean,
val occurredAt: LocalDateTime,
) {
fun toDomain(now: LocalDateTime): StreakWithFever =
StreakWithFever(
fun toDomain(now: LocalDateTime): StreakWithFever {
if (Duration.between(occurredAt, now).abs() > REQUEST_TIME_TOLERANCE) {
throw InvalidStreakTimeException()
}

return StreakWithFever(
streak = Streak.of(coordinates.map { Coordinate(it.y, it.x) }),
clientIsFever = isFever,
occurredAt = now
occurredAt = occurredAt
)
}

companion object {
private val REQUEST_TIME_TOLERANCE: Duration = Duration.ofSeconds(2)
}
}

data class StreakWithFever(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.snackgame.server.game.snackgame.exception

class InvalidStreakTimeException : SnackgameException("허용된 시간차를 초과하였습니다.") {
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class SnackgameServiceTest {
listOf(
StreakWithMeta(
coordinates = coordinates,
isFever = false
isFever = false,
LocalDateTime.now()
)
)
)
Expand Down Expand Up @@ -85,7 +86,8 @@ class SnackgameServiceTest {
listOf(
StreakWithMeta(
coordinates = coordinates,
isFever = false
isFever = true,
LocalDateTime.now()
)
)
)
Expand Down
Loading