From be656fb07539f1c506516bcbba144b9aca3be2f4 Mon Sep 17 00:00:00 2001 From: devxb Date: Sat, 8 Jun 2024 11:15:04 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=EB=AA=A8=EB=93=A0=20persona=20?= =?UTF-8?q?=EC=9D=91=EB=8B=B5=EC=97=90=20dropRate=EB=A5=BC=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=EC=8B=9C=ED=82=A8=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../render/controller/PersonaController.kt | 49 ++++++++++++++++--- .../response/PersonaEnumResponse.kt | 15 ++++++ .../controller/response/PersonaResponse.kt | 1 + .../controller/response/UserResponse.kt | 8 ++- .../gitanimals/render/domain/PersonaType.kt | 21 ++++++-- .../render/domain/response/PersonaResponse.kt | 4 +- src/main/resources/application.properties | 4 +- src/main/resources/persona/animal/rabbit.svg | 18 +++---- 8 files changed, 96 insertions(+), 24 deletions(-) create mode 100644 src/main/kotlin/org/gitanimals/render/controller/response/PersonaEnumResponse.kt diff --git a/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt b/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt index 5a24e7b..2fdf74e 100644 --- a/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt +++ b/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt @@ -4,8 +4,10 @@ import org.gitanimals.render.app.AuthorizationException import org.gitanimals.render.app.UserFacade import org.gitanimals.render.controller.request.AddPersonaRequest import org.gitanimals.render.controller.response.ErrorResponse +import org.gitanimals.render.controller.response.PersonaEnumResponse import org.gitanimals.render.controller.response.PersonaResponse import org.gitanimals.render.controller.response.UserResponse +import org.gitanimals.render.domain.PersonaType import org.gitanimals.render.domain.UserService import org.gitanimals.render.domain.request.PersonaChangeRequest import org.springframework.http.HttpHeaders @@ -30,7 +32,13 @@ class PersonaController( ): PersonaResponse { val persona = userFacade.getPersona(token, personaId) - return PersonaResponse(persona.id, persona.type, persona.level, persona.visible) + return PersonaResponse( + persona.id, + persona.type, + persona.level, + persona.visible, + persona.dropRate + ) } @PatchMapping("/personas") @@ -46,9 +54,27 @@ class PersonaController( changedPersona.type, changedPersona.level, changedPersona.visible, + changedPersona.dropRate, + ) + } + + @GetMapping("/personas/infos") + @ResponseStatus(HttpStatus.OK) + fun getAllPersonaInfo(): Map> { + return mapOf( + "personas" to PersonaType.entries + .sortedByDescending { it.weight } + .asSequence() + .map { PersonaEnumResponse.from(it) } + .toList() ) } + @ExceptionHandler(IllegalArgumentException::class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse = + ErrorResponse.from(exception) + @PostMapping("/internals/personas") fun addPersona( @RequestHeader(HttpHeaders.AUTHORIZATION) token: String, @@ -63,7 +89,13 @@ class PersonaController( addPersonaRequest.level, ) - return PersonaResponse(persona.id, persona.type, persona.level, persona.visible) + return PersonaResponse( + persona.id, + persona.type, + persona.level, + persona.visible, + persona.dropRate, + ) } @DeleteMapping("/internals/personas") @@ -73,14 +105,15 @@ class PersonaController( ): PersonaResponse { val persona = userFacade.deletePersona(token, personaId) - return PersonaResponse(persona.id, persona.type, persona.level, persona.visible) + return PersonaResponse( + persona.id, + persona.type, + persona.level, + persona.visible, + persona.dropRate, + ) } - @ExceptionHandler(IllegalArgumentException::class) - @ResponseStatus(HttpStatus.BAD_REQUEST) - fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse = - ErrorResponse.from(exception) - @ExceptionHandler(AuthorizationException::class) @ResponseStatus(HttpStatus.UNAUTHORIZED) fun handleAuthorizationException(exception: AuthorizationException): ErrorResponse = diff --git a/src/main/kotlin/org/gitanimals/render/controller/response/PersonaEnumResponse.kt b/src/main/kotlin/org/gitanimals/render/controller/response/PersonaEnumResponse.kt new file mode 100644 index 0000000..8d5fc6b --- /dev/null +++ b/src/main/kotlin/org/gitanimals/render/controller/response/PersonaEnumResponse.kt @@ -0,0 +1,15 @@ +package org.gitanimals.render.controller.response + +import org.gitanimals.render.domain.PersonaType + +data class PersonaEnumResponse( + val type: PersonaType, + val dropRate: String, +) { + + companion object { + fun from(personaType: PersonaType): PersonaEnumResponse { + return PersonaEnumResponse(personaType, personaType.getDropRate()) + } + } +} diff --git a/src/main/kotlin/org/gitanimals/render/controller/response/PersonaResponse.kt b/src/main/kotlin/org/gitanimals/render/controller/response/PersonaResponse.kt index 304133d..51fbf24 100644 --- a/src/main/kotlin/org/gitanimals/render/controller/response/PersonaResponse.kt +++ b/src/main/kotlin/org/gitanimals/render/controller/response/PersonaResponse.kt @@ -7,4 +7,5 @@ data class PersonaResponse( val type: PersonaType, val level: String, val visible: Boolean, + val dropRate: String, ) diff --git a/src/main/kotlin/org/gitanimals/render/controller/response/UserResponse.kt b/src/main/kotlin/org/gitanimals/render/controller/response/UserResponse.kt index 9725545..fb61032 100644 --- a/src/main/kotlin/org/gitanimals/render/controller/response/UserResponse.kt +++ b/src/main/kotlin/org/gitanimals/render/controller/response/UserResponse.kt @@ -14,7 +14,13 @@ data class UserResponse( user.id.toString(), user.name, user.personas.map { - PersonaResponse(it.id.toString(), it.type, it.level().toString(), it.visible) + PersonaResponse( + it.id.toString(), + it.type, + it.level().toString(), + it.visible, + it.type.getDropRate() + ) }.toList() ) } diff --git a/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt b/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt index 9414062..d61df5b 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt @@ -1,11 +1,12 @@ package org.gitanimals.render.domain +import java.text.DecimalFormat import kotlin.math.atan2 import kotlin.math.max import kotlin.math.min import kotlin.random.Random -enum class PersonaType(private val weight: Double) { +enum class PersonaType(val weight: Double) { GOOSE(1.0) { override fun loadSvg(user: User, persona: Persona, mode: Mode): String { val goose = gooseSvg.replace("*{act}", act(persona.id)) @@ -1071,7 +1072,7 @@ enum class PersonaType(private val weight: Double) { StringBuilder().moveRandomly("mole", id, 40, "180s", 5, 14.0) .toString() }, - MOLE_GRASS(0.1){ + MOLE_GRASS(0.1) { override fun loadSvg(user: User, persona: Persona, mode: Mode): String { return moleGrassSvg.replace("*{act}", act(persona.id)) .replace("*{id}", persona.id.toString()) @@ -1086,7 +1087,7 @@ enum class PersonaType(private val weight: Double) { StringBuilder().moveRandomly("mole", id, 40, "180s", 5, 14.0) .toString() }, - RABBIT(0.9){ + RABBIT(0.9) { override fun loadSvg(user: User, persona: Persona, mode: Mode): String { return rabbitSvg.replace("*{act}", act(persona.id)) .replace("*{id}", persona.id.toString()) @@ -1107,6 +1108,19 @@ enum class PersonaType(private val weight: Double) { require(weight in 0.000..1.0) { "PersonaType's weight should be between 0.000 to 1.0" } } + fun getDropRate(): String { + val allPersonaCount = personas.size + val personaCount = (weight * 1000).toInt() + + val dropRate = (personaCount.toDouble() / allPersonaCount.toDouble()) * 100.0 + + if (dropRate < 1.0) { + return "${dropRateFormat.format(dropRate)}%" + } + + return "${dropRate.toInt()}%" + } + fun load(user: User, persona: Persona, mode: Mode): String = loadSvg(user, persona, mode) .drawContribution(mode, user) @@ -1145,6 +1159,7 @@ enum class PersonaType(private val weight: Double) { } companion object { + private val dropRateFormat = DecimalFormat("#.#") private val maxWeight = lazy { var maxWeight = 0 diff --git a/src/main/kotlin/org/gitanimals/render/domain/response/PersonaResponse.kt b/src/main/kotlin/org/gitanimals/render/domain/response/PersonaResponse.kt index 6e42cf4..519b7a8 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/response/PersonaResponse.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/response/PersonaResponse.kt @@ -8,6 +8,7 @@ data class PersonaResponse( val type: PersonaType, val level: String, val visible: Boolean, + val dropRate: String, ) { companion object { fun from(persona: Persona): PersonaResponse { @@ -15,7 +16,8 @@ data class PersonaResponse( persona.id.toString(), persona.type, persona.level.value.toString(), - persona.visible + persona.visible, + persona.type.getDropRate() ) } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 24dcc81..2c7e970 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,8 +2,8 @@ server.name=gitanimals-render server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/gitanimalsrender -spring.datasource.username=a -spring.datasource.password=a +spring.datasource.username=root +spring.datasource.password=Dlwnsdud0414 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect diff --git a/src/main/resources/persona/animal/rabbit.svg b/src/main/resources/persona/animal/rabbit.svg index c855c06..31afbd2 100644 --- a/src/main/resources/persona/animal/rabbit.svg +++ b/src/main/resources/persona/animal/rabbit.svg @@ -4,13 +4,13 @@ @keyframes rabbit-head-*{id}-move { 0% { - transform: translate(0, -10px); + transform: translate(0, -1px); } 50% { - transform: translate(0, -11px); + transform: translate(0, -2px); } 100% { - transform: translate(0, -10px); + transform: translate(0, -1px); } } @@ -34,7 +34,7 @@ transform: translate(-2px, 0px) scaleX(1); } 15% { - transform: translate(-2px, 0px) scaleX(1.1); + transform: translate(-2px, 0px) scaleX(1.2); } 30% { transform: translate(-2px, 0px) scaleX(1); @@ -68,7 +68,7 @@ - @@ -88,7 +88,7 @@ - + @@ -97,7 +97,7 @@ - + - + - + From d40a1b6a7cc1e7bbfcf07b53ced55f531e2c770b Mon Sep 17 00:00:00 2001 From: devxb Date: Sat, 8 Jun 2024 11:22:27 +0900 Subject: [PATCH 2/2] =?UTF-8?q?perf:=20dropRate=EB=A5=BC=20=EC=BA=90?= =?UTF-8?q?=EC=8B=B1=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitanimals/render/domain/PersonaType.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt b/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt index d61df5b..89e32df 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/PersonaType.kt @@ -6,7 +6,7 @@ import kotlin.math.max import kotlin.math.min import kotlin.random.Random -enum class PersonaType(val weight: Double) { +enum class PersonaType(val weight: Double, private var dropRate: String? = null) { GOOSE(1.0) { override fun loadSvg(user: User, persona: Persona, mode: Mode): String { val goose = gooseSvg.replace("*{act}", act(persona.id)) @@ -1109,16 +1109,20 @@ enum class PersonaType(val weight: Double) { } fun getDropRate(): String { + return this.dropRate ?: loadDropRate() + } + + private fun loadDropRate(): String { val allPersonaCount = personas.size val personaCount = (weight * 1000).toInt() - val dropRate = (personaCount.toDouble() / allPersonaCount.toDouble()) * 100.0 - - if (dropRate < 1.0) { - return "${dropRateFormat.format(dropRate)}%" + val dropRateTemp = (personaCount.toDouble() / allPersonaCount.toDouble()) * 100.0 + this.dropRate = if (dropRateTemp < 1.0) { + "${dropRateFormat.format(dropRateTemp)}%" + } else { + "${dropRateTemp.toInt()}%" } - - return "${dropRate.toInt()}%" + return dropRate!! } fun load(user: User, persona: Persona, mode: Mode): String =