예제 #1
0
        public static void StartingPositionClicked(SkirmishGameSettings settings, byte clickedPosition)
        {
            var currentlyAssignedPlayer = settings.Slots.FirstOrDefault(s => s.StartPosition == clickedPosition)?.Index ?? -1;

            var assignablePlayers = settings.Slots
                                    .Where(CanAssignPosition)
                                    .Select(s => s.Index)
                                    .ToList();

            var indexOfCurrentlyAssignedPlayer = assignablePlayers.IndexOf(currentlyAssignedPlayer);

            if (currentlyAssignedPlayer >= 0)
            {
                // the clicked position is already assigned
                if (indexOfCurrentlyAssignedPlayer < 0)
                {
                    // the assigned player cannot be removed
                    return;
                }

                settings.Slots[currentlyAssignedPlayer].StartPosition = 0;
                Logger.Trace($"Removed player {currentlyAssignedPlayer} from position {clickedPosition}");
            }

            // find the next assignable player without a starting position
            for (int index = indexOfCurrentlyAssignedPlayer + 1; index < assignablePlayers.Count; index++)
            {
                var nextPlayer = assignablePlayers[index];
                var slot       = settings.Slots[nextPlayer];
                if (slot.StartPosition == 0)
                {
                    slot.StartPosition = clickedPosition;
                    Logger.Trace($"Assigned player {slot.Index} to position {clickedPosition}");
                    return;
                }
            }

            // if the clicked position is free and all assignable players already have a position,
            // re-assigned the local player
            if (currentlyAssignedPlayer < 0)
            {
                settings.LocalSlot.StartPosition = clickedPosition;
                Logger.Trace($"Assigned local player {settings.LocalSlotIndex} to position {clickedPosition}");
            }

            bool CanAssignPosition(SkirmishSlot slot)
            {
                var isLocalSlot = slot.Index == settings.LocalSlotIndex;
                var isAI        = slot.State == SkirmishSlotState.EasyArmy ||
                                  slot.State == SkirmishSlotState.MediumArmy ||
                                  slot.State == SkirmishSlotState.HardArmy;

                return(isLocalSlot || (settings.IsHost && isAI));
            }
        }
        public void JustHost_HostClickedEmpty_AssignsHost()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            GameOptionsUtil.StartingPositionClicked(settings, 2);

            Assert.Equal(2, settings.Slots[0].StartPosition);
        }
        public void HostAndAI_HostClickedEmptyTwice_AssignsAI()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            settings.Slots[1].State = SkirmishSlotState.EasyArmy;

            GameOptionsUtil.StartingPositionClicked(settings, 4);
            GameOptionsUtil.StartingPositionClicked(settings, 4);

            Assert.Equal(0, settings.Slots[0].StartPosition);
            Assert.Equal(4, settings.Slots[1].StartPosition);
        }
        public void HostAndHuman_HostClickedEmptyTwice_DoesNothing()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            settings.Slots[1].State = SkirmishSlotState.Human;

            GameOptionsUtil.StartingPositionClicked(settings, 4);
            GameOptionsUtil.StartingPositionClicked(settings, 4);

            Assert.Equal(0, settings.Slots[0].StartPosition);
            Assert.Equal(0, settings.Slots[1].StartPosition);
        }
        public void HostAndHuman_HostClickedHost_UnassignsHost()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            settings.Slots[0].StartPosition = 1;
            settings.Slots[1].State         = SkirmishSlotState.Human;
            settings.Slots[1].StartPosition = 2;

            GameOptionsUtil.StartingPositionClicked(settings, 1);

            Assert.Equal(0, settings.Slots[0].StartPosition);
            Assert.Equal(2, settings.Slots[1].StartPosition);
        }
        public void HostAndAI_HostClickedAI_UnassignsAI()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            settings.Slots[0].StartPosition = 1;
            settings.Slots[1].State         = SkirmishSlotState.MediumArmy;
            settings.Slots[1].StartPosition = 2;

            GameOptionsUtil.StartingPositionClicked(settings, 2);

            Assert.Equal(1, settings.Slots[0].StartPosition);
            Assert.Equal(0, settings.Slots[1].StartPosition);
        }
        public void HostHumanAndAI_HumanClickedEmpty_AssignsHuman()
        {
            var settings = new SkirmishGameSettings(false)
            {
                LocalSlotIndex = 1
            };

            settings.Slots[0].StartPosition = 1;
            settings.Slots[1].State         = SkirmishSlotState.Human;
            settings.Slots[1].StartPosition = 2;
            settings.Slots[2].State         = SkirmishSlotState.EasyArmy;
            settings.Slots[2].StartPosition = 4;

            GameOptionsUtil.StartingPositionClicked(settings, 3);

            Assert.Equal(1, settings.Slots[0].StartPosition);
            Assert.Equal(3, settings.Slots[1].StartPosition);
            Assert.Equal(4, settings.Slots[2].StartPosition);
        }
        public void HostAIAndHuman_HostClickedEmpty_AssignsHost()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            settings.Slots[0].StartPosition = 1;
            settings.Slots[1].State         = SkirmishSlotState.EasyArmy;
            settings.Slots[1].StartPosition = 2;
            settings.Slots[2].State         = SkirmishSlotState.Human;
            settings.Slots[2].StartPosition = 4;

            GameOptionsUtil.StartingPositionClicked(settings, 3);

            Assert.Equal(3, settings.Slots[0].StartPosition);
            Assert.Equal(2, settings.Slots[1].StartPosition);
            Assert.Equal(4, settings.Slots[2].StartPosition);
        }
        public void HostAIAndHuman_HostClickedHuman_DoesNothing()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            settings.Slots[0].StartPosition = 1;
            settings.Slots[1].State         = SkirmishSlotState.HardArmy;
            settings.Slots[1].StartPosition = 2;
            settings.Slots[2].State         = SkirmishSlotState.Human;
            settings.Slots[2].StartPosition = 4;

            GameOptionsUtil.StartingPositionClicked(settings, 4);

            Assert.Equal(1, settings.Slots[0].StartPosition);
            Assert.Equal(2, settings.Slots[1].StartPosition);
            Assert.Equal(4, settings.Slots[2].StartPosition);
        }
        public void ComplexInteraction()
        {
            var settings = new SkirmishGameSettings(true)
            {
                LocalSlotIndex = 0
            };

            // host clicks on 1 and assigns host
            GameOptionsUtil.StartingPositionClicked(settings, 1);
            Assert.Equal(1, settings.Slots[0].StartPosition);

            // host clicks on 2 and updates host
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(2, settings.Slots[0].StartPosition);

            // host clicks on 2 again and unassigns host
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(0, settings.Slots[0].StartPosition);

            // host adds AI
            settings.Slots[1].State = SkirmishSlotState.EasyArmy;

            // host clicks on 3 and assigns host
            GameOptionsUtil.StartingPositionClicked(settings, 3);
            Assert.Equal(3, settings.Slots[0].StartPosition);

            // host clicks on 3 again and assigns AI
            GameOptionsUtil.StartingPositionClicked(settings, 3);
            Assert.Equal(0, settings.Slots[0].StartPosition);
            Assert.Equal(3, settings.Slots[1].StartPosition);

            // human joins
            settings.Slots[2].State = SkirmishSlotState.Human;

            // host clicks on 3 and unassigns AI
            GameOptionsUtil.StartingPositionClicked(settings, 3);
            Assert.Equal(0, settings.Slots[1].StartPosition);

            // human selected position 4
            settings.Slots[2].StartPosition = 4;

            // host clicks on 4 and nothing happens
            GameOptionsUtil.StartingPositionClicked(settings, 4);
            Assert.Equal(0, settings.Slots[0].StartPosition);
            Assert.Equal(4, settings.Slots[2].StartPosition);

            // host closes slot 3
            settings.Slots[3].State = SkirmishSlotState.Closed;

            // host adds another AI
            settings.Slots[4].State = SkirmishSlotState.HardArmy;

            // host clicks on 2 and assigns host
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(2, settings.Slots[0].StartPosition);

            // host clicks on 2 again and assigns first AI
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(0, settings.Slots[0].StartPosition);
            Assert.Equal(2, settings.Slots[1].StartPosition);

            // host clicks on 2 again and assigns second AI
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(0, settings.Slots[1].StartPosition);
            Assert.Equal(2, settings.Slots[4].StartPosition);

            // host clicks on 2 again and unassigns second AI
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(0, settings.Slots[4].StartPosition);

            // host clicks on 2 and assigns host
            GameOptionsUtil.StartingPositionClicked(settings, 2);
            Assert.Equal(2, settings.Slots[0].StartPosition);

            // host clicks on 1 and assigns first AI
            GameOptionsUtil.StartingPositionClicked(settings, 1);
            Assert.Equal(1, settings.Slots[1].StartPosition);

            // host clicks on 3 and assigns second AI
            GameOptionsUtil.StartingPositionClicked(settings, 3);
            Assert.Equal(3, settings.Slots[4].StartPosition);

            // host clicks on 6 and assigns host
            GameOptionsUtil.StartingPositionClicked(settings, 6);
            Assert.Equal(6, settings.Slots[0].StartPosition);

            Assert.Equal(1, settings.Slots[1].StartPosition);
            Assert.Equal(4, settings.Slots[2].StartPosition);
            Assert.Equal(3, settings.Slots[4].StartPosition);
        }