-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from TeamTroublePainter/feature/DRAW-404
DRAW-404 마피아 게임 결과 정규화
- Loading branch information
Showing
10 changed files
with
213 additions
and
7 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
adapter/rdb/src/main/kotlin/com/xorker/draw/mafia/DrawData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>>, | ||
) |
48 changes: 43 additions & 5 deletions
48
adapter/rdb/src/main/kotlin/com/xorker/draw/mafia/MafiaGameResultAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
adapter/rdb/src/main/kotlin/com/xorker/draw/player/PlayerHistoryJpaEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
adapter/rdb/src/main/kotlin/com/xorker/draw/player/PlayerHistoryJpaRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
22 changes: 22 additions & 0 deletions
22
adapter/rdb/src/main/resources/db/migration/V6__normalization_mafia_game_result_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '플레이어 정보'; |
11 changes: 11 additions & 0 deletions
11
adapter/rdb/src/main/resources/db/migration/V7__update_player_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '마피아 입력 단어'; |
5 changes: 5 additions & 0 deletions
5
adapter/rdb/src/main/resources/db/migration/V8__update_player_history_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
domain/src/main/kotlin/com/xorker/draw/player/ResultType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("시민 패배"), | ||
; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("시민"), | ||
; | ||
} |