1804
|
1 |
module HWProtoNEState where
|
|
2 |
|
|
3 |
import qualified Data.IntMap as IntMap
|
|
4 |
import Maybe
|
|
5 |
import Data.List
|
|
6 |
import Data.Word
|
|
7 |
--------------------------------------
|
|
8 |
import CoreTypes
|
|
9 |
import Actions
|
|
10 |
import Utils
|
|
11 |
|
|
12 |
handleCmd_NotEntered :: CmdHandler
|
|
13 |
|
|
14 |
onLoginFinished :: Int -> String -> Word16 -> Clients -> [Action]
|
|
15 |
onLoginFinished clID clientNick clProto clients =
|
|
16 |
if (null $ clientNick) || (clProto == 0) then
|
|
17 |
[]
|
|
18 |
else
|
|
19 |
(RoomAddThisClient 0)
|
1834
|
20 |
: CheckRegistered
|
1804
|
21 |
: answerLobbyNicks
|
|
22 |
-- ++ (answerServerMessage client clients)
|
|
23 |
where
|
|
24 |
lobbyNicks = filter (\n -> (not (null n))) $ map nick $ IntMap.elems clients
|
|
25 |
answerLobbyNicks = if not $ null lobbyNicks then
|
|
26 |
[AnswerThisClient (["LOBBY:JOINED"] ++ lobbyNicks)]
|
|
27 |
else
|
|
28 |
[]
|
|
29 |
|
|
30 |
|
|
31 |
handleCmd_NotEntered clID clients _ ["NICK", newNick] =
|
|
32 |
if not . null $ nick client then
|
|
33 |
[ProtocolError "Nick already chosen"]
|
|
34 |
else if haveSameNick then
|
|
35 |
[AnswerThisClient ["WARNING", "Nick collision"]]
|
|
36 |
++ [ByeClient ""]
|
|
37 |
else
|
|
38 |
[ModifyClient (\c -> c{nick = newNick}),
|
|
39 |
AnswerThisClient ["NICK", newNick]]
|
|
40 |
++ (onLoginFinished clID newNick (clientProto client) clients)
|
|
41 |
where
|
|
42 |
client = clients IntMap.! clID
|
|
43 |
haveSameNick = isJust $ find (\cl -> newNick == nick cl) $ IntMap.elems clients
|
|
44 |
|
|
45 |
|
|
46 |
handleCmd_NotEntered clID clients _ ["PROTO", protoNum] =
|
|
47 |
if clientProto client > 0 then
|
|
48 |
[ProtocolError "Protocol already known"]
|
|
49 |
else if parsedProto == 0 then
|
|
50 |
[ProtocolError "Bad number"]
|
|
51 |
else
|
|
52 |
[ModifyClient (\c -> c{clientProto = parsedProto}),
|
|
53 |
AnswerThisClient ["PROTO", show parsedProto]]
|
|
54 |
++ (onLoginFinished clID (nick client) parsedProto clients)
|
|
55 |
where
|
|
56 |
client = clients IntMap.! clID
|
|
57 |
parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
|
|
58 |
|
|
59 |
|
|
60 |
handleCmd_NotEntered _ _ _ ["DUMP"] =
|
|
61 |
[Dump]
|
|
62 |
|
|
63 |
|
|
64 |
handleCmd_NotEntered clID _ _ _ = [ProtocolError "Incorrect command (state: not entered)"]
|