<p>Duality of the universe: there's true state of the universe used in simulation and there's state the the players perceive. These most likely will always be in conflict. One possible solution is to separate these completely. Perform simulation in one system and record what players see in other.</p>

<p>For every type of entity in the game, there's two sets of data: real and reported. Reports are tied to time and faction. Examples are given for planets. Thus, we have <em>Planet</em>, <em>PlanetReport</em> and <em>CollatedPlanetReport</em>. First is the real entity, second is report of that entity tied in time and faction. Third one is aggregated information a faction has of given entity. In database two first ones are:</p>

<pre><code>
Planet json
    name Text
    position Int
    starSystemId StarSystemId
    ownerId FactionId Maybe
    gravity Double
    SystemPosition starSystemId position
    deriving Show

PlanetReport json
    planetId PlanetId
    ownerId  FactionId Maybe
    starSystemId StarSystemId
    name Text Maybe
    position Int Maybe
    gravity Double Maybe
    factionId FactionId
    date Int
    deriving Show
</code></pre>
<p>Third one is defined as a datatype:</p>

<pre><code>
data CollatedPlanetReport = CollatedPlanetReport
    { cprPlanetId :: Key Planet
    , cprSystemId :: Key StarSystem
    , cprOwnerId  :: Maybe (Key Faction)
    , cprName     :: Maybe Text
    , cprPosition :: Maybe Int
    , cprGravity  :: Maybe Double
    , cprDate     :: Int
    } deriving Show
</code></pre>

<p>Data from database need to be transformed before working on it. Usually it's 1:1 mapping, but sometimes it makes sense to enrich it (turning IDs into names for example). For this we use <em>ReportTransform</em> type class:</p>

<pre><code>
-- | Class to transform a report stored in db to respective collated report
class ReportTransform a b where
    fromReport :: a -&gt; b

instance ReportTransform PlanetReport CollatedPlanetReport where
    fromReport report =
	CollatedPlanetReport (planetReportPlanetId report)
			     (planetReportStarSystemId report)
			     (planetReportOwnerId report)
			     (planetReportName report)
			     (planetReportPosition report)
			     (planetReportGravity report)
			     (planetReportDate report)
</code></pre>

<p>To easily combine bunch of collated reports together, we define instances
    of <em>semigroup</em> and <em>monoid</em> for collated report data.
    Semigroup defines an associative binary operation (&lt;&gt;) and monoid defines a zero or empty item (mempty). My explanation about Monoid and Semigroup were a bit rambling, so maybe have a look at <a href="https://wiki.haskell.org/Monoid">https://wiki.haskell.org/Monoid</a> which explains it in detail.</p>

<pre><code>
instance Semigroup CollatedPlanetReport where
    (&lt;&gt;) a b = CollatedPlanetReport (cprPlanetId a)
				    (cprSystemId a)
				    (cprOwnerId a &lt;|&gt; cprOwnerId b)
				    (cprName a &lt;|&gt; cprName b)
				    (cprPosition a &lt;|&gt; cprPosition b)
				    (cprGravity a &lt;|&gt; cprGravity b)
				    (max (cprDate a) (cprDate b))

instance Monoid CollatedPlanetReport where
    mempty = CollatedPlanetReport (toSqlKey 0) (toSqlKey 0) Nothing Nothing Nothing Nothing 0
</code></pre>

<p>In some cases there might be a list of collated reports that are about different entities of same type (several reports for every planet in solar system). For those cases, we need a way to tell what reports belong together:</p>

<pre><code>
-- | Class to indicate if two reports are about same entity
class Grouped a where
    sameGroup :: a -&gt; a -&gt; Bool

instance Grouped PlanetReport where
    sameGroup a b =
	planetReportPlanetId a == planetReportPlanetId b
</code></pre>

<p>After this, processing a list of reports for same entity is short amount of very general code:</p>

<pre><code>
-- | Combine list of reports and form a single collated report
--   Resulting report will have facts from the possibly partially empty reports
--   If a fact is not present for a given field, Nothing is left there
collateReport :: (Monoid a, ReportTransform b a) =&gt; [b] -&gt; a
collateReport reports = mconcat (map fromReport reports)
</code></pre>

<p>For reports of multiple entities is bit more complex, as they need to be sorted first, but the code is similarly general:</p>

<pre><code>
-- | Combine list of reports and form a list of collated reports
--   Each reported entity is given their own report
collateReports :: (Grouped b, Monoid a, ReportTransform b a) =&gt; [b] -&gt; [a]
collateReports [] = []
collateReports s@(x:_) = collateReport itemsOfKind : collateReports restOfItems
    where split = span (sameGroup x) s
	  itemsOfKind = fst split
	  restOfItems = snd split
</code></pre>

<p>Final step is to either render reports as HTML or send them as JSON back to client. For JSON case we need one more type class instance (ToJSON) that can be automatically generated. After that handler function can be defined. After authenticating the user and checking that they are member of a faction, reports of specific planet (defined by its primary key) are retrieved from database, collated, turned into JSON and sent back to client:</p>

<pre><code>
$(deriveJSON defaultOptions {fieldLabelModifier = drop 3} ''CollatedPlanetReport)

getApiPlanetR :: Key Planet -&gt; Handler Value
getApiPlanetR planetId = do
    (_, _, fId) &lt;- apiRequireFaction
    loadedPlanetReports &lt;- runDB $ selectList [ PlanetReportPlanetId ==. planetId
					      , PlanetReportFactionId ==. fId ] [ Asc PlanetReportDate ]
    let planetReport = collateReport $ map entityVal loadedPlanetReports :: CollatedPlanetReport
    return $ toJSON planetReport
</code></pre>

<p>For those interested seeing some code, source is available at <a href="https://github.com/Tuula/deep-sky/">https://github.com/Tuula/deep-sky/</a> (<a href="https://github.com/Tuula/deep-sky/tree/baa0807dd36b61fd02174b17c10013862af4ec18">https://github.com/Tuula/deep-sky/tree/baa0807dd36b61fd02174b17c10013862af4ec18</a> is situation before lots of Elm related changes that I mentioned in passing in the previous episode)</p>

