Untitled

mail@pastecode.io avatar
unknown
scala
7 months ago
5.0 kB
3
Indexable
Never
 override object viewFormat extends WireFormat[MemoryView]:

    override def encode(v: MemoryView): Value =
      v match
        case MemoryView(stateView, alreadyMatched) =>
          stateView match
            //Playing
            case StateView.Playing(phase, currentPlayer, board) =>  
              Obj(
                //StateView 
                "playing" -> Obj(

                  //PhaseView
                  "phase" -> Str(phase match
                    case PhaseView.SelectingCards => "selecting"
                    case PhaseView.CardsSelected => "selected"
                    case PhaseView.Waiting => "waiting"
                    case PhaseView.GoodMatch => "good"
                    case PhaseView.BadMatch => "bad"
                  ),

                  //CurrentPlayer
                  "currentPlayer" -> Str(
                    currentPlayer
                  ),
                  
                  //Board
                  "board" -> Arr.from(board.map(cardView => cardView match
                    case CardView.Selected => Obj("selected" -> Str("selected"))
                    case CardView.FaceDown => Obj("faceDown" -> Str("faceDown"))
                    case CardView.FaceUp(card) => Obj("faceUp" -> Str(card))
                    case CardView.AlreadyMatched(card) => Obj("alreadyMatched" -> Str(card))
                  ))
                ),

                //AlreadyMatched
                "alreadyMatched" -> Arr(
                  alreadyMatched.map((userId, cards) => 
                    Obj(
                      "userId" -> Str(userId),
                      "cards" -> Arr(cards.map(card => Str(card)))
                    )
                  )
                )
              )
            
            //Finished
            case StateView.Finished(winnerIds) =>
              Obj(
                //StateView
                "finished" -> Arr(winnerIds.map(winnerId => Str(winnerId)))
                ,

                //AlreadyMatched
                "alreadyMatched" -> Arr(
                  alreadyMatched.map((userId, cards) => 
                    Obj(
                      "userId" -> Str(userId),
                      "cards" -> Arr(cards.map(card => Str(card)))
                    )
                  )
                )
              )
          
    override def decode(js: Value): Try[MemoryView] =
      object CardViewWire extends WireFormat[CardView]:
        override def encode(card: CardView): Value =
          card match
            case CardView.Selected => Obj("selected" -> Str("selected"))
            case CardView.FaceDown => Obj("faceDown" -> Str("faceDown"))
            case CardView.FaceUp(card) => Obj("faceUp" -> Str(card))
            case CardView.AlreadyMatched(card) => Obj("alreadyMatched" -> Str(card))
        override def decode(js: Value): Try[CardView] =
          Try {
            js match
              case Obj(value) =>
                if value.contains("selected") then
                  CardView.Selected
                else if value.contains("faceDown") then
                  CardView.FaceDown
                else if value.contains("faceUp") then
                  val card = StringWire.decode(value("faceUp")).get
                  CardView.FaceUp(card)
                else if value.contains("alreadyMatched") then
                  val card = StringWire.decode(value("alreadyMatched")).get
                  CardView.AlreadyMatched(card)
                else
                  throw DecodingException("Invalid CardView")
              case _ => throw DecodingException("Invalid CardView")
          }

      Try {
        js match
          case Obj(value) => 
            if value.contains("playing") then
              //Playing Case
              val playing = value("playing")
              val phase = StringWire.decode(playing("phase")).get match
                case "selecting" => PhaseView.SelectingCards
                case "selected" => PhaseView.CardsSelected
                case "waiting" => PhaseView.Waiting
                case "good" => PhaseView.GoodMatch
                case "bad" => PhaseView.BadMatch
              val currentPlayer = StringWire.decode(playing("currentPlayer")).get
              
              val boardView = SeqWire(CardViewWire).decode(playing("board")).get
              val alreadyMatched = MapWire(StringWire, SeqWire(StringWire)).decode(value("alreadyMatched")).get

              MemoryView(StateView.Playing(phase, currentPlayer, boardView), alreadyMatched)

            else 
              //Finished Case
              val stateView = value("finished").asInstanceOf[Arr].value
              val winnerIds = SetWire(StringWire).decode(stateView).get

              val alreadyMatched = MapWire(StringWire, SeqWire(StringWire)).decode(value("alreadyMatched")).get

              MemoryView(StateView.Finished(winnerIds), alreadyMatched)


          case _ => throw DecodingException("Invalid MemoryView")
      }
Leave a Comment