@@ -8,9 +8,8 @@ import jakarta.websocket.CloseReason.CloseCodes.VIOLATED_POLICY
88import jakarta.websocket.Session
99import pp.api.data.ChangeName
1010import pp.api.data.ChatMessage
11- import pp.api.data.GamePhase
12- import pp.api.data.GamePhase.CARDS_REVEALED
13- import pp.api.data.GamePhase.PLAYING
11+ import pp.api.data.GamePhase.CardsRevealed
12+ import pp.api.data.GamePhase.Playing
1413import pp.api.data.PlayCard
1514import pp.api.data.RevealCards
1615import pp.api.data.Room
@@ -100,8 +99,8 @@ class Rooms {
10099 is ChangeName -> changeName(session, request.name)
101100 is PlayCard -> playCard(session, request.cardValue)
102101 is ChatMessage -> chatMessage(session, request.message)
103- is RevealCards -> changeGamePhase (session, CARDS_REVEALED )
104- is StartNewRound -> changeGamePhase (session, PLAYING )
102+ is RevealCards -> changeGamePhaseToCardsRevealed (session)
103+ is StartNewRound -> changeGamePhaseToPlaying (session)
105104 else -> {
106105 // spotlessApply keeps generating this else if it doesnt exist
107106 }
@@ -179,7 +178,7 @@ class Rooms {
179178
180179 private fun playCard (session : Session , cardValue : String? ) {
181180 get(session)?.let { (room, user) ->
182- if (room.gamePhase == PLAYING ) {
181+ if (room.gamePhase is Playing ) {
183182 if (cardValue != null && cardValue !in room.deck) {
184183 update(room withInfo " ${user.username} tried to play card with illegal value: $cardValue " )
185184 } else {
@@ -200,35 +199,30 @@ class Rooms {
200199 }
201200 }
202201
203- private fun changeGamePhase (session : Session , newGamePhase : GamePhase ) {
202+ private fun changeGamePhaseToPlaying (session : Session ) {
204203 get(session)?.let { (room, user) ->
205- val canRevealCards = room.gamePhase == PLAYING && newGamePhase == CARDS_REVEALED
206- val canStartNextRound = room.gamePhase == CARDS_REVEALED && newGamePhase == PLAYING
207-
208- if (canRevealCards || canStartNextRound) {
204+ if (room.gamePhase is CardsRevealed ) {
209205 val updatedRoom = room.run {
210206 copy(
211- gamePhase = newGamePhase,
212- users = if (newGamePhase == CARDS_REVEALED ) {
213- users
214- } else {
215- users.map { user ->
216- user.copy(
217- cardValue = null
218- )
219- }
207+ gamePhase = Playing ,
208+ users = users.map { user ->
209+ user.copy(
210+ cardValue = null
211+ )
220212 }
221213 )
222214 }
223- val message = if (newGamePhase == CARDS_REVEALED ) " revealed the cards" else " started a new round"
224- update(updatedRoom withInfo " ${user.username} $message " )
225- } else {
226- val error = " ${user.username} tried to change game phase to $newGamePhase , but that's illegal"
227- update(room withInfo error)
215+ update(updatedRoom withInfo " ${user.username} started a new round" )
228216 }
229217 }
230218 }
231219
220+ private fun changeGamePhaseToCardsRevealed (session : Session ) {
221+ get(session)?.let { (room, user) ->
222+ update(room withCardsRevealedBy user)
223+ }
224+ }
225+
232226 private operator fun get (roomId : String ): Room ? = allRooms.firstOrNull { it.roomId == roomId }
233227 private operator fun get (session : Session ): Pair <Room , User >? = allRooms
234228 .firstOrNull {
0 commit comments