Exemple #1
0
        /// <summary>
        /// This method decodes a message from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
            {
                throw new ApplicationException("Invalid byte array");
            }
            else if (bytes.PeekInt16() != ClassId)
            {
                throw new ApplicationException("Invalid class id");
            }
            else
            {
                Int16 objType   = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                base.Decode(bytes);

                agentType   = (PossibleAgentType)bytes.GetByte();
                agentStatus = (PossibleAgentStatus)bytes.GetByte();
                aNumber     = bytes.GetString();
                firstName   = bytes.GetString();
                lastName    = bytes.GetString();
                strength    = bytes.GetDouble();
                speed       = bytes.GetDouble();
                points      = bytes.GetDouble();
                location    = bytes.GetDistributableObject() as FieldLocation;

                bytes.RestorePreviosReadLimit();
            }
        }
        public AgentInfo FindClosestToLocation(FieldLocation location, params AgentInfo.PossibleAgentType[] types)
        {
            log.Debug("Enter FindClosestToLocation");
            double    closestDistance = 0;
            AgentInfo closestAgent    = null;

            lock (myLock)
            {
                foreach (AgentInfo agent in agents)
                {
                    log.DebugFormat("Consider, Id={0}, Type={1}, Strength={2}", agent.Id, agent.AgentType, agent.AgentStatus);
                    if ((types == null || types.Length == 0 || types.Contains(agent.AgentType)) && agent.Strength > 0)
                    {
                        double distance = FieldLocation.Distance(location, agent.Location);
                        log.DebugFormat("distance={0}", distance);
                        if (closestAgent == null || distance < closestDistance)
                        {
                            log.Debug("new closests");
                            closestAgent    = agent;
                            closestDistance = distance;
                        }
                    }
                }
            }

            return(closestAgent);
        }
 /// <summary>
 /// Constructor used by senders of a message
 /// </summary>
 /// <param name="username"></param>
 /// <param name="password"></param>
 public Move(Int16 componentId, FieldLocation toSquare, Tick tick)
     : base(PossibleTypes.Move)
 {
     ComponentId = componentId;
     ToSquare = toSquare;
     EnablingTick = tick;
 }
Exemple #4
0
        /// <summary>
        /// Factor method to create a FieldLocation 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>
        new public static FieldLocation Create(ByteList bytes)
        {
            FieldLocation result = new FieldLocation();

            result.Decode(bytes);
            return(result);
        }
 /// <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;
 }
Exemple #6
0
        public static float Distance(FieldLocation loc1, FieldLocation loc2)
        {
            float result = float.PositiveInfinity;

            if (loc1 != null & loc2 != null)
            {
                result = Convert.ToSingle(Math.Sqrt(Math.Pow(loc1.x - loc2.x, 2) + Math.Pow(loc1.y - loc2.y, 2)));
            }
            return(result);
        }
