author | unc0rr |
Sun, 14 Dec 2008 13:48:29 +0000 | |
changeset 1545 | 76b90ff9739a |
parent 1513 | a35c90263e27 |
child 1558 | 3370b7ffeb5c |
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, |
|
1317 | 61 |
params :: Map.Map String [String] |
851 | 62 |
} |
1492 | 63 |
createRoom = ( |
64 |
RoomInfo |
|
65 |
"" |
|
66 |
"" |
|
67 |
0 |
|
68 |
[] |
|
69 |
"+rnd+" |
|
70 |
False |
|
71 |
1 |
|
72 |
0 |
|
73 |
False |
|
74 |
False |
|
75 |
Map.empty |
|
76 |
) |
|
851 | 77 |
|
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
78 |
data ServerInfo = |
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
79 |
ServerInfo |
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
80 |
{ |
1492 | 81 |
isDedicated :: Bool, |
82 |
serverMessage :: String, |
|
1493 | 83 |
listenPort :: PortNumber, |
84 |
loginsNumber :: Int, |
|
85 |
lastHourUsers :: [UTCTime] |
|
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
86 |
} |
1492 | 87 |
newServerInfo = ( |
88 |
ServerInfo |
|
89 |
True |
|
90 |
"<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>" |
|
91 |
46631 |
|
1493 | 92 |
0 |
93 |
[] |
|
1492 | 94 |
) |
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
95 |
|
1082 | 96 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
97 |
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
|
98 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [ClientInfo] |
1492 | 99 |
type Answer = ServerInfo -> (HandlesSelector, [String]) |
1491
0b1f44751509
Make answers creation more abstract, in prepare for a conversion
unc0rr
parents:
1484
diff
changeset
|
100 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, [Answer]) |
1082 | 101 |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
102 |
|
902 | 103 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
104 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
105 |
||
1082 | 106 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
107 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 108 |
|
894 | 109 |
maybeRead :: Read a => String -> Maybe a |
110 |
maybeRead s = case reads s of |
|
111 |
[(x, rest)] | all isSpace rest -> Just x |
|
112 |
_ -> Nothing |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
113 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
114 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
115 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
116 |
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
|
117 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
118 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
119 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 120 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
121 |
--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
|
122 |
--clientByHandle chandle clients = find (\c -> handle c == chandle) clients |
1466 | 123 |
|
1082 | 124 |
sameRoom :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
125 |
sameRoom client clients rooms = filter (\ci -> room ci == room client) clients |
1082 | 126 |
|
1484 | 127 |
noRoomSameProto :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
128 |
noRoomSameProto client clients _ = filter (null . room) $ filter (\ci -> protocol client == protocol ci) clients |
1484 | 129 |
|
1082 | 130 |
othersInRoom :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
131 |
othersInRoom client clients rooms = filter (client /=) $ filter (\ci -> room ci == room client) clients |
1082 | 132 |
|
133 |
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
|
134 |
fromRoom roomName _ clients _ = filter (\ci -> room ci == roomName) clients |
1082 | 135 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
136 |
allClients :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
137 |
allClients _ clients _ = clients |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1411
diff
changeset
|
138 |
|
1082 | 139 |
clientOnly :: HandlesSelector |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1493
diff
changeset
|
140 |
clientOnly client _ _ = [client] |
1082 | 141 |
|
142 |
noChangeClients :: ClientsTransform |
|
143 |
noChangeClients a = a |
|
144 |
||
145 |
modifyClient :: ClientInfo -> ClientsTransform |
|
1321 | 146 |
modifyClient _ [] = error "modifyClient: no such client" |
1082 | 147 |
modifyClient client (cl:cls) = |
148 |
if cl == client then |
|
149 |
client : cls |
|
150 |
else |
|
151 |
cl : (modifyClient client cls) |
|
152 |
||
1408 | 153 |
modifyRoomClients :: RoomInfo -> (ClientInfo -> ClientInfo) -> ClientsTransform |
154 |
modifyRoomClients clientsroom clientMod clients = map (\c -> if name clientsroom == room c then clientMod c else c) clients |
|
155 |
||
1082 | 156 |
noChangeRooms :: RoomsTransform |
157 |
noChangeRooms a = a |
|
158 |
||
159 |
addRoom :: RoomInfo -> RoomsTransform |
|
160 |
addRoom room rooms = room:rooms |
|
161 |
||
162 |
removeRoom :: String -> RoomsTransform |
|
163 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
1317 | 164 |
|
1321 | 165 |
modifyRoom :: RoomInfo -> RoomsTransform |
166 |
modifyRoom _ [] = error "changeRoomConfig: no such room" |
|
167 |
modifyRoom room (rm:rms) = |
|
168 |
if name room == name rm then |
|
169 |
room : rms |
|
1317 | 170 |
else |
1402 | 171 |
rm : modifyRoom room rms |
1327 | 172 |
|
173 |
modifyTeam :: RoomInfo -> TeamInfo -> RoomInfo |
|
174 |
modifyTeam room team = room{teams = replaceTeam team $ teams room} |
|
175 |
where |
|
176 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
177 |
replaceTeam team (t:teams) = |
|
178 |
if teamname team == teamname t then |
|
179 |
team : teams |
|
180 |
else |
|
181 |
t : replaceTeam team teams |