author | unc0rr |
Fri, 04 Sep 2009 16:39:51 +0000 | |
changeset 2351 | a4a17b8df591 |
parent 2349 | ba7a0813c532 |
child 2381 | 959da8402cac |
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) _) = |
|
26 |
return $ (foldr1 (.) |
|
27 |
$ List.intersperse (\a -> ':':a) |
|
28 |
$ concatMap (\n -> (\(a, b) -> [showHex a, showHex b]) $ divMod n 65536) [a, b, c, d]) [] |
|
29 |
||
1804 | 30 |
toEngineMsg :: String -> String |
31 |
toEngineMsg msg = Base64.encode (fromIntegral (length msg) : (UTF8.encode msg)) |
|
32 |
||
2304 | 33 |
fromEngineMsg :: String -> Maybe String |
2349 | 34 |
fromEngineMsg msg = liftM (map w2c) (Base64.decode msg >>= removeLength) |
2304 | 35 |
where |
2310 | 36 |
removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing |
2304 | 37 |
removeLength _ = Nothing |
38 |
||
39 |
isLegalNetCommand :: String -> Bool |
|
40 |
isLegalNetCommand msg = test decoded |
|
41 |
where |
|
42 |
decoded = fromEngineMsg msg |
|
43 |
test Nothing = False |
|
44 |
test (Just (m:ms)) = m `Set.member` legalMessages |
|
2305 | 45 |
test _ = False |
2309 | 46 |
legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghb12345" ++ slotMessages |
2349 | 47 |
slotMessages = "\128\129\130\131\132\133\134\135\136\137\138" |
1804 | 48 |
|
49 |
maybeRead :: Read a => String -> Maybe a |
|
50 |
maybeRead s = case reads s of |
|
51 |
[(x, rest)] | all isSpace rest -> Just x |
|
52 |
_ -> Nothing |
|
53 |
||
54 |
teamToNet team = [ |
|
55 |
"ADD_TEAM", |
|
56 |
teamname team, |
|
57 |
teamgrave team, |
|
58 |
teamfort team, |
|
59 |
teamvoicepack team, |
|
60 |
teamowner team, |
|
61 |
show $ difficulty team |
|
62 |
] |
|
63 |
++ hhsInfo |
|
64 |
where |
|
65 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
|
66 |
||
67 |
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo |
|
68 |
modifyTeam team room = room{teams = replaceTeam team $ teams room} |
|
69 |
where |
|
70 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
71 |
replaceTeam team (t:teams) = |
|
72 |
if teamname team == teamname t then |
|
73 |
team : teams |
|
74 |
else |
|
75 |
t : replaceTeam team teams |
|
76 |
||
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
77 |
illegalName :: String -> Bool |
2349 | 78 |
illegalName = all isSpace |
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
79 |
|
1804 | 80 |
protoNumber2ver :: Word16 -> String |
81 |
protoNumber2ver 17 = "0.9.7-dev" |
|
82 |
protoNumber2ver 19 = "0.9.7" |
|
83 |
protoNumber2ver 20 = "0.9.8-dev" |
|
84 |
protoNumber2ver 21 = "0.9.8" |
|
85 |
protoNumber2ver 22 = "0.9.9-dev" |
|
86 |
protoNumber2ver 23 = "0.9.9" |
|
87 |
protoNumber2ver 24 = "0.9.10-dev" |
|
88 |
protoNumber2ver 25 = "0.9.10" |
|
1953 | 89 |
protoNumber2ver 26 = "0.9.11-dev" |
2113 | 90 |
protoNumber2ver 27 = "0.9.11" |
91 |
protoNumber2ver 28 = "0.9.12-dev" |
|
1804 | 92 |
protoNumber2ver _ = "Unknown" |
93 |
||
1964 | 94 |
askFromConsole :: String -> IO String |
95 |
askFromConsole msg = do |
|
96 |
putStr msg |
|
97 |
hFlush stdout |
|
98 |
getLine |