author | unc0rr |
Mon, 10 Nov 2008 15:57:59 +0000 | |
changeset 1492 | 2da1fe033f23 |
parent 1491 | 0b1f44751509 |
child 1493 | 1e422bc5d863 |
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 |
1478 | 10 |
import Data.Time |
1492 | 11 |
import Network |
849 | 12 |
|
851 | 13 |
data ClientInfo = |
1082 | 14 |
ClientInfo |
851 | 15 |
{ |
1082 | 16 |
chan :: TChan [String], |
851 | 17 |
handle :: Handle, |
1478 | 18 |
host :: String, |
19 |
connectTime :: UTCTime, |
|
851 | 20 |
nick :: String, |
894 | 21 |
protocol :: Word16, |
851 | 22 |
room :: String, |
1391 | 23 |
isMaster :: Bool, |
1403 | 24 |
isReady :: Bool, |
1391 | 25 |
forceQuit :: Bool |
851 | 26 |
} |
27 |
||
1082 | 28 |
instance Eq ClientInfo where |
29 |
a1 == a2 = handle a1 == handle a2 |
|
30 |
||
1317 | 31 |
data HedgehogInfo = |
32 |
HedgehogInfo String String |
|
33 |
||
1083 | 34 |
data TeamInfo = |
35 |
TeamInfo |
|
36 |
{ |
|
1329 | 37 |
teamowner :: String, |
1317 | 38 |
teamname :: String, |
1321 | 39 |
teamcolor :: String, |
40 |
teamgrave :: String, |
|
41 |
teamfort :: String, |
|
42 |
difficulty :: Int, |
|
1327 | 43 |
hhnum :: Int, |
1317 | 44 |
hedgehogs :: [HedgehogInfo] |
1083 | 45 |
} |
46 |
||
851 | 47 |
data RoomInfo = |
48 |
RoomInfo |
|
49 |
{ |
|
50 |
name :: String, |
|
1083 | 51 |
password :: String, |
1317 | 52 |
roomProto :: Word16, |
53 |
teams :: [TeamInfo], |
|
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1329
diff
changeset
|
54 |
gamemap :: String, |
1350 | 55 |
gameinprogress :: Bool, |
1396 | 56 |
playersIn :: Int, |
1403 | 57 |
readyPlayers :: Int, |
1411 | 58 |
isRestrictedJoins :: Bool, |
59 |
isRestrictedTeams :: Bool, |
|
1317 | 60 |
params :: Map.Map String [String] |
851 | 61 |
} |
1492 | 62 |
createRoom = ( |
63 |
RoomInfo |
|
64 |
"" |
|
65 |
"" |
|
66 |
0 |
|
67 |
[] |
|
68 |
"+rnd+" |
|
69 |
False |
|
70 |
1 |
|
71 |
0 |
|
72 |
False |
|
73 |
False |
|
74 |
Map.empty |
|
75 |
) |
|
851 | 76 |
|
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
77 |
data ServerInfo = |
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
78 |
ServerInfo |
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
79 |
{ |
1492 | 80 |
isDedicated :: Bool, |
81 |
serverMessage :: String, |
|
82 |
listenPort :: PortNumber |
|
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
83 |
} |
1492 | 84 |
newServerInfo = ( |
85 |
ServerInfo |
|
86 |
True |
|
87 |
"<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>" |
|
88 |
46631 |
|
89 |
) |
|
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
90 |
|
1082 | 91 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
92 |
type RoomsTransform = [RoomInfo] -> [RoomInfo] |
|
93 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle] |
|
1492 | 94 |
type Answer = ServerInfo -> (HandlesSelector, [String]) |
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
95 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, [Answer]) |
1082 | 96 |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
97 |
|
902 | 98 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
99 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
100 |
||
1082 | 101 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
102 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 103 |
|
894 | 104 |
maybeRead :: Read a => String -> Maybe a |
105 |
maybeRead s = case reads s of |
|
106 |
[(x, rest)] | all isSpace rest -> Just x |
|
107 |
_ -> Nothing |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
108 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
109 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
110 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
111 |
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
|
112 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
113 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
114 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 115 |
|
1466 | 116 |
clientByHandle :: Handle -> [ClientInfo] -> Maybe ClientInfo |
117 |
clientByHandle chandle clients = find (\c -> handle c == chandle) clients |
|
118 |
||
1082 | 119 |
sameRoom :: HandlesSelector |
120 |
sameRoom client clients rooms = map handle $ filter (\ci -> room ci == room client) clients |
|
121 |
||
1484 | 122 |
noRoomSameProto :: HandlesSelector |
123 |
noRoomSameProto client clients _ = map handle $ filter (null . room) $ filter (\ci -> protocol client == protocol ci) clients |
|
124 |
||
1082 | 125 |
othersInRoom :: HandlesSelector |
126 |
othersInRoom client clients rooms = map handle $ filter (client /=) $ filter (\ci -> room ci == room client) clients |
|
127 |
||
128 |
fromRoom :: String -> HandlesSelector |
|
129 |
fromRoom roomName _ clients _ = map handle $ filter (\ci -> room ci == roomName) clients |
|
130 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
131 |
allClients :: HandlesSelector |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
132 |
allClients _ clients _ = map handle $ clients |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
133 |
|
1082 | 134 |
clientOnly :: HandlesSelector |
135 |
clientOnly client _ _ = [handle client] |
|
136 |
||
137 |
noChangeClients :: ClientsTransform |
|
138 |
noChangeClients a = a |
|
139 |
||
140 |
modifyClient :: ClientInfo -> ClientsTransform |
|
1321 | 141 |
modifyClient _ [] = error "modifyClient: no such client" |
1082 | 142 |
modifyClient client (cl:cls) = |
143 |
if cl == client then |
|
144 |
client : cls |
|
145 |
else |
|
146 |
cl : (modifyClient client cls) |
|
147 |
||
1408 | 148 |
modifyRoomClients :: RoomInfo -> (ClientInfo -> ClientInfo) -> ClientsTransform |
149 |
modifyRoomClients clientsroom clientMod clients = map (\c -> if name clientsroom == room c then clientMod c else c) clients |
|
150 |
||
1082 | 151 |
noChangeRooms :: RoomsTransform |
152 |
noChangeRooms a = a |
|
153 |
||
154 |
addRoom :: RoomInfo -> RoomsTransform |
|
155 |
addRoom room rooms = room:rooms |
|
156 |
||
157 |
removeRoom :: String -> RoomsTransform |
|
158 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
1317 | 159 |
|
1321 | 160 |
modifyRoom :: RoomInfo -> RoomsTransform |
161 |
modifyRoom _ [] = error "changeRoomConfig: no such room" |
|
162 |
modifyRoom room (rm:rms) = |
|
163 |
if name room == name rm then |
|
164 |
room : rms |
|
1317 | 165 |
else |
1402 | 166 |
rm : modifyRoom room rms |
1327 | 167 |
|
168 |
modifyTeam :: RoomInfo -> TeamInfo -> RoomInfo |
|
169 |
modifyTeam room team = room{teams = replaceTeam team $ teams room} |
|
170 |
where |
|
171 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
172 |
replaceTeam team (t:teams) = |
|
173 |
if teamname team == teamname t then |
|
174 |
team : teams |
|
175 |
else |
|
176 |
t : replaceTeam team teams |