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