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