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