Skip to content

Commit

Permalink
DRAW-217 fix: 플레이 중인 방에 모두 나가면 해당 방 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
comforest committed Aug 6, 2024
1 parent f17a4cd commit 94b672e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ internal class MafiaGameAdapter : MafiaGameRepository, RoomRepository {
}

override fun removeGameInfo(gameInfo: MafiaGameInfo) {
gameInfo.room.players
.map { it.userId }
.forEach { userData.remove(it) }

val phase = gameInfo.phase
if (phase is MafiaPhaseWithTimer) {
phase.job.cancel()
}

data.remove(gameInfo.room.id)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MafiaGameMessengerImpl(
val body = MafiaGameTurnInfoBody(
phase.round,
phase.turn,
phase.timerJob.startTime,
phase.job.startTime,
phase.turnList[phase.turn].userId,
phase.drawData.map { it.second },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ internal class MafiaGameRoomService(
val player = gameInfo.findPlayer(session.user.id) ?: return

player.disconnect()

if (gameInfo.room.players.all { it.isConnect().not() }) {
mafiaGameRepository.removeGameInfo(gameInfo)
}
mafiaGameRepository.saveGameInfo(gameInfo)
mafiaGameMessenger.broadcastPlayerList(gameInfo)
}
Expand All @@ -65,7 +69,6 @@ internal class MafiaGameRoomService(

if (gameInfo.room.players.isEmpty()) {
mafiaGameRepository.removeGameInfo(gameInfo)
return
}

if (gameInfo.room.owner == player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class MafiaGameService(
val phase = gameInfo.phase
assertTurn(phase, session.user.id)

phase.timerJob.cancel()
phase.job.cancel()

mafiaPhasePlayGameProcessor.processNextTurn(gameInfo) {
mafiaPhaseService.vote(gameInfo.room.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal class MafiaPhasePlayGameProcessor(
gameOption.turnTime
}

phase.timerJob = timerRepository.startTimer(time) {
phase.job = timerRepository.startTimer(time) {
processNextTurn(gameInfo, nextStep)
}

Expand Down
22 changes: 13 additions & 9 deletions domain/src/main/kotlin/com/xorker/draw/mafia/MafiaPhase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ sealed class MafiaPhase {
data object Wait : MafiaPhase()

data class Ready(
val job: JobWithStartTime,
override val job: JobWithStartTime,
override val turnList: List<MafiaPlayer>,
val mafiaPlayer: MafiaPlayer,
val keyword: MafiaKeyword,
) : MafiaPhase(), MafiaPhaseWithTurnList {
) : MafiaPhase(), MafiaPhaseWithTurnList, MafiaPhaseWithTimer {
fun toPlaying(job: JobWithStartTime): Playing {
return Playing(
turnList = turnList,
mafiaPlayer = mafiaPlayer,
keyword = keyword,
drawData = mutableListOf(),
timerJob = job,
job = job,
)
}
}
Expand All @@ -35,8 +35,8 @@ sealed class MafiaPhase {
val keyword: MafiaKeyword,
var turnInfo: TurnInfo = TurnInfo(),
val drawData: MutableList<Pair<UserId, Map<String, Any>>>,
var timerJob: JobWithStartTime,
) : MafiaPhase(), MafiaPhaseWithTurnList, TurnInfo {
override var job: JobWithStartTime,
) : MafiaPhase(), MafiaPhaseWithTurnList, TurnInfo, MafiaPhaseWithTimer {
override val round: Int
get() = turnInfo.round

Expand Down Expand Up @@ -76,13 +76,13 @@ sealed class MafiaPhase {
}

data class Vote(
val job: JobWithStartTime,
override val job: JobWithStartTime,
val mafiaPlayer: MafiaPlayer,
val keyword: MafiaKeyword,
val drawData: MutableList<Pair<UserId, Map<String, Any>>>,
val players: Map<UserId, Vector<UserId>>,
override val turnList: List<MafiaPlayer>,
) : MafiaPhase(), MafiaPhaseWithTurnList {
) : MafiaPhase(), MafiaPhaseWithTurnList, MafiaPhaseWithTimer {
fun toInferAnswer(job: JobWithStartTime): InferAnswer {
return InferAnswer(
job = job,
Expand All @@ -106,13 +106,13 @@ sealed class MafiaPhase {
}

class InferAnswer(
val job: JobWithStartTime,
override val job: JobWithStartTime,
val mafiaPlayer: MafiaPlayer,
val keyword: MafiaKeyword,
val drawData: MutableList<Pair<UserId, Map<String, Any>>>,
var answer: String? = null,
override val turnList: List<MafiaPlayer>,
) : MafiaPhase(), MafiaPhaseWithTurnList {
) : MafiaPhase(), MafiaPhaseWithTurnList, MafiaPhaseWithTimer {
fun toEnd(job: JobWithStartTime): End {
return End(
job = job,
Expand Down Expand Up @@ -148,6 +148,10 @@ interface MafiaPhaseWithTurnList {
}
}

interface MafiaPhaseWithTimer {
val job: JobWithStartTime
}

@OptIn(ExperimentalContracts::class)
inline fun <reified T : MafiaPhase> assertIs(phase: MafiaPhase) {
contract {
Expand Down

0 comments on commit 94b672e

Please sign in to comment.