Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
comforest committed Oct 14, 2024
2 parents 4a31dfb + 3aaf9aa commit d2c30b8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class RedisLockAdapter(
override fun lock(key: String) {
while (getLock(key).not()) {
try {
Thread.sleep(50)
Thread.sleep(SLEEP_TIME)
} catch (e: InterruptedException) {
throw UnSupportedException
}
Expand All @@ -28,10 +28,12 @@ internal class RedisLockAdapter(
private fun getLock(key: String): Boolean {
return redisTemplate
.opsForValue()
.setIfAbsent(key + LOCK, LOCK, Duration.ofSeconds(1)) ?: throw NotFoundLockKeyException
.setIfAbsent(key + LOCK, LOCK, Duration.ofSeconds(LOCK_TIME)) ?: throw NotFoundLockKeyException
}

companion object {
private const val LOCK = "lock"
private const val LOCK_TIME = 1L
private const val SLEEP_TIME = 50L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class RedisMafiaPhase @JsonCreator constructor(
@JsonProperty("turnList") val turnList: List<RedisMafiaPlayer>? = null,
@JsonProperty("mafiaPlayer") val mafiaPlayer: RedisMafiaPlayer? = null,
@JsonProperty("keyword") val keyword: RedisMafiaKeyword? = null,
@JsonProperty("drawData") val drawData: List<Pair<Long, Map<String, Any>>>? = null,
@JsonProperty("drawData") val drawData: List<RedisDrawInfo>? = null,
@JsonProperty("players") val players: Map<Long, List<Long>>? = null,
@JsonProperty("round") val round: Int? = null,
@JsonProperty("turn") val turn: Int? = null,
Expand All @@ -21,6 +21,11 @@ data class RedisMafiaPhase @JsonCreator constructor(
@JsonProperty("mafiaWin") val isMafiaWin: Boolean? = null,
)

data class RedisDrawInfo @JsonCreator constructor(
@JsonProperty("userId") val userId: Long,
@JsonProperty("draw") val draw: Map<String, Any>,
)

enum class RedisMafiaPhaseStatus {
WAIT,
READY,
Expand Down Expand Up @@ -102,7 +107,10 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
category = keyword.category,
),
drawData = drawData.map { pair ->
Pair(pair.first.value, pair.second)
RedisDrawInfo(
userId = pair.first.value,
draw = pair.second,
)
},
round = turnInfo.round,
turn = turnInfo.turn,
Expand All @@ -129,7 +137,10 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
category = keyword.category,
),
drawData = drawData.map { pair ->
Pair(pair.first.value, pair.second)
RedisDrawInfo(
userId = pair.first.value,
draw = pair.second,
)
},
players = serializePlayers(this.players),
)
Expand All @@ -155,7 +166,10 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
category = keyword.category,
),
drawData = drawData.map { pair ->
Pair(pair.first.value, pair.second)
RedisDrawInfo(
userId = pair.first.value,
draw = pair.second,
)
},
answer = answer,
)
Expand All @@ -181,7 +195,10 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
category = keyword.category,
),
drawData = drawData.map { pair ->
Pair(pair.first.value, pair.second)
RedisDrawInfo(
userId = pair.first.value,
draw = pair.second,
)
},
answer = answer,
showAnswer = showAnswer,
Expand All @@ -207,8 +224,8 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) {
mafiaPlayer = mafiaPlayer!!.toPlayer(),
keyword = keyword!!.toMafiaKeyword(),
turnInfo = TurnInfo(round!!, turn!!),
drawData = drawData!!.map { pair ->
Pair(UserId(pair.first), pair.second)
drawData = drawData!!.map { item ->
Pair(UserId(item.userId), item.draw)
}.toMutableList(),
)

Expand All @@ -218,8 +235,8 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) {
},
mafiaPlayer = mafiaPlayer!!.toPlayer(),
keyword = keyword!!.toMafiaKeyword(),
drawData = drawData!!.map { pair ->
Pair(UserId(pair.first), pair.second)
drawData = drawData!!.map { item ->
Pair(UserId(item.userId), item.draw)
}.toMutableList(),
players = deserializePlayers(players!!),
)
Expand All @@ -230,8 +247,8 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) {
},
mafiaPlayer = mafiaPlayer!!.toPlayer(),
keyword = keyword!!.toMafiaKeyword(),
drawData = drawData!!.map { pair ->
Pair(UserId(pair.first), pair.second)
drawData = drawData!!.map { item ->
Pair(UserId(item.userId), item.draw)
}.toMutableList(),
answer = answer,
)
Expand All @@ -242,8 +259,8 @@ fun RedisMafiaPhase.toMafiaPhase(): MafiaPhase = when (status) {
},
mafiaPlayer = mafiaPlayer!!.toPlayer(),
keyword = keyword!!.toMafiaKeyword(),
drawData = drawData!!.map { pair ->
Pair(UserId(pair.first), pair.second)
drawData = drawData!!.map { item ->
Pair(UserId(item.userId), item.draw)
}.toMutableList(),
answer = answer,
showAnswer = showAnswer!!,
Expand Down

0 comments on commit d2c30b8

Please sign in to comment.