author | smxx |
Thu, 04 Feb 2010 20:45:03 +0000 | |
changeset 2747 | 7889a3a9724f |
parent 2448 | 30b4a7c8e9b2 |
child 2867 | 9be6693c78cb |
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 |
2403 | 31 |
toEngineMsg msg = Base64.encode (fromIntegral (length encodedMsg) : encodedMsg) |
32 |
where |
|
33 |
encodedMsg = UTF8.encode msg |
|
1804 | 34 |
|
2304 | 35 |
fromEngineMsg :: String -> Maybe String |
2349 | 36 |
fromEngineMsg msg = liftM (map w2c) (Base64.decode msg >>= removeLength) |
2304 | 37 |
where |
2310 | 38 |
removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing |
2304 | 39 |
removeLength _ = Nothing |
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 |
2304 | 43 |
where |
44 |
decoded = fromEngineMsg msg |
|
2381
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2349
diff
changeset
|
45 |
check Nothing = (False, False) |
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2349
diff
changeset
|
46 |
check (Just (m:ms)) = (m `Set.member` legalMessages, m == '+') |
959da8402cac
Don't store keepalive messages in game server spectators buffer
unc0rr
parents:
2349
diff
changeset
|
47 |
check _ = (False, False) |
2309 | 48 |
legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghb12345" ++ slotMessages |
2349 | 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 |
|
53 |
[(x, rest)] | all isSpace rest -> Just x |
|
54 |
_ -> Nothing |
|
55 |
||
56 |
teamToNet team = [ |
|
57 |
"ADD_TEAM", |
|
58 |
teamname team, |
|
59 |
teamgrave team, |
|
60 |
teamfort team, |
|
61 |
teamvoicepack team, |
|
2747 | 62 |
teamflag team, |
1804 | 63 |
teamowner team, |
64 |
show $ difficulty team |
|
65 |
] |
|
66 |
++ hhsInfo |
|
67 |
where |
|
68 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
|
69 |
||
70 |
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo |
|
71 |
modifyTeam team room = room{teams = replaceTeam team $ teams room} |
|
72 |
where |
|
73 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
74 |
replaceTeam team (t:teams) = |
|
75 |
if teamname team == teamname t then |
|
76 |
team : teams |
|
77 |
else |
|
78 |
t : replaceTeam team teams |
|
79 |
||
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
80 |
illegalName :: String -> Bool |
2349 | 81 |
illegalName = all isSpace |
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
82 |
|
1804 | 83 |
protoNumber2ver :: Word16 -> String |
84 |
protoNumber2ver 17 = "0.9.7-dev" |
|
85 |
protoNumber2ver 19 = "0.9.7" |
|
86 |
protoNumber2ver 20 = "0.9.8-dev" |
|
87 |
protoNumber2ver 21 = "0.9.8" |
|
88 |
protoNumber2ver 22 = "0.9.9-dev" |
|
89 |
protoNumber2ver 23 = "0.9.9" |
|
90 |
protoNumber2ver 24 = "0.9.10-dev" |
|
91 |
protoNumber2ver 25 = "0.9.10" |
|
1953 | 92 |
protoNumber2ver 26 = "0.9.11-dev" |
2113 | 93 |
protoNumber2ver 27 = "0.9.11" |
94 |
protoNumber2ver 28 = "0.9.12-dev" |
|
2448 | 95 |
protoNumber2ver 29 = "0.9.12" |
96 |
protoNumber2ver 30 = "0.9.13-dev" |
|
1804 | 97 |
protoNumber2ver _ = "Unknown" |
98 |
||
1964 | 99 |
askFromConsole :: String -> IO String |
100 |
askFromConsole msg = do |
|
101 |
putStr msg |
|
102 |
hFlush stdout |
|
103 |
getLine |