Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAW-221 방 생성 / 참여 & 랜덤 키워드 locale 로직 추가 #59

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.xorker.draw.mafia

import java.util.Locale
import org.springframework.stereotype.Component

@Component
internal class MafiaKeywordAdapter(
private val wordJpaRepository: WordJpaRepository,
) : MafiaKeywordRepository {

override fun getRandomKeyword(locale: Locale): MafiaKeyword {
val word = wordJpaRepository.findRandomWord(locale.language)
override fun getRandomKeyword(language: String): MafiaKeyword {
val word = wordJpaRepository.findRandomWord(language)

return word.toDomain()
}
Expand Down
18 changes: 13 additions & 5 deletions core/src/main/kotlin/com/xorker/draw/mafia/MafiaGameRoomService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xorker.draw.mafia

import com.xorker.draw.exception.InvalidRequestValueException
import com.xorker.draw.exception.NotFoundRoomException
import com.xorker.draw.room.Room
import com.xorker.draw.websocket.Session
Expand All @@ -21,7 +22,7 @@ internal class MafiaGameRoomService(
if (gameInfo == null) {
if (request.roomId != null) throw NotFoundRoomException
val player = MafiaPlayer(userId, request.nickname, generateColor(null))
gameInfo = createGameInfo(session, player)
gameInfo = createGameInfo(session, request.locale, player)
} else {
val player = gameInfo.findPlayer(userId)
if (player != null) {
Expand Down Expand Up @@ -86,17 +87,22 @@ internal class MafiaGameRoomService(
.first()
}

private fun createGameInfo(session: Session, player: MafiaPlayer): MafiaGameInfo {
val room = createRoom(session, player)
private fun createGameInfo(session: Session, locale: String, player: MafiaPlayer): MafiaGameInfo {
val room = createRoom(session, locale, player)
return MafiaGameInfo(
room,
MafiaPhase.Wait,
MafiaGameOption(),
)
}

private fun createRoom(session: Session, player: MafiaPlayer): Room<MafiaPlayer> {
val room = Room(session.roomId, player, 10)
private fun createRoom(session: Session, locale: String, player: MafiaPlayer): Room<MafiaPlayer> {
val language = locale.lowercase()
if (language !in languages) {
throw InvalidRequestValueException
}

val room = Room(session.roomId, language, player, 10)
return room
}

Expand All @@ -113,5 +119,7 @@ internal class MafiaGameRoomService(
"FD66C1",
"7E91A6",
)

private val languages = mutableSetOf("ko", "en")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.xorker.draw.mafia.MafiaKeywordRepository
import com.xorker.draw.mafia.MafiaPhase
import com.xorker.draw.mafia.MafiaPlayer
import com.xorker.draw.timer.TimerRepository
import java.util.*
import org.springframework.stereotype.Component
import kotlin.random.Random

Expand All @@ -23,7 +22,7 @@ internal class MafiaPhaseStartGameProcessor(
val turnList = generateTurnList(players)

val mafiaIndex = Random.nextInt(0, players.size)
val keyword = mafiaKeywordRepository.getRandomKeyword(Locale.KOREAN) // TODO extract room locale
val keyword = mafiaKeywordRepository.getRandomKeyword(room.locale)

val job = timerRepository.startTimer(gameOption.readyTime, nextStep)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class SessionService : SessionUseCase, SessionEventListener {
return userIdMap[userId]
}

override fun connectSession(session: Session, roomId: SessionInitializeRequest) {
override fun connectSession(session: Session, request: SessionInitializeRequest) {
registerSession(session)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ data object UnSupportedException : CriticalException("crt003", "정의하지 않
data object InvalidMafiaGamePlayingPhaseStatusException : CriticalException("crt004", "마피아 게임 Playing 단계에서 유효하지 않은 상태") { private fun readResolve(): Any = InvalidMafiaGamePlayingPhaseStatusException }
data object InvalidBroadcastException : CriticalException("crt005", "유효하지 않은 브로드캐스트 상태") { private fun readResolve(): Any = InvalidBroadcastException }
data object InvalidMafiaGameVotePhaseStatusException : CriticalException("crt006", "마피아 게임 Vote 단계에서 유효하지 않은 상태") { private fun readResolve(): Any = InvalidMafiaGameVotePhaseStatusException }
class InvalidMafiaPhaseException(message: String) : CriticalException("crt004", message)
class InvalidMafiaPhaseException(message: String) : CriticalException("crt007", message)
//endregion
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ interface MafiaGameRepository {
fun saveGameInfo(gameInfo: MafiaGameInfo)
fun removeGameInfo(gameInfo: MafiaGameInfo)
fun getGameInfo(roomId: RoomId): MafiaGameInfo?

fun getGameInfo(userId: UserId): MafiaGameInfo?
fun removePlayer(userId: UserId)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.xorker.draw.mafia

import java.util.Locale

interface MafiaKeywordRepository {
fun getRandomKeyword(locale: Locale): MafiaKeyword
fun getRandomKeyword(language: String): MafiaKeyword
}

This file was deleted.

7 changes: 5 additions & 2 deletions domain/src/main/kotlin/com/xorker/draw/room/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ value class RoomId(val value: String)

interface Room<P : Player> {
val id: RoomId
val locale: String
var owner: P
val maxMemberNum: Int
val players: List<P>
Expand All @@ -26,13 +27,15 @@ interface Room<P : Player> {

fun <P : Player> Room(
id: RoomId,
locale: String,
owner: P,
maxMemberNum: Int,
players: MutableList<P> = mutableListOf(owner),
): Room<P> = RoomImpl(id, owner, maxMemberNum, players)
): Room<P> = RoomImpl(id, locale, owner, maxMemberNum, players)

class RoomImpl<P : Player>(
override val id: RoomId,
override val locale: String,
override var owner: P,
override val maxMemberNum: Int,
private val _players: MutableList<P> = mutableListOf(owner),
Expand Down Expand Up @@ -61,5 +64,5 @@ class RoomImpl<P : Player>(
_players.clear()
}

override fun copy(): Room<P> = RoomImpl(id, owner, maxMemberNum, _players)
override fun copy(): Room<P> = RoomImpl(id, locale, owner, maxMemberNum, _players)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ enum class ResponseAction(
) {
PLAYER_LIST("대기방 갱신"),
GAME_INFO("마피아 게임 정보"),
GAME_READY("마피아 게임 준비"),
PLAYER_TURN_LIST("플레이어 턴 순서"),
DRAW("그림 데이터"),
TURN_INFO("새로운 턴 시작"),
VOTE_STATUS("마피아 투표 현황"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xorker.draw.websocket

interface SessionEventListener {
fun connectSession(session: Session, roomId: SessionInitializeRequest)
fun connectSession(session: Session, request: SessionInitializeRequest)
fun disconnectSession(session: Session)
fun exitSession(session: Session)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ data class SessionInitializeRequest(
val accessToken: String,
val roomId: String?,
val nickname: String,
val locale: String,
)
Loading