netserver/HWProto.hs
author unc0rr
Fri, 09 May 2008 12:58:10 +0000
changeset 921 8dd71d960cbc
parent 903 d4e5d8cbe449
child 1082 596b1dcdc1df
permissions -rw-r--r--
If there's more hedgehogs in AI's team than in others, then it will be less afraid to give damage to friend hedgehog
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     1
module HWProto where
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     2
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     3
import IO
896
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
     4
import Data.List
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     5
import Data.Word
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     6
import Miscutils
896
93df8ac94382 Handle password parameter on JOIN
unc0rr
parents: 895
diff changeset
     7
import Maybe (fromMaybe, fromJust)
890
1d8c4a5ec622 - Improve server core
unc0rr
parents:
diff changeset
     8
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
     9
-- 'noInfo' clients state command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    10
handleCmd_noInfo :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    11
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    12
handleCmd_noInfo clhandle clients rooms ("NICK":newNick:[]) =
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    13
	if not . null $ nick client then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    14
		(clients, rooms, [clhandle], ["ERROR", "The nick already chosen"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    15
	else if haveSameNick then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    16
		(clients, rooms, [clhandle], ["WARNING", "Choose another nick"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    17
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    18
		(modifyClient clhandle clients (\cl -> cl{nick = newNick}), rooms, [clhandle], ["NICK", newNick])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    19
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    20
		haveSameNick = not . null $ filter (\cl -> newNick == nick cl) clients
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    21
		client = clientByHandle clhandle clients
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    22
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    23
handleCmd_noInfo clhandle clients rooms ("PROTO":protoNum:[]) =
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    24
	if protocol client > 0 then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    25
		(clients, rooms, [clhandle], ["ERROR", "Protocol number already known"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    26
	else if parsedProto == 0 then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    27
		(clients, rooms, [clhandle], ["ERROR", "Bad input"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    28
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    29
		(modifyClient clhandle clients (\cl -> cl{protocol = parsedProto}), rooms, [], [])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    30
	where
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    31
		parsedProto = fromMaybe 0 (maybeRead protoNum :: Maybe Word16)
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    32
		client = clientByHandle clhandle clients
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    33
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    34
handleCmd_noInfo clhandle clients rooms _ = (clients, rooms, [clhandle], ["ERROR", "Bad command or incorrect parameter"])
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    35
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    36
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    37
-- 'noRoom' clients state command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    38
handleCmd_noRoom :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    39
903
d4e5d8cbe449 Implement LIST command
unc0rr
parents: 902
diff changeset
    40
handleCmd_noRoom clhandle clients rooms ("LIST":[]) =
d4e5d8cbe449 Implement LIST command
unc0rr
parents: 902
diff changeset
    41
		(clients, rooms, [clhandle], ["ROOMS"] ++ map (\r -> name r) rooms)
d4e5d8cbe449 Implement LIST command
unc0rr
parents: 902
diff changeset
    42
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    43
handleCmd_noRoom clhandle clients rooms ("CREATE":newRoom:roomPassword:[]) =
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    44
	if haveSameRoom then
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    45
		(clients, rooms, [clhandle], ["WARNING", "There's already a room with that name"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    46
	else
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    47
		(modifyClient clhandle clients (\cl -> cl{room = newRoom, isMaster = True}), (RoomInfo newRoom roomPassword):rooms, [clhandle], ["JOINS", nick client])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    48
	where
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    49
		haveSameRoom = not . null $ filter (\room -> newRoom == name room) rooms
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    50
		client = clientByHandle clhandle clients
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    51
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    52
handleCmd_noRoom clhandle clients rooms ("CREATE":newRoom:[]) =
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    53
	handleCmd_noRoom clhandle clients rooms ["CREATE", newRoom, ""]
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    54
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    55
handleCmd_noRoom clhandle clients rooms ("JOIN":roomName:roomPassword:[]) =
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    56
	if noSuchRoom then
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    57
		(clients, rooms, [clhandle], ["WARNING", "There's no room with that name"])
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    58
	else if roomPassword /= password (roomByName roomName rooms) then
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    59
		(clients, rooms, [clhandle], ["WARNING", "Wrong password"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    60
	else
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    61
		(modifyClient clhandle clients (\cl -> cl{room = roomName}), rooms, clhandle : (fromRoomHandles roomName clients), ["JOINS", nick client])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    62
	where
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    63
		noSuchRoom = null $ filter (\room -> roomName == name room) rooms
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    64
		client = clientByHandle clhandle clients
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    65
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    66
handleCmd_noRoom clhandle clients rooms ("JOIN":roomName:[]) =
902
3cc10f0aae37 Finish conversion
unc0rr
parents: 901
diff changeset
    67
	handleCmd_noRoom clhandle clients rooms ["JOIN", roomName, ""]
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    68
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    69
handleCmd_noRoom clhandle clients rooms _ = (clients, rooms, [clhandle], ["ERROR", "Bad command or incorrect parameter"])
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    70
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    71
-- 'inRoom' clients state command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    72
handleCmd_inRoom :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    73
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    74
handleCmd_inRoom clhandle clients rooms _ = (clients, rooms, [clhandle], ["ERROR", "Bad command or incorrect parameter"])
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    75
898
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    76
-- state-independent command handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    77
handleCmd :: Handle -> [ClientInfo] -> [RoomInfo] -> [String] -> ([ClientInfo], [RoomInfo], [Handle], [String])
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    78
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    79
handleCmd clhandle clients rooms ("QUIT":xs) =
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    80
	if null (room client) then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    81
		(clients, rooms, [clhandle], ["QUIT"])
898
344ba7dba23d Handle QUIT of the master: disconnect all roommates
unc0rr
parents: 897
diff changeset
    82
	else if isMaster client then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    83
		(clients, filter (\rm -> room client /= name rm) rooms, roomMates, ["ROOMABANDONED"]) -- core disconnects clients on ROOMABANDONED command
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    84
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    85
		(clients, rooms, roomMates, ["QUIT", nick client])
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    86
	where
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    87
		client = clientByHandle clhandle clients
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    88
		roomMates = fromRoomHandles (room client) clients
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    89
895
6aee2f335726 - Remove old hwserv code
unc0rr
parents: 894
diff changeset
    90
-- check state and call state-dependent commmand handlers
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    91
handleCmd clhandle clients rooms cmd =
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    92
	if null (nick client) || protocol client == 0 then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    93
		handleCmd_noInfo clhandle clients rooms cmd
897
35d91fa3753b 'In room' state stub
unc0rr
parents: 896
diff changeset
    94
	else if null (room client) then
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    95
		handleCmd_noRoom clhandle clients rooms cmd
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    96
	else
901
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    97
		handleCmd_inRoom clhandle clients rooms cmd
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    98
	where
2f5ce9a584f9 Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents: 899
diff changeset
    99
		client = clientByHandle clhandle clients