Example #1
0
        public static string Spin(string content)
        {
            //quick data sanity check
            if (content == null)
            {
                throw new ArgumentException("Text content to spin is required.");
            }

            //get index of the start and ending bracess
            var start = content.IndexOf(OPEN_BRACE);
            var end   = content.IndexOf(CLOSE_BRACE);

            //return the original content if:
            //  if there are no braces {} at all
            //  there is no start brace {
            //  the end is before the start } {
            if (start == -1 && end == -1 || start == -1 || end < start)
            {
                return(content);
            }

            //replace first brace
            var substring = content.Substring(start + 1, content.Length - (start + 1));

            //recursion
            var rest = Spin(substring);

            end = rest.IndexOf(CLOSE_BRACE);

            //check for issues
            if (end == -1)
            {
                throw new FormatException("Unbalanced brace.");
            }

            //get spin options
            var options = rest.Substring(0, end).Split(DELIMITER);

            //update permutations count
            permutations *= options.Length;

            //get random item
            var item = options[Randomizer.Generate(options.Length)];

            //substitute content and recurse the rest
            return(content.Substring(0, start) + item + Spin(rest.Substring(end + 1, rest.Length - (end + 1))));
        }
Example #2
0
        internal void Apply(ProcessTurnCommand command)
        {
            foreach (var move in ActiveTurnMoves)
            {
                if (move.Deployment) //deployed
                {
                    ApplyDomainEvent(new DeployedEvent(Id, Guid.NewGuid(), move.Player, move.Source));
                }
                else //attack
                {
                    //pick a random unit from the opposing players units in this category to hit
                    var possiblyAttacked = Units.Where(m => m.Value.OwnedByPlayer != move.Player && m.Value.Type == move.Target).ToArray();
                    if (!possiblyAttacked.Any())
                    {
                        continue;                          // they all died! :(
                    }
                    var attacked      = possiblyAttacked[_rand.Generate(possiblyAttacked.Length)].Key;
                    var attackResults = AttackCalculator.Attack(_rand, move.Source, move.Target);

                    if (attackResults.success)
                    {
                        ApplyDomainEvent(new DamagedEvent(Id, attacked, move.Target, attackResults.dmg));

                        //check for destruction
                        if (Units[attacked].Health <= attackResults.dmg)
                        {
                            ApplyDomainEvent(new DestroyedEvent(Id, attacked, Units[attacked].OwnedByPlayer, Units[attacked].Type));
                        }
                    }
                    else if (attackResults.exc == null)
                    {
                        // haha, failed, do nothing
                    }
                    else
                    {
                        //push an error event...
                    }
                }
            }

            ApplyDomainEvent(new MatchTurnEndedEvent(Id, CurrentTurn));
        }
        private static (bool success, int dmg, Exception exc) SoldierAttack(IRandomizer rand, UnitType attacked)
        {
            var roll   = rand.Generate(100) + 1;
            int chance = 0;
            int dmg    = 0;

            switch (attacked)
            {
            case UnitType.Infantry:
                chance = Soldier_Soldier_Chance;
                dmg    = Soldier_Soldier_Dmg;
                break;

            case UnitType.PlaneBomber:
                chance = Soldier_BomberPlane_Chance;
                dmg    = Soldier_BomberPlane_Dmg;
                break;

            case UnitType.PlaneFighter:
                chance = Soldier_FighterPlane_Chance;
                dmg    = Soldier_FighterPlane_Dmg;
                break;

            case UnitType.TankHeavy:
                chance = Soldier_HeavyTank_Chance;
                dmg    = Soldier_HeavyTank_Dmg;
                break;

            case UnitType.TankLight:
                chance = Soldier_LightTank_Chance;
                dmg    = Soldier_LightTank_Dmg;
                break;
            }
            if (roll <= chance)
            {
                return(true, dmg, null);
            }
            return(false, 0, null);
        }