author | unc0rr |
Sun, 28 Sep 2008 15:55:07 +0000 | |
changeset 1293 | 5943efa67299 |
parent 1083 | 3448dd03483f |
child 1304 | 05cebf68ebd8 |
permissions | -rw-r--r-- |
849 | 1 |
module Miscutils where |
2 |
||
3 |
import IO |
|
4 |
import Control.Concurrent.STM |
|
894 | 5 |
import Data.Word |
6 |
import Data.Char |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
7 |
import Data.List |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
8 |
import Maybe (fromJust) |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
9 |
|
849 | 10 |
|
851 | 11 |
data ClientInfo = |
1082 | 12 |
ClientInfo |
851 | 13 |
{ |
1082 | 14 |
chan :: TChan [String], |
851 | 15 |
handle :: Handle, |
16 |
nick :: String, |
|
894 | 17 |
protocol :: Word16, |
851 | 18 |
room :: String, |
19 |
isMaster :: Bool |
|
20 |
} |
|
21 |
||
1082 | 22 |
instance Eq ClientInfo where |
23 |
a1 == a2 = handle a1 == handle a2 |
|
24 |
||
1083 | 25 |
data TeamInfo = |
26 |
TeamInfo |
|
27 |
{ |
|
28 |
teamname :: String |
|
29 |
} |
|
30 |
||
851 | 31 |
data RoomInfo = |
32 |
RoomInfo |
|
33 |
{ |
|
34 |
name :: String, |
|
1083 | 35 |
password :: String, |
36 |
teams :: [TeamInfo] |
|
851 | 37 |
} |
38 |
||
1082 | 39 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
40 |
type RoomsTransform = [RoomInfo] -> [RoomInfo] |
|
41 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle] |
|
42 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, HandlesSelector, [String]) |
|
43 |
||
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
44 |
|
902 | 45 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
46 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
47 |
||
1082 | 48 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
49 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 50 |
|
894 | 51 |
maybeRead :: Read a => String -> Maybe a |
52 |
maybeRead s = case reads s of |
|
53 |
[(x, rest)] | all isSpace rest -> Just x |
|
54 |
_ -> Nothing |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
55 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
56 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
57 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
58 |
deleteBy2t eq x (y:ys) = if y `eq` x then ys else y : deleteBy2t eq x ys |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
59 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
60 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
61 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 62 |
|
63 |
sameRoom :: HandlesSelector |
|
64 |
sameRoom client clients rooms = map handle $ filter (\ci -> room ci == room client) clients |
|
65 |
||
66 |
othersInRoom :: HandlesSelector |
|
67 |
othersInRoom client clients rooms = map handle $ filter (client /=) $ filter (\ci -> room ci == room client) clients |
|
68 |
||
69 |
fromRoom :: String -> HandlesSelector |
|
70 |
fromRoom roomName _ clients _ = map handle $ filter (\ci -> room ci == roomName) clients |
|
71 |
||
72 |
clientOnly :: HandlesSelector |
|
73 |
clientOnly client _ _ = [handle client] |
|
74 |
||
75 |
noChangeClients :: ClientsTransform |
|
76 |
noChangeClients a = a |
|
77 |
||
78 |
modifyClient :: ClientInfo -> ClientsTransform |
|
79 |
modifyClient client (cl:cls) = |
|
80 |
if cl == client then |
|
81 |
client : cls |
|
82 |
else |
|
83 |
cl : (modifyClient client cls) |
|
84 |
||
85 |
noChangeRooms :: RoomsTransform |
|
86 |
noChangeRooms a = a |
|
87 |
||
88 |
addRoom :: RoomInfo -> RoomsTransform |
|
89 |
addRoom room rooms = room:rooms |
|
90 |
||
91 |
removeRoom :: String -> RoomsTransform |
|
92 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
93 |
||
94 |
badCmd :: [String] |
|
95 |
badCmd = ["ERROR", "Bad command, state or incorrect parameter"] |