module Rattletrap.Type.ReplicationValue where

import qualified Data.Foldable as Foldable
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.Replication.Destroyed as Destroyed
import qualified Rattletrap.Type.Replication.Spawned as Spawned
import qualified Rattletrap.Type.Replication.Updated as Updated
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 ReplicationValue
  = Spawned Spawned.Spawned
  -- ^ Creates a new actor.
  | Updated Updated.Updated
  -- ^ Updates an existing actor.
  | Destroyed Destroyed.Destroyed
  -- ^ Destroys an existing actor.
  deriving (ReplicationValue -> ReplicationValue -> Bool
(ReplicationValue -> ReplicationValue -> Bool)
-> (ReplicationValue -> ReplicationValue -> Bool)
-> Eq ReplicationValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ReplicationValue -> ReplicationValue -> Bool
$c/= :: ReplicationValue -> ReplicationValue -> Bool
== :: ReplicationValue -> ReplicationValue -> Bool
$c== :: ReplicationValue -> ReplicationValue -> Bool
Eq, Int -> ReplicationValue -> ShowS
[ReplicationValue] -> ShowS
ReplicationValue -> String
(Int -> ReplicationValue -> ShowS)
-> (ReplicationValue -> String)
-> ([ReplicationValue] -> ShowS)
-> Show ReplicationValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ReplicationValue] -> ShowS
$cshowList :: [ReplicationValue] -> ShowS
show :: ReplicationValue -> String
$cshow :: ReplicationValue -> String
showsPrec :: Int -> ReplicationValue -> ShowS
$cshowsPrec :: Int -> ReplicationValue -> ShowS
Show)

instance Json.FromJSON ReplicationValue where
  parseJSON :: Value -> Parser ReplicationValue
parseJSON = String
-> (Object -> Parser ReplicationValue)
-> Value
-> Parser ReplicationValue
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Json.withObject String
"ReplicationValue" ((Object -> Parser ReplicationValue)
 -> Value -> Parser ReplicationValue)
-> (Object -> Parser ReplicationValue)
-> Value
-> Parser ReplicationValue
forall a b. (a -> b) -> a -> b
$ \Object
object -> [Parser ReplicationValue] -> Parser ReplicationValue
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
Foldable.asum
    [ (Spawned -> ReplicationValue)
-> Parser Spawned -> Parser ReplicationValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Spawned -> ReplicationValue
Spawned (Parser Spawned -> Parser ReplicationValue)
-> Parser Spawned -> Parser ReplicationValue
forall a b. (a -> b) -> a -> b
$ Object -> String -> Parser Spawned
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"spawned"
    , (Updated -> ReplicationValue)
-> Parser Updated -> Parser ReplicationValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Updated -> ReplicationValue
Updated (Parser Updated -> Parser ReplicationValue)
-> Parser Updated -> Parser ReplicationValue
forall a b. (a -> b) -> a -> b
$ Object -> String -> Parser Updated
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"updated"
    , (Destroyed -> ReplicationValue)
-> Parser Destroyed -> Parser ReplicationValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Destroyed -> ReplicationValue
Destroyed (Parser Destroyed -> Parser ReplicationValue)
-> Parser Destroyed -> Parser ReplicationValue
forall a b. (a -> b) -> a -> b
$ Object -> String -> Parser Destroyed
forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"destroyed"
    ]

instance Json.ToJSON ReplicationValue where
  toJSON :: ReplicationValue -> Value
toJSON ReplicationValue
x = case ReplicationValue
x of
    Spawned Spawned
y -> [Pair] -> Value
Json.object [String -> Spawned -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"spawned" Spawned
y]
    Updated Updated
y -> [Pair] -> Value
Json.object [String -> Updated -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"updated" Updated
y]
    Destroyed Destroyed
y -> [Pair] -> Value
Json.object [String -> Destroyed -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"destroyed" Destroyed
y]

schema :: Schema.Schema
schema :: Schema
schema = String -> Value -> Schema
Schema.named String
"replicationValue" (Value -> Schema) -> ([Value] -> Value) -> [Value] -> Schema
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Value] -> Value
Schema.oneOf ([Value] -> Schema) -> [Value] -> Schema
forall a b. (a -> b) -> a -> b
$ ((String, Schema) -> Value) -> [(String, Schema)] -> [Value]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
  (\(String
k, Schema
v) -> [(Pair, Bool)] -> Value
Schema.object [(String -> Value -> Pair
forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
k (Value -> Pair) -> Value -> Pair
forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
v, Bool
True)])
  [ (String
"spawned", Schema
Spawned.schema)
  , (String
"updated", Schema
Updated.schema)
  , (String
"destroyed", Schema
Destroyed.schema)
  ]

