Skip to content

Commit

Permalink
release: 0.6.4 (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Oct 20, 2024
2 parents 8cfc022 + 97ec6fe commit 74cf745
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
43 changes: 41 additions & 2 deletions src/main/kotlin/org/gitanimals/coupon/app/CouponFacade.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.gitanimals.coupon.app

import org.gitanimals.coupon.app.event.CouponUsed
import org.gitanimals.coupon.controller.response.CouponResponses
import org.gitanimals.coupon.app.response.CouponUsedResponse
import org.gitanimals.coupon.domain.Coupon
import org.gitanimals.coupon.domain.CouponCodes
import org.gitanimals.coupon.domain.CouponService
import org.rooftop.netx.api.SagaManager
import org.springframework.stereotype.Service
Expand All @@ -14,19 +15,57 @@ class CouponFacade(
private val identityApi: IdentityApi,
) {

fun useCoupon(token: String, code: String, dynamic: String) {
fun useCoupon(token: String, code: String, dynamic: String): CouponUsedResponse? {
val user = identityApi.getUserByToken(token)

require(couponService.isValidCoupon(user.id.toLong(), code)) {
"Cannot use coupon code $code"
}

return when (CouponCodes.getByCode(code)) {
CouponCodes.NEW_USER_BONUS_PET -> useBonusCoupon(token, code, dynamic)
CouponCodes.HALLOWEEN_2024,
CouponCodes.HALLOWEEN_2024_STAR_BONUS -> {
val picked = halloweenCandidates.random()
useBonusCoupon(token, code, picked)
return CouponUsedResponse(picked)
}
}
}

private fun useBonusCoupon(token: String, code: String, dynamic: String): CouponUsedResponse? {
val user = identityApi.getUserByToken(token)

sagaManager.startSync(CouponUsed(user.id.toLong(), user.username, code, dynamic))
return null
}

fun getUsedCoupons(token: String): List<Coupon> {
val user = identityApi.getUserByToken(token)

return couponService.getCouponsByUserId(user.id.toLong())
}

private companion object {
private val halloweenCandidates = mutableListOf<String>().also { candidates ->
repeat(500) {
candidates.add("SLIME_PUMPKIN_1")
}
repeat(302) {
candidates.add("SLIME_PUMPKIN_2")
}
repeat(150) {
candidates.add("GHOST")
}
repeat(30) {
candidates.add("GHOST_KING")
}
repeat(15) {
candidates.add("SCREAM")
}
repeat(3) {
candidates.add("SCREAM_GHOST")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.gitanimals.coupon.app.response

data class CouponUsedResponse(
val result: String
) {

fun toResponse() = mapOf("result" to result)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CouponController(
fun useCoupon(
@RequestHeader(HttpHeaders.AUTHORIZATION) token: String,
@RequestBody couponRequest: CouponRequest,
) = couponFacade.useCoupon(token, couponRequest.code, couponRequest.dynamic)
) = couponFacade.useCoupon(token, couponRequest.code, couponRequest.dynamic)?.toResponse()

@GetMapping("/coupons/users")
@ResponseStatus(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package org.gitanimals.coupon.controller.request

data class CouponRequest(
val code: String,
val dynamic: String,
val dynamic: String = "",
)
9 changes: 9 additions & 0 deletions src/main/kotlin/org/gitanimals/coupon/domain/CouponCodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ package org.gitanimals.coupon.domain
enum class CouponCodes {

NEW_USER_BONUS_PET,
HALLOWEEN_2024,
HALLOWEEN_2024_STAR_BONUS,
;

companion object {
fun getByCode(code: String): CouponCodes {
return CouponCodes.valueOf(code.uppercase())
}
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/org/gitanimals/gotcha/domain/GotchaType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ enum class GotchaType(
Capsule.of("SLOTH_KING", 0.05),
Capsule.of("SLOTH_SUNGLASSES", 0.06),
Capsule.of("TURTLE", 0.03),
Capsule.of("GHOST", 0.05),
Capsule.of("GHOST_KING", 0.01),
Capsule.of("SCREAM", 0.005),
Capsule.of("SCREAM_GHOST", 0.001),
Capsule.of("SLIME_PUMPKIN_1", 0.08),
Capsule.of("SLIME_PUMPKIN_2", 0.08),
)
}
}

0 comments on commit 74cf745

Please sign in to comment.