summaryrefslogtreecommitdiff
path: root/HsBot/IRC/User.hs
blob: 3c9bb3be45fac1d1d7ef9d255ad8417792181d41 (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
module HsBot.IRC.User where

import List

import HsBot.General.Render

data User = User { 
      userName :: String,
      userMessages :: Int,
      userPts :: Int 
   } deriving (Show, Read) 

userMakeDefault :: String -> User
userMakeDefault name = 
	User { 
		userName = name, 
		userMessages = 0,
		userPts = 0
	}

instance Eq User where 
   x == y = (userPts x) == (userPts y)

instance Ord User where
	x < y = (userPts x) < (userPts y)
	x > y = (userPts x) > (userPts y)
	x >= y = not (x < y)
	x <= y = not (x > y)

instance Render User where
	render user = userName user ++ ": " ++ 
		(show $ userPts user) ++ "pts; " ++
		(show $ userMessages user) ++ "msgs"

userEquals :: User -> User -> Bool
userEquals x y = (userName x) == (userName y)

userGetIfExists :: String -> [User] -> Maybe User
userGetIfExists name [] = Nothing
userGetIfExists name (x:xs) 
	| userName x == name = Just x
	| otherwise = userGetIfExists name xs

userDeleteIfExists :: String -> [User] -> [User]
userDeleteIfExists name [] = []
userDeleteIfExists name (x:xs) 
	| userName x == name = xs
	| otherwise = x : userDeleteIfExists name xs

userGet :: String -> [User] -> (User, [User])
userGet name xs =
	case userGetIfExists name xs of
		Just user -> (user, user : xs)
		Nothing -> let user = userMakeDefault name in (user, user : xs)

usersUpdate :: String -> [User] -> (User -> User) -> [User]
usersUpdate name xs update = 
   let (user, _) = userGet name xs
    in (update user) : (userDeleteIfExists name xs)