author | unc0rr |
Thu, 08 Jan 2009 22:40:56 +0000 | |
changeset 1620 | 5f4cd6708eb5 |
parent 1618 | 2b9cadc232ab |
child 1646 | 19b3784ac9d2 |
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 |
||
1618 | 133 |
otherLobbyClients :: HandlesSelector |
134 |
otherLobbyClients client clients rooms = filter (\ci -> room ci == []) clients |
|
135 |
||
1484 | 136 |
noRoomSameProto :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
137 |
noRoomSameProto client clients _ = filter (null . room) $ filter (\ci -> protocol client == protocol ci) clients |
1484 | 138 |
|
1082 | 139 |
othersInRoom :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
140 |
othersInRoom client clients rooms = filter (client /=) $ filter (\ci -> room ci == room client) clients |
1082 | 141 |
|
142 |
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
|
143 |
fromRoom roomName _ clients _ = filter (\ci -> room ci == roomName) clients |
1082 | 144 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
145 |
allClients :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
146 |
allClients _ clients _ = clients |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
147 |
|
1082 | 148 |
clientOnly :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
149 |
clientOnly client _ _ = [client] |
1082 | 150 |
|
151 |
noChangeClients :: ClientsTransform |
|
152 |
noChangeClients a = a |
|
153 |
||
154 |
modifyClient :: ClientInfo -> ClientsTransform |
|
1321 | 155 |
modifyClient _ [] = error "modifyClient: no such client" |
1082 | 156 |
modifyClient client (cl:cls) = |
157 |
if cl == client then |
|
158 |
client : cls |
|
159 |
else |
|
160 |
cl : (modifyClient client cls) |
|
161 |
||
1408 | 162 |
modifyRoomClients :: RoomInfo -> (ClientInfo -> ClientInfo) -> ClientsTransform |
163 |
modifyRoomClients clientsroom clientMod clients = map (\c -> if name clientsroom == room c then clientMod c else c) clients |
|
164 |
||
1082 | 165 |
noChangeRooms :: RoomsTransform |
166 |
noChangeRooms a = a |
|
167 |
||
168 |
addRoom :: RoomInfo -> RoomsTransform |
|
169 |
addRoom room rooms = room:rooms |
|
170 |
||
171 |
removeRoom :: String -> RoomsTransform |
|
172 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
1317 | 173 |
|
1321 | 174 |
modifyRoom :: RoomInfo -> RoomsTransform |
175 |
modifyRoom _ [] = error "changeRoomConfig: no such room" |
|
176 |
modifyRoom room (rm:rms) = |
|
177 |
if name room == name rm then |
|
178 |
room : rms |
|
1317 | 179 |
else |
1402 | 180 |
rm : modifyRoom room rms |
1327 | 181 |
|
182 |
modifyTeam :: RoomInfo -> TeamInfo -> RoomInfo |
|
183 |
modifyTeam room team = room{teams = replaceTeam team $ teams room} |
|
184 |
where |
|
185 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
186 |
replaceTeam team (t:teams) = |
|
187 |
if teamname team == teamname t then |
|
188 |
team : teams |
|
189 |
else |
|
190 |
t : replaceTeam team teams |
|
1577 | 191 |
|
192 |
proto2ver :: Word16 -> String |
|
193 |
proto2ver 17 = "0.9.7-dev" |
|
194 |
proto2ver 19 = "0.9.7" |
|
195 |
proto2ver 20 = "0.9.8-dev" |
|
1617 | 196 |
proto2ver 21 = "0.9.8" |
197 |
proto2ver 22 = "0.9.9-dev" |
|
1578 | 198 |
proto2ver _ = "Unknown" |