Advertisement
Guest User

PartyT

a guest
Jun 4th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE ConstraintKinds, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, NoImplicitPrelude,
  2.     RankNTypes, RecursiveDo, TupleSections, TypeFamilies #-}
  3. {-# LANGUAGE ScopedTypeVariables #-}
  4. module Magus.Party where
  5.  
  6. import Control.Applicative
  7. import Control.Monad
  8. import Control.Monad.Trans
  9. import Control.Monad.Fix
  10. import Control.Monad.IO.Class (MonadIO, liftIO)
  11. import Data.Either
  12. import Data.Eq
  13. import Data.Function (const, id, flip, ($), (.))
  14. import Data.Functor ((<$>), (<&>), ($>))
  15. import Data.Int
  16. import Data.Maybe
  17. import Data.Map (Map, singleton)
  18. import Data.Tuple (fst, uncurry)
  19. import Discord (
  20.     Snowflake, Gateway, ThreadIdType, RestChan
  21.   , RestCallException, UserRequest(CreateDM, GetUser)
  22.   , restCall
  23.   )
  24. import Prelude ((+))
  25. import Reflex
  26. import System.IO (IO)
  27.  
  28. import qualified Data.Map as M
  29. import Magus.Types
  30. import qualified Prelude as P
  31.  
  32. type DLogin = (RestChan, Gateway, [ThreadIdType])
  33.  
  34. newtype PartyT t m a = PartyT
  35.   { runPartyT
  36.   :: Behavior t Int
  37.   -> Event t (Map Int (Snowflake, Either RestCallException Player))
  38.   -> m (a, Event t (Map Int Snowflake))
  39.   }
  40.   -- deriving (Functor, Applicative, Monad, MonadFix, MonadIO) --, MonadException, MonadAsyncException)
  41.  
  42. runPartyD :: forall t m w a.
  43.   ( Reflex t
  44.   , Monad m
  45.   , MonadFix m
  46.   , MonadHold t m
  47.   , PerformEvent t m
  48.   , MonadIO (Performable m)
  49.   ) => DLogin
  50.     -> PartyT t m a
  51.     -> m a
  52. runPartyD dis w = mdo
  53.   d_id <- count e_w
  54.  
  55.   performEvent $ e_w  <&> \x -> liftIO $ P.putStrLn $ P.show x
  56.   performEvent $ e_kp <&> \x -> liftIO $ P.putStrLn $ P.show x
  57.  
  58.   (x, e_w) <- runPartyT w (current d_id) e_kp -- PartyT e w
  59.   -- x <- runPartyT w b_id e_kp -- PartyT e w
  60.   -- let e_w = never
  61.   -- (x, e_w) <- runEventWriterT $ runPartyT w b_id e_kp -- PartyT e w
  62.  
  63.   -- (x, e_w) <- runEventWriterT $ runPartyT w b_id e_kp -- PartyT e w
  64.   -- ((x, e), e_w) <- runPartyT w b_id e_kp -- PartyT e w
  65.   -- (x, e_w) <- runPartyT w b_id e_kp -- PartyT e w
  66.   -- let e :: Event t (Int, Either RestCallException (Snowflake, Player)) = e_w
  67.   -- tag e_w
  68.   -- (f_p, e_kp) <- fmap fanEither .
  69.   e_kp :: Event t (Map Int (Snowflake, Either RestCallException Player)) <- performEvent $ e_w <&> \xs ->
  70.     forM xs $ \k -> do
  71.       ep <- liftIO $ fetchParticipant dis k
  72.       pure $ (k, ep)
  73.  
  74.   -- e <- requestParticipantCache dis e_w
  75.   pure x
  76.  
  77.  
  78. mapPartyT :: (m (a, Event t (Map Int Snowflake)) -> n (b, Event t (Map Int Snowflake))) -> PartyT t m a -> PartyT t n b
  79. -- mapPartyT :: (m a -> n b) -> PartyT t m a -> PartyT t n b
  80. mapPartyT f m = PartyT $ \b e -> f $ runPartyT m b e
  81. {-# INLINE mapPartyT #-}
  82.  
  83. liftPartyT :: (Reflex t, Monad m) => m a -> PartyT t m a
  84. -- liftPartyT m = PartyT $ (const . const) m
  85. -- liftPartyT m = PartyT . lift
  86. liftPartyT m = PartyT $ \b e -> (, never) <$> m
  87. {-# INLINE liftPartyT #-}
  88.  
  89. instance (Functor m) => Functor (PartyT t m) where
  90.   fmap f = mapPartyT $ fmap $ \ ~(a, e) -> (f a, e)
  91.   -- fmap f = mapPartyT $ fmap $ \ ~a -> f a
  92.   {-# INLINE fmap #-}
  93.  
  94. instance (Monad m, Reflex t, Applicative m) => Applicative (PartyT t m) where
  95.   pure = lift . pure
  96.   {-# INLINE pure #-}
  97.   f <*> v = PartyT $ \b e -> liftA2 k (runPartyT f b e) (runPartyT v b e)
  98.     -- where k ~(a, e) ~(b, e') = (a b, leftmost [e, e'])
  99.     where k ~(a, e) ~(b, e') = (a b, e `P.mappend` e')
  100.  
  101. -- | CHECK
  102. instance (Monad m, Reflex t, Alternative m) => Alternative (PartyT t m) where
  103.     empty = liftPartyT empty
  104.     {-# INLINE empty #-}
  105.     m <|> n = PartyT $ \b e -> runPartyT m b e <|> runPartyT n b e
  106.     {-# INLINE (<|>) #-}
  107.  
  108. instance (Monad m, Reflex t) => Monad (PartyT t m) where
  109.   return = lift . return
  110.   {-# INLINE return #-}
  111.   m >>= k  = PartyT $ \b e -> do
  112.       (~a, e_w) <- runPartyT m b e
  113.       -- ~a <- runPartyT m b e
  114.       runPartyT (k a) b e
  115.       -- P.undefined
  116.       -- return b
  117.   {-# INLINE (>>=) #-}
  118.   fail msg = lift $ fail msg
  119.   {-# INLINE fail #-}
  120.  
  121. instance (Reflex t, MonadPlus m) => MonadPlus (PartyT t m) where
  122.   mzero       = lift mzero
  123.   {-# INLINE mzero #-}
  124.   m `mplus` n = PartyT $ \dis d_m -> runPartyT m dis d_m `mplus` runPartyT n dis d_m
  125.   {-# INLINE mplus #-}
  126.  
  127. instance Reflex t => MonadTrans (PartyT t) where
  128.   lift = liftPartyT
  129.   -- -- lift = liftPartyT
  130.   -- -- lift = PartyT . lift
  131.   -- {-# INLINE lift #-}
  132.  
  133. instance (Reflex t, MonadIO m) => MonadIO (PartyT t m) where
  134.   liftIO = lift . liftIO
  135.   {-# INLINE liftIO #-}
  136.  
  137. instance (MonadSample t m, Reflex t) => MonadSample t (PartyT t m) where
  138.   sample = lift . sample
  139.  
  140. instance (MonadHold t m, Reflex t) => MonadHold t (PartyT t m) where
  141.   hold a0 = lift . hold a0
  142.   holdDyn a0 = lift . holdDyn a0
  143.   holdIncremental a0 = lift . holdIncremental a0
  144.   buildDynamic a0 = lift . buildDynamic a0
  145.   headE = lift . headE
  146.  
  147. instance (MonadFix m, Reflex t) => MonadFix (PartyT t m) where
  148.     -- mfix f = PartyT $ \b e -> mfix $ \a -> runPartyT (f a) b e
  149.     mfix f = PartyT $ \b e -> mfix $ \ ~(a, _) -> runPartyT (f a) b e
  150.     {-# INLINE mfix #-}
  151.  
  152. instance (PerformEvent t m, Reflex t) => PerformEvent t (PartyT t m) where
  153.   -- type Performable (PartyT t m) = PartyT t (Performable m)
  154.   type Performable (PartyT t m) = Performable m
  155.   performEvent_ = lift . performEvent_
  156.   performEvent  = lift . performEvent
  157.  
  158. instance (Reflex t, TriggerEvent t m) => TriggerEvent t (PartyT t m) where
  159.   newTriggerEvent = lift newTriggerEvent
  160.   newTriggerEventWithOnComplete = lift newTriggerEventWithOnComplete
  161.   newEventWithLazyTriggerWithOnComplete = lift . newEventWithLazyTriggerWithOnComplete
  162.  
  163. instance PostBuild t m => PostBuild t (PartyT t m) where
  164.   getPostBuild = lift getPostBuild
  165.  
  166.  
  167. -- | Party
  168. class DiscordParty t m where
  169.   -- TODO Accept multiple Snowflakes
  170.   invite ::
  171.        Event t Snowflake
  172.     -- -> m (Event t (Snowflake, Either RestCallException Player))
  173.     -> m (Event t (Map Snowflake (Either RestCallException Player)))
  174.  
  175. type FetchConstraints t m =
  176.   ( MonadHold t m
  177.   , MonadIO m
  178.   , MonadIO (Performable m)
  179.   , PerformEvent t m
  180.   )
  181.  
  182. instance (FetchConstraints t m, MonadFix m) => DiscordParty t (PartyT t m) where
  183. -- instance (FetchConstraints t m, MonadFix m, EventWriter t Snowflake (PartyT t m)) => DiscordParty t (PartyT t m) where
  184.   invite e_k = do
  185.     -- tellEvent e_k
  186.     -- performEvent $ e_k <&> \x -> do
  187.     --   liftIO $ P.putStrLn $ P.show x
  188.  
  189.     PartyT $ \b_id e_res -> do
  190.       let e_i = attach b_id e_k
  191.       b_k <- hold 0 (fst <$> traceEvent "ID" e_i)
  192.       let e_r = attachWith (,) b_k e_res
  193.       -- pure $ Reflex.mapMaybe (\(i, (k, s, p)) -> if i == k then Just (s, p) else Nothing) e_r
  194.       -- pure $ (Reflex.mapMaybe (\(i, (k, s, p)) -> if i == k then Just (s, p) else Nothing) e_r, e_i)
  195.  
  196.       let x :: (Event t (Snowflake, Either RestCallException Player), Event t (Int, Snowflake))
  197.             = (Reflex.mapMaybe (\(i, xs) -> M.lookup i xs) e_r, e_i)
  198.       pure $ (Reflex.mapMaybe (\(i, xs) -> uncurry singleton <$> M.lookup i xs) e_r, traceEvent "MAP" (uncurry singleton <$> e_i))
  199.  
  200. fetchParticipant ::
  201.        (RestChan, Gateway, [ThreadIdType])
  202.     -> Snowflake
  203.     -> IO (Either RestCallException Player)
  204. fetchParticipant dis i = do
  205.   eu <- restCall dis (GetUser i)
  206.   ec <- restCall dis (CreateDM i)
  207.   pure $ Player <$> eu <*> ec
  208.  
  209.  
  210.  
  211. -- fetchParticipantEvent ::
  212. --   ( FetchConstraints t m
  213. --   ) => (RestChan, Gateway, [ThreadIdType])
  214. --     -> Event t Snowflake
  215. --     -> m (Event t (Either RestCallException Player))
  216. -- fetchParticipantEvent dis e = performEvent $ liftIO . fetchParticipant dis <$> e
  217.  
  218. -- requestParticipantCache :: forall t m.
  219. --   ( FetchConstraints t m
  220. --   , MonadFix m
  221. --   ) => (RestChan, Gateway, [ThreadIdType])
  222. --     -> Event t Snowflake
  223. --     -> m (Event t (Snowflake, Player))
  224. -- -- requestParticipantCache dis d_m e = do
  225. -- requestParticipantCache dis e = mdo
  226. --   let e_mp = attachWith (\m i -> (i, M.lookup i m)) (current d_m) e
  227.  
  228. --   (f_p, e_kp) <- fmap fanEither . performEvent $ e_mp <&> \(i, mp) -> case mp of
  229. --     Nothing -> do
  230. --       liftIO $ P.putStrLn "Nothing..."
  231. --       ep <- liftIO $ fetchParticipant dis i
  232. --       pure $ (i,) <$> ep
  233. --     -- TODO Rid of unnecessary inserts
  234. --     Just p  -> do
  235. --       liftIO $ P.putStrLn "JUST!!!!"
  236. --       pure $ pure (i, p)
  237.  
  238. --   performEvent $ e_kp <&> (liftIO . P.putStrLn . P.show)
  239. --   performEvent $ f_p <&> (liftIO . P.putStrLn . P.show)
  240.  
  241. --   d_m <- accumDyn (\m (i, p) -> M.insert i p m) M.empty e_kp
  242.  
  243. --   pure e_kp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement