Exemplo n.º 1
0
        public void EmptyData_SerializedDeserialized_Success()
        {
            var target = ServerYouConnectedTransferMessage.Create();

            var parser = TransferMessageParserUtils.FindWriter(target);

            Assert.IsTrue(parser.IsWritable(target));
            var data = parser.Write(target);

            Console.WriteLine(data);
            var obj = (ServerYouConnectedTransferMessage)parser.Read(data);

            Assert.AreEqual(target.PlayerId, obj.PlayerId);
            Assert.AreEqual(target.UnitsData, obj.UnitsData);
        }
Exemplo n.º 2
0
        public void WithData_SerializedDeserialized_Success()
        {
            var target = ServerYouConnectedTransferMessage.Create()
                         .SetPlayerId(35)
                         .AddUnit(3, UnitUtils.UnitType.Ranger, 2, 1, 0, 0, true, 0, 0, 0);

            var parser = TransferMessageParserUtils.FindWriter(target);

            Assert.IsTrue(parser.IsWritable(target));
            var data = parser.Write(target);

            Console.WriteLine(data);
            var obj = (ServerYouConnectedTransferMessage)parser.Read(data);

            Assert.AreEqual(target.PlayerId, obj.PlayerId);
            Assert.AreEqual(target.UnitsData.Count, obj.UnitsData.Count);
            Assert.AreEqual(target.UnitsData[0].UnitType, obj.UnitsData[0].UnitType);
            Assert.AreEqual(target.UnitsData[0].VisionRange, obj.UnitsData[0].VisionRange);
            Assert.AreEqual(target.UnitsData[0].MoveRange, obj.UnitsData[0].MoveRange);
            Assert.AreEqual(target.UnitsData[0].UnitId, obj.UnitsData[0].UnitId);
            Assert.AreEqual(target.UnitsData[0].AttackFriendlyFire, obj.UnitsData[0].AttackFriendlyFire);
        }
Exemplo n.º 3
0
        protected override void Handle(ServerComponent server, int connectionKey, ClientConnectTransferMessage message)
        {
            var gameState = server.Entity.GetComponent <GameStateComponent>();

            var newPlayer = new Player
            {
                PlayerId   = connectionKey + 100500,
                PlayerName = message.PlayerName,
                Units      = new List <Unit>()
            };
            var unitsCount = Math.Min(message.UnitsData.Count, gameState.MaxUnitsCount);

            for (var i = 0; i < unitsCount; i++)
            {
                var newUnit = UnitUtils.BuildUnit(message.UnitsData[i].UnitType);
                newUnit.UnitId = i + 200;

                var skillsCount = Math.Min(message.UnitsData[i].Skills.Count, gameState.MaxSkillsCount);
                for (var j = 0; j < skillsCount; j++)
                {
                    UnitUtils.ApplySkill(newPlayer, newUnit, message.UnitsData[i].Skills[j]);
                }

                newPlayer.Units.Add(newUnit);
            }

            for (var i = unitsCount; i < gameState.MaxUnitsCount; i++)
            {
                var newUnit = UnitUtils.GetRandomUnit();
                newUnit.UnitId = i + 200;

                newPlayer.Units.Add(newUnit);
            }

            server.Response[connectionKey].Enqueue(ServerYouConnectedTransferMessage.Create()
                                                   .SetPlayerId(newPlayer.PlayerId)
                                                   .PopulateUnits(newPlayer.Units));

            foreach (var player in gameState.Players)
            {
                server.Response[connectionKey].Enqueue(ServerClientConnectedTransferMessage.Create()
                                                       .SetData(player.Value.PlayerId, player.Value.PlayerName, gameState.MaxPlayersCount, gameState.Players.Count)
                                                       .PopulateUnits(player.Value.Units));
            }

            gameState.Players[connectionKey] = newPlayer;

            foreach (var player in gameState.Players)
            {
                server.Response[player.Key].Enqueue(ServerClientConnectedTransferMessage.Create()
                                                    .SetData(newPlayer.PlayerId, message.PlayerName, gameState.MaxPlayersCount, gameState.Players.Count)
                                                    .PopulateUnits(newPlayer.Units));
            }

            if (gameState.Players.Count == gameState.MaxPlayersCount)
            {
                ServerLogic.StartNewGame(gameState);

                foreach (var player in gameState.Players)
                {
                    var responses = server.Response[player.Key];
                    responses.Enqueue(ServerGameStartedTransferMessage.Create().SetSize(gameState.Map.GetLength(0), gameState.Map.GetLength(1)));
                    responses.Enqueue(ServerLogic.BuildCurrentStateForPlayer(gameState, player.Value));
                    responses.Enqueue(ServerYourTurnTransferMessage.Create());
                }
            }
        }