author | unc0rr |
Thu, 20 Nov 2008 15:48:50 +0000 | |
changeset 1504 | 1603a796f42a |
parent 1493 | 1e422bc5d863 |
child 1513 | a35c90263e27 |
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, |
|
1493 | 82 |
listenPort :: PortNumber, |
83 |
loginsNumber :: Int, |
|
84 |
lastHourUsers :: [UTCTime] |
|
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
85 |
} |
1492 | 86 |
newServerInfo = ( |
87 |
ServerInfo |
|
88 |
True |
|
89 |
"<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>" |
|
90 |
46631 |
|
1493 | 91 |
0 |
92 |
[] |
|
1492 | 93 |
) |
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
94 |
|
1082 | 95 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
96 |
type RoomsTransform = [RoomInfo] -> [RoomInfo] |
|
97 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle] |
|
1492 | 98 |
type Answer = ServerInfo -> (HandlesSelector, [String]) |
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
99 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, [Answer]) |
1082 | 100 |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
101 |
|
902 | 102 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
103 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
104 |
||
1082 | 105 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
106 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 107 |
|
894 | 108 |
maybeRead :: Read a => String -> Maybe a |
109 |
maybeRead s = case reads s of |
|
110 |
[(x, rest)] | all isSpace rest -> Just x |
|
111 |
_ -> Nothing |
|
901
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 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
114 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
115 |
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
|
116 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
117 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
118 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 119 |
|
1466 | 120 |
clientByHandle :: Handle -> [ClientInfo] -> Maybe ClientInfo |
121 |
clientByHandle chandle clients = find (\c -> handle c == chandle) clients |
|
122 |
||
1082 | 123 |
sameRoom :: HandlesSelector |
124 |
sameRoom client clients rooms = map handle $ filter (\ci -> room ci == room client) clients |
|
125 |
||
1484 | 126 |
noRoomSameProto :: HandlesSelector |
127 |
noRoomSameProto client clients _ = map handle $ filter (null . room) $ filter (\ci -> protocol client == protocol ci) clients |
|
128 |
||
1082 | 129 |
othersInRoom :: HandlesSelector |
130 |
othersInRoom client clients rooms = map handle $ filter (client /=) $ filter (\ci -> room ci == room client) clients |
|
131 |
||
132 |
fromRoom :: String -> HandlesSelector |
|
133 |
fromRoom roomName _ clients _ = map handle $ filter (\ci -> room ci == roomName) clients |
|
134 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
135 |
allClients :: HandlesSelector |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
136 |
allClients _ clients _ = map handle $ clients |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
137 |
|
1082 | 138 |
clientOnly :: HandlesSelector |
139 |
clientOnly client _ _ = [handle client] |
|
140 |
||
141 |
noChangeClients :: ClientsTransform |
|
142 |
noChangeClients a = a |
|
143 |
||
144 |
modifyClient :: ClientInfo -> ClientsTransform |
|
1321 | 145 |
modifyClient _ [] = error "modifyClient: no such client" |
1082 | 146 |
modifyClient client (cl:cls) = |
147 |
if cl == client then |
|
148 |
client : cls |
|
149 |
else |
|
150 |
cl : (modifyClient client cls) |
|
151 |
||
1408 | 152 |
modifyRoomClients :: RoomInfo -> (ClientInfo -> ClientInfo) -> ClientsTransform |
153 |
modifyRoomClients clientsroom clientMod clients = map (\c -> if name clientsroom == room c then clientMod c else c) clients |
|
154 |
||
1082 | 155 |
noChangeRooms :: RoomsTransform |
156 |
noChangeRooms a = a |
|
157 |
||
158 |
addRoom :: RoomInfo -> RoomsTransform |
|
159 |
addRoom room rooms = room:rooms |
|
160 |
||
161 |
removeRoom :: String -> RoomsTransform |
|
162 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
1317 | 163 |
|
1321 | 164 |
modifyRoom :: RoomInfo -> RoomsTransform |
165 |
modifyRoom _ [] = error "changeRoomConfig: no such room" |
|
166 |
modifyRoom room (rm:rms) = |
|
167 |
if name room == name rm then |
|
168 |
room : rms |
|
1317 | 169 |
else |
1402 | 170 |
rm : modifyRoom room rms |
1327 | 171 |
|
172 |
modifyTeam :: RoomInfo -> TeamInfo -> RoomInfo |
|
173 |
modifyTeam room team = room{teams = replaceTeam team $ teams room} |
|
174 |
where |
|
175 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
176 |
replaceTeam team (t:teams) = |
|
177 |
if teamname team == teamname t then |
|
178 |
team : teams |
|
179 |
else |
|
180 |
t : replaceTeam team teams |