Пример #1
0
 /// <summary>
 /// Constructor used by senders of a message
 /// </summary>
 /// <param name="username"></param>
 /// <param name="password"></param>
 public ThrowBomb(Int16 bsId, Bomb bomb, FieldLocation towardsSquare, Tick tick)
     : base(PossibleTypes.Move)
 {
     ThrowingBrilliantStudentId = bsId;
     Bomb = bomb;
     TowardsSquare = towardsSquare;
     EnablingTick = tick;
 }
Пример #2
0
        public void Bomb_CheckEncodeAndDecode()
        {
            Tick t1 = new Tick();
            List<Excuse> eList = new List<Excuse> { CreateExcuse(10), CreateExcuse(11), CreateExcuse(12) };
            List<WhiningTwine> wtList = new List<WhiningTwine> { CreateTwine(20), CreateTwine(21), CreateTwine(22) };
            Bomb b1 = new Bomb(1, eList, wtList, t1);
            Assert.AreEqual(1, b1.CreatorId);
            Assert.AreSame(eList, b1.Excuses);
            Assert.AreSame(wtList, b1.Twine);
            Assert.AreSame(t1, b1.BuiltOnTick);

            ByteList bytes = new ByteList();
            b1.Encode(bytes);
            Bomb b2 = Bomb.Create(bytes);
            Assert.AreEqual(b1.CreatorId, b2.CreatorId);
            Assert.AreEqual(b1.Excuses.Count, b2.Excuses.Count);
            Assert.AreEqual(b1.Twine.Count, b2.Twine.Count);

            Assert.AreEqual(b1.BuiltOnTick.LogicalClock, b2.BuiltOnTick.LogicalClock);
            Assert.AreEqual(b1.BuiltOnTick.HashCode, b2.BuiltOnTick.HashCode);

            bytes.Clear();
            b1.Encode(bytes);
            bytes.GetByte();            // Read one byte, which will throw the length off
            try
            {
                b2 = Bomb.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }

            bytes.Clear();
            b1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                b2 = Bomb.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
Пример #3
0
        public void Bomb_CheckConstructors()
        {
            Bomb b = new Bomb();
            Assert.AreEqual(0, b.CreatorId);
            Assert.IsNotNull(b.Excuses);
            Assert.IsNotNull(b.Twine);
            Assert.AreEqual(0, b.Excuses.Count);
            Assert.AreEqual(0, b.Twine.Count);
            Assert.IsNull(b.BuiltOnTick);

            Tick t1 = new Tick();
            List<Excuse> eList = new List<Excuse> { CreateExcuse(10), CreateExcuse(11), CreateExcuse(12) };
            List<WhiningTwine> wtList = new List<WhiningTwine> { CreateTwine(20), CreateTwine(21), CreateTwine(22) };
            b = new Bomb(1, eList, wtList, t1);
            Assert.AreEqual(1, b.CreatorId);
            Assert.AreSame(eList, b.Excuses);
            Assert.AreSame(wtList, b.Twine);
            Assert.AreSame(t1, b.BuiltOnTick);
        }
Пример #4
0
        public void Bomb_CheckProperties()
        {
            Tick t1 = new Tick();
            List<Excuse> eList = new List<Excuse> { CreateExcuse(10), CreateExcuse(11), CreateExcuse(12) };
            List<WhiningTwine> wtList = new List<WhiningTwine> { CreateTwine(20), CreateTwine(21), CreateTwine(22) };
            Bomb b = new Bomb(1, eList, wtList, t1);
            Assert.AreEqual(1, b.CreatorId);
            Assert.AreSame(eList, b.Excuses);
            Assert.AreSame(wtList, b.Twine);
            Assert.AreSame(t1, b.BuiltOnTick);

            b.CreatorId = 135;
            Assert.AreEqual(135, b.CreatorId);
            b.CreatorId = 0;
            Assert.AreEqual(0, b.CreatorId);
            b.CreatorId = Int16.MaxValue;
            Assert.AreEqual(Int16.MaxValue, b.CreatorId);

            b.Excuses = new List<Excuse>();
            Assert.IsNotNull(b.Excuses);
            Assert.AreNotSame(eList, b.Excuses);
            b.Excuses = eList;
            Assert.AreSame(eList, b.Excuses);

            b.Twine = new List<WhiningTwine>();
            Assert.IsNotNull(b.Twine);
            Assert.AreNotSame(wtList, b.Twine);
            b.Twine = wtList;
            Assert.AreSame(wtList, b.Twine);
        }
Пример #5
0
 /// <summary>
 /// Factor method to create a Excuse from a byte list
 /// </summary>
 /// <param name="bytes">A byte list from which the distributable object will be decoded</param>
 /// <returns>A new object of this class</returns>
 public static new Bomb Create(ByteList bytes)
 {
     Bomb result = new Bomb();
     result.Decode(bytes);
     return result;
 }
Пример #6
0
        public static DistributableObject Create(ByteList bytes)
        {
            DistributableObject result = null;

            if (bytes == null || bytes.RemainingToRead < 4)
            {
                throw new ApplicationException("Invalid byte array");
            }

            DISTRIBUTABLE_CLASS_IDS objType = (DISTRIBUTABLE_CLASS_IDS)bytes.PeekInt16();

            switch (objType)
            {
            case DISTRIBUTABLE_CLASS_IDS.MessageNumber:
                result = MessageNumber.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.Bomb:
                result = Bomb.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.AgentInfo:
                result = AgentInfo.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.AgentList:
                result = AgentList.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.EndPoint:
                result = EndPoint.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.Excuse:
                result = Excuse.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.FieldLocation:
                result = FieldLocation.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.GameConfiguration:
                result = GameConfiguration.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.PlayingFieldLayout:
                result = PlayingFieldLayout.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.Tick:
                result = Tick.Create(bytes);
                break;

            case DISTRIBUTABLE_CLASS_IDS.WhiningTwine:
                result = WhiningTwine.Create(bytes);
                break;

            default:
                throw new ApplicationException(string.Format("Invalid Class Id={0}", objType));
            }
            return(result);
        }