gameServer/Utils.hs
author szczur
Sat, 13 Nov 2010 08:54:39 -0500
branch0.9.14
changeset 4270 d1673536b6e5
parent 4266 bc6e9859f142
child 4309 a69c73c5d173
child 4326 85adae559981
permissions -rw-r--r--
last-minute pl updates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module Utils where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
import Control.Concurrent
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import Control.Concurrent.STM
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Data.Char
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import Data.Word
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import qualified Data.Map as Map
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import qualified Data.IntMap as IntMap
2304
a6e733ad0366 Implement filtering in server
unc0rr
parents: 2150
diff changeset
     9
import qualified Data.Set as Set
2310
581e59f123a2 Fix filtering
unc0rr
parents: 2309
diff changeset
    10
import Data.ByteString.Internal (w2c)
1917
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1804
diff changeset
    11
import Numeric
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1804
diff changeset
    12
import Network.Socket
1964
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1953
diff changeset
    13
import System.IO
1917
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1804
diff changeset
    14
import qualified Data.List as List
2349
ba7a0813c532 Some fixes suggested by hlint
unc0rr
parents: 2310
diff changeset
    15
import Control.Monad
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    16
import Maybe
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
-------------------------------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
import qualified Codec.Binary.Base64 as Base64
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    19
import qualified Data.ByteString.UTF8 as BUTF8
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    20
import qualified Data.ByteString as B
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
1917
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1804
diff changeset
    23
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    24
sockAddr2String :: SockAddr -> IO String
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    25
sockAddr2String (SockAddrInet _ hostAddr) = inet_ntoa hostAddr
1917
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1804
diff changeset
    26
sockAddr2String (SockAddrInet6 _ _ (a, b, c, d) _) =
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    27
    return $ (foldr1 (.)
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    28
        $ List.intersperse (\a -> ':':a)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    29
        $ concatMap (\n -> (\(a, b) -> [showHex a, showHex b]) $ divMod n 65536) [a, b, c, d]) []
1917
c94045b70142 - Better ip2string implementation
unc0rr
parents: 1804
diff changeset
    30
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    31
toEngineMsg :: String -> String
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    32
toEngineMsg msg = Base64.encode (fromIntegral (B.length encodedMsg) : (B.unpack encodedMsg))
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    33
    where
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    34
    encodedMsg = BUTF8.fromString msg
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    36
fromEngineMsg :: String -> Maybe String
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    37
fromEngineMsg msg = liftM (map w2c) (Base64.decode msg >>= removeLength)
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    38
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    39
        removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    40
        removeLength _ = Nothing
2304
a6e733ad0366 Implement filtering in server
unc0rr
parents: 2150
diff changeset
    41
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    42
checkNetCmd :: String -> (Bool, Bool)
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    43
checkNetCmd msg = check decoded
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    44
    where
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    45
        decoded = fromEngineMsg msg
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    46
        check Nothing = (False, False)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    47
        check (Just (m:ms)) = (m `Set.member` legalMessages, m == '+')
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    48
        check _ = (False, False)
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    49
        legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghb12345" ++ slotMessages
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    50
        slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
maybeRead :: Read a => String -> Maybe a
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
maybeRead s = case reads s of
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    54
    [(x, rest)] | all isSpace rest -> Just x
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    55
    _         -> Nothing
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    57
teamToNet :: Word16 -> TeamInfo -> [String]
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    58
teamToNet protocol team 
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    59
    | protocol < 30 = [
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    60
        "ADD_TEAM",
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    61
        teamname team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    62
        teamgrave team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    63
        teamfort team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    64
        teamvoicepack team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    65
        teamowner team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    66
        show $ difficulty team
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    67
        ]
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    68
        ++ hhsInfo
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    69
    | otherwise = [
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    70
        "ADD_TEAM",
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    71
        teamname team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    72
        teamgrave team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    73
        teamfort team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    74
        teamvoicepack team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    75
        teamflag team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    76
        teamowner team,
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    77
        show $ difficulty team
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    78
        ]
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    79
        ++ hhsInfo
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    80
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    81
        hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
modifyTeam team room = room{teams = replaceTeam team $ teams room}
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    85
    where
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    86
    replaceTeam _ [] = error "modifyTeam: no such team"
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    87
    replaceTeam team (t:teams) =
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    88
        if teamname team == teamname t then
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    89
            team : teams
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    90
        else
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
    91
            t : replaceTeam team teams
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    93
illegalName :: String -> Bool
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    94
illegalName = all isSpace
2150
45b695f3a7b9 Forbid room names and nicknames consisting only of space characters
unc0rr
parents: 2113
diff changeset
    95
4242
5e3c5fe2cb14 Revert to old server in branch 0.9.14
unc0rr
parents: 3671
diff changeset
    96
protoNumber2ver :: Word16 -> String
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    97
protoNumber2ver 17 = "0.9.7-dev"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    98
protoNumber2ver 19 = "0.9.7"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    99
protoNumber2ver 20 = "0.9.8-dev"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   100
protoNumber2ver 21 = "0.9.8"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
protoNumber2ver 22 = "0.9.9-dev"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
protoNumber2ver 23 = "0.9.9"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
protoNumber2ver 24 = "0.9.10-dev"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
protoNumber2ver 25 = "0.9.10"
1953
fd9c8e3c734d Add a special message for old hedgewars versions
unc0rr
parents: 1917
diff changeset
   105
protoNumber2ver 26 = "0.9.11-dev"
2113
89d0fa6734af Update server protocol number to version mapping
unc0rr
parents: 1964
diff changeset
   106
protoNumber2ver 27 = "0.9.11"
89d0fa6734af Update server protocol number to version mapping
unc0rr
parents: 1964
diff changeset
   107
protoNumber2ver 28 = "0.9.12-dev"
2448
30b4a7c8e9b2 Teach server to know 0.9.12 and 0.9.13-dev
unc0rr
parents: 2403
diff changeset
   108
protoNumber2ver 29 = "0.9.12"
30b4a7c8e9b2 Teach server to know 0.9.12 and 0.9.13-dev
unc0rr
parents: 2403
diff changeset
   109
protoNumber2ver 30 = "0.9.13-dev"
3297
0c59b991007e 31 is 0.9.13
unc0rr
parents: 2952
diff changeset
   110
protoNumber2ver 31 = "0.9.13"
0c59b991007e 31 is 0.9.13
unc0rr
parents: 2952
diff changeset
   111
protoNumber2ver 32 = "0.9.14-dev"
4266
bc6e9859f142 Add 0.9.14 and 0.9.15-dev mapping
unC0Rr
parents: 4242
diff changeset
   112
protoNumber2ver 33 = "0.9.14"
bc6e9859f142 Add 0.9.14 and 0.9.15-dev mapping
unC0Rr
parents: 4242
diff changeset
   113
protoNumber2ver 34 = "0.9.15-dev"
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   114
protoNumber2ver _ = "Unknown"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
1964
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1953
diff changeset
   116
askFromConsole :: String -> IO String
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1953
diff changeset
   117
askFromConsole msg = do
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   118
    putStr msg
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   119
    hFlush stdout
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2747
diff changeset
   120
    getLine