module GameSession
    ( createSession
    , joinSession
    , leaveSession
    , maxPlayers ) where

import Data.Time.Clock (UTCTime)

import DB
import Types

maxPlayers :: Int
maxPlayers = 6

removeItem :: Eq a => a -> [a] -> [a]
removeItem _ [] = []
removeItem r (x:xs) | r == x = removeItem r xs
                    | otherwise = x : removeItem r xs

leaveSession :: User -> Session -> Session
leaveSession u s =
    case userRole u of
        Just r ->
            Session (sessionOpen s + 1)
                    (removeItem u $ sessionUsers s)
                    (r : sessionRoles s)
                    (sessionTime s)
        Nothing -> error "No Role"

joinSession :: User -> Session -> UTCTime -> Session
joinSession u s =
    case userRole u of
        Just r ->
            Session (sessionOpen s - 1)
                    (u : sessionUsers s)
                    (removeItem r $ sessionRoles s)
        Nothing -> error "No Role"

createSession :: [Role] -> UTCTime -> Session
createSession rs = do
    Session maxPlayers [] rs
