Compare commits

...

6 Commits
4.8.0 ... 4.9.1

Author SHA1 Message Date
TeamCity
bf6ffeadb0 ci: bump version to v4.9.1 2025-12-10 08:46:31 +00:00
fa3d21e303 fix: FRO-29 Websocket Communication (#104)
Reviewed-on: #104
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2025-12-10 09:42:50 +01:00
TeamCity
33efc4e107 ci: bump version to v4.9.0 2025-12-06 09:19:38 +00:00
8d697fd311 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>
2025-12-06 10:17:04 +01:00
TeamCity
b9e60b5d4a ci: bump version to v4.8.1 2025-12-05 18:27:14 +00:00
270f44cc1f fix: BAC-29 Implement Mappers for Common Classes (#101)
Reviewed-on: #101
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
2025-12-05 19:24:10 +01:00
20 changed files with 417 additions and 22 deletions

View File

@@ -209,3 +209,18 @@
### Features
* FRO-3 FRO-4 Added vue compontents to ingame and lobby ([#100](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/100)) ([194df56](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/194df5691ccda1c21ebe9157c4396a4a21aa921d))
## (2025-12-05)
### Bug Fixes
* BAC-29 Implement Mappers for Common Classes ([#101](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/101)) ([270f44c](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/270f44cc1f3447ffcc33fb19a47c52391c69972b))
## (2025-12-06)
### Features
* BAC-30 Implement Jackson Mapping via DTOs ([#102](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/102)) ([8d697fd](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/8d697fd311478cf792b4631377de4522ecbda9f7))
## (2025-12-10)
### Bug Fixes
* FRO-29 Websocket Communication ([#104](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/issues/104)) ([fa3d21e](https://git.janis-eccarius.de/KnockOutWhist/KnockOutWhist-Web/commit/fa3d21e3038eb07369764850a9ad9badd269ac57))

View File

@@ -0,0 +1,35 @@
package dto
import dto.subDTO.*
import logic.game.GameLobby
import model.users.User
import scala.util.Try
case class GameInfoDTO(
gameId: String,
self: Option[PlayerDTO],
hand: Option[HandDTO],
playerQueue: PlayerQueueDTO,
currentTrick: Option[TrickDTO],
currentRound: Option[RoundDTO]
)
object GameInfoDTO {
def apply(lobby: GameLobby, user: User): GameInfoDTO = {
val selfPlayer = Try {
Some(lobby.getPlayerByUser(user))
}.getOrElse(None)
GameInfoDTO(
gameId = lobby.id,
self = selfPlayer.map(PlayerDTO(_)),
hand = selfPlayer.flatMap(_.currentHand()).map(HandDTO(_)),
playerQueue = PlayerQueueDTO(lobby.logic),
currentTrick = lobby.logic.getCurrentTrick.map(TrickDTO(_)),
currentRound = lobby.logic.getCurrentRound.map(RoundDTO(_))
)
}
}

View File

@@ -0,0 +1,22 @@
package dto
import dto.subDTO.UserDTO
import logic.game.GameLobby
import model.users.User
case class LobbyInfoDTO(gameId: String, users: List[UserDTO], self: UserDTO, maxPlayers: Int)
object LobbyInfoDTO {
def apply(lobby: GameLobby, user: User): LobbyInfoDTO = {
val session = lobby.getUserSession(user.id)
LobbyInfoDTO(
gameId = lobby.id,
users = lobby.getPlayers.values.map(user => UserDTO(user)).toList,
self = UserDTO(session),
maxPlayers = lobby.maxPlayers,
)
}
}

View File

@@ -0,0 +1,27 @@
package dto
import dto.subDTO.PlayerDTO
import logic.game.GameLobby
import model.users.User
import scala.util.Try
case class TieInfoDTO(gameId: String, currentPlayer: Option[PlayerDTO], self: Option[PlayerDTO], tiedPlayers: Seq[PlayerDTO], highestAmount: Int)
object TieInfoDTO {
def apply(lobby: GameLobby, user: User): TieInfoDTO = {
val selfPlayer = Try {
Some(lobby.getPlayerByUser(user))
}.getOrElse(None)
TieInfoDTO(
gameId = lobby.id,
currentPlayer = lobby.logic.playerTieLogic.currentTiePlayer().map(PlayerDTO.apply),
self = selfPlayer.map(PlayerDTO.apply),
tiedPlayers = lobby.logic.playerTieLogic.getTiedPlayers.map(PlayerDTO.apply),
highestAmount = lobby.logic.playerTieLogic.highestAllowedNumber()
)
}
}

View File

@@ -0,0 +1,31 @@
package dto
import dto.subDTO.{HandDTO, PlayerDTO}
import logic.game.GameLobby
import model.users.User
import scala.util.Try
case class TrumpInfoDTO(
gameId: String,
chooser: Option[PlayerDTO],
self: Option[PlayerDTO],
selfHand: Option[HandDTO],
)
object TrumpInfoDTO {
def apply(lobby: GameLobby, user: User): TrumpInfoDTO = {
val selfPlayer = Try {
Some(lobby.getPlayerByUser(user))
}.getOrElse(None)
TrumpInfoDTO(
gameId = lobby.id,
chooser = lobby.logic.getTrumpPlayer.map(PlayerDTO(_)),
self = selfPlayer.map(PlayerDTO(_)),
selfHand = selfPlayer.flatMap(_.currentHand()).map(HandDTO(_))
)
}
}

View File

@@ -0,0 +1,34 @@
package dto
import dto.subDTO.PodiumPlayerDTO
import logic.game.GameLobby
import model.users.User
case class WonInfoDTO(
gameId: String,
winner: Option[PodiumPlayerDTO],
allPlayers: Seq[PodiumPlayerDTO]
)
object WonInfoDTO {
def apply(lobby: GameLobby, user: User): WonInfoDTO = {
val matchImpl = lobby.logic.getCurrentMatch
if (matchImpl.isEmpty) {
throw new IllegalStateException("No current match available in game logic")
}
val allPlayersDTO: Seq[PodiumPlayerDTO] = matchImpl.get.totalplayers.map { player =>
PodiumPlayerDTO(lobby.logic, player)
}
val selfPlayerDTO = lobby.getPlayerByUser(user)
val winnerDTO = lobby.logic.getWinner
WonInfoDTO(
gameId = lobby.id,
winner = winnerDTO.map(player => PodiumPlayerDTO(lobby.logic, player)),
allPlayers = allPlayersDTO
)
}
}

View File

@@ -0,0 +1,31 @@
package dto.subDTO
import de.knockoutwhist.cards.Card
import util.WebUIUtils
case class CardDTO(identifier: String, path: String, idx: Option[Int]) {
def toCard: Card = {
WebUIUtils.stringToCard(identifier)
}
}
object CardDTO {
def apply(card: Card, index: Int): CardDTO = {
CardDTO(
identifier = WebUIUtils.cardtoString(card),
path = WebUIUtils.cardToPath(card),
idx = Some(index)
)
}
def apply(card: Card): CardDTO = {
CardDTO(
identifier = WebUIUtils.cardtoString(card),
path = WebUIUtils.cardToPath(card),
idx = None
)
}
}

View 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) }
)
}
}

View 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
)
}
}

View 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)
}
}
}

