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 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);
        }
        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
        }
Esempio n. 5
0
        /// <summary>
        /// Factor method to create a message from a byte list
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>A new message of the right specialization</returns>
        new public static Request Create(ByteList bytes)
        {
            Request result = null;

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

            Int16 msgType = bytes.PeekInt16();

            switch (msgType)
            {
            case (Int16)MESSAGE_CLASS_IDS.JoinGame:
                result = JoinGame.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.AddComponent:
                result = AddComponent.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.RemoveComponent:
                result = RemoveComponent.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.StartGame:
                result = StartGame.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.EndGame:
                result = EndGame.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.GetResource:
                result = GetResource.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.TickDelivery:
                result = TickDelivery.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ValidateTick:
                result = ValidateTick.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.Move:
                result = Move.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ThrowBomb:
                result = ThrowBomb.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.Eat:
                result = Eat.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ChangeStrength:
                result = ChangeStrength.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.Collaborate:
                result = Collaborate.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.GetStatus:
                result = GetStatus.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.ExitGame:
                result = ExitGame.Create(bytes);
                break;

            case (Int16)MESSAGE_CLASS_IDS.StartUpdateStream:
                result = StartUpdateStream.Create(bytes);
                break;

            default:
                throw new ApplicationException("Invalid Message Class Id");
            }

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Factor method to create a message from a byte list
        /// </summary>
        /// <param name="messageBytes">A byte list from which the message will be decoded</param>
        /// <returns>A new message of the right specialization</returns>
        public static new JoinGame Create(ByteList messageBytes)
        {
            JoinGame result = null;

            if (messageBytes == null || messageBytes.RemainingToRead < MinimumEncodingLength)
                throw new ApplicationException("Invalid message byte array");
            else if (messageBytes.PeekInt16() != ClassId)
                throw new ApplicationException("Invalid message class id");
            else
            {
                result = new JoinGame();
                result.Decode(messageBytes);
            }

            return result;
        }