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