author | unc0rr |
Wed, 08 Oct 2008 15:55:23 +0000 | |
changeset 1319 | fedaae857a50 |
parent 1317 | 13cf8c5a7428 |
child 1321 | d7dc4e86201e |
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) |
1317 | 9 |
import qualified Data.Map as Map |
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 |
||
1317 | 25 |
data HedgehogInfo = |
26 |
HedgehogInfo String String |
|
27 |
||
1083 | 28 |
data TeamInfo = |
29 |
TeamInfo |
|
30 |
{ |
|
1317 | 31 |
teamname :: String, |
32 |
hedgehogs :: [HedgehogInfo] |
|
1083 | 33 |
} |
34 |
||
851 | 35 |
data RoomInfo = |
36 |
RoomInfo |
|
37 |
{ |
|
38 |
name :: String, |
|
1083 | 39 |
password :: String, |
1317 | 40 |
roomProto :: Word16, |
41 |
teams :: [TeamInfo], |
|
42 |
params :: Map.Map String [String] |
|
851 | 43 |
} |
44 |
||
1082 | 45 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
46 |
type RoomsTransform = [RoomInfo] -> [RoomInfo] |
|
47 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle] |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1083
diff
changeset
|
48 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, [(HandlesSelector, [String])]) |
1082 | 49 |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
50 |
|
902 | 51 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
52 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
53 |
||
1082 | 54 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
55 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 56 |
|
894 | 57 |
maybeRead :: Read a => String -> Maybe a |
58 |
maybeRead s = case reads s of |
|
59 |
[(x, rest)] | all isSpace rest -> Just x |
|
60 |
_ -> Nothing |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
61 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
62 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
63 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
64 |
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
|
65 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
66 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
67 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 68 |
|
69 |
sameRoom :: HandlesSelector |
|
70 |
sameRoom client clients rooms = map handle $ filter (\ci -> room ci == room client) clients |
|
71 |
||
72 |
othersInRoom :: HandlesSelector |
|
73 |
othersInRoom client clients rooms = map handle $ filter (client /=) $ filter (\ci -> room ci == room client) clients |
|
74 |
||
75 |
fromRoom :: String -> HandlesSelector |
|
76 |
fromRoom roomName _ clients _ = map handle $ filter (\ci -> room ci == roomName) clients |
|
77 |
||
78 |
clientOnly :: HandlesSelector |
|
79 |
clientOnly client _ _ = [handle client] |
|
80 |
||
81 |
noChangeClients :: ClientsTransform |
|
82 |
noChangeClients a = a |
|
83 |
||
84 |
modifyClient :: ClientInfo -> ClientsTransform |
|
85 |
modifyClient client (cl:cls) = |
|
86 |
if cl == client then |
|
87 |
client : cls |
|
88 |
else |
|
89 |
cl : (modifyClient client cls) |
|
90 |
||
91 |
noChangeRooms :: RoomsTransform |
|
92 |
noChangeRooms a = a |
|
93 |
||
94 |
addRoom :: RoomInfo -> RoomsTransform |
|
95 |
addRoom room rooms = room:rooms |
|
96 |
||
97 |
removeRoom :: String -> RoomsTransform |
|
98 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
1317 | 99 |
|
100 |
changeRoomConfig :: String -> String -> [String] -> RoomsTransform |
|
101 |
changeRoomConfig _ _ _ [] = error "changeRoomConfig: no such room" |
|
102 |
changeRoomConfig roomName paramName paramStrs (room:rooms) = |
|
103 |
if roomName == name room then |
|
104 |
room{params = Map.insert paramName paramStrs (params room)} : rooms |
|
105 |
else |
|
106 |
room : changeRoomConfig roomName paramName paramStrs rooms |