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