Skip to content

Commit

Permalink
Merge pull request #56 from YAPP-Github/feature/DRAW-217
Browse files Browse the repository at this point in the history
DRAW-217 플레이 중인 방에 모두 나가면 해당 방 삭제
  • Loading branch information
comforest authored Aug 13, 2024
2 parents 790331c + 5aabe80 commit 5c18d90
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 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 { removePlayer(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 @@ -25,7 +25,6 @@ import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseVoteMessag
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseWaitBody
import com.xorker.draw.websocket.message.response.dto.phase.MafiaPhaseWaitMessage
import com.xorker.draw.websocket.message.response.dto.phase.toResponse
import java.time.LocalDateTime
import org.springframework.stereotype.Component

@Component
Expand Down Expand Up @@ -64,7 +63,7 @@ internal class MafiaPhaseMessengerImpl(
MafiaPhasePlayingBody(
round = phase.round,
turn = phase.turn,
startTurnTime = LocalDateTime.now(), // TOOD: 턴 시스템 도입 시 수정
startTurnTime = phase.job.startTime,
draw = phase.getDraw(),
currentDraw = phase.getCurrentDraw(),
mafiaGameInfo = generateMafiaGameInfoMessage(phase.mafiaPlayer, phase.turnList, phase.keyword, gameOption),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ internal class MafiaGameRoomService(
val player = gameInfo.findPlayer(session.user.id) ?: return

player.disconnect()
mafiaGameRepository.saveGameInfo(gameInfo)
mafiaGameMessenger.broadcastPlayerList(gameInfo)

if (gameInfo.room.players.all { it.isConnect().not() }) {
mafiaGameRepository.removeGameInfo(gameInfo)
} else {
mafiaGameRepository.saveGameInfo(gameInfo)
mafiaGameMessenger.broadcastPlayerList(gameInfo)
}
}

override fun exitSession(session: Session) {
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 5c18d90

Please sign in to comment.