author | sheepluva |
Fri, 06 May 2016 20:00:55 +0200 | |
changeset 11810 | 42c11b73f68e |
parent 11046 | 47a8c19ecb60 |
permissions | -rw-r--r-- |
10460
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
1 |
{- |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
10460
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
4 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
8 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
12 |
* GNU General Public License for more details. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
13 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
17 |
\-} |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
10212
diff
changeset
|
18 |
|
3435 | 19 |
module HandlerUtils where |
20 |
||
21 |
import Control.Monad.Reader |
|
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
22 |
import qualified Data.ByteString.Char8 as B |
4614 | 23 |
import Data.List |
3435 | 24 |
|
25 |
import RoomsAndClients |
|
26 |
import CoreTypes |
|
9109
878f06e9c484
- Fix issue 521 by resending FULLMAPCONFIG on game finish to those who joined mid-game. Semitested.
unc0rr
parents:
6541
diff
changeset
|
27 |
|
3435 | 28 |
|
10212 | 29 |
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
30 |
||
3435 | 31 |
thisClient :: Reader (ClientIndex, IRnC) ClientInfo |
32 |
thisClient = do |
|
33 |
(ci, rnc) <- ask |
|
34 |
return $ rnc `client` ci |
|
35 |
||
3568 | 36 |
thisRoom :: Reader (ClientIndex, IRnC) RoomInfo |
37 |
thisRoom = do |
|
38 |
(ci, rnc) <- ask |
|
39 |
let ri = clientRoom rnc ci |
|
40 |
return $ rnc `room` ri |
|
41 |
||
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
42 |
clientNick :: Reader (ClientIndex, IRnC) B.ByteString |
3435 | 43 |
clientNick = liftM nick thisClient |
44 |
||
45 |
roomOthersChans :: Reader (ClientIndex, IRnC) [ClientChan] |
|
46 |
roomOthersChans = do |
|
47 |
(ci, rnc) <- ask |
|
48 |
let ri = clientRoom rnc ci |
|
3542 | 49 |
return $ map (sendChan . client rnc) $ filter (/= ci) (roomClients rnc ri) |
3435 | 50 |
|
4614 | 51 |
roomSameClanChans :: Reader (ClientIndex, IRnC) [ClientChan] |
52 |
roomSameClanChans = do |
|
53 |
(ci, rnc) <- ask |
|
54 |
let ri = clientRoom rnc ci |
|
55 |
let otherRoomClients = map (client rnc) . filter (/= ci) $ roomClients rnc ri |
|
56 |
let cl = rnc `client` ci |
|
4986
33fe91b2bcbf
Use Maybe for storing client's clan, allows less error-prone spectator checks
unc0rr
parents:
4975
diff
changeset
|
57 |
let sameClanClients = Prelude.filter (\c -> clientClan c == clientClan cl) otherRoomClients |
33fe91b2bcbf
Use Maybe for storing client's clan, allows less error-prone spectator checks
unc0rr
parents:
4975
diff
changeset
|
58 |
return $ map sendChan sameClanClients |
4614 | 59 |
|
3543 | 60 |
roomClientsChans :: Reader (ClientIndex, IRnC) [ClientChan] |
61 |
roomClientsChans = do |
|
62 |
(ci, rnc) <- ask |
|
63 |
let ri = clientRoom rnc ci |
|
64 |
return $ map (sendChan . client rnc) (roomClients rnc ri) |
|
65 |
||
3435 | 66 |
thisClientChans :: Reader (ClientIndex, IRnC) [ClientChan] |
67 |
thisClientChans = do |
|
68 |
(ci, rnc) <- ask |
|
4932 | 69 |
return [sendChan (rnc `client` ci)] |
3435 | 70 |
|
6541
08ed346ed341
Send full room info on room add and update events. Less(?) traffic, but current frontend doesn't behave good with this change to server.
unc0rr
parents:
4989
diff
changeset
|
71 |
sameProtoChans :: Reader (ClientIndex, IRnC) [ClientChan] |
08ed346ed341
Send full room info on room add and update events. Less(?) traffic, but current frontend doesn't behave good with this change to server.
unc0rr
parents:
4989
diff
changeset
|
72 |
sameProtoChans = do |
08ed346ed341
Send full room info on room add and update events. Less(?) traffic, but current frontend doesn't behave good with this change to server.
unc0rr
parents:
4989
diff
changeset
|
73 |
(ci, rnc) <- ask |
08ed346ed341
Send full room info on room add and update events. Less(?) traffic, but current frontend doesn't behave good with this change to server.
unc0rr
parents:
4989
diff
changeset
|
74 |
let p = clientProto (rnc `client` ci) |
08ed346ed341
Send full room info on room add and update events. Less(?) traffic, but current frontend doesn't behave good with this change to server.
unc0rr
parents:
4989
diff
changeset
|
75 |
return . map sendChan . filter (\c -> clientProto c == p) . map (client rnc) $ allClients rnc |
08ed346ed341
Send full room info on room add and update events. Less(?) traffic, but current frontend doesn't behave good with this change to server.
unc0rr
parents:
4989
diff
changeset
|
76 |
|
4989 | 77 |
answerClient :: [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
4932 | 78 |
answerClient msg = liftM ((: []) . flip AnswerClients msg) thisClientChans |
3501 | 79 |
|
80 |
allRoomInfos :: Reader (a, IRnC) [RoomInfo] |
|
81 |
allRoomInfos = liftM ((\irnc -> map (room irnc) $ allRooms irnc) . snd) ask |
|
4614 | 82 |
|
83 |
clientByNick :: B.ByteString -> Reader (ClientIndex, IRnC) (Maybe ClientIndex) |
|
84 |
clientByNick n = do |
|
85 |
(_, rnc) <- ask |
|
86 |
let allClientIDs = allClients rnc |
|
9433 | 87 |
return $ find (\clId -> let cl = client rnc clId in n == nick cl && not (isChecker cl)) allClientIDs |
4614 | 88 |
|
10194 | 89 |
|
90 |
roomAdminOnly :: Reader (ClientIndex, IRnC) [Action] -> Reader (ClientIndex, IRnC) [Action] |
|
91 |
roomAdminOnly h = thisClient >>= \cl -> if isMaster cl then h else return [] |
|
92 |
||
93 |
||
94 |
serverAdminOnly :: Reader (ClientIndex, IRnC) [Action] -> Reader (ClientIndex, IRnC) [Action] |
|
95 |
serverAdminOnly h = thisClient >>= \cl -> if isAdministrator cl then h else return [] |