View 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
)
}
}

View 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))
)
}
}

View File

@@ -0,0 +1,17 @@
package dto.subDTO
import de.knockoutwhist.rounds.Trick
case class TrickDTO(cards: Map[String, CardDTO], firstCard: Option[CardDTO], winner: Option[PlayerDTO])
object TrickDTO {
def apply(trick: Trick): TrickDTO = {
TrickDTO(
cards = trick.cards.map { case (card, player) => player.id.toString -> CardDTO(card) },
firstCard = trick.firstCard.map(card => CardDTO(card)),
winner = trick.winner.map(player => PlayerDTO(player))
)
}
}

View 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
)
}
}

View File

@@ -9,10 +9,14 @@ import scalafx.scene.image.Image
object WebUIUtils {
def cardtoImage(card: Card): Html = {
views.html.render.card.apply(f"images/cards/${cardtoString(card)}.png")(card.toString)
views.html.render.card.apply(cardToPath(card))(card.toString)
}
def cardToPath(card: Card): String = {
f"images/cards/${cardtoString(card)}.png"
}
def cardtoString(card: Card) = {
def cardtoString(card: Card): String = {
val s = card.suit match {
case Spades => "S"
case Hearts => "H"
@@ -36,6 +40,31 @@ object WebUIUtils {
}
f"$cv$s"
}
def stringToCard(cardStr: String): Card = {
val cv = cardStr.charAt(0) match {
case 'A' => Ace
case 'K' => King
case 'Q' => Queen
case 'J' => Jack
case 'T' => Ten
case '9' => Nine
case '8' => Eight
case '7' => Seven
case '6' => Six
case '5' => Five
case '4' => Four
case '3' => Three
case '2' => Two
}
val s = cardStr.charAt(1) match {
case 'S' => Spades
case 'H' => Hearts
case 'C' => Clubs
case 'D' => Diamonds
}
Card(cv, s)
}
/**
* Map a Hand to a JsArray of cards

View File

@@ -1,25 +1,37 @@
package util
import de.knockoutwhist.control.GameState
import de.knockoutwhist.control.GameState.{FinishedMatch, InGame, Lobby, SelectTrump, TieBreak}
import de.knockoutwhist.utils.events.SimpleEvent
import dto.subDTO.{CardDTO, HandDTO, PlayerDTO, PlayerQueueDTO, PodiumPlayerDTO, RoundDTO, TrickDTO, UserDTO}
import dto.{GameInfoDTO, LobbyInfoDTO, TieInfoDTO, TrumpInfoDTO, WonInfoDTO}
import model.sessions.UserSession
import play.api.libs.json.{JsValue, Json}
import play.api.libs.json.{JsValue, Json, OFormat}
import tools.jackson.databind.json.JsonMapper
import tools.jackson.module.scala.ScalaModule
import util.mapper.*
object WebsocketEventMapper {
private val scalaModule = ScalaModule.builder()
.addAllBuiltinModules()
.supportScala3Classes(true)
.build()
implicit val cardFormat: OFormat[CardDTO] = Json.format[CardDTO]
implicit val handFormat: OFormat[HandDTO] = Json.format[HandDTO]
implicit val playerFormat: OFormat[PlayerDTO] = Json.format[PlayerDTO]
implicit val queueFormat: OFormat[PlayerQueueDTO] = Json.format[PlayerQueueDTO]
implicit val podiumPlayerFormat: OFormat[PodiumPlayerDTO] = Json.format[PodiumPlayerDTO]
implicit val roundFormat: OFormat[RoundDTO] = Json.format[RoundDTO]
implicit val trickFormat: OFormat[TrickDTO] = Json.format[TrickDTO]
implicit val userFormat: OFormat[UserDTO] = Json.format[UserDTO]
private val mapper = JsonMapper.builder().addModule(scalaModule).build()
private var customMappers: Map[String,SimpleEventMapper[SimpleEvent]] = Map()
implicit val gameInfoDTOFormat: OFormat[GameInfoDTO] = Json.format[GameInfoDTO]
implicit val lobbyFormat: OFormat[LobbyInfoDTO] = Json.format[LobbyInfoDTO]
implicit val tieInfoFormat: OFormat[TieInfoDTO] = Json.format[TieInfoDTO]
implicit val trumpInfoFormat: OFormat[TrumpInfoDTO] = Json.format[TrumpInfoDTO]
implicit val wonInfoDTOFormat: OFormat[WonInfoDTO] = Json.format[WonInfoDTO]
private var specialMappers: Map[String,SimpleEventMapper[SimpleEvent]] = Map()
private def registerCustomMapper[T <: SimpleEvent](mapper: SimpleEventMapper[T]): Unit = {
customMappers = customMappers + (mapper.id -> mapper.asInstanceOf[SimpleEventMapper[SimpleEvent]])
specialMappers = specialMappers + (mapper.id -> mapper.asInstanceOf[SimpleEventMapper[SimpleEvent]])
}
// Register all custom mappers here
@@ -37,19 +49,28 @@ object WebsocketEventMapper {
registerCustomMapper(TurnEventMapper)
def toJson(obj: SimpleEvent, session: UserSession): JsValue = {
val data: Option[JsValue] = if (customMappers.contains(obj.id)) {
Some(customMappers(obj.id).toJson(obj, session))
val data: Option[JsValue] = if (specialMappers.contains(obj.id)) {
Some(specialMappers(obj.id).toJson(obj, session))
}else {
None
}
if (data.isEmpty) {
return Json.obj()
}
Json.obj(
"id" -> ("request-" + java.util.UUID.randomUUID().toString),
"event" -> obj.id,
"state" -> toJson(session),
"data" -> data
)
}
def toJson(session: UserSession): JsValue = {
session.gameLobby.getLogic.getCurrentState match {
case Lobby => Json.toJson(LobbyInfoDTO(session.gameLobby, session.user))
case InGame => Json.toJson(GameInfoDTO(session.gameLobby, session.user))
case SelectTrump => Json.toJson(TrumpInfoDTO(session.gameLobby, session.user))
case TieBreak => Json.toJson(TieInfoDTO(session.gameLobby, session.user))
case FinishedMatch => Json.toJson(WonInfoDTO(session.gameLobby, session.user))
case _ => Json.obj()
}
}
}

View File

@@ -20,7 +20,7 @@
</p>
</div>
@if(player.equals(gamelobby.logic.playerTieLogic.currentTiePlayer())) {
@if(gamelobby.logic.playerTieLogic.currentTiePlayer().contains(player)) {
@defining(gamelobby.logic.playerTieLogic.highestAllowedNumber()) { maxNum =>
<div class="alert alert-info" role="alert" aria-live="polite">
Pick a number between 1 and @{
@@ -71,7 +71,7 @@
}
} else {
<div class="alert alert-warning" role="alert" aria-live="polite">
<strong>@gamelobby.logic.playerTieLogic.currentTiePlayer()</strong>
<strong>@gamelobby.logic.playerTieLogic.currentTiePlayer().get</strong>
is currently picking a number for the cut.
</div>

View File

@@ -1,3 +1,3 @@
MAJOR=4
MINOR=8
PATCH=0
MINOR=9
PATCH=1