author | koda |
Thu, 22 Oct 2009 21:03:38 +0000 | |
changeset 2576 | 2eb7ce1c1f19 |
parent 2448 | 30b4a7c8e9b2 |
child 2747 | 7889a3a9724f |
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, |
|
62 |
teamowner team, |
|
63 |
show $ difficulty team |
|
64 |
] |
|
65 |
++ hhsInfo |
|
66 |
where |
|
67 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team |
|
68 |
||
69 |
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo |
|
70 |
modifyTeam team room = room{teams = replaceTeam team $ teams room} |
|
71 |
where |
|
72 |
replaceTeam _ [] = error "modifyTeam: no such team" |
|
73 |
replaceTeam team (t:teams) = |
|
74 |
if teamname team == teamname t then |
|
75 |
team : teams |
|
76 |
else |
|
77 |
t : replaceTeam team teams |
|
78 |
||
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
79 |
illegalName :: String -> Bool |
2349 | 80 |
illegalName = all isSpace |
2150
45b695f3a7b9
Forbid room names and nicknames consisting only of space characters
unc0rr
parents:
2113
diff
changeset
|
81 |
|
1804 | 82 |
protoNumber2ver :: Word16 -> String |
83 |
protoNumber2ver 17 = "0.9.7-dev" |
|
84 |
protoNumber2ver 19 = "0.9.7" |
|
85 |
protoNumber2ver 20 = "0.9.8-dev" |
|
86 |
protoNumber2ver 21 = "0.9.8" |
|
87 |
protoNumber2ver 22 = "0.9.9-dev" |
|
88 |
protoNumber2ver 23 = "0.9.9" |
|
89 |
protoNumber2ver 24 = "0.9.10-dev" |
|
90 |
protoNumber2ver 25 = "0.9.10" |
|
1953 | 91 |
protoNumber2ver 26 = "0.9.11-dev" |
2113 | 92 |
protoNumber2ver 27 = "0.9.11" |
93 |
protoNumber2ver 28 = "0.9.12-dev" |
|
2448 | 94 |
protoNumber2ver 29 = "0.9.12" |
95 |
protoNumber2ver 30 = "0.9.13-dev" |
|
1804 | 96 |
protoNumber2ver _ = "Unknown" |
97 |
||
1964 | 98 |
askFromConsole :: String -> IO String |
99 |
askFromConsole msg = do |
|
100 |
putStr msg |
|
101 |
hFlush stdout |
|
102 |
getLine |