Skip to content

Commit

Permalink
server: fix ghc-mod/hlint alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
hlian committed May 26, 2015
1 parent 312b726 commit 17c4ab8
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions server/Main.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{-# LANGUAGE OverloadedStrings, NoImplicitPrelude, NamedFieldPuns #-}

import BasePrelude hiding ((\\), finally, read)
import Control.Concurrent (MVar)
import qualified Control.Concurrent as C
import Control.Concurrent.Chan (Chan)
import qualified Control.Concurrent.Chan as Ch
import Control.Concurrent.Suspend (sDelay)
import Control.Concurrent.Timer (repeatedTimer, stopTimer, TimerIO)
Expand Down Expand Up @@ -82,14 +80,13 @@ heartbeat wait db =
heartbeatFilterM =
filterM $ \(player, (_, lastPongTime, conn)) -> do
now <- getUnixTime
case (diffUnixTime now lastPongTime > delta) of
True -> do
putStrLn ("+ GCing " <> show player)
WS.sendClose conn ("pong better" :: Text)
return False
False -> do
ping conn
return True
if diffUnixTime now lastPongTime > delta then do
putStrLn ("+ GCing " <> show player)
WS.sendClose conn ("pong better" :: Text)
return False
else do
ping conn
return True

broadcast :: (A.ToJSON a) => DB -> a -> IO ()
broadcast db obj =
Expand Down Expand Up @@ -118,9 +115,9 @@ scoring hill db scores = do
scores' =
M.mapWithKey (\player (loc, _, _) -> score player + score' loc) db
score player =
maybe 0 id (M.lookup player scores)
fromMaybe 0 (M.lookup player scores)
score' loc =
if distance loc > 0.01 then 1 / (distance loc) else 100
if distance loc > 0.01 then 1 / distance loc else 100
distance loc =
let (x0, y0) = loc
(x1, y1) = hill in
Expand All @@ -135,7 +132,7 @@ dataflow onConnect pending = do
conn <- WS.acceptRequest pending
initial <- WS.receiveData conn
case A.decode initial of
Nothing -> do
Nothing ->
putStrLn ("Invalid registration: " <> show initial)
Just registration -> do
(onMove, onPong, onDisconnect) <- onConnect registration conn
Expand All @@ -144,7 +141,7 @@ dataflow onConnect pending = do
case A.decode message of
Just move ->
lift $ onMove move
Nothing -> do
Nothing ->
case A.decode message of
Just pong ->
lift $ onPong pong
Expand Down Expand Up @@ -214,7 +211,7 @@ mainWithState :: MVar DB ->
IO ()
mainWithState state didConnect didMove = do
timers <- makeTimers state didConnect didMove
(`finally` (forM_ timers stopTimer)) server
finally server (forM_ timers stopTimer)
where
server =
runServer (dataflow application)
Expand All @@ -231,7 +228,7 @@ mainWithState state didConnect didMove = do
renew = modify state $ \db -> do
now <- getUnixTime
return (M.alter (drug now) player db)
onConnect = do
onConnect =
read state >>= \db -> do
case (player, M.lookup player db) of
(Player email, Just _) -> error ("This email address is already taken: " <> T.unpack email)
Expand All @@ -249,7 +246,7 @@ mainWithState state didConnect didMove = do
let db' = M.delete player db
Ch.writeChan didMove (player, conn)
return db'
onPong _ = do
onPong _ =
void renew

runServer :: (WS.PendingConnection -> IO ()) -> IO ()
Expand All @@ -268,13 +265,13 @@ runServer server = do
return ()

parseExceptions :: ParseException -> IO ()
parseExceptions _ = do
parseExceptions _ =
throw WS.ConnectionClosed

main :: IO ()
main = do
hSetBuffering stdout LineBuffering
state <- C.newMVar (M.empty)
state <- C.newMVar M.empty
didConnect <- Ch.newChan
didMove <- Ch.newChan
mainWithState state didConnect didMove

0 comments on commit 17c4ab8

Please sign in to comment.