Implement re-synchronization after connection loss

This commit is contained in:
Tissevert 2020-01-25 10:57:16 +01:00
parent ff9194d416
commit 9270ce17aa

View file

@ -14,7 +14,7 @@ import qualified Data.Map as Map (delete, empty, member)
import Control.Monad.Reader (ReaderT, ask) import Control.Monad.Reader (ReaderT, ask)
import Control.Monad.Trans (lift) import Control.Monad.Trans (lift)
import Hanafuda.KoiKoi (Step(..)) import Hanafuda.KoiKoi (Step(..))
import Hanafuda.Message (T(..), FromClient(..)) import Hanafuda.Message (Coordinates(..), FromClient(..), T(..), orderCoordinates)
import qualified Hanafuda.Message as Message ( import qualified Hanafuda.Message as Message (
T(..), FromClient, PublicGame(..), PublicState(..) T(..), FromClient, PublicGame(..), PublicState(..)
) )
@ -82,6 +82,22 @@ answer state (Error {error}) = do
debug $ "Received error from server : " `append` pack error debug $ "Received error from server : " `append` pack error
return state return state
answer state@(Connected {games}) (Relay {from, message = Sync {latestKnown}})
| not $ Map.member from games =
send (Yield {onGameID = gameID latestKnown, to = from}) >> return state
| otherwise =
case orderCoordinates latestKnownHere latestKnown of
Just LT -> send $ Yield {onGameID = gameID latestKnown, to = from}
Just GT -> send $ Share {gameSave = game}
_ -> return ()
>> return state
where
game = games ! from
latestKnownHere = Message.coordinates $ Message.public game
answer state@(Connected {games}) (Relay {from, message = Yield {}}) =
send (Share {gameSave = games ! from}) >> return state
{- {-
- Ignore - Ignore
-} -}