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