author | unc0rr |
Fri, 24 Oct 2008 17:53:14 +0000 | |
changeset 1411 | df78c9571bc7 |
parent 1408 | fab171a17968 |
child 1461 | 87e5a6c3882c |
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 |
849 | 10 |
|
851 | 11 |
data ClientInfo = |
1082 | 12 |
ClientInfo |
851 | 13 |
{ |
1082 | 14 |
chan :: TChan [String], |
851 | 15 |
handle :: Handle, |
16 |
nick :: String, |
|
894 | 17 |
protocol :: Word16, |
851 | 18 |
room :: String, |
1391 | 19 |
isMaster :: Bool, |
1403 | 20 |
isReady :: Bool, |
1391 | 21 |
forceQuit :: Bool |
851 | 22 |
} |
23 |
||
1082 | 24 |
instance Eq ClientInfo where |
25 |
a1 == a2 = handle a1 == handle a2 |
|
26 |
||
1317 | 27 |
data HedgehogInfo = |
28 |
HedgehogInfo String String |
|
29 |
||
1083 | 30 |
data TeamInfo = |
31 |
TeamInfo |
|
32 |
{ |
|
1329 | 33 |
teamowner :: String, |
1317 | 34 |
teamname :: String, |
1321 | 35 |
teamcolor :: String, |
36 |
teamgrave :: String, |
|
37 |
teamfort :: String, |
|
38 |
difficulty :: Int, |
|
1327 | 39 |
hhnum :: Int, |
1317 | 40 |
hedgehogs :: [HedgehogInfo] |
1083 | 41 |
} |
42 |
||
851 | 43 |
data RoomInfo = |
44 |
RoomInfo |
|
45 |
{ |
|
46 |
name :: String, |
|
1083 | 47 |
password :: String, |
1317 | 48 |
roomProto :: Word16, |
49 |
teams :: [TeamInfo], |
|
1333
b0b0510eb82d
- Fix a bug with chosen map (new clinet gets wrong information)
unc0rr
parents:
1329
diff
changeset
|
50 |
gamemap :: String, |
1350 | 51 |
gameinprogress :: Bool, |
1396 | 52 |
playersIn :: Int, |
1403 | 53 |
readyPlayers :: Int, |
1411 | 54 |
isRestrictedJoins :: Bool, |
55 |
isRestrictedTeams :: Bool, |
|
1317 | 56 |
params :: Map.Map String [String] |
851 | 57 |
} |
1411 | 58 |
createRoom = (RoomInfo "" "" 0 [] "+rnd+" False 1 0 False False Map.empty) |
851 | 59 |
|
1082 | 60 |
type ClientsTransform = [ClientInfo] -> [ClientInfo] |
61 |
type RoomsTransform = [RoomInfo] -> [RoomInfo] |
|
62 |
type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle] |
|
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1083
diff
changeset
|
63 |
type CmdHandler = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [String] -> (ClientsTransform, RoomsTransform, [(HandlesSelector, [String])]) |
1082 | 64 |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
65 |
|
902 | 66 |
roomByName :: String -> [RoomInfo] -> RoomInfo |
67 |
roomByName roomName rooms = fromJust $ find (\room -> roomName == name room) rooms |
|
68 |
||
1082 | 69 |
tselect :: [ClientInfo] -> STM ([String], ClientInfo) |
70 |
tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci)) |
|
889 | 71 |
|
894 | 72 |
maybeRead :: Read a => String -> Maybe a |
73 |
maybeRead s = case reads s of |
|
74 |
[(x, rest)] | all isSpace rest -> Just x |
|
75 |
_ -> Nothing |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
76 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
77 |
deleteBy2t :: (a -> b -> Bool) -> b -> [a] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
78 |
deleteBy2t _ _ [] = [] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
79 |
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
|
80 |
|
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
81 |
deleteFirstsBy2t :: (a -> b -> Bool) -> [a] -> [b] -> [a] |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
895
diff
changeset
|
82 |
deleteFirstsBy2t eq = foldl (flip (deleteBy2t eq)) |
1082 | 83 |
|
84 |
sameRoom :: HandlesSelector |
|
85 |
sameRoom client clients rooms = map handle $ filter (\ci -> room ci == room client) clients |
|
86 |
||
87 |
othersInRoom :: HandlesSelector |
|
88 |
othersInRoom client clients rooms = map handle $ filter (client /=) $ filter (\ci -> room ci == room client) clients |
|
89 |
||
90 |
fromRoom :: String -> HandlesSelector |
|
91 |
fromRoom roomName _ clients _ = map handle $ filter (\ci -> room ci == roomName) clients |
|
92 |
||
93 |
clientOnly :: HandlesSelector |
|
94 |
clientOnly client _ _ = [handle client] |
|
95 |
||
96 |
noChangeClients :: ClientsTransform |
|
97 |
noChangeClients a = a |
|
98 |
||
99 |
modifyClient :: ClientInfo -> ClientsTransform |
|
1321 | 100 |
modifyClient _ [] = error "modifyClient: no such client" |
1082 | 101 |
modifyClient client (cl:cls) = |
102 |
if cl == client then |
|
103 |
client : cls |
|
104 |
else |
|
105 |
cl : (modifyClient client cls) |
|
106 |
||
1408 | 107 |
modifyRoomClients :: RoomInfo -> (ClientInfo -> ClientInfo) -> ClientsTransform |
108 |
modifyRoomClients clientsroom clientMod clients = map (\c -> if name clientsroom == room c then clientMod c else c) clients |
|
109 |
||
1082 | 110 |
noChangeRooms :: RoomsTransform |
111 |
noChangeRooms a = a |
|
112 |
||
113 |
addRoom :: RoomInfo -> RoomsTransform |
|
114 |
addRoom room rooms = room:rooms |
|
115 |
||
116 |
removeRoom :: String -> RoomsTransform |
|
117 |
removeRoom roomname rooms = filter (\rm -> roomname /= name rm) rooms |
|
1317 | 118 |
|
1321 | 119 |
modifyRoom :: RoomInfo -> RoomsTransform |
120 |
modifyRoom _ [] = error "changeRoomConfig: no such room" |
|
121 |
modifyRoom room (rm:rms) = |
|
122 |
if name room == name rm then |
|
123 |
room : rms |
|
1317 | 124 |
else |
1402 | 125 |
rm : modifyRoom room rms |
1327 | 126 |
|
127 |
modifyTeam :: RoomInfo -> TeamInfo -> RoomInfo |
|
128 |
modifyTeam room team = room{teams = replaceTeam team $ teams room} |
|
129 |
where |
|
130 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
131 |
replaceTeam team (t:teams) = |
|
132 |
if teamname team == teamname t then |
|
133 |
team : teams |
|
134 |
else |
|
135 |
t : replaceTeam team teams |