Skip to content

Commit

Permalink
Merge pull request #12 from TeamTroublePainter/feature/DRAW-404
Browse files Browse the repository at this point in the history
DRAW-404 마피아 게임 결과 정규화
  • Loading branch information
SunwoongH authored Nov 3, 2024
2 parents 37f2048 + 0fbee2e commit 985b51c
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 7 deletions.
7 changes: 7 additions & 0 deletions adapter/rdb/src/main/kotlin/com/xorker/draw/mafia/DrawData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.xorker.draw.mafia

import com.xorker.draw.user.UserId

data class DrawData(
val draw: MutableList<Pair<UserId, Map<String, Any>>>,
)
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
package com.xorker.draw.mafia

import com.fasterxml.jackson.databind.ObjectMapper
import com.xorker.draw.exception.NotFoundUserException
import com.xorker.draw.exception.NotFoundWordException
import com.xorker.draw.player.PlayerHistoryJpaEntity
import com.xorker.draw.player.ResultType
import com.xorker.draw.player.RoleType
import com.xorker.draw.user.UserJpaEntity
import com.xorker.draw.user.UserJpaRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
internal class MafiaGameResultAdapter(
private val objectMapper: ObjectMapper,
private val userJpaRepository: UserJpaRepository,
private val mafiaGameResultJpaRepository: MafiaGameResultJpaRepository,
private val wordJpaRepository: WordJpaRepository,
) : MafiaGameResultRepository {

@Transactional
override fun saveMafiaGameResult(gameInfo: MafiaGameInfo) {
val room = gameInfo.room

val phase = gameInfo.phase
assertIs<MafiaPhase.End>(phase)

val keyword = phase.keyword
val findEntity = wordJpaRepository.findByKeyword(keyword.answer) ?: throw NotFoundWordException
val word = wordJpaRepository.findByKeyword(keyword.answer) ?: throw NotFoundWordException

val drawData = DrawData(phase.drawData)
val draw = objectMapper.writeValueAsString(drawData)

val mafiaGameResult = gameInfo.toMafiaGameResult()
val gameResult = MafiaGameResultJpaEntity.of(room.locale, draw, phase.answer, room.isRandomMatching, word.id)

val serializedMafiaGameResult = objectMapper.writeValueAsString(mafiaGameResult)
val mafia = phase.mafiaPlayer
room.players.forEach { player ->
val user = userJpaRepository.findByIdOrNull(player.userId.value) ?: throw NotFoundUserException

val entity = MafiaGameResultJpaEntity.of(serializedMafiaGameResult, findEntity.id)
createPlayer(phase, mafia, player, user, gameResult)
}

mafiaGameResultJpaRepository.save(gameResult)
}

mafiaGameResultJpaRepository.save(entity)
private fun createPlayer(
phase: MafiaPhase.End,
mafia: MafiaPlayer,
player: MafiaPlayer,
user: UserJpaEntity,
gameResult: MafiaGameResultJpaEntity,
) = if (phase.isMafiaWin) {
if (mafia.userId == player.userId) {
PlayerHistoryJpaEntity.of(ResultType.MAFIA_WIN, RoleType.MAFIA, user, gameResult)
} else {
PlayerHistoryJpaEntity.of(ResultType.CITIZEN_LOSE, RoleType.CITIZEN, user, gameResult)
}
} else {
if (mafia.userId == player.userId) {
PlayerHistoryJpaEntity.of(ResultType.MAFIA_LOSE, RoleType.MAFIA, user, gameResult)
} else {
PlayerHistoryJpaEntity.of(ResultType.CITIZEN_WIN, RoleType.CITIZEN, user, gameResult)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.xorker.draw.mafia

import com.xorker.draw.BaseJpaEntity
import com.xorker.draw.player.PlayerHistoryJpaEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.OneToMany
import jakarta.persistence.Table
import org.hibernate.annotations.Cascade
import org.hibernate.annotations.CascadeType

@Entity
@Table(name = "mafia_game_result")
Expand All @@ -20,13 +25,45 @@ internal class MafiaGameResultJpaEntity : BaseJpaEntity() {
lateinit var gameResult: String
protected set

@Column(name = "language", columnDefinition = "varchar(20)")
lateinit var language: String
protected set

@Column(name = "draw", columnDefinition = "TEXT")
lateinit var draw: String
protected set

@Column(name = "mafia_answer", columnDefinition = "varchar(50)")
var mafiaAnswer: String? = null
protected set

@Column(name = "is_random_matching", columnDefinition = "boolean")
var isRandomMatching: Boolean = false
protected set

@Column(name = "word_id", columnDefinition = "bigint")
var wordId: Long = 0
protected set

@OneToMany(mappedBy = "mafiaGameResult", fetch = FetchType.LAZY)
@Cascade(CascadeType.PERSIST)
val players: MutableList<PlayerHistoryJpaEntity> = mutableListOf()

fun removePlayer(player: PlayerHistoryJpaEntity) {
players.remove(player)
}

fun addPlayer(player: PlayerHistoryJpaEntity) {
players.add(player)
}

companion object {
internal fun of(gameResult: String, wordId: Long): MafiaGameResultJpaEntity {
internal fun of(locale: String, draw: String, mafiaAnswer: String?, isRandomMatching: Boolean, wordId: Long): MafiaGameResultJpaEntity {
return MafiaGameResultJpaEntity().apply {
this.gameResult = gameResult
this.language = locale
this.draw = draw
this.mafiaAnswer = mafiaAnswer
this.isRandomMatching = isRandomMatching
this.wordId = wordId
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.xorker.draw.player

import com.xorker.draw.BaseJpaEntity
import com.xorker.draw.mafia.MafiaGameResultJpaEntity
import com.xorker.draw.user.UserJpaEntity
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table

@Entity
@Table(name = "player_history")
internal class PlayerHistoryJpaEntity : BaseJpaEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "player_id", nullable = false)
var id: Long = 0
protected set

@Enumerated(EnumType.STRING)
@Column(name = "result", columnDefinition = "varchar(20)")
lateinit var result: ResultType
protected set

@Enumerated(EnumType.STRING)
@Column(name = "role", columnDefinition = "varchar(20)")
lateinit var role: RoleType
protected set

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
lateinit var user: UserJpaEntity

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mafia_game_result_id")
lateinit var mafiaGameResult: MafiaGameResultJpaEntity

fun addMafiaGameResult(gameResult: MafiaGameResultJpaEntity) {
mafiaGameResult = gameResult
gameResult.addPlayer(this)
}

companion object {
fun of(result: ResultType, role: RoleType, user: UserJpaEntity, gameResult: MafiaGameResultJpaEntity): PlayerHistoryJpaEntity {
val player = PlayerHistoryJpaEntity().apply {
this.result = result
this.role = role
this.user = user
}
player.addMafiaGameResult(gameResult)
return player
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.xorker.draw.player

import org.springframework.data.jpa.repository.JpaRepository

internal interface PlayerHistoryJpaRepository : JpaRepository<PlayerHistoryJpaEntity, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
alter table mafia_game_result
modify column game_result text NULL;
alter table mafia_game_result
add column language varchar(20) NOT NULL;
alter table mafia_game_result
add column draw text NOT NULL;
alter table mafia_game_result
add column mafia_answer varchar(50) NULL;

create table player
(
player_id bigint NOT NULL AUTO_INCREMENT COMMENT 'PK',
result varchar(20) NOT NULL COMMENT '게임 승리 여부',
role varchar(20) NOT NULL COMMENT '플레이어 역할',
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
user_id bigint NOT NULL COMMENT 'FK - Users',
mafia_game_result_id bigint NOT NULL COMMENT 'FK - MafiaGameResult',
PRIMARY KEY PK_player (player_id),
KEY IDX_createdat (created_at),
KEY IDX_updatedat (updated_at)
) COMMENT '플레이어 정보';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
rename table player TO player_history;

alter table mafia_game_result
modify column language varchar(20) NOT NULL COMMENT '게임 방 언어';
alter table mafia_game_result
modify column draw text NOT NULL COMMENT '그림 데이터';
alter table mafia_game_result
modify column mafia_answer varchar(50) NULL COMMENT '마피아 입력 단어';

alter table player_history
add column is_random_matching boolean NULL COMMENT '마피아 입력 단어';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table player_history
drop column is_random_matching;

alter table mafia_game_result
add column is_random_matching boolean NULL COMMENT '마피아 입력 단어';
11 changes: 11 additions & 0 deletions domain/src/main/kotlin/com/xorker/draw/player/ResultType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xorker.draw.player

enum class ResultType(
val description: String,
) {
MAFIA_WIN("마피아 승리"),
MAFIA_LOSE("마피아 패배"),
CITIZEN_WIN("시민 승리"),
CITIZEN_LOSE("시민 패배"),
;
}
9 changes: 9 additions & 0 deletions domain/src/main/kotlin/com/xorker/draw/player/RoleType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.xorker.draw.player

enum class RoleType(
val description: String,
) {
MAFIA("마피아"),
CITIZEN("시민"),
;
}

0 comments on commit 985b51c

Please sign in to comment.