author | nemo |
Tue, 02 Mar 2010 19:46:35 +0000 | |
changeset 2912 | 76d43f7458ad |
parent 2867 | 9be6693c78cb |
child 2952 | 18fada739b55 |
permissions | -rw-r--r-- |
1804 | 1 |
module Utils where |
2 |
||
3 |
import Control.Concurrent |
|
4 |
import Control.Concurrent.STM |
|
5 |
import Data.Char |
|
6 |
import Data.Word |
|
7 |
import qualified Data.Map as Map |
|
8 |
import qualified Data.IntMap as IntMap |
|
2304 | 9 |
import qualified Data.Set as Set |
2310 | 10 |
import Data.ByteString.Internal (w2c) |
1917 | 11 |
import Numeric |
12 |
import Network.Socket |
|
1964 | 13 |
import System.IO |
1917 | 14 |
import qualified Data.List as List |
2349 | 15 |
import Control.Monad |
2304 | 16 |
import Maybe |
1804 | 17 |
------------------------------------------------- |
18 |
import qualified Codec.Binary.Base64 as Base64 |
|
19 |
import qualified Codec.Binary.UTF8.String as UTF8 |
|
20 |
import CoreTypes |
|
21 |
||
1917 | 22 |
|
23 |
sockAddr2String :: SockAddr -> IO String |
|
24 |
sockAddr2String (SockAddrInet _ hostAddr) = inet_ntoa hostAddr |
|
25 |
sockAddr2String (SockAddrInet6 _ _ (a, b, c, d) _) = |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
26 |
return $ (foldr1 (.) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
27 |
$ List.intersperse (\a -> ':':a) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
28 |
$ concatMap (\n -> (\(a, b) -> [showHex a, showHex b]) $ divMod n 65536) [a, b, c, d]) [] |
1917 | 29 |
|
1804 | 30 |
toEngineMsg :: String -> String |
2403 | 31 |
toEngineMsg msg = Base64.encode (fromIntegral (length encodedMsg) : encodedMsg) |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
32 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
33 |
encodedMsg = UTF8.encode msg |
1804 | 34 |
|
2304 | 35 |
fromEngineMsg :: String -> Maybe String |
2349 | 36 |
fromEngineMsg msg = liftM (map w2c) (Base64.decode msg >>= removeLength) |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
37 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
38 |
removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
39 |
removeLength _ = Nothing |
2304 | 40 |
|
2381
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2349
diff
changeset
|
41 |
checkNetCmd :: String -> (Bool, Bool) |
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2349
diff
changeset
|
42 |
checkNetCmd msg = check decoded |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
43 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
44 |
decoded = fromEngineMsg msg |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
45 |
check Nothing = (False, False) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
46 |
check (Just (m:ms)) = (m `Set.member` legalMessages, m == '+') |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
47 |
check _ = (False, False) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
48 |
legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghb12345" ++ slotMessages |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
49 |
slotMessages = "\128\129\130\131\132\133\134\135\136\137\138" |
1804 | 50 |
|
51 |
maybeRead :: Read a => String -> Maybe a |
|
52 |
maybeRead s = case reads s of |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
53 |
[(x, rest)] | all isSpace rest -> Just x |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
54 |
_ -> Nothing |
1804 | 55 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
56 |
teamToNet :: Word16 -> TeamInfo -> [String] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
57 |
teamToNet protocol team |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
58 |
| protocol < 30 = [ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
59 |
"ADD_TEAM", |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
60 |
teamname team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
61 |
teamgrave team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
62 |
teamfort team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
63 |
teamvoicepack team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
64 |
teamowner team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
65 |
show $ difficulty team |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
66 |
] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
67 |
++ hhsInfo |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
68 |
| otherwise = [ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
69 |
"ADD_TEAM", |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
70 |
teamname team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
71 |
teamgrave team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
72 |
teamfort team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
73 |
teamvoicepack team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
74 |
teamflag team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
75 |
teamowner team, |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
76 |
show $ difficulty team |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
77 |
] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
78 |
++ hhsInfo |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
79 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
80 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
1804 | 81 |
|
82 |
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo |
|
83 |
modifyTeam team room = room{teams = replaceTeam team $ teams room} |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
84 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
85 |
replaceTeam _ [] = error "modifyTeam: no such team" |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
86 |
replaceTeam team (t:teams) = |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
87 |
if teamname team == teamname t then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
88 |
team : teams |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
89 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
90 |
t : replaceTeam team teams |
1804 | 91 |
|
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
92 |
illegalName :: String -> Bool |
2349 | 93 |
illegalName = all isSpace |
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
94 |
|
1804 | 95 |
protoNumber2ver :: Word16 -> String |
96 |
protoNumber2ver 17 = "0.9.7-dev" |
|
97 |
protoNumber2ver 19 = "0.9.7" |
|
98 |
protoNumber2ver 20 = "0.9.8-dev" |
|
99 |
protoNumber2ver 21 = "0.9.8" |
|
100 |
protoNumber2ver 22 = "0.9.9-dev" |
|
101 |
protoNumber2ver 23 = "0.9.9" |
|
102 |
protoNumber2ver 24 = "0.9.10-dev" |
|
103 |
protoNumber2ver 25 = "0.9.10" |
|
1953 | 104 |
protoNumber2ver 26 = "0.9.11-dev" |
2113 | 105 |
protoNumber2ver 27 = "0.9.11" |
106 |
protoNumber2ver 28 = "0.9.12-dev" |
|
2448 | 107 |
protoNumber2ver 29 = "0.9.12" |
108 |
protoNumber2ver 30 = "0.9.13-dev" |
|
1804 | 109 |
protoNumber2ver _ = "Unknown" |
110 |
||
1964 | 111 |
askFromConsole :: String -> IO String |
112 |
askFromConsole msg = do |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
113 |
putStr msg |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
114 |
hFlush stdout |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2747
diff
changeset
|
115 |
getLine |