blob: a0cc6646f5b73ad1bef304b1e2ad614b4de314f9 (
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
|
-- {-# LANGUAGE MultiParamTypeClasses #-}
-- {-# LANGUAGE FunctionalDependencies #-}
-- {-# LANGUAGE FlexibleInstances #-}
-- {-# LANGUAGE NoMonomorphismRestriction #-}
module HsBot.Base.State where
import qualified Data.Map as M
import List
import HsBot.IRC.User
import HsBot.Base.Database
data State = State {
isReady :: Bool,
currentSender :: String,
currentChannel :: String,
line :: String,
users :: [User],
} deriving (Show, Read)
makeDefaultState :: State
makeDefaultState = State {
isReady = False,
currentSender = "",
currentChannel = "",
line = "",
users = [],
}
stateNumUsers :: State -> Int
stateNumUsers state = length $ users state
stateSortedUsers :: State -> [User]
stateSortedUsers state = sort $ users state
stateUpdateUser :: State -> String -> (User -> User) -> State
stateUpdateUser state name update =
let updatedUsers = usersUpdate name (users state) update
in state { users = updatedUsers }
stateLoad :: String -> IO State
stateLoad databaseFile = do
file <- readFile databaseFile
return ( read file :: State )
stateSave :: String -> State -> IO ()
stateSave databaseFile = writeFile databaseFile . show
stateSaveIO :: String -> IO State -> IO ()
stateSaveIO databaseFile state = do
state' <- state
writeFile databaseFile (show state')
|