author | Vekhir |
Sun, 11 Feb 2024 20:33:15 +0100 | |
changeset 15985 | e42d1819b150 |
parent 15983 | 2c92499daa67 |
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 |
||
15983 | 21 |
import Control.Monad |
3435 | 22 |
import Control.Monad.Reader |
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
23 |
import qualified Data.ByteString.Char8 as B |
4614 | 24 |
import Data.List |
15878
fc3cb23fd26f
Allow to see rooms of incompatible versions in the lobby
S.D.
parents:
11046
diff
changeset
|
25 |
import Data.Word |
3435 | 26 |
|
27 |
import RoomsAndClients |
|
28 |
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
|
29 |
|
3435 | 30 |
|
10212 | 31 |
type CmdHandler = [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
32 |
||
3435 | 33 |
thisClient :: Reader (ClientIndex, IRnC) ClientInfo |
34 |
thisClient = do |
|
35 |
(ci, rnc) <- ask |
|
36 |
return $ rnc `client` ci |
|
37 |
||
3568 | 38 |
thisRoom :: Reader (ClientIndex, IRnC) RoomInfo |
39 |
thisRoom = do |
|
40 |
(ci, rnc) <- ask |
|
41 |
let ri = clientRoom rnc ci |
|
42 |
return $ rnc `room` ri |
|
43 |
||
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3435
diff
changeset
|
44 |
clientNick :: Reader (ClientIndex, IRnC) B.ByteString |
3435 | 45 |
clientNick = liftM nick thisClient |
46 |
||
47 |
roomOthersChans :: Reader (ClientIndex, IRnC) [ClientChan] |
|
48 |
roomOthersChans = do |
|
49 |
(ci, rnc) <- ask |
|
50 |
let ri = clientRoom rnc ci |
|
3542 | 51 |
return $ map (sendChan . client rnc) $ filter (/= ci) (roomClients rnc ri) |
3435 | 52 |
|
4614 | 53 |
roomSameClanChans :: Reader (ClientIndex, IRnC) [ClientChan] |
54 |
roomSameClanChans = do |
|
55 |
(ci, rnc) <- ask |
|
56 |
let ri = clientRoom rnc ci |
|
57 |
let otherRoomClients = map (client rnc) . filter (/= ci) $ roomClients rnc ri |
|
58 |
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
|
59 |
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
|
60 |
return $ map sendChan sameClanClients |
4614 | 61 |
|
3543 | 62 |
roomClientsChans :: Reader (ClientIndex, IRnC) [ClientChan] |
63 |
roomClientsChans = do |
|
64 |
(ci, rnc) <- ask |
|
65 |
let ri = clientRoom rnc ci |
|
66 |
return $ map (sendChan . client rnc) (roomClients rnc ri) |
|
67 |
||
3435 | 68 |
thisClientChans :: Reader (ClientIndex, IRnC) [ClientChan] |
69 |
thisClientChans = do |
|
70 |
(ci, rnc) <- ask |
|
4932 | 71 |
return [sendChan (rnc `client` ci)] |
3435 | 72 |
|
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
|
73 |
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
|
74 |
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
|
75 |
(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
|
76 |
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
|
77 |
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
|
78 |
|
15878
fc3cb23fd26f
Allow to see rooms of incompatible versions in the lobby
S.D.
parents:
11046
diff
changeset
|
79 |
allChansProto :: Reader (ClientIndex, IRnC) [(ClientChan, Word16)] |
fc3cb23fd26f
Allow to see rooms of incompatible versions in the lobby
S.D.
parents:
11046
diff
changeset
|
80 |
allChansProto = do |
fc3cb23fd26f
Allow to see rooms of incompatible versions in the lobby
S.D.
parents:
11046
diff
changeset
|
81 |
(ci, rnc) <- ask |
fc3cb23fd26f
Allow to see rooms of incompatible versions in the lobby
S.D.
parents:
11046
diff
changeset
|
82 |
return . map ((\c -> (sendChan c, clientProto c)) . client rnc) $ allClients rnc |
fc3cb23fd26f
Allow to see rooms of incompatible versions in the lobby
S.D.
parents:
11046
diff
changeset
|
83 |
|
4989 | 84 |
answerClient :: [B.ByteString] -> Reader (ClientIndex, IRnC) [Action] |
4932 | 85 |
answerClient msg = liftM ((: []) . flip AnswerClients msg) thisClientChans |
3501 | 86 |
|
87 |
allRoomInfos :: Reader (a, IRnC) [RoomInfo] |
|
88 |
allRoomInfos = liftM ((\irnc -> map (room irnc) $ allRooms irnc) . snd) ask |
|
4614 | 89 |
|
90 |
clientByNick :: B.ByteString -> Reader (ClientIndex, IRnC) (Maybe ClientIndex) |
|
91 |
clientByNick n = do |
|
92 |
(_, rnc) <- ask |
|
93 |
let allClientIDs = allClients rnc |
|
9433 | 94 |
return $ find (\clId -> let cl = client rnc clId in n == nick cl && not (isChecker cl)) allClientIDs |
4614 | 95 |
|
10194 | 96 |
|
97 |
roomAdminOnly :: Reader (ClientIndex, IRnC) [Action] -> Reader (ClientIndex, IRnC) [Action] |
|
98 |
roomAdminOnly h = thisClient >>= \cl -> if isMaster cl then h else return [] |
|
99 |
||
100 |
||
101 |
serverAdminOnly :: Reader (ClientIndex, IRnC) [Action] -> Reader (ClientIndex, IRnC) [Action] |
|
102 |
serverAdminOnly h = thisClient >>= \cl -> if isAdministrator cl then h else return [] |