bitPut :: ReplicationValue -> BitPut.BitPut
bitPut :: ReplicationValue -> BitPut
bitPut ReplicationValue
value = case ReplicationValue
value of
  Spawned Spawned
x -> Bool -> BitPut
BitPut.bool Bool
True BitPut -> BitPut -> BitPut
forall a. Semigroup a => a -> a -> a
<> Bool -> BitPut
BitPut.bool Bool
True BitPut -> BitPut -> BitPut
forall a. Semigroup a => a -> a -> a
<> Spawned -> BitPut
Spawned.bitPut Spawned
x
  Updated Updated
x -> Bool -> BitPut
BitPut.bool Bool
True BitPut -> BitPut -> BitPut
forall a. Semigroup a => a -> a -> a
<> Bool -> BitPut
BitPut.bool Bool
False BitPut -> BitPut -> BitPut
forall a. Semigroup a => a -> a -> a
<> Updated -> BitPut
Updated.bitPut Updated
x
  Destroyed Destroyed
x -> Bool -> BitPut
BitPut.bool Bool
False BitPut -> BitPut -> BitPut
forall a. Semigroup a => a -> a -> a
<> Destroyed -> BitPut
Destroyed.bitPut Destroyed
x

bitGet
  :: Maybe Str.Str
  -> Version.Version
  -> ClassAttributeMap.ClassAttributeMap
  -> CompressedWord.CompressedWord
  -> Map.Map CompressedWord.CompressedWord U32.U32
  -> BitGet.BitGet
       ( Map.Map CompressedWord.CompressedWord U32.U32
       , ReplicationValue
       )
bitGet :: Maybe Str
-> Version
-> ClassAttributeMap
-> CompressedWord
-> Map CompressedWord U32
-> BitGet (Map CompressedWord U32, ReplicationValue)
bitGet Maybe Str
matchType Version
version ClassAttributeMap
classAttributeMap CompressedWord
actorId Map CompressedWord U32
actorMap =
  String
-> BitGet (Map CompressedWord U32, ReplicationValue)
-> BitGet (Map CompressedWord U32, ReplicationValue)
forall a. String -> BitGet a -> BitGet a
BitGet.label String
"ReplicationValue" (BitGet (Map CompressedWord U32, ReplicationValue)
 -> BitGet (Map CompressedWord U32, ReplicationValue))
-> BitGet (Map CompressedWord U32, ReplicationValue)
-> BitGet (Map CompressedWord U32, ReplicationValue)
forall a b. (a -> b) -> a -> b
$ do
    Bool
isOpen <- BitGet Bool
BitGet.bool
    if Bool
isOpen
      then do
        Bool
isNew <- BitGet Bool
BitGet.bool
        if Bool
isNew
          then do
            (Map CompressedWord U32
newActorMap, Spawned
spawned) <- Maybe Str
-> Version
-> ClassAttributeMap
-> CompressedWord
-> Map CompressedWord U32
-> BitGet (Map CompressedWord U32, Spawned)
Spawned.bitGet
              Maybe Str
matchType
              Version
version
              ClassAttributeMap
classAttributeMap
              CompressedWord
actorId
              Map CompressedWord U32
actorMap
            (Map CompressedWord U32, ReplicationValue)
-> BitGet (Map CompressedWord U32, ReplicationValue)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map CompressedWord U32
newActorMap, Spawned -> ReplicationValue
Spawned Spawned
spawned)
          else do
            Updated
updated <- Version
-> ClassAttributeMap
-> Map CompressedWord U32
-> CompressedWord
-> BitGet Updated
Updated.bitGet
              Version
version
              ClassAttributeMap
classAttributeMap
              Map CompressedWord U32
actorMap
              CompressedWord
actorId
            (Map CompressedWord U32, ReplicationValue)
-> BitGet (Map CompressedWord U32, ReplicationValue)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map CompressedWord U32
actorMap, Updated -> ReplicationValue
Updated Updated
updated)
      else do
        Destroyed
destroyed <- BitGet Destroyed
Destroyed.bitGet
        (Map CompressedWord U32, ReplicationValue)
-> BitGet (Map CompressedWord U32, ReplicationValue)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map CompressedWord U32
actorMap, Destroyed -> ReplicationValue
Destroyed Destroyed
destroyed)