Compare commits
No commits in common. "0d2e0a1af8e63c5fdf170f8562e992d725403ce7" and "1a5b4b364ae6bc91b9346fd8973b00929fc44345" have entirely different histories.
0d2e0a1af8
...
1a5b4b364a
|
@ -53,7 +53,7 @@ class CanService(private val repository: CanRepository) {
|
||||||
}
|
}
|
||||||
.map {
|
.map {
|
||||||
val title: String = when (it.canUsage) {
|
val title: String = when (it.canUsage) {
|
||||||
CanUsage.DONATION, CanUsage.SPIN_ROULETTE -> {
|
CanUsage.DONATION -> {
|
||||||
if (it.room != null) {
|
if (it.room != null) {
|
||||||
"[라이브 후원] ${it.room!!.member!!.nickname}"
|
"[라이브 후원] ${it.room!!.member!!.nickname}"
|
||||||
} else if (it.audioContent != null) {
|
} else if (it.audioContent != null) {
|
||||||
|
|
|
@ -4,6 +4,5 @@ enum class CanUsage {
|
||||||
LIVE,
|
LIVE,
|
||||||
DONATION,
|
DONATION,
|
||||||
CHANGE_NICKNAME,
|
CHANGE_NICKNAME,
|
||||||
ORDER_CONTENT,
|
ORDER_CONTENT
|
||||||
SPIN_ROULETTE
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
data class CreateOrUpdateRouletteRequest(
|
|
||||||
val can: Int,
|
|
||||||
val isActive: Boolean,
|
|
||||||
val items: List<RouletteItem>
|
|
||||||
)
|
|
|
@ -1,6 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
data class GetRouletteResponse(
|
|
||||||
val can: Int,
|
|
||||||
val items: List<RouletteItem>
|
|
||||||
)
|
|
|
@ -1,18 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id
|
|
||||||
import org.springframework.data.redis.core.RedisHash
|
|
||||||
|
|
||||||
@RedisHash("roulette")
|
|
||||||
data class Roulette(
|
|
||||||
@Id
|
|
||||||
val creatorId: Long,
|
|
||||||
var can: Int,
|
|
||||||
var isActive: Boolean,
|
|
||||||
var items: List<RouletteItem>
|
|
||||||
)
|
|
||||||
|
|
||||||
data class RouletteItem(
|
|
||||||
val title: String,
|
|
||||||
val percentage: Int
|
|
||||||
)
|
|
|
@ -1,52 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
|
||||||
import kr.co.vividnext.sodalive.common.SodaException
|
|
||||||
import kr.co.vividnext.sodalive.member.Member
|
|
||||||
import kr.co.vividnext.sodalive.member.MemberRole
|
|
||||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam
|
|
||||||
import org.springframework.web.bind.annotation.RestController
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/roulette")
|
|
||||||
class RouletteController(private val service: RouletteService) {
|
|
||||||
@PostMapping
|
|
||||||
fun createOrUpdateRoulette(
|
|
||||||
@RequestBody request: CreateOrUpdateRouletteRequest,
|
|
||||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
|
||||||
) = run {
|
|
||||||
if (member == null || member.role != MemberRole.CREATOR) {
|
|
||||||
throw SodaException("로그인 정보를 확인해주세요.")
|
|
||||||
}
|
|
||||||
|
|
||||||
ApiResponse.ok(service.createOrUpdateRoulette(memberId = member.id!!, request = request))
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
fun getRoulette(
|
|
||||||
@RequestParam creatorId: Long,
|
|
||||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
|
||||||
): ApiResponse<GetRouletteResponse> {
|
|
||||||
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
|
|
||||||
|
|
||||||
val response = service.getRoulette(creatorId = creatorId)
|
|
||||||
?: throw SodaException("룰렛을 사용할 수 없습니다.")
|
|
||||||
|
|
||||||
return ApiResponse.ok(response)
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/spin")
|
|
||||||
fun spinRoulette(
|
|
||||||
@RequestBody request: SpinRouletteRequest,
|
|
||||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
|
||||||
) = run {
|
|
||||||
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
|
|
||||||
|
|
||||||
ApiResponse.ok(service.spinRoulette(request = request, memberId = member.id!!))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,7 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository
|
|
||||||
import org.springframework.stereotype.Repository
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
interface RouletteRepository : CrudRepository<Roulette, Long>
|
|
|
@ -1,100 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
import kr.co.vividnext.sodalive.can.payment.CanPaymentService
|
|
||||||
import kr.co.vividnext.sodalive.can.use.CanUsage
|
|
||||||
import kr.co.vividnext.sodalive.common.SodaException
|
|
||||||
import kr.co.vividnext.sodalive.live.room.LiveRoomRepository
|
|
||||||
import kr.co.vividnext.sodalive.member.MemberRole
|
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
|
||||||
import org.springframework.stereotype.Service
|
|
||||||
import org.springframework.transaction.annotation.Transactional
|
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
|
||||||
import kotlin.concurrent.write
|
|
||||||
|
|
||||||
@Service
|
|
||||||
class RouletteService(
|
|
||||||
private val canPaymentService: CanPaymentService,
|
|
||||||
private val repository: RouletteRepository,
|
|
||||||
private val roomRepository: LiveRoomRepository
|
|
||||||
) {
|
|
||||||
private val tokenLocks: MutableMap<Long, ReentrantReadWriteLock> = mutableMapOf()
|
|
||||||
|
|
||||||
fun createOrUpdateRoulette(memberId: Long, request: CreateOrUpdateRouletteRequest) {
|
|
||||||
rouletteValidate(request)
|
|
||||||
|
|
||||||
val roulette = repository.findByIdOrNull(id = memberId) ?: Roulette(
|
|
||||||
creatorId = memberId,
|
|
||||||
can = request.can,
|
|
||||||
isActive = request.isActive,
|
|
||||||
items = request.items
|
|
||||||
)
|
|
||||||
|
|
||||||
val lock = getOrCreateLock(memberId = memberId)
|
|
||||||
lock.write {
|
|
||||||
repository.save(roulette)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun rouletteValidate(request: CreateOrUpdateRouletteRequest) {
|
|
||||||
if (request.can < 5) {
|
|
||||||
throw SodaException("룰렛 금액은 최소 5캔 입니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.items.size < 2 || request.items.size > 6) {
|
|
||||||
throw SodaException("룰렛 옵션은 최소 2개, 최대 6개까지 설정할 수 있습니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
val percentage = request.items.asSequence()
|
|
||||||
.map { it.percentage }
|
|
||||||
.reduce { acc, percentage -> acc + percentage }
|
|
||||||
|
|
||||||
if (percentage != 100) {
|
|
||||||
throw SodaException("옵션 확률의 합이 100%가 아닙니다.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getRoulette(creatorId: Long): GetRouletteResponse? {
|
|
||||||
val roulette = repository.findByIdOrNull(id = creatorId)
|
|
||||||
|
|
||||||
if (roulette == null || !roulette.isActive || roulette.items.isEmpty()) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return GetRouletteResponse(can = roulette.can, items = roulette.items)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
fun spinRoulette(request: SpinRouletteRequest, memberId: Long): GetRouletteResponse {
|
|
||||||
// STEP 1 - 라이브 정보 가져오기
|
|
||||||
val room = roomRepository.findByIdOrNull(request.roomId)
|
|
||||||
?: throw SodaException("해당하는 라이브가 없습니다.")
|
|
||||||
|
|
||||||
val host = room.member ?: throw SodaException("잘못된 요청입니다.")
|
|
||||||
|
|
||||||
if (host.role != MemberRole.CREATOR) {
|
|
||||||
throw SodaException("비비드넥스트와 계약한\n크리에이터에게만 후원을 하실 수 있습니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// STEP 2 - 룰렛 데이터 가져오기
|
|
||||||
val roulette = repository.findByIdOrNull(id = host.id!!)
|
|
||||||
|
|
||||||
if (roulette == null || !roulette.isActive || roulette.items.isEmpty()) {
|
|
||||||
throw SodaException("룰렛을 사용할 수 없습니다.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// STEP 3 - 캔 사용
|
|
||||||
canPaymentService.spendCan(
|
|
||||||
memberId = memberId,
|
|
||||||
needCan = roulette.can,
|
|
||||||
canUsage = CanUsage.SPIN_ROULETTE,
|
|
||||||
liveRoom = room,
|
|
||||||
container = request.container
|
|
||||||
)
|
|
||||||
|
|
||||||
return GetRouletteResponse(can = roulette.can, items = roulette.items)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getOrCreateLock(memberId: Long): ReentrantReadWriteLock {
|
|
||||||
return tokenLocks.computeIfAbsent(memberId) { ReentrantReadWriteLock() }
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
package kr.co.vividnext.sodalive.live.roulette
|
|
||||||
|
|
||||||
data class SpinRouletteRequest(
|
|
||||||
val roomId: Long,
|
|
||||||
val container: String
|
|
||||||
)
|
|
Loading…
Reference in New Issue