circe codec derivation to models

This commit is contained in:
efim
2023-04-23 15:00:00 +04:00
parent c6bfdacd1d
commit df35f09b71
7 changed files with 148 additions and 48 deletions

View File

@@ -1,6 +1,7 @@
package industries.sunshine.planningpoker.common
import java.util.UUID
import io.circe._
object Models {
@@ -20,58 +21,41 @@ object Models {
allowedCards: List[String],
round: RoundState,
canCloseRound: Boolean = false
) {
) derives Codec.AsObject {
def playersCount: Int = players.size
}
object RoomStateView {
val me = Player("wormy", PlayerID(1))
val testRoom = {
val pony = Player("pony", PlayerID(2))
RoomStateView(
players = List(me, Player("birdy", PlayerID(3)), pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = VotingRound(myCard = Some("s"), alreadyVoted = List(me, pony)),
canCloseRound = true
)
}
val testOpenedRoom = {
val pony = Player("pony", PlayerID(10))
val birdy = Player("birdy", PlayerID(11))
val horsey = Player("horsey", PlayerID(12))
RoomStateView(
players = List(me, birdy, pony, horsey),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = ViewingRound(
Map(me.id -> "xs", pony.id -> "l", birdy.id -> "s", horsey.id -> "m")
),
canCloseRound = true
)
}
given Encoder[Map[PlayerID, String]] = Encoder.encodeMap[PlayerID, String]
given Decoder[Map[PlayerID, String]] = Decoder.decodeMap[PlayerID, String]
given Codec[Map[PlayerID, String]] = Codec.from(summon[Decoder[Map[PlayerID, String]]], summon[Encoder[Map[PlayerID, String]]])
}
trait RoundState
enum RoundState derives Codec.AsObject:
/** view state for round before votes are open player can know their vote and
* who of the other players have voted
*/
final case class VotingRound(
case Voting(
myCard: Option[String],
alreadyVoted: List[Player]
) extends RoundState
)
/** view state for round after opening the votes
*/
final case class ViewingRound(
case Viewing(
votes: Map[PlayerID, String]
) extends RoundState
)
final case class PlayerID(id: Long)
final case class Player(name: String, id: PlayerID)
final case class PlayerID(id: Long) derives Codec.AsObject
object PlayerID {
given KeyEncoder[PlayerID] with
def apply(key: PlayerID): String = key.toString
given KeyDecoder[PlayerID] with
def apply(key: String): Option[PlayerID] = key.toLongOption.map(PlayerID.apply(_))
}
final case class RoomID(id: Long)
final case class Player(name: String, id: PlayerID) derives Codec.AsObject
final case class RoomID(id: Long) derives Codec.AsObject
}

View File

@@ -1,5 +1,8 @@
package industries.sunshine.planningpoker
import io.circe.generic.semiauto._
import io.circe._
object Requests {
final case class LogIn(roomName: String, nickname: String, password: String)
final case class LogIn(roomName: String, nickname: String, password: String) derives Codec.AsObject
}

View File

@@ -0,0 +1,106 @@
package industries.sunshine.planningpoker
import industries.sunshine.planningpoker.common.Models.*
object TestModels {
val me = Player("wormy", PlayerID(1))
val pony = Player("pony", PlayerID(10))
val birdy = Player("birdy", PlayerID(11))
val horsey = Player("horsey", PlayerID(12))
val testRoom = {
RoomStateView(
players = List(me, birdy, pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = Some("s"), alreadyVoted = List(me, pony)),
canCloseRound = true
)
}
val testOpenedRoom = {
RoomStateView(
players = List(me, birdy, pony, horsey),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Viewing(
Map(me.id -> "xs", pony.id -> "l", birdy.id -> "s", horsey.id -> "m")
),
canCloseRound = true
)
}
val testChangesList = List(
RoomStateView(
players = List(me),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = None, alreadyVoted = List.empty),
canCloseRound = true
),
RoomStateView(
players = List(me, pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = None, alreadyVoted = List.empty),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = None, alreadyVoted = List.empty),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = None, alreadyVoted = List(birdy)),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = Some("m"), alreadyVoted = List(birdy)),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = Some("m"), alreadyVoted = List(birdy)),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony, horsey),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = Some("m"), alreadyVoted = List(birdy)),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony, horsey),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = Some("m"), alreadyVoted = List(birdy, horsey)),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony, horsey),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Voting(myCard = Some("m"), alreadyVoted = List(birdy, horsey, pony)),
canCloseRound = true
),
RoomStateView(
players = List(me, birdy, pony, horsey),
me = me.id,
allowedCards = List("xs", "s", "m", "l", "xl"),
round = RoundState.Viewing(
Map(me.id -> "m", pony.id -> "l", birdy.id -> "s", horsey.id -> "m")
),
canCloseRound = true
)
)
}