Skip to content

Commit

Permalink
refactor: 30개가 넘는 persona가 보여지지는 않지만, 저장되도록 수정한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Apr 14, 2024
1 parent 6fec0c8 commit 12751f0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/main/kotlin/org/gitanimals/render/domain/Persona.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Persona(
@Embedded
val level: Level,

@Column(name = "visible", nullable = false)
val visible: Boolean,

@JsonIgnore
@JoinColumn(name = "user_id")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
Expand All @@ -28,10 +31,16 @@ class Persona(
constructor(
type: PersonaType,
level: Long,
) : this(type = type, level = Level(level))
visible: Boolean
) : this(type = type, level = Level(level), visible = visible)


fun toSvg(): String = type.load(this)
fun toSvg(): String {
if (!visible) {
return ""
}
return type.load(this)
}

fun level(): Long = level.value
}
30 changes: 23 additions & 7 deletions src/main/kotlin/org/gitanimals/render/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ class User(
fun updateContribution(contribution: Int) {
val currentYear = ZonedDateTime.now(ZoneId.of("UTC")).year
val currentYearContribution =
contributions.first { it.year == currentYear }
contributions.firstOrNull { it.year == currentYear }
?: run {
val currentYearContribution = Contribution(currentYear, 0, Instant.now())
contributions.add(currentYearContribution)
currentYearContribution
}

val newContribution = contribution - currentYearContribution.contribution

currentYearContribution.contribution += newContribution
lastPersonaGivePoint += newContribution
currentYearContribution.lastUpdatedContribution = Instant.now()
levelUpPersonas(newContribution)
giveNewPersona()
}

private fun levelUpPersonas(newContribution: Int) {
Expand All @@ -86,15 +90,27 @@ class User(
}
}

private fun giveNewPersona() {
fun giveNewPersona() {
if (lastPersonaGivePoint < FOR_NEW_PERSONA_COUNT) {
return
}
lastPersonaGivePoint %= FOR_NEW_PERSONA_COUNT.toInt()
if (personas.size >= MAX_PERSONA_COUNT) {
return

val newPersona = when (personas.size >= MAX_PERSONA_COUNT) {
true -> Persona(
type = PersonaType.random(),
level = Level(0),
visible = false,
user = this
)

false -> Persona(
type = PersonaType.random(),
level = Level(0),
visible = true,
user = this
)
}
val newPersona = Persona(type = PersonaType.random(), level = Level(0), user = this)
personas.add(newPersona)
}

Expand Down Expand Up @@ -202,7 +218,7 @@ class User(
max((totalContributionCount / FOR_INIT_PERSONA_COUNT), 1)
).toInt()
) {
personas.add(Persona(PersonaType.random(), 0))
personas.add(Persona(PersonaType.random(), 0, true))
}
return personas
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/org/gitanimals/render/domain/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class UserService(
@Retryable(retryFor = [ObjectOptimisticLockingFailureException::class], maxAttempts = 10)
@Transactional
fun updateContributions(username: String, contribution: Int) {
getUserByName(username)
.updateContribution(contribution)
val user = getUserByName(username)
user.updateContribution(contribution)
user.giveNewPersona()
}

fun isContributionUpdatedBeforeOneHour(name: String): Boolean =
Expand Down
12 changes: 12 additions & 0 deletions src/test/kotlin/org/gitanimals/render/domain/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ internal class UserTest : DescribeSpec({
}
}
}

describe("giveNewPersona 메소드는") {
val user = User.newUser("new-user", mutableMapOf())
context("펫이 30마리가 넘을경우, visible false의 pet을 생성한다.") {
repeat(99) {
user.updateContribution(30 * (it + 1))
user.giveNewPersona()
}

user.personas.count { !it.visible } shouldBeEqual 70
}
}
}) {
private companion object {
private const val ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down

0 comments on commit 12751f0

Please sign in to comment.