Skip to content

Commit

Permalink
Merge branch 'feature/DRAW-406' into sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
comforest committed Oct 13, 2024
2 parents ab3156a + 3325045 commit 4129dfa
Show file tree
Hide file tree
Showing 16 changed files with 378 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
package com.xorker.draw.mafia

import com.xorker.draw.mafia.dto.RedisMafiaGameInfo
import com.xorker.draw.mafia.dto.toRedisMafiaGameInfo
import com.fasterxml.jackson.databind.ObjectMapper
import com.xorker.draw.mafia.entity.MafiaGameOptionRedisRepository
import com.xorker.draw.mafia.entity.MafiaPhaseRedisRepository
import com.xorker.draw.mafia.entity.MafiaRoomRedisRepository
import com.xorker.draw.mafia.entity.toDomain
import com.xorker.draw.mafia.entity.toEntity
import com.xorker.draw.room.Room
import com.xorker.draw.room.RoomId
import com.xorker.draw.room.RoomRepository
import com.xorker.draw.support.metric.MetricManager
import com.xorker.draw.timer.TimerRepository
import com.xorker.draw.user.UserId
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component

@Component
internal class MafiaGameAdapter(
private val metricManager: MetricManager,
private val redisTemplateWithObject: RedisTemplate<String, RedisMafiaGameInfo>,
private val gameOptionRedisRepository: MafiaGameOptionRedisRepository,
private val phaseRedisRepository: MafiaPhaseRedisRepository,
private val roomRedisRepository: MafiaRoomRedisRepository,
private val redisTemplate: RedisTemplate<String, String>,
private val timerRepository: TimerRepository,
private val objectMapper: ObjectMapper,
) : MafiaGameRepository, RoomRepository {

override fun saveGameInfo(gameInfo: MafiaGameInfo) {
val room = gameInfo.room
if (room.isEmpty()) {
removeGameInfo(gameInfo)
} else {
val findGameInfo = redisTemplateWithObject
.opsForValue()
.get(room.id.value)
val roomId = gameInfo.room.id

if (findGameInfo == null) {
if (roomRedisRepository.existsById(roomId.value).not()) {
metricManager.increaseGameCount()
}

redisTemplateWithObject
.opsForValue()
.set(room.id.value, gameInfo.toRedisMafiaGameInfo())
gameOptionRedisRepository.save(gameInfo.gameOption.toEntity(roomId))
phaseRedisRepository.save(gameInfo.phase.toEntity(roomId, objectMapper))
roomRedisRepository.save(gameInfo.room.toEntity())

room.players.forEach {
redisTemplate
.opsForValue()
.set(it.userId.value.toString(), room.id.value)
.set("Player:${it.userId.value}", room.id.value)
}
}
}
Expand All @@ -59,36 +65,36 @@ internal class MafiaGameAdapter(
timerRepository.cancelTimer(room.id)
}

redisTemplateWithObject.delete(room.id.value)
gameOptionRedisRepository.deleteById(room.id.value)
phaseRedisRepository.deleteById(room.id.value)
roomRedisRepository.deleteById(room.id.value)
}

override fun getGameInfo(roomId: RoomId): MafiaGameInfo? {
return redisTemplateWithObject
.opsForValue()
.get(roomId.value)
?.toMafiaGameInfo()
val room = roomRedisRepository.findByIdOrNull(roomId.value)?.toDomain() ?: return null
val phase = phaseRedisRepository.findByIdOrNull(roomId.value)?.toDomain(objectMapper) ?: return null
val gameOption = gameOptionRedisRepository.findByIdOrNull(roomId.value)?.toDomain() ?: return null

return MafiaGameInfo(
room = room,
phase = phase,
gameOption = gameOption,
)
}

override fun getGameInfo(userId: UserId): MafiaGameInfo? {
val roomId = redisTemplate
.opsForValue()
.get(userId.value.toString()) ?: return null

return redisTemplateWithObject
.opsForValue()
.get(roomId)
?.toMafiaGameInfo()
return getGameInfo(RoomId(roomId))
}

override fun removePlayer(userId: UserId) {
redisTemplate.delete(userId.value.toString())
}

override fun getRoom(roomId: RoomId): Room<MafiaPlayer>? {
return redisTemplateWithObject
.opsForValue()
.get(roomId.value)
?.toMafiaGameInfo()
?.room
return roomRedisRepository.findByIdOrNull(roomId.value)?.toDomain() ?: return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ fun MafiaGameInfo.toRedisMafiaGameInfo(): RedisMafiaGameInfo = RedisMafiaGameInf
id = room.owner.userId.value,
nickname = room.owner.nickname,
color = room.owner.color,
isConnect = room.owner.isConnect(),
isConnect = room.owner.isConnect,
),
maxMemberNum = room.maxMemberNum,
players = room.players.map { player ->
RedisMafiaPlayer(
id = player.userId.value,
nickname = player.nickname,
color = player.color,
isConnect = player.isConnect(),
isConnect = player.isConnect,
)
},
isRandomMatching = room.isRandomMatching,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
id = player.userId.value,
nickname = player.nickname,
color = player.color,
isConnect = player.isConnect(),
isConnect = player.isConnect,
)
},
mafiaPlayer = RedisMafiaPlayer(
id = mafiaPlayer.userId.value,
nickname = mafiaPlayer.nickname,
color = mafiaPlayer.color,
isConnect = mafiaPlayer.isConnect(),
isConnect = mafiaPlayer.isConnect,
),
keyword = RedisMafiaKeyword(
answer = keyword.answer,
Expand All @@ -88,14 +88,14 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
id = player.userId.value,
nickname = player.nickname,
color = player.color,
isConnect = player.isConnect(),
isConnect = player.isConnect,
)
},
mafiaPlayer = RedisMafiaPlayer(
id = mafiaPlayer.userId.value,
nickname = mafiaPlayer.nickname,
color = mafiaPlayer.color,
isConnect = mafiaPlayer.isConnect(),
isConnect = mafiaPlayer.isConnect,
),
keyword = RedisMafiaKeyword(
answer = keyword.answer,
Expand All @@ -115,14 +115,14 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
id = player.userId.value,
nickname = player.nickname,
color = player.color,
isConnect = player.isConnect(),
isConnect = player.isConnect,
)
},
mafiaPlayer = RedisMafiaPlayer(
id = mafiaPlayer.userId.value,
nickname = mafiaPlayer.nickname,
color = mafiaPlayer.color,
isConnect = mafiaPlayer.isConnect(),
isConnect = mafiaPlayer.isConnect,
),
keyword = RedisMafiaKeyword(
answer = keyword.answer,
Expand All @@ -141,14 +141,14 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
id = player.userId.value,
nickname = player.nickname,
color = player.color,
isConnect = player.isConnect(),
isConnect = player.isConnect,
)
},
mafiaPlayer = RedisMafiaPlayer(
id = mafiaPlayer.userId.value,
nickname = mafiaPlayer.nickname,
color = mafiaPlayer.color,
isConnect = mafiaPlayer.isConnect(),
isConnect = mafiaPlayer.isConnect,
),
keyword = RedisMafiaKeyword(
answer = keyword.answer,
Expand All @@ -167,14 +167,14 @@ fun MafiaPhase.toRedisMafiaPhase(): RedisMafiaPhase {
id = player.userId.value,
nickname = player.nickname,
color = player.color,
isConnect = player.isConnect(),
isConnect = player.isConnect,
)
},
mafiaPlayer = RedisMafiaPlayer(
id = mafiaPlayer.userId.value,
nickname = mafiaPlayer.nickname,
color = mafiaPlayer.color,
isConnect = mafiaPlayer.isConnect(),
isConnect = mafiaPlayer.isConnect,
),
keyword = RedisMafiaKeyword(
answer = keyword.answer,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.xorker.draw.mafia.entity

import com.xorker.draw.mafia.MafiaGameOption
import com.xorker.draw.room.RoomId
import java.time.Duration
import org.springframework.data.annotation.Id

internal data class MafiaGameOptionRedisEntity(
@Id val id: String,
val minimum: Int,
val maximum: Int,
val readyTime: Long,
val introAnimationTime: Long,
val roundAnimationTime: Long,
val round: Int,
val turnTime: Long,
val turnCount: Int,
val voteTime: Long,
val answerTime: Long,
val endTime: Long,
)

internal fun MafiaGameOption.toEntity(roomId: RoomId): MafiaGameOptionRedisEntity = MafiaGameOptionRedisEntity(
id = roomId.value,
minimum = this.minimum,
maximum = this.maximum,
readyTime = this.readyTime.toMillis(),
introAnimationTime = this.introAnimationTime.toMillis(),
roundAnimationTime = this.roundAnimationTime.toMillis(),
round = this.round,
turnTime = this.turnTime.toMillis(),
turnCount = this.turnCount,
voteTime = this.voteTime.toMillis(),
answerTime = this.answerTime.toMillis(),
endTime = this.endTime.toMillis(),
)

internal fun MafiaGameOptionRedisEntity.toDomain(): MafiaGameOption = MafiaGameOption(
minimum = this.minimum,
maximum = this.maximum,
readyTime = Duration.ofMillis(this.readyTime),
introAnimationTime = Duration.ofMillis(this.introAnimationTime),
roundAnimationTime = Duration.ofMillis(this.roundAnimationTime),
round = this.round,
turnTime = Duration.ofMillis(this.turnTime),
turnCount = this.turnCount,
voteTime = Duration.ofMillis(this.voteTime),
answerTime = Duration.ofMillis(this.answerTime),
endTime = Duration.ofMillis(this.endTime),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.mafia.entity

import org.springframework.data.repository.CrudRepository

internal interface MafiaGameOptionRedisRepository : CrudRepository<MafiaGameOptionRedisEntity, String>
Loading

0 comments on commit 4129dfa

Please sign in to comment.