Skip to content

Commit

Permalink
release: 0.1.7 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Apr 14, 2024
2 parents e24745f + 12751f0 commit 9fe3506
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 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
11 changes: 6 additions & 5 deletions src/main/kotlin/org/gitanimals/render/domain/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.gitanimals.render.domain

import org.springframework.dao.OptimisticLockingFailureException
import org.springframework.orm.ObjectOptimisticLockingFailureException
import org.springframework.retry.annotation.Retryable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -21,17 +21,18 @@ class UserService(
return getUserByName(username).createLineAnimation(personaId)
}

@Retryable(retryFor = [OptimisticLockingFailureException::class], maxAttempts = 10)
@Retryable(retryFor = [ObjectOptimisticLockingFailureException::class], maxAttempts = 10)
@Transactional
fun increaseVisit(username: String) {
getUserByName(username).increaseVisitCount()
}

@Retryable(retryFor = [OptimisticLockingFailureException::class], maxAttempts = 10)
@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 9fe3506

Please sign in to comment.