feat: BAC-30 Implement Jackson Mapping via DTOs (#102)
Reviewed-on: #102 Co-authored-by: Janis <janis.e.20@gmx.de> Co-committed-by: Janis <janis.e.20@gmx.de>
This commit is contained in:
23
knockoutwhistweb/app/dto/subDTO/CardDTO.scala
Normal file
23
knockoutwhistweb/app/dto/subDTO/CardDTO.scala
Normal file
@@ -0,0 +1,23 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.cards.Card
|
||||
import util.WebUIUtils
|
||||
|
||||
case class CardDTO(identifier: String, path: String, idx: Int) {
|
||||
|
||||
def toCard: Card = {
|
||||
WebUIUtils.stringToCard(identifier)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object CardDTO {
|
||||
|
||||
def apply(card: Card, index: Int = 0): CardDTO = {
|
||||
CardDTO(
|
||||
identifier = WebUIUtils.cardtoString(card),
|
||||
path = WebUIUtils.cardToPath(card),
|
||||
idx = index
|
||||
)
|
||||
}
|
||||
}
|
||||
15
knockoutwhistweb/app/dto/subDTO/HandDTO.scala
Normal file
15
knockoutwhistweb/app/dto/subDTO/HandDTO.scala
Normal file
@@ -0,0 +1,15 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.cards.Hand
|
||||
|
||||
case class HandDTO(card: List[CardDTO])
|
||||
|
||||
object HandDTO {
|
||||
|
||||
def apply(hand: Hand): HandDTO = {
|
||||
HandDTO(
|
||||
card = hand.cards.zipWithIndex.map { case (card, idx) => CardDTO(card, idx) }
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
17
knockoutwhistweb/app/dto/subDTO/PlayerDTO.scala
Normal file
17
knockoutwhistweb/app/dto/subDTO/PlayerDTO.scala
Normal file
@@ -0,0 +1,17 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.player.AbstractPlayer
|
||||
|
||||
case class PlayerDTO(id: String, name: String, dogLife: Boolean)
|
||||
|
||||
object PlayerDTO {
|
||||
|
||||
def apply(player: AbstractPlayer): PlayerDTO = {
|
||||
PlayerDTO(
|
||||
id = player.id.toString,
|
||||
name = player.name,
|
||||
dogLife = player.isInDogLife
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
19
knockoutwhistweb/app/dto/subDTO/PlayerQueueDTO.scala
Normal file
19
knockoutwhistweb/app/dto/subDTO/PlayerQueueDTO.scala
Normal file
@@ -0,0 +1,19 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.control.GameLogic
|
||||
|
||||
case class PlayerQueueDTO(currentPlayer: Option[PlayerDTO], queue: Seq[PlayerDTO])
|
||||
|
||||
object PlayerQueueDTO {
|
||||
|
||||
def apply(logic: GameLogic): PlayerQueueDTO = {
|
||||
val currentPlayerDTO = logic.getCurrentPlayer.map(PlayerDTO(_))
|
||||
val queueDTO = logic.getPlayerQueue.map(_.duplicate().flatMap(player => Some(PlayerDTO(player))).toSeq)
|
||||
if (queueDTO.isEmpty) {
|
||||
PlayerQueueDTO(currentPlayerDTO, Seq.empty)
|
||||
} else {
|
||||
PlayerQueueDTO(currentPlayerDTO, queueDTO.get)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
47
knockoutwhistweb/app/dto/subDTO/PodiumPlayerDTO.scala
Normal file
47
knockoutwhistweb/app/dto/subDTO/PodiumPlayerDTO.scala
Normal file
@@ -0,0 +1,47 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.control.GameLogic
|
||||
import de.knockoutwhist.player.AbstractPlayer
|
||||
import de.knockoutwhist.rounds.Match
|
||||
|
||||
case class PodiumPlayerDTO(
|
||||
player: PlayerDTO,
|
||||
position: Int,
|
||||
roundsWon: Int,
|
||||
tricksWon: Int
|
||||
)
|
||||
|
||||
object PodiumPlayerDTO {
|
||||
|
||||
def apply(gameLogic: GameLogic, player: AbstractPlayer): PodiumPlayerDTO = {
|
||||
val matchImplOpt = gameLogic.getCurrentMatch
|
||||
if (matchImplOpt.isEmpty) {
|
||||
throw new IllegalStateException("No current match available in game logic")
|
||||
}
|
||||
val matchImpl: Match = matchImplOpt.get
|
||||
var roundsWon = 0
|
||||
var tricksWon = 0
|
||||
for (round <- matchImpl.roundlist) {
|
||||
if (round.winner.contains(player)) {
|
||||
roundsWon += 1
|
||||
}
|
||||
for (trick <- round.tricklist) {
|
||||
if (trick.winner.contains(player)) {
|
||||
tricksWon += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PodiumPlayerDTO(
|
||||
player = PlayerDTO(player),
|
||||
position = if (gameLogic.getWinner.contains(player)) {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
},
|
||||
roundsWon = roundsWon,
|
||||
tricksWon = tricksWon
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
18
knockoutwhistweb/app/dto/subDTO/RoundDTO.scala
Normal file
18
knockoutwhistweb/app/dto/subDTO/RoundDTO.scala
Normal file
@@ -0,0 +1,18 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.cards.Card
|
||||
import de.knockoutwhist.cards.CardValue.Ace
|
||||
|
||||
case class RoundDTO(trumpSuit: CardDTO, firstRound: Boolean, tricklist: List[TrickDTO])
|
||||
|
||||
object RoundDTO {
|
||||
|
||||
def apply(round: de.knockoutwhist.rounds.Round): RoundDTO = {
|
||||
RoundDTO(
|
||||
trumpSuit = CardDTO(Card(Ace, round.trumpSuit)),
|
||||
firstRound = round.firstRound,
|
||||
tricklist = round.tricklist.map(trick => TrickDTO(trick))
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
17
knockoutwhistweb/app/dto/subDTO/TrickDTO.scala
Normal file
17
knockoutwhistweb/app/dto/subDTO/TrickDTO.scala
Normal file
@@ -0,0 +1,17 @@
|
||||
package dto.subDTO
|
||||
|
||||
import de.knockoutwhist.rounds.Trick
|
||||
|
||||
case class TrickDTO(cards: Map[PlayerDTO, CardDTO], firstCard: Option[CardDTO], winner: Option[PlayerDTO])
|
||||
|
||||
object TrickDTO {
|
||||
|
||||
def apply(trick: Trick): TrickDTO = {
|
||||
TrickDTO(
|
||||
cards = trick.cards.map { case (card, player) => PlayerDTO(player) -> CardDTO(card) },
|
||||
firstCard = trick.firstCard.map(card => CardDTO(card)),
|
||||
winner = trick.winner.map(player => PlayerDTO(player))
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
17
knockoutwhistweb/app/dto/subDTO/UserDTO.scala
Normal file
17
knockoutwhistweb/app/dto/subDTO/UserDTO.scala
Normal file
@@ -0,0 +1,17 @@
|
||||
package dto.subDTO
|
||||
|
||||
import model.sessions.UserSession
|
||||
|
||||
case class UserDTO(id: String, username: String, host: Boolean = false)
|
||||
|
||||
object UserDTO {
|
||||
|
||||
def apply(user: UserSession): UserDTO = {
|
||||
UserDTO(
|
||||
id = user.id.toString,
|
||||
username = user.name,
|
||||
host = user.host
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user