module Rattletrap.Type.Frame where

import qualified Data.Map as Map
import qualified Rattletrap.BitGet as BitGet
import qualified Rattletrap.BitPut as BitPut
import qualified Rattletrap.Schema as Schema
import qualified Rattletrap.Type.ClassAttributeMap as ClassAttributeMap
import qualified Rattletrap.Type.CompressedWord as CompressedWord
import qualified Rattletrap.Type.F32 as F32
import qualified Rattletrap.Type.List as List
import qualified Rattletrap.Type.Replication as Replication
import qualified Rattletrap.Type.Str as Str
import qualified Rattletrap.Type.U32 as U32
import qualified Rattletrap.Type.Version as Version
import qualified Rattletrap.Utility.Json as Json

data Frame = Frame
  { Frame -> F32
time :: F32.F32
  -- ^ Time in seconds since the beginning of the match.
  , Frame -> F32
delta :: F32.F32
  -- ^ Time in seconds since the last frame. Usually about 0.03 since there
  -- are 30 frames per second.
  , Frame -> List Replication
replications :: List.List Replication.Replication
  }
  deriving (Frame -> Frame -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Frame -> Frame -> Bool
$c/= :: Frame -> Frame -> Bool
== :: Frame -> Frame -> Bool
$c== :: Frame -> Frame -> Bool
Eq, Int -> Frame -> ShowS
[Frame] -> ShowS
Frame -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Frame] -> ShowS
$cshowList :: [Frame] -> ShowS
show :: Frame -> String
$cshow :: Frame -> String
showsPrec :: Int -> Frame -> ShowS
$cshowsPrec :: Int -> Frame -> ShowS
Show)

instance Json.FromJSON Frame where
  parseJSON :: Value -> Parser Frame
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Json.withObject String
"Frame" forall a b. (a -> b) -> a -> b
$ \Object
object -> do
    F32
time <- forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"time"
    F32
delta <- forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"delta"
    List Replication
replications <- forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"replications"
    forall (f :: * -> *) a. Applicative f => a -> f a
pure Frame { F32
time :: F32
time :: F32
time, F32
delta :: F32
delta :: F32
delta, List Replication
replications :: List Replication
replications :: List Replication
replications }

instance Json.ToJSON Frame where
  toJSON :: Frame -> Value
toJSON Frame
x = [(Key, Value)] -> Value
Json.object
    [ forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"time" forall a b. (a -> b) -> a -> b
$ Frame -> F32
time Frame
x
    , forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"delta" forall a b. (a -> b) -> a -> b
$ Frame -> F32
delta Frame
x
    , forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"replications" forall a b. (a -> b) -> a -> b
$ Frame -> List Replication
replications Frame
x
    ]

schema :: Schema.Schema
schema :: Schema
schema = String -> Value -> Schema
Schema.named String
"frame" forall a b. (a -> b) -> a -> b
$ [((Key, Value), Bool)] -> Value
Schema.object
  [ (forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"time" forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
F32.schema, Bool
True)
  , (forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"delta" forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
F32.schema, Bool
True)
  , ( forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"replications" forall b c a. (b -> c) -> (a -> b) -> a -> c
. Schema -> Value
Schema.json forall a b. (a -> b) -> a -> b
$ Schema -> Schema
List.schema Schema
Replication.schema
    , Bool
True
    )
  ]

putFrames :: List.List Frame -> BitPut.BitPut
putFrames :: List Frame -> BitPut
putFrames = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Frame -> BitPut
bitPut forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. List a -> [a]
List.toList

bitPut :: Frame -> BitPut.BitPut
bitPut :: Frame -> BitPut
bitPut Frame
frame =
  F32 -> BitPut
F32.bitPut (Frame -> F32
time Frame
frame)
    forall a. Semigroup a => a -> a -> a
<> F32 -> BitPut
F32.bitPut (Frame -> F32
delta Frame
frame)
    forall a. Semigroup a => a -> a -> a
<> List Replication -> BitPut
Replication.putReplications (Frame -> List Replication
replications Frame
frame)

decodeFramesBits
  :: Maybe Str.Str
  -> Version.Version
  -> Int
  -> Word
  -> ClassAttributeMap.ClassAttributeMap
  -> BitGet.BitGet (List.List Frame)
decodeFramesBits :: Maybe Str
-> Version
-> Int
-> Word
-> ClassAttributeMap
-> BitGet (List Frame)
decodeFramesBits Maybe Str
matchType Version
version Int
count Word
limit ClassAttributeMap
classes =
  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ Maybe Str
