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