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