public void StatusReply_TestEverything()
        {
            AgentInfo agentInfo = new AgentInfo(1001, AgentInfo.PossibleAgentType.BrilliantStudent) { ANumber = "A0001", FirstName = "Joe", LastName = "Jone" };

            StatusReply r1 = new StatusReply(Reply.PossibleStatus.Success, agentInfo);
            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.AreSame(agentInfo, r1.Info);

            r1 = new StatusReply(Reply.PossibleStatus.Success, agentInfo, "test note");
            Assert.AreEqual(Reply.PossibleStatus.Success, r1.Status);
            Assert.AreSame(agentInfo, r1.Info);
            Assert.AreEqual("test note", r1.Note);

            ByteList byteList = new ByteList();
            r1.Encode(byteList);

            Message msg = Message.Create(byteList);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is StatusReply);
            StatusReply r2 = msg as StatusReply;
            Assert.AreEqual(r1.Status, r2.Status);
            Assert.AreEqual(r1.Info.Id, r2.Info.Id);
            Assert.AreEqual(r1.Info.LastName, r2.Info.LastName);
            Assert.AreEqual(r1.Note, r2.Note);
        }
        public void AgentInfo_CheckEncodeAndDecode()
        {
            EndPoint ep = new EndPoint("129.123.7.24:1345");
            AgentInfo info1 = 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
            };

            ByteList bytes = new ByteList();
            info1.Encode(bytes);
            AgentInfo info2 = AgentInfo.Create(bytes);
            Assert.AreEqual(info1.Id, info2.Id);
            Assert.AreEqual(info1.AgentType, info2.AgentType);
            Assert.AreEqual(info1.ANumber, info2.ANumber);
            Assert.AreEqual(info1.FirstName, info2.FirstName);
            Assert.AreEqual(info1.LastName, info2.LastName);
            Assert.AreEqual(info1.Strength, info2.Strength);
            Assert.AreEqual(info1.Speed, info2.Speed);
            Assert.AreEqual(info1.Points, info2.Points);
            Assert.AreEqual(info1.Location.X, info2.Location.X);
            Assert.AreEqual(info1.Location.Y, info2.Location.Y);
            Assert.AreEqual(info1.CommunicationEndPoint.Address, info2.CommunicationEndPoint.Address);
            Assert.AreEqual(info1.CommunicationEndPoint.Port, info2.CommunicationEndPoint.Port);

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

            bytes.Clear();
            info1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                info2 = AgentInfo.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public void AgentList_CheckProperties()
        {
            AgentInfo info1 = new AgentInfo(10, AgentInfo.PossibleAgentType.ExcuseGenerator);
            AgentInfo info2 = new AgentInfo(11, AgentInfo.PossibleAgentType.WhiningSpinner);
            AgentInfo info3 = new AgentInfo(12, AgentInfo.PossibleAgentType.BrilliantStudent);

            AgentList list = new AgentList();
            list.Add(info1);
            list.Add(info2);
            list.Add(info3);
            Assert.AreSame(info1, list[0]);
            Assert.AreSame(info2, list[1]);
            Assert.AreSame(info3, list[2]);
        }
        public void AgentList_CheckEncodeAndDecode()
        {
            AgentInfo info1 = new AgentInfo(10, AgentInfo.PossibleAgentType.ExcuseGenerator);
            AgentInfo info2 = new AgentInfo(11, AgentInfo.PossibleAgentType.WhiningSpinner);
            AgentInfo info3 = new AgentInfo(12, AgentInfo.PossibleAgentType.BrilliantStudent);

            AgentList list1 = new AgentList();
            list1.Add(info1);
            list1.Add(info2);
            list1.Add(info3);

            ByteList bytes = new ByteList();
            list1.Encode(bytes);
            AgentList list2 = AgentList.Create(bytes);
            Assert.AreEqual(3, list2.Count);
            Assert.AreEqual(10, list2[0].Id);
            Assert.AreEqual(11, list2[1].Id);
            Assert.AreEqual(12, list2[2].Id);

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

            bytes.Clear();
            list1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                list2 = AgentList.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public void JoinGame_Properties()
        {
            AgentInfo agentInfo = new AgentInfo(1001, AgentInfo.PossibleAgentType.BrilliantStudent) { ANumber = "A00001", FirstName = "Joe", LastName = "Jones" };
            JoinGame jg = new JoinGame(10, agentInfo);
            Assert.AreEqual(10, jg.GameId);
            Assert.AreEqual("A00001", jg.AgentInfo.ANumber);
            Assert.AreEqual("Joe", jg.AgentInfo.FirstName);
            Assert.AreEqual("Jones", jg.AgentInfo.LastName);
            Assert.AreSame(agentInfo, jg.AgentInfo);

            jg.GameId = 20;
            Assert.AreEqual(20, jg.GameId);
            jg.AgentInfo = null;
            Assert.IsNull(jg.AgentInfo);
            jg.AgentInfo = agentInfo;
            Assert.AreSame(agentInfo, jg.AgentInfo);

            Assert.AreEqual(Message.MESSAGE_CLASS_IDS.JoinGame, jg.MessageTypeId());
        }
        public void AgentInfo_CheckConstructors()
        {
            AgentInfo info = new AgentInfo();
            Assert.AreEqual(0, info.Id);
            Assert.IsNull(info.ANumber);
            Assert.IsNull(info.FirstName);
            Assert.IsNull(info.LastName);
            Assert.AreEqual(0, info.Strength);
            Assert.AreEqual(0, info.Speed);
            Assert.AreEqual(0, info.Points);
            Assert.IsNull(info.Location);
            Assert.IsNull(info.CommunicationEndPoint);

            info = new AgentInfo(10, AgentInfo.PossibleAgentType.ExcuseGenerator);
            Assert.AreEqual(10, info.Id);
            Assert.IsNull(info.ANumber);
            Assert.IsNull(info.FirstName);
            Assert.IsNull(info.LastName);
            Assert.AreEqual(0, info.Strength);
            Assert.AreEqual(0, info.Speed);
            Assert.AreEqual(0, info.Points);
            Assert.IsNull(info.Location);
            Assert.IsNull(info.CommunicationEndPoint);
            Assert.AreEqual(AgentInfo.PossibleAgentType.ExcuseGenerator, info.AgentType);

            EndPoint ep = new EndPoint("129.123.7.24:1345");
            info = new AgentInfo(20, AgentInfo.PossibleAgentType.WhiningSpinner, ep);
            Assert.AreEqual(20, info.Id);
            Assert.IsNull(info.ANumber);
            Assert.IsNull(info.FirstName);
            Assert.IsNull(info.LastName);
            Assert.AreEqual(0, info.Strength);
            Assert.AreEqual(0, info.Speed);
            Assert.AreEqual(0, info.Points);
            Assert.IsNull(info.Location);
            Assert.AreSame(ep, info.CommunicationEndPoint);
            Assert.AreEqual(AgentInfo.PossibleAgentType.WhiningSpinner, info.AgentType);
        }
        public void JoinGame_EncodingAndDecoding()
        {
            AgentInfo agentInfo = new AgentInfo(1001, AgentInfo.PossibleAgentType.BrilliantStudent) { ANumber = "A0001", FirstName = "Joe", LastName = "Jone" };
            JoinGame jg1 = new JoinGame(10, agentInfo);
            Assert.AreEqual(10, jg1.GameId);
            Assert.AreSame(agentInfo, jg1.AgentInfo);

            ByteList bytes = new ByteList();
            jg1.Encode(bytes);
            JoinGame jg2 = JoinGame.Create(bytes);
            Assert.AreEqual(jg1.GameId, jg2.GameId);

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

            bytes.Clear();
            jg1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                jg2 = JoinGame.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }
        public void CreateEncodingSamples()
        {
            StreamWriter writer = new StreamWriter("MessageSamples.txt");

            MessageNumber msgNumber = MessageNumber.Create(100, 120);
            MessageNumber conversationNumber = MessageNumber.Create(200, 240);
            AgentInfo agentInfo = new AgentInfo(10, AgentInfo.PossibleAgentType.BrilliantStudent, new Common.EndPoint("129.123.5.10:1234"))
                                        {
                                            AgentStatus = AgentInfo.PossibleAgentStatus.InGame,
                                            ANumber = "A00001",
                                            FirstName = "Joe",
                                            LastName = "Jones",
                                            Location = new FieldLocation(10, 20),
                                            Points = 100,
                                            Strength = 200,
                                            Speed = 1.2
                                        };

            AckNak ackNak = new AckNak(Reply.PossibleStatus.Success, agentInfo, "Test Message")
                                        {
                                            MessageNr = msgNumber,
                                            ConversationId = conversationNumber,
                                            IntResult = 99,
                                            Note = "Test Note"
                                        };
            writer.WriteLine("AckNak");
            writer.WriteLine("\tMessageNr={0}", ackNak.MessageNr.ToString());
            writer.WriteLine("\tConversationId={0}", ackNak.ConversationId.ToString());
            writer.WriteLine("\tReplyType={0}", ackNak.ReplyType);
            writer.WriteLine("\tAckNak Status={0}", ackNak.Status);
            writer.WriteLine("\tAgent Info:");
            writer.WriteLine("\t\tId={0}", agentInfo.Id);
            writer.WriteLine("\t\tAgentStatus={0}", agentInfo.AgentStatus);
            writer.WriteLine("\t\tANumber={0}", agentInfo.ANumber);
            writer.WriteLine("\t\tFirstName={0}", agentInfo.FirstName);
            writer.WriteLine("\t\tLastName={0}", agentInfo.LastName);
            writer.WriteLine("\t\tLocation={0}", agentInfo.Location.ToString());
            writer.WriteLine("\t\tPoints={0}", agentInfo.Points);
            writer.WriteLine("\t\tStrength={0}", agentInfo.Strength);
            writer.WriteLine("\t\tSpeed={0}", agentInfo.Speed);

            ByteList byteList = new ByteList();
            ackNak.Encode(byteList);

            writer.WriteLine("");
            writer.WriteLine("Encoding:");
            writer.WriteLine(byteList.CreateLogString());
            writer.WriteLine("");
            writer.WriteLine("------------------------------------");
            writer.WriteLine("");

            JoinGame joinGame = new JoinGame(20, agentInfo)
                                        {
                                            MessageNr = msgNumber,
                                            ConversationId = conversationNumber,
                                        };

            writer.WriteLine("JoinGame");
            writer.WriteLine("\tMessageNr={0}", joinGame.MessageNr.ToString());
            writer.WriteLine("\tConversationId={0}", joinGame.ConversationId.ToString());
            writer.WriteLine("\tGameId={0}", joinGame.GameId);
            writer.WriteLine("\tAgent Info:");
            writer.WriteLine("\t\tId={0}", agentInfo.Id);
            writer.WriteLine("\t\tAgentStatus={0}", agentInfo.AgentStatus);
            writer.WriteLine("\t\tANumber={0}", agentInfo.ANumber);
            writer.WriteLine("\t\tFirstName={0}", agentInfo.FirstName);
            writer.WriteLine("\t\tLastName={0}", agentInfo.LastName);
            writer.WriteLine("\t\tLocation={0}", agentInfo.Location.ToString());
            writer.WriteLine("\t\tPoints={0}", agentInfo.Points);
            writer.WriteLine("\t\tStrength={0}", agentInfo.Strength);
            writer.WriteLine("\t\tSpeed={0}", agentInfo.Speed);

            byteList = new ByteList();
            joinGame.Encode(byteList);

            writer.WriteLine("");
            writer.WriteLine("Encoding:");
            writer.WriteLine(byteList.CreateLogString());
            writer.WriteLine("");
            writer.WriteLine("------------------------------------");
            writer.WriteLine("");

            // TODO: All of the other message types
        }
 /// <summary>
 /// Constructor used by senders of a message
 /// </summary>
 /// <param name="username"></param>
 /// <param name="password"></param>
 public AddComponent(AgentInfo component)
     : base(PossibleTypes.AddComponent)
 {
     Component = component;
 }
 /// <summary>
 /// Constructor used by all specializations, which in turn are used by
 /// senders of a message 
 /// </summary>
 /// <param name="conversationId">conversation id</param>
 /// <param name="status">Status of the ack/nak</status>
 /// <param name="note">error message or note</note>
 public StatusReply(PossibleStatus status, AgentInfo info, string note = null)
     : base(Reply.PossibleTypes.StatusReply, status, note)
 {
     Info = info;
 }
        public void JoinGame_TestConstructorsAndFactories()
        {
            JoinGame jg = new JoinGame();
            Assert.AreEqual(0, jg.GameId);
            Assert.IsNull(jg.AgentInfo);

            AgentInfo agentInfo = new AgentInfo(1001, AgentInfo.PossibleAgentType.BrilliantStudent) { ANumber = "A0001", FirstName = "Joe", LastName = "Jone" };
            jg = new JoinGame(10, agentInfo);
            Assert.AreEqual(10, jg.GameId);

            Assert.AreSame(agentInfo, jg.AgentInfo);

            ByteList bytes = new ByteList();
            jg.Encode(bytes);
            Message msg = Message.Create(bytes);
            Assert.IsNotNull(msg);
            Assert.IsTrue(msg is JoinGame);
            JoinGame jg2 = msg as JoinGame;
            Assert.AreEqual(jg.GameId, jg2.GameId);
        }
 /// <summary>
 /// Factor method to create an object of this class 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 AgentInfo Create(ByteList bytes)
 {
     AgentInfo result = new AgentInfo();
     result.Decode(bytes);
     return result;
 }
        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);
        }
 /// <summary>
 /// Constructor used by senders of a message
 /// </summary>
 /// <param name="username"></param>
 /// <param name="password"></param>
 public JoinGame(Int16 gameId, AgentInfo agentInfo)
     : base(PossibleTypes.JoinGame)
 {
     GameId = gameId;
     AgentInfo = agentInfo;
 }