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