-> Version
-> Int
-> Word
-> ClassAttributeMap
-> Map CompressedWord U32
-> Int
-> [Frame]
-> BitGet (Map CompressedWord U32, List Frame)
decodeFramesBitsWith
    Maybe Str
matchType
    Version
version
    Int
count
    Word
limit
    ClassAttributeMap
classes
    forall k a. Map k a
Map.empty
    Int
0
    []

decodeFramesBitsWith
  :: Maybe Str.Str
  -> Version.Version
  -> Int
  -> Word
  -> ClassAttributeMap.ClassAttributeMap
  -> Map.Map CompressedWord.CompressedWord U32.U32
  -> Int
  -> [Frame]
  -> BitGet.BitGet
       ( Map.Map CompressedWord.CompressedWord U32.U32
       , List.List Frame
       )
decodeFramesBitsWith :: Maybe Str
-> Version
-> Int
-> Word
-> ClassAttributeMap
-> Map CompressedWord U32
-> Int
-> [Frame]
-> BitGet (Map CompressedWord U32, List Frame)
decodeFramesBitsWith Maybe Str
matchType Version
version Int
count Word
limit ClassAttributeMap
classes Map CompressedWord U32
actorMap Int
index [Frame]
frames
  = if Int
index forall a. Ord a => a -> a -> Bool
>= Int
count
    then forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map CompressedWord U32
actorMap, forall a. [a] -> List a
List.fromList forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
reverse [Frame]
frames)
    else do
      (Map CompressedWord U32
newActorMap, Frame
frame) <-
        forall a. String -> BitGet a -> BitGet a
BitGet.label (String
"element (" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show Int
index forall a. Semigroup a => a -> a -> a
<> String
")")
          forall a b. (a -> b) -> a -> b
$ Maybe Str
-> Version
-> Word
-> ClassAttributeMap
-> Map CompressedWord U32
-> BitGet (Map CompressedWord U32, Frame)
bitGet Maybe Str
matchType Version
version Word
limit ClassAttributeMap
classes Map CompressedWord U32
actorMap
      Maybe Str
-> Version
-> Int
-> Word
-> ClassAttributeMap
-> Map CompressedWord U32
-> Int
-> [Frame]
-> BitGet (Map CompressedWord U32, List Frame)
decodeFramesBitsWith
          Maybe Str
matchType
          Version
version
          Int
count
          Word
limit
          ClassAttributeMap
classes
          Map CompressedWord U32
newActorMap
          (Int
index forall a. Num a => a -> a -> a
+ Int
1)
        forall a b. (a -> b) -> a -> b
$ Frame
frame
        forall a. a -> [a] -> [a]
: [Frame]
frames

bitGet
  :: Maybe Str.Str
  -> Version.Version
  -> Word
  -> ClassAttributeMap.ClassAttributeMap
  -> Map.Map CompressedWord.CompressedWord U32.U32
  -> BitGet.BitGet
       (Map.Map CompressedWord.CompressedWord U32.U32, Frame)
bitGet :: Maybe Str
-> Version
-> Word
-> ClassAttributeMap
-> Map CompressedWord U32
-> BitGet (Map CompressedWord U32, Frame)
bitGet Maybe Str
matchType Version
version Word
limit ClassAttributeMap
classes Map CompressedWord U32
actorMap = forall a. String -> BitGet a -> BitGet a
BitGet.label String
"Frame" forall a b. (a -> b) -> a -> b
$ do
  F32
time <- forall a. String -> BitGet a -> BitGet a
BitGet.label String
"time" BitGet F32
F32.bitGet
  F32
delta <- forall a. String -> BitGet a -> BitGet a
BitGet.label String
"delta" BitGet F32
F32.bitGet
  (Map CompressedWord U32
newActorMap, List Replication
replications) <-
    forall a. String -> BitGet a -> BitGet a
BitGet.label String
"replications" forall a b. (a -> b) -> a -> b
$ Maybe Str
-> Version
-> Word
-> ClassAttributeMap
-> Map CompressedWord U32
-> BitGet (Map CompressedWord U32, List Replication)
Replication.decodeReplicationsBits
      Maybe Str
matchType
      Version
version
      Word
limit
      ClassAttributeMap
classes
      Map CompressedWord U32
actorMap
  forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map CompressedWord U32
newActorMap, Frame { F32
time :: F32
time :: F32
time, F32
delta :: F32
delta :: F32
delta, List Replication
replications :: List Replication
replications :: List Replication
replications })