Exemple #7
0
        public override bool Equals(object obj)
        {
            bool result = false;

            if (obj != null && obj is FieldLocation)
            {
                FieldLocation other = obj as FieldLocation;
                result = (X == other.X && Y == other.Y);
            }
            return(result);
        }
        public void FieldLocation_CheckContructors()
        {
            FieldLocation loc = new FieldLocation();
            Assert.AreEqual(0, loc.X);
            Assert.AreEqual(0, loc.Y);
            Assert.AreEqual(false, loc.Immutable);

            loc = new FieldLocation(10, 20);
            Assert.AreEqual(10, loc.X);
            Assert.AreEqual(20, loc.Y);
            Assert.AreEqual(false, loc.Immutable);

            loc = new FieldLocation(-20, -30, true);
            Assert.AreEqual(-20, loc.X);
            Assert.AreEqual(-30, loc.Y);
            Assert.AreEqual(true, loc.Immutable);

            loc = new FieldLocation(false);
            Assert.AreEqual(0, loc.X);
            Assert.AreEqual(0, loc.Y);
            Assert.AreEqual(false, loc.Immutable);

            loc = new FieldLocation(true);
            Assert.AreEqual(0, loc.X);
            Assert.AreEqual(0, loc.Y);
            Assert.AreEqual(true, loc.Immutable);

            loc = new FieldLocation { X = 10, Y = 30 };
            Assert.AreEqual(10, loc.X);
            Assert.AreEqual(30, loc.Y);
            Assert.AreEqual(false, loc.Immutable);

            loc = new ImmutableFieldLocation();
            Assert.AreEqual(0, loc.X);
            Assert.AreEqual(0, loc.Y);
            Assert.AreEqual(true, loc.Immutable);

            loc = new ImmutableFieldLocation { X = 0, Y = 0 };
            Assert.AreEqual(0, loc.X);
            Assert.AreEqual(0, loc.Y);
            Assert.AreEqual(true, loc.Immutable);

            loc = new ImmutableFieldLocation { X = 100, Y = 200 };
            Assert.AreEqual(100, loc.X);
            Assert.AreEqual(200, loc.Y);
            Assert.AreEqual(true, loc.Immutable);

            loc = new ImmutableFieldLocation { X = -200, Y = -300 };
            Assert.AreEqual(-200, loc.X);
            Assert.AreEqual(-300, loc.Y);
            Assert.AreEqual(true, loc.Immutable);
        }
        public void FieldLocation_CheckDistance()
        {
            FieldLocation loc1 = new FieldLocation { X = 100, Y = 100 };
            FieldLocation loc2 = new FieldLocation { X = 103, Y = 104 };

            Assert.AreEqual(5, FieldLocation.Distance(loc1, loc2));
            Assert.AreEqual(5, FieldLocation.Distance(loc2, loc1));
            Assert.AreEqual(float.PositiveInfinity, FieldLocation.Distance(null, loc2));
            Assert.AreEqual(float.PositiveInfinity, FieldLocation.Distance(loc1, null));
            Assert.AreEqual(float.PositiveInfinity, FieldLocation.Distance(null, null));

            loc1 = new FieldLocation { X = 0, Y = 0 };
            loc2 = new FieldLocation { X = 5, Y = 12 };
            Assert.AreEqual(13, FieldLocation.Distance(loc1, loc2));
            Assert.AreEqual(13, FieldLocation.Distance(loc2, loc1));
        }
        public void FieldLocation_CheckEncodeDecode()
        {
            ByteList bytes = new ByteList();

            FieldLocation loc1 = new FieldLocation { X = 100, Y = 200 };
            loc1.Encode(bytes);
            Assert.AreEqual(9, bytes.Length);
            Assert.AreEqual(3, bytes[0]);
            Assert.AreEqual(238, bytes[1]);
            Assert.AreEqual(0, bytes[2]);
            Assert.AreEqual(5, bytes[3]);
            Assert.AreEqual(0, bytes[4]);
            Assert.AreEqual(100, bytes[5]);
            Assert.AreEqual(0, bytes[6]);
            Assert.AreEqual(200, bytes[7]);
            Assert.AreEqual(0, bytes[8]);

            FieldLocation loc2 = FieldLocation.Create(bytes);
            Assert.AreEqual(loc1.X, loc2.X);
            Assert.AreEqual(loc1.Y, loc2.Y);
            Assert.AreEqual(false, loc2.Immutable);
        }
        public void FieldLocation_CheckProperties()
        {
            FieldLocation loc = new FieldLocation();
            Assert.AreEqual(0, loc.X);
            Assert.AreEqual(0, loc.Y);
            Assert.AreEqual(false, loc.Immutable);

            loc.X = 100;
            Assert.AreEqual(100, loc.X);
            loc.X = -200;
            Assert.AreEqual(-200, loc.X);
            loc.X = 0;
            Assert.AreEqual(0, loc.X);

            loc.Y = 100;
            Assert.AreEqual(100, loc.Y);
            loc.Y = -200;
            Assert.AreEqual(-200, loc.Y);
            loc.Y = 0;
            Assert.AreEqual(0, loc.Y);

            loc = new ImmutableFieldLocation { X = 10, Y = 20 };
            Assert.AreEqual(10, loc.X);
            Assert.AreEqual(20, loc.Y);
            Assert.AreEqual(true, loc.Immutable);

            loc.X = 100;
            Assert.AreEqual(10, loc.X);
            loc.X = -200;
            Assert.AreEqual(10, loc.X);
            loc.X = 0;
            Assert.AreEqual(10, loc.X);

            loc.Y = 100;
            Assert.AreEqual(20, loc.Y);
            loc.Y = -200;
            Assert.AreEqual(20, loc.Y);
            loc.Y = 0;
            Assert.AreEqual(20, loc.Y);
        }
