-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.hs
40 lines (32 loc) · 1.54 KB
/
user.hs
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
module User where
import World
import Data
import Control.Concurrent.MVar
getUser name boxOfWorld =
takeMVar boxOfWorld >>= \instanceOfWorld ->
getUser' name (usersAll instanceOfWorld) instanceOfWorld >>= \user ->
(if userIsConnected name $ usersConnected instanceOfWorld
then putMVar boxOfWorld instanceOfWorld
else putMVar boxOfWorld (connectUser (setConnected user instanceOfWorld) instanceOfWorld))
>> return user
getUser' name [] instanceOfWorld = return $ createUser name instanceOfWorld
getUser' name (x:xs) instanceOfWorld =
if (userName x) == name
then return x
else getUser' name xs instanceOfWorld
userIsConnected name [] = False
userIsConnected name (x:xs) =
if userName x == name
then True
else userIsConnected name xs
createUser name instanceOfWorld =
User { userName = name, connectionIndex = length (usersConnected instanceOfWorld), userRoom = head (roomsAll instanceOfWorld) }
setConnected user instanceOfWorld =
User { userName = userName user, connectionIndex = length (usersConnected instanceOfWorld), userRoom = userRoom user }
connectUser user instanceOfWorld =
World { roomsAll = roomsAll instanceOfWorld, usersAll = user:(usersAll instanceOfWorld), usersConnected = user:(usersConnected instanceOfWorld) }
disconnectUser user instanceOfWorld =
World { roomsAll = roomsAll instanceOfWorld, usersAll = usersAll instanceOfWorld, usersConnected = disconnectUser' user (usersConnected instanceOfWorld) }
disconnectUser' user usersConnected =
let (ys, zs) = splitAt (connectionIndex user) usersConnected in
ys ++ (tail zs)