~ubuntu-branches/ubuntu/maverick/hedgewars/maverick-proposed

« back to all changes in this revision

Viewing changes to .pc/haskell-backwards-compat.patch/gameServer/Utils.hs

  • Committer: Package Import Robot
  • Author(s): Evan Broder
  • Date: 2011-11-22 04:49:51 UTC
  • mfrom: (3.2.15 sid)
  • Revision ID: package-import@ubuntu.com-20111122044951-elrt8tvvs5andmuw
Tags: 0.9.17-1~maverick0.1
* Backport 0.9.17-1 to Maverick to fix network play (LP: #852603):
  - debian/patches/haskell-backwards-compat.patch: Replace or
    reimplement functions used by the server that weren't available in
    Maverick's Haskell stack
  - Drop libghc-bytestring-show-dev build-dependency.
  - Add 6's to get libghc6-deepseq-dev, libghc6-utf8-string-dev
    build-dependencies
  - Change libghc6-network-dev build-dependency to libghc6-network-bytestring-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE OverloadedStrings #-}
 
2
module Utils where
 
3
 
 
4
import Data.Char
 
5
import Data.Word
 
6
import qualified Data.Map as Map
 
7
import qualified Data.Set as Set
 
8
import qualified Data.Char as Char
 
9
import Numeric
 
10
import Network.Socket
 
11
import System.IO
 
12
import qualified Data.List as List
 
13
import Control.Monad
 
14
import qualified Codec.Binary.Base64 as Base64
 
15
import qualified Data.ByteString.Lazy as BL
 
16
import qualified Text.Show.ByteString as BS
 
17
import qualified Data.ByteString.Char8 as B
 
18
import qualified Data.ByteString.UTF8 as UTF8
 
19
import qualified Data.ByteString as BW
 
20
-------------------------------------------------
 
21
import CoreTypes
 
22
 
 
23
 
 
24
sockAddr2String :: SockAddr -> IO B.ByteString
 
25
sockAddr2String (SockAddrInet _ hostAddr) = liftM B.pack $ inet_ntoa hostAddr
 
26
sockAddr2String (SockAddrInet6 _ _ (a, b, c, d) _) =
 
27
    return $ B.pack $ (foldr1 (.)
 
28
        $ List.intersperse (':':)
 
29
        $ concatMap (\n -> (\(a0, a1) -> [showHex a0, showHex a1]) $ divMod n 65536) [a, b, c, d]) []
 
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 :: TeamInfo -> [B.ByteString]
 
37
teamToNet team =
 
38
        "ADD_TEAM"
 
39
        : teamname team
 
40
        : teamgrave team
 
41
        : teamfort team
 
42
        : teamvoicepack team
 
43
        : teamflag team
 
44
        : teamowner team
 
45
        : (showB . difficulty $ team)
 
46
        : hhsInfo
 
47
    where
 
48
        hhsInfo = concatMap (\(HedgehogInfo n hat) -> [n, hat]) $ hedgehogs team
 
49
 
 
50
modifyTeam :: TeamInfo -> RoomInfo -> RoomInfo
 
51
modifyTeam team room = room{teams = replaceTeam team $ teams room}
 
52
    where
 
53
    replaceTeam _ [] = error "modifyTeam: no such team"
 
54
    replaceTeam tm (t:ts) =
 
55
        if teamname tm == teamname t then
 
56
            tm : ts
 
57
        else
 
58
            t : replaceTeam tm ts
 
59
 
 
60
illegalName :: B.ByteString -> Bool
 
61
illegalName s = B.null s || B.all isSpace s || isSpace (B.head s) || isSpace (B.last s) || B.any isIllegalChar s
 
62
    where
 
63
        isIllegalChar c = c `List.elem` "$()*+?[]^{|}"
 
64
 
 
65
protoNumber2ver :: Word16 -> B.ByteString
 
66
protoNumber2ver v = Map.findWithDefault "Unknown" v vermap
 
67
    where
 
68
        vermap = Map.fromList [
 
69
            (17, "0.9.7-dev")
 
70
            , (19, "0.9.7")
 
71
            , (20, "0.9.8-dev")
 
72
            , (21, "0.9.8")
 
73
            , (22, "0.9.9-dev")
 
74
            , (23, "0.9.9")
 
75
            , (24, "0.9.10-dev")
 
76
            , (25, "0.9.10")
 
77
            , (26, "0.9.11-dev")
 
78
            , (27, "0.9.11")
 
79
            , (28, "0.9.12-dev")
 
80
            , (29, "0.9.12")
 
81
            , (30, "0.9.13-dev")
 
82
            , (31, "0.9.13")
 
83
            , (32, "0.9.14-dev")
 
84
            , (33, "0.9.14")
 
85
            , (34, "0.9.15-dev")
 
86
            , (35, "0.9.14.1")
 
87
            , (37, "0.9.15")
 
88
            , (38, "0.9.16-dev")
 
89
            , (39, "0.9.16")
 
90
            , (40, "0.9.17-dev")
 
91
            , (41, "0.9.17")
 
92
            , (42, "0.9.18-dev")
 
93
            ]
 
94
 
 
95
askFromConsole :: B.ByteString -> IO B.ByteString
 
96
askFromConsole msg = do
 
97
    B.putStr msg
 
98
    hFlush stdout
 
99
    B.getLine
 
100
 
 
101
 
 
102
unfoldrE :: (b -> Either b (a, b)) -> b -> ([a], b)
 
103
unfoldrE f b  =
 
104
    case f b of
 
105
        Right (a, new_b) -> let (a', b') = unfoldrE f new_b in (a : a', b')
 
106
        Left new_b       -> ([], new_b)
 
107
 
 
108
showB :: (BS.Show a) => a -> B.ByteString
 
109
showB = B.concat . BL.toChunks . BS.show
 
110
 
 
111
readInt_ :: (Num a) => B.ByteString -> a
 
112
readInt_ str =
 
113
  case B.readInt str of
 
114
       Just (i, t) | B.null t -> fromIntegral i
 
115
       _                      -> 0 
 
116
 
 
117
cutHost :: B.ByteString -> B.ByteString
 
118
cutHost = B.intercalate "." .  flip (++) ["*","*"] . List.take 2 . B.split '.'
 
119
 
 
120
caseInsensitiveCompare :: B.ByteString -> B.ByteString -> Bool
 
121
caseInsensitiveCompare a b = f a == f b
 
122
    where
 
123
        f = map Char.toUpper . UTF8.toString