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