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
|
1917
|
9 |
import Numeric
|
|
10 |
import Network.Socket
|
|
11 |
import qualified Data.List as List
|
1804
|
12 |
-------------------------------------------------
|
|
13 |
import qualified Codec.Binary.Base64 as Base64
|
|
14 |
import qualified Codec.Binary.UTF8.String as UTF8
|
|
15 |
import CoreTypes
|
|
16 |
|
1917
|
17 |
|
|
18 |
sockAddr2String :: SockAddr -> IO String
|
|
19 |
sockAddr2String (SockAddrInet _ hostAddr) = inet_ntoa hostAddr
|
|
20 |
sockAddr2String (SockAddrInet6 _ _ (a, b, c, d) _) =
|
|
21 |
return $ (foldr1 (.)
|
|
22 |
$ List.intersperse (\a -> ':':a)
|
|
23 |
$ concatMap (\n -> (\(a, b) -> [showHex a, showHex b]) $ divMod n 65536) [a, b, c, d]) []
|
|
24 |
|
1804
|
25 |
toEngineMsg :: String -> String
|
|
26 |
toEngineMsg msg = Base64.encode (fromIntegral (length msg) : (UTF8.encode msg))
|
|
27 |
|
|
28 |
--tselect :: [ClientInfo] -> STM ([String], ClientInfo)
|
|
29 |
--tselect = foldl orElse retry . map (\ci -> (flip (,) ci) `fmap` readTChan (chan ci))
|
|
30 |
|
|
31 |
maybeRead :: Read a => String -> Maybe a
|
|
32 |
maybeRead s = case reads s of
|
|
33 |
[(x, rest)] | all isSpace rest -> Just x
|
|
34 |
_ -> Nothing
|
|
35 |
|
|
36 |
teamToNet team = [
|
|
37 |
"ADD_TEAM",
|
|
38 |
teamname team,
|
|
39 |
teamgrave team,
|
|
40 |
teamfort team,
|
|
41 |
teamvoicepack team,
|
|
42 |
teamowner team,
|
|
43 |
show $ difficulty team
|
|
44 |
]
|
|
45 |
++ hhsInfo
|
|
46 |
where
|
|
47 |
hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team
|
|
48 |
|
|
49 |
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo
|
|
50 |
modifyTeam team room = room{teams = replaceTeam team $ teams room}
|
|
51 |
where
|
|
52 |
replaceTeam _ [] = error "modifyTeam: no such team"
|
|
53 |
replaceTeam team (t:teams) =
|
|
54 |
if teamname team == teamname t then
|
|
55 |
team : teams
|
|
56 |
else
|
|
57 |
t : replaceTeam team teams
|
|
58 |
|
|
59 |
protoNumber2ver :: Word16 -> String
|
|
60 |
protoNumber2ver 17 = "0.9.7-dev"
|
|
61 |
protoNumber2ver 19 = "0.9.7"
|
|
62 |
protoNumber2ver 20 = "0.9.8-dev"
|
|
63 |
protoNumber2ver 21 = "0.9.8"
|
|
64 |
protoNumber2ver 22 = "0.9.9-dev"
|
|
65 |
protoNumber2ver 23 = "0.9.9"
|
|
66 |
protoNumber2ver 24 = "0.9.10-dev"
|
|
67 |
protoNumber2ver 25 = "0.9.10"
|
|
68 |
protoNumber2ver _ = "Unknown"
|
|
69 |
|