Functional banana traveler. What happened to my tick event?

I am trying to implement a tick event and a little lower to demonstrate that it is not working. I would like to get an idea of ​​why it is not working.

gameloop :: TChan UAC -> IO () gameloop commandChannel = do (tickHandler, tickSink) <- newAddHandler networkDescr <- compile $ makeNetworkDescription commandChannel tickHandler actuate networkDescr forkIO $ forever $ (timer 10) >>= tickSink return () makeNetworkDescription :: forall t . Frameworks t => TChan UAC -> AddHandler () -> Moment t () makeNetworkDescription commandChannel tickHandler = do eTick <- fromAddHandler tickHandler bCChannel <- fromPoll $ grabCommands commandChannel -- test fromPoll test <- initial bCChannel liftIO $ appendFile "testPoll.txt" $ show test -- end fromPoll test let eCChannel = bCChannel <@ eTick liftIO $ print "hi there\n" reactimate $ (\n -> appendFile "Commands.txt" (show n)) <$> eCChannel grabCommands :: TChan UAC -> IO [UAC] grabCommands unval = do result <- (atomically $ readTChan unval) `untilM` (atomically $ isEmptyTChan unval) liftIO $ print $ show result return result timer :: TimeOut -> IO () timer ms = do threadDelay ms 

Here are some test data.

 main :: IO () main = do commandChan <- atomically $ newTChan :: IO (TChan UAC) forkIO $ gameloop commandChan liftIO $ print "back from fork\n" atomically $ populateCC commandChan playerCommands return () populateCC :: TChan UAC -> [UAC] -> STM () populateCC pChannel pCommands = mapM_ (writeTChan pChannel) pCommands playerCommands = [UAC (PlayerCommand (CommandData (T.pack "1" :: AID) Look) (T.pack "1")), UAC (PlayerCommand (CommandData (T.pack "2" :: AID) (Move Mongo)) (T.pack "2")) ] 

when I execute the above Main , I get this output.

 "back from fork\n" "[UAC (PlayerCommand (CommandData \"1\" Look) \"1\"),UAC (PlayerCommand (CommandData \"2\" (Move Mongo)) \"2\")]" "hi there\n" 

The Commands.txt file never appears. I attribute this issue to a failed tick event.

I got the idea of ​​implementing a timer from this , but I'm wondering if I'm thinking about it wrong. Any ideas?

Edit: I wanted the fromPoll confidence fromPoll do the right thing. I added the test above, and it is.

+4
source share
1 answer

It seems to me that the problem is not with the tick event, but with the fact that you model the player’s teams as Behavior .

If you think denotationally and imagine that behavior is a time-varying value of Behavior a = Time -> a , does it make sense to model player teams this way? What is a player’s team, for example, the time frame between 3s and 4s ? Does the argument you give fromPoll for this semantics?

The fact is that grabCommands has a serious side effect: calling it removes commands from the channel, so it is not even idempotent. In addition, it is blocked when there is no command. I think this is ultimately the reason why the tick event does not work: the network is blocked trying to execute fromPoll action. However, the main problem is deeper: the correct way to model player commands is to use Event rather than Behavior .

+3
source

Source: https://habr.com/ru/post/1488318/


All Articles