Exemple #12
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);
        }
 public static float Distance(FieldLocation loc1, FieldLocation loc2)
 {
     float result = float.PositiveInfinity;
     if (loc1 != null & loc2 != null)
         result = Convert.ToSingle(Math.Sqrt(Math.Pow(loc1.x - loc2.x,2) + Math.Pow(loc1.y - loc2.y,2)));
     return result;
 }
 /// <summary>
 /// Factor method to create a FieldLocation 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 FieldLocation Create(ByteList bytes)
 {
     FieldLocation result = new FieldLocation();
     result.Decode(bytes);
     return result;
 }
        /// <summary>
        /// This method decodes a message from a byte list.  It can onlt be called from within the class hierarchy.
        /// </summary>
        /// <param name="messageBytes"></param>
        protected override void Decode(ByteList bytes)
        {
            if (bytes == null || bytes.RemainingToRead < MinimumEncodingLength)
                throw new ApplicationException("Invalid byte array");
            else if (bytes.PeekInt16() != ClassId)
                throw new ApplicationException("Invalid class id");
            else
            {
                Int16 objType = bytes.GetInt16();
                Int16 objLength = bytes.GetInt16();

                bytes.SetNewReadLimit(objLength);

                base.Decode(bytes);

                agentType = (PossibleAgentType)bytes.GetByte();
                agentStatus = (PossibleAgentStatus)bytes.GetByte();
                aNumber = bytes.GetString();
                firstName = bytes.GetString();
                lastName = bytes.GetString();
                strength = bytes.GetDouble();
                speed = bytes.GetDouble();
                points = bytes.GetDouble();
                location = bytes.GetDistributableObject() as FieldLocation;

                bytes.RestorePreviosReadLimit();
            }
        }
        public void AgentInfo_CheckProperties()
        {
            EndPoint ep = new EndPoint("129.123.7.24:1345");
            AgentInfo info = new AgentInfo(20, AgentInfo.PossibleAgentType.WhiningSpinner, ep)
                    {   ANumber = "A00001",
                        FirstName = "Joe",
                        LastName = "Jones",
                        Location = new FieldLocation(10, 20, false),
                        Strength = 1200.5,
                        Speed = 1500.0,
                        Points = 3332.42 };

            Assert.AreEqual(20, info.Id);
            Assert.AreEqual(AgentInfo.PossibleAgentType.WhiningSpinner, info.AgentType);
            Assert.AreEqual("A00001", info.ANumber);
            Assert.AreEqual("Joe", info.FirstName);
            Assert.AreEqual("Jones", info.LastName);
            Assert.AreEqual(1200.5, info.Strength);
            Assert.AreEqual(1500.0, info.Speed);
            Assert.AreEqual(3332.42, info.Points);
            Assert.AreEqual(10, info.Location.X);
            Assert.AreEqual(20, info.Location.Y);
            Assert.AreSame(ep, info.CommunicationEndPoint);

            info.Changed += ChangedEventHandler;

            // Id Property
            recentStateChange = null;
            info.Id = 1002;
            Assert.AreEqual(1002, info.Id);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            recentStateChange = null;
            info.Id = 0;
            Assert.AreEqual(0, info.Id);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.Id = Int16.MaxValue;
            Assert.AreEqual(Int16.MaxValue, info.Id);
            info.Id = 10;
            Assert.AreEqual(10, info.Id);

            // AgentType
            recentStateChange = null;
            info.AgentType = AgentInfo.PossibleAgentType.BrilliantStudent;
            Assert.AreEqual(AgentInfo.PossibleAgentType.BrilliantStudent, info.AgentType);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Status
            info.AgentStatus = AgentInfo.PossibleAgentStatus.InGame;
            Assert.AreEqual(AgentInfo.PossibleAgentStatus.InGame, info.AgentStatus);
            info.AgentStatus = AgentInfo.PossibleAgentStatus.TryingToJoin;
            Assert.AreEqual(AgentInfo.PossibleAgentStatus.TryingToJoin, info.AgentStatus);
            info.AgentStatus = AgentInfo.PossibleAgentStatus.WonGame;
            Assert.AreEqual(AgentInfo.PossibleAgentStatus.WonGame, info.AgentStatus);

            // ANumber
            recentStateChange = null;
            info.ANumber = "A000234";
            Assert.AreEqual("A000234", info.ANumber);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.ANumber = null;
            Assert.IsNull(info.ANumber);
            info.ANumber = "A012345";
            Assert.AreEqual("A012345", info.ANumber);

            // FirstName
            recentStateChange = null;
            info.FirstName = "Henry";
            Assert.AreEqual("Henry", info.FirstName);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.FirstName = null;
            Assert.IsNull(info.FirstName);
            info.FirstName = "John";
            Assert.AreEqual("John", info.FirstName);

            // LastName
            recentStateChange = null;
            info.LastName = "Franks";
            Assert.AreEqual("Franks", info.LastName);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            info.LastName = null;
            Assert.IsNull(info.LastName);
            info.LastName = "Jones";
            Assert.AreEqual("Jones", info.LastName);

            // Strength
            recentStateChange = null;
            info.Strength = 123.45;
            Assert.AreEqual(123.45, info.Strength);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Speed
            recentStateChange = null;
            info.Speed = 23.456;
            Assert.AreEqual(23.456, info.Speed);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Speed
            recentStateChange = null;
            info.Points = 53.6;
            Assert.AreEqual(53.6, info.Points);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // Location
            recentStateChange = null;
            FieldLocation f = new FieldLocation(10, 20);
            info.Location = f;
            Assert.AreSame(f, info.Location);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);

            // CommunicationEndPoint
            recentStateChange = null;
            EndPoint ep1 = new EndPoint(3242, 1000);
            info.CommunicationEndPoint = ep1;
            Assert.AreSame(ep1, info.CommunicationEndPoint);
            Assert.IsNotNull(recentStateChange);
            Assert.AreEqual(recentStateChange.Type, StateChange.ChangeType.UPDATE);
            Assert.AreSame(info, recentStateChange.Subject);
        }