예제 #1
0
 public void SetReplayPosition(Vector2 position, string trigger, int status)
 {
     // if player has moved and they are not out, move the player and set their direction
     if (_position != position && _status != PieceStatus.OUT)
     {
         float xDiff = position.x - _position.x;
         if (xDiff < 0.0f && _dir != PieceDir.LEFT)
         {
             transform.eulerAngles = new Vector3(0.0f, 180.0f, 0.0f);
             _dir = PieceDir.LEFT;
         }
         else if (xDiff > 0.0f && _dir != PieceDir.RIGHT)
         {
             transform.eulerAngles = new Vector3(0.0f, 0.0f, 0.0f);
             _dir = PieceDir.RIGHT;
         }
         _position = position;
         SetPosition();
     }
     // check if player is out
     if (_status != PieceStatus.OUT && (PieceStatus)status == PieceStatus.OUT)
     {
         //Debug.Log("piece is out!");
         _status   = PieceStatus.OUT;
         enabled   = true;
         _outTimer = OUT_TIME;
         StopAnimation();
     }
     // set animation trigger for Run, Throw, Hit and Default
     if (trigger != "" && _status != PieceStatus.OUT)
     {
         Anim.SetTrigger(trigger);
     }
 }
예제 #2
0
    public bool GetHit(int hitPoints)
    {
        if (_animationState != PieceAnimationState.HIT)
        {
            Anim.SetTrigger("Hit");
            _lastTrigger    = "Hit";
            _animationState = PieceAnimationState.HIT;
        }
        //Debug.Log(this.name + " GetHit");

        bool hadFlag = false;

        _healthCalc -= hitPoints;
        if (_healthCalc <= 0)
        {
            _healthCalc = 0;
            _status     = PieceStatus.OUT;
            enabled     = true;
            _outTimer   = OUT_TIME;
            StopAnimation();
            _team.Downs++;
            _downs++;

            if (_hasFlag)
            {
                hadFlag  = true;
                _hasFlag = false;
            }
        }

        return(hadFlag);
    }
예제 #3
0
 public Move(int playerID, string trigger, Vector2 destination, PieceStatus status, bool hasFlag)
 {
     ID = playerID;
     T  = trigger;
     X  = Utilities.RoundToDecimals(destination.x, 5);
     Y  = Utilities.RoundToDecimals(destination.y, 5);
     S  = (int)status;
     F  = hasFlag;
 }
예제 #4
0
 void SetStandardStats()
 {
     _status           = PieceStatus.NORMAL;
     _pathIndex        = 0;
     _dir              = PieceDir.NONE;
     _moveState        = PieceMoveState.NONE;
     _hasFlag          = false;
     _coolDownTimer    = 0.0f;
     _animationState   = PieceAnimationState.NONE;
     _closestToFlag    = false;
     _downs            = 0;
     _takedowns        = 0;
     _pickups          = 0;
     _balloonsThrown   = 0;
     _distanceWithFlag = 0.0f;
 }
예제 #5
0
        public void TestPeerTransfer()
        {
            TorrentInfo torrent;
            Peer        peer;
            IPEndPoint  endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 4009);
            TcpClient   tcp;

            byte[]            data;
            PieceManager      pm;
            ThrottlingManager tm;

            PieceStatus[] bitField;

            TorrentInfo.TryLoad(@"C:\Users\aljaz\Desktop\Torrent1\Torrent2.torrent", out torrent);

            tcp = new TcpClient();
            tcp.ReceiveBufferSize = 1024 * 1024;
            tcp.SendBufferSize    = 1024 * 1024;
            tcp.Connect(endpoint);

            data = new byte[torrent.Length];

            tm = new ThrottlingManager();
            tm.WriteSpeedLimit = 1024 * 1024;
            tm.ReadSpeedLimit  = 1024 * 1024;

            bitField = new PieceStatus[torrent.PiecesCount];

            pm = new PieceManager(torrent.InfoHash, torrent.Length, torrent.PieceHashes, torrent.PieceLength, torrent.BlockLength, bitField);
            pm.PieceCompleted += (sender, e) =>
            {
                Assert.AreEqual(torrent.PieceHashes.ElementAt(e.PieceIndex), e.PieceData.CalculateSha1Hash().ToHexaDecimalString());
            };

            peer = new Peer(new PeerCommunicator(tm, tcp), pm, "-UT3530-B9731F4C29D30E7DEA1F9FA7");
            peer.CommunicationErrorOccurred += (sender, e) =>
            {
                Assert.Fail();
            };

            Thread.Sleep(1000000);
        }
예제 #6
0
        public void TestPieceManager()
        {
            PieceManager pieceManager;

            string[] pieceHashes;
            byte[]   data;
            byte[]   blockData = new byte[2];
            byte[]   pieceData = new byte[4];
            Piece    piece;

            PieceStatus[] bitField;

            data = "3e 57 0d 55 18 c1 d4 14 0c f1 3e 97 ce 13 7d 16".Replace(" ", string.Empty).ToByteArray();

            pieceHashes = new string[]
            {
                "ab da 6a ad 3b 55 4e 19 04 71 d0 d9 9c 1e 3c 2f 62 94 9a 96".Replace(" ", string.Empty),
                "80 a5 92 ae 03 ef d0 c8 bf b7 09 5b 2f 16 b4 00 07 5b 36 08".Replace(" ", string.Empty),
                "8a 46 51 54 30 f7 34 22 77 fe 24 51 5d 0a 7d dd c8 6b 90 6d".Replace(" ", string.Empty),
                "a7 30 37 bd fe 86 fc 6e 66 8b 18 df 51 a8 65 2a 1f ad ef af".Replace(" ", string.Empty)
            };

            bitField = new PieceStatus[pieceHashes.Count()];

            pieceManager = new PieceManager(data.CalculateSha1Hash().ToHexaDecimalString(), data.Length, pieceHashes, 4, 2, bitField);
            pieceManager.PieceCompleted += (sender, e) =>
            {
                Assert.IsNotNull(sender);
                Assert.IsNotNull(e);
                Assert.AreEqual(pieceManager, sender);

                Buffer.BlockCopy(data, e.PieceIndex * 4, pieceData, 0, pieceData.Length);

                CollectionAssert.AreEqual(pieceData, e.PieceData);
                Assert.AreEqual(PieceStatus.Present, pieceManager.BitField[e.PieceIndex]);

                if (pieceManager.IsComplete)
                {
                    CollectionAssert.AreEqual(new PieceStatus[] { PieceStatus.Present, PieceStatus.Present, PieceStatus.Present, PieceStatus.Present }, pieceManager.BitField);
                }
            };

            CollectionAssert.AreEqual(new PieceStatus[] { PieceStatus.Missing, PieceStatus.Missing, PieceStatus.Missing, PieceStatus.Missing }, pieceManager.BitField);
            Assert.AreEqual(2, pieceManager.BlockCount);
            Assert.AreEqual(2, pieceManager.BlockLength);
            Assert.AreEqual(4, pieceManager.PieceLength);
            Assert.AreEqual(false, pieceManager.IsComplete);

            foreach (var pieceIndex in new int[] { 0, 3, 2, 1 })
            {
                piece = pieceManager.CheckOut(pieceIndex);

                Assert.AreEqual(2, piece.BlockCount);
                Assert.AreEqual(2, piece.BlockLength);
                Assert.AreEqual(false, piece.IsCompleted);
                Assert.AreEqual(false, piece.IsCorrupted);
                Assert.AreEqual(pieceHashes[pieceIndex], piece.PieceHash);
                Assert.AreEqual(pieceIndex, piece.PieceIndex);
                Assert.AreEqual(4, piece.PieceData.Length);
                Assert.AreEqual(PieceStatus.CheckedOut, pieceManager.BitField[pieceIndex]);
                CollectionAssert.AreEqual(new byte[] { 0x0, 0x0, 0x0, 0x0 }, piece.PieceData);

                foreach (var blockOffseet in new int[] { 0, 2 })
                {
                    Buffer.BlockCopy(data, (pieceIndex * 4) + blockOffseet, blockData, 0, blockData.Length);

                    piece.PutBlock(blockOffseet, blockData);

                    Assert.AreEqual(2, piece.BlockCount);
                    Assert.AreEqual(2, piece.BlockLength);
                    Assert.AreEqual(blockOffseet == 2, piece.IsCompleted);
                    Assert.AreEqual(false, piece.IsCorrupted);
                    Assert.AreEqual(pieceHashes[pieceIndex], piece.PieceHash);
                    Assert.AreEqual(pieceIndex, piece.PieceIndex);
                    Assert.AreEqual(4, piece.PieceData.Length);
                }

                Buffer.BlockCopy(data, pieceIndex * 4, pieceData, 0, pieceData.Length);

                CollectionAssert.AreEqual(pieceData, piece.PieceData);
                Assert.AreEqual(PieceStatus.Present, pieceManager.BitField[pieceIndex]);
            }
        }
예제 #7
0
 public Chesspiece(string id, Color color, PieceStatus status, PieceName pieceName) : base(id, color, status)
 {
     PieceName = pieceName;
 }
예제 #8
0
        /// <summary>
        /// Verifies the specified file if it corresponds with the piece hashes.
        /// </summary>
        /// <returns>
        /// The bit field.
        /// </returns>
        public PieceStatus[] Verify()
        {
            PieceStatus[] bitField = new PieceStatus[this.pieceHashes.Count()];
            long          pieceStart;
            long          pieceEnd;
            long          previousPieceIndex = 0;
            long          torrentStartOffset = 0;
            long          torrentEndOffset;
            long          fileOffset;

            byte[] pieceData   = new byte[this.pieceLength];
            int    pieceOffset = 0;
            int    length      = 0;
            bool   ignore      = false;
            bool   download    = false;

            this.CheckIfObjectIsDisposed();

            lock (this.locker)
            {
                foreach (var file in this.files)
                {
                    torrentEndOffset = torrentStartOffset + file.Key.Length;

                    pieceStart = (torrentStartOffset - (torrentStartOffset % this.pieceLength)) / this.pieceLength;

                    pieceEnd  = (torrentEndOffset - (torrentEndOffset % this.pieceLength)) / this.pieceLength;
                    pieceEnd -= torrentEndOffset % this.pieceLength == 0 ? 1 : 0;

                    Debug.WriteLine($"verifying file {file.Value.Name}");

                    for (long pieceIndex = pieceStart; pieceIndex <= pieceEnd; pieceIndex++)
                    {
                        if (pieceIndex > previousPieceIndex)
                        {
                            bitField[previousPieceIndex] = this.GetStatus(ignore, download, this.pieceHashes.ElementAt((int)previousPieceIndex), pieceData.CalculateSha1Hash(0, pieceOffset).ToHexaDecimalString());

                            previousPieceIndex = pieceIndex;
                            pieceOffset        = 0;
                            ignore             = false;
                            download           = false;
                        }

                        fileOffset  = (pieceIndex - pieceStart) * this.pieceLength;
                        fileOffset -= pieceIndex > pieceStart ? torrentStartOffset % this.pieceLength : 0;

                        length = (int)Math.Min(this.pieceLength - pieceOffset, file.Key.Length - fileOffset);

                        if (file.Key.Download)
                        {
                            this.Read(file.Value, fileOffset, length, pieceData, pieceOffset);

                            download = true;
                        }
                        else
                        {
                            ignore = true;
                        }

                        ignore   = ignore && !file.Key.Download;
                        download = download || file.Key.Download;

                        pieceOffset += length;
                    }

                    torrentStartOffset = torrentEndOffset;
                }

                // last piece
                bitField[previousPieceIndex] = this.GetStatus(ignore, download, this.pieceHashes.ElementAt((int)previousPieceIndex), pieceData.CalculateSha1Hash(0, pieceOffset).ToHexaDecimalString());
            }

            return(bitField);
        }
예제 #9
0
 public void SetLastInfo()
 {
     _lastHasFlag  = _hasFlag;
     _lastPosition = _position;
     _lastStatus   = _status;
 }