aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Types/Session.hs
blob: 210cd28a50c54e2c3cef51c3169627931b6b3fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module Types.Session where

import Types.Classes
import Types.Clocks
import Network.CGI.Cookie
import System.Time
import Data.List.Split (splitOn)
import Data.Classes
import Data.Maybe (fromJust)

data Session = Session
    { currentClass :: Maybe Class
    , currentClock :: Clock }
    deriving (Eq)

-- If we were using a proper monad stack, this should be of type App
-- Cookie where it uses the reader monad to get the CalendarTime and
-- maybe the session from the state monad

makeClassCookie             :: ClockTime -> Session -> Cookie
makeClassCookie now session =
    Cookie { cookieName     = "class_cookie"
           , cookieValue    =
               case currentClass session of
                   Just c  -> show c
                   Nothing -> "Nothing"
           , cookieExpires  = Just $ endOfSchoolDay now
           , cookieDomain   = Nothing
           , cookiePath     = Just "/sariul/cgi-bin"
           , cookieSecure   = False}

makeClockCookie             :: ClockTime -> Session -> Cookie
makeClockCookie now session =
    Cookie { cookieName     = "clock_cookie"
           , cookieValue    =
               case currentClock session of
                   CountDownClock -> "0"
                   CountUpClock   -> "1"
           , cookieExpires  = Just $ endOfSchoolDay now
           , cookieDomain   = Nothing
           , cookiePath     = Just "/sariul/cgi-bin"
           , cookieSecure   = False}

makeSsCookie               :: ClockTime -> Session -> Cookie
makeSsCookie now session =
    Cookie { cookieName    = "ss_cookie"
           , cookieValue    =
               case currentClass session of
                   Nothing -> "0"
                   Just c -> show $ numberOfSs c
           , cookieExpires = Just $ endOfSchoolDay now
           , cookieDomain  = Nothing
-- make the cookie path not absolute
           , cookiePath    = Just "/sariul/cgi-bin"
           , cookieSecure  = False}

endOfSchoolDay       :: ClockTime -> CalendarTime
endOfSchoolDay now   = (toUTCTime . addToClockTime noTimeDiff { tdHour = hoursTilEndOfDay }) now
  where
    koreanTime       = ((toUTCTime . addToClockTime noTimeDiff { tdHour = 9 }) now)
                       { ctTZ = 9 * 60 * 60
                       , ctTZName = "KST"}
    hoursTilEndOfDay = if   hoursTilEndOfDay' > 0
                       then hoursTilEndOfDay'
                       else 1
    hoursTilEndOfDay' = 18 - ctHour koreanTime

-- could use Maybe monad in the below!

parseClassCookie   :: Maybe String -> Maybe Class
parseClassCookie s =
    case s of
        Just s  -> parseClassCookie' s
        Nothing -> Nothing

parseClassCookie' :: String -> Maybe Class
parseClassCookie' s =
    case splitOn "-" s of
        g:c:[] -> lookupSariulClass (read g) (read c)
        _      -> Nothing