예제 #1
0
    public static Robot GetRobot(RobotDifficulty difficulty, DiceService diceService)
    {
      var damageMaximum = 0;
      var healthMaximum = 0;

      switch (difficulty)
      {
        case RobotDifficulty.Difficult:
          {
            damageMaximum = 50;
            healthMaximum = 50;
            break;
          }
        case RobotDifficulty.Moderate:
          {
            damageMaximum = 35;
            healthMaximum = 35;
            break;
          }
        case RobotDifficulty.Easy:
          {
            damageMaximum = 20;
            healthMaximum = 20;
            break;
          }
      }
      var robot = new Robot
      {
        Name = "Robot",
        Difficulty = difficulty,
        Health = diceService.Roll(healthMaximum),
        DamageMaximum = diceService.Roll(damageMaximum)
      };
      return robot;
    }
예제 #2
0
        public static BattleResult PerformBattle(string userId, RobotDifficulty difficulty)
        {
            BattleResult battleResult;

            try
            {
                using (var heroContext = new HeroContext())
                {
                    string validationMessage;
                    var    hero = heroContext.Heroes.Single(p => p.UserId == userId);
                    if (!ValidationService.Validate(hero, 0, out validationMessage))
                    {
                        throw new Exception(validationMessage);
                    }

                    var dice  = new DiceService();
                    var robot = RobotFactory.GetRobot(difficulty, dice);

                    battleResult = performBattle(hero, robot, dice);
                    heroContext.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                Trace.TraceError(exception.Message);
                throw;
            }
            return(battleResult);
        }
예제 #3
0
    public static Result VisitMedicBay(string userId)
    {
      var result = new Result();
      try
      {
        using (var heroContext = new HeroContext())
        {
          var hero = heroContext.Heroes.Single(p => p.UserId == userId);

          string validationMessage;
          if (!ValidationService.Validate(hero, 1, out validationMessage))
          {
            throw new Exception(validationMessage);
          }

          var dice = new DiceService();
          var healthRestored = restoreHealth(hero, dice);
          heroContext.SaveChanges();

          var successMessage = string.Format("The Medic Bay restored {0} health!", healthRestored);
          result.Message = successMessage;
          result.Hero = hero;

          ActivityService.QueueActivity(userId, EventType.Medic, successMessage);
        }
      }
      catch (Exception exception)
      {
        Trace.TraceError(exception.Message);
        throw;
      }
      return result;
    }
예제 #4
0
        public static Result VisitMedicBay(string userId)
        {
            var result = new Result();

            try
            {
                using (var heroContext = new HeroContext())
                {
                    var hero = heroContext.Heroes.Single(p => p.UserId == userId);

                    string validationMessage;
                    if (!ValidationService.Validate(hero, 1, out validationMessage))
                    {
                        throw new Exception(validationMessage);
                    }

                    var dice           = new DiceService();
                    var healthRestored = restoreHealth(hero, dice);
                    heroContext.SaveChanges();

                    var successMessage = string.Format("The Medic Bay restored {0} health!", healthRestored);
                    result.Message = successMessage;
                    result.Hero    = hero;

                    ActivityService.QueueActivity(userId, EventType.Medic, successMessage);
                }
            }
            catch (Exception exception)
            {
                Trace.TraceError(exception.Message);
                throw;
            }
            return(result);
        }
예제 #5
0
 public int CollectCredits(DiceService diceService, RobotDifficulty difficulty)
 {
   var maxCredits = 0;
   switch (difficulty)
   {
     case RobotDifficulty.Difficult:
       {
         maxCredits = DifficultRobotMaxCredits;
         break;
       }
     case RobotDifficulty.Moderate:
       {
         maxCredits = ModerateRobotMaxCredits;
         break;
       }
     case RobotDifficulty.Easy:
       {
         maxCredits = EasyRobotMaxCredits;
         break;
       }
   }
   var creditsEarned = diceService.Roll(maxCredits);
   Credits += creditsEarned;
   return creditsEarned;
 }
예제 #6
0
    public static BattleResult PerformBattle(string userId, RobotDifficulty difficulty)
    {
      BattleResult battleResult;
      try
      {
        using (var heroContext = new HeroContext())
        {
          string validationMessage;
          var hero = heroContext.Heroes.Single(p => p.UserId == userId);
          if (!ValidationService.Validate(hero, 0, out validationMessage))
          {
            throw new Exception(validationMessage);
          }
          
          var dice = new DiceService();
          var robot = RobotFactory.GetRobot(difficulty, dice);

          battleResult = performBattle(hero, robot, dice);
          heroContext.SaveChanges();
        }
      }
      catch (Exception exception)
      {
        Trace.TraceError(exception.Message);
        throw;
      }
      return battleResult;
    }
예제 #7
0
 private static int upgradeWeapon(Hero hero, DiceService diceService)
 {
   var weaponImprovement = diceService.Roll(12, 2);
   hero.WeaponBonus += weaponImprovement;
   hero.Credits -= 10;
   hero.MovesRemaining -= 1;
   return weaponImprovement;
 }
예제 #8
0
    public int AwardBonusMoves(DiceService diceService, RobotDifficulty difficulty)
    {
      if (difficulty != RobotDifficulty.Difficult) return 0; 

      var bonusMoves = diceService.Roll(3);
      MovesRemaining = MovesRemaining + bonusMoves;
      return bonusMoves;
    }
예제 #9
0
 private static int improveArmor(Hero hero, DiceService diceService)
 {
   var armorImprovement = diceService.Roll(12, 2);
   hero.ArmorBonus += armorImprovement;
   hero.Credits -= 10;
   hero.MovesRemaining -= 1;
   return armorImprovement;
 }
예제 #10
0
 private static int restoreHealth(Hero hero, DiceService diceService)
 {
   var healthRestored = diceService.Roll(20, 5);
   hero.Credits -= 1;
   hero.Health += healthRestored;
   hero.MovesRemaining -= 1;
   return healthRestored;
 }
예제 #11
0
 private static int performTraining(Hero hero, DiceService diceService)
 {
   var trainingImprovement = diceService.Roll(10);
   hero.TrainingLevel += trainingImprovement;
   hero.Credits -= 1;
   hero.MovesRemaining -= 1;
   return trainingImprovement;
 }
예제 #12
0
        private static int improveArmor(Hero hero, DiceService diceService)
        {
            var armorImprovement = diceService.Roll(12, 2);

            hero.ArmorBonus     += armorImprovement;
            hero.Credits        -= 10;
            hero.MovesRemaining -= 1;
            return(armorImprovement);
        }
예제 #13
0
        private static int restoreHealth(Hero hero, DiceService diceService)
        {
            var healthRestored = diceService.Roll(20, 5);

            hero.Credits        -= 1;
            hero.Health         += healthRestored;
            hero.MovesRemaining -= 1;
            return(healthRestored);
        }
예제 #14
0
        private static int upgradeWeapon(Hero hero, DiceService diceService)
        {
            var weaponImprovement = diceService.Roll(12, 2);

            hero.WeaponBonus    += weaponImprovement;
            hero.Credits        -= 10;
            hero.MovesRemaining -= 1;
            return(weaponImprovement);
        }
예제 #15
0
        private static int performTraining(Hero hero, DiceService diceService)
        {
            var trainingImprovement = diceService.Roll(10);

            hero.TrainingLevel  += trainingImprovement;
            hero.Credits        -= 1;
            hero.MovesRemaining -= 1;
            return(trainingImprovement);
        }
예제 #16
0
        private static BattleResult performBattle(
            Hero hero,
            Robot robot,
            DiceService diceService)
        {
            var battleResult = new BattleResult();
            var round        = 0;

            while (hero.Health > 0 && robot.Health > 0)
            {
                var battleRound = new BattleRound();

                round++;
                battleRound.RoundNumber = round;

                battleRound.RobotHealthBeginning = robot.Health;
                battleRound.HeroDamageInflicted  = robot.Defend(hero.Attack(diceService));
                battleRound.RobotHealth          = robot.Health;

                battleRound.HeroHealthBeginning  = hero.Health;
                battleRound.RobotDamageInflicted = hero.Defend(robot.Attack(hero, diceService));
                battleRound.HeroHealth           = hero.Health;

                battleResult.BattleRounds.Add(battleRound);
            }
            if (hero.Health > 0)
            {
                hero.MovesRemaining--;
                hero.Wins++;
                hero.Health = hero.Health;
                battleResult.CreditsEarned =
                    hero.CollectCredits(diceService, robot.Difficulty);

                battleResult.BonusMovesAwarded =
                    hero.AwardBonusMoves(diceService, robot.Difficulty);
            }
            else
            {
                hero.MovesRemaining = 0;
                hero.Losses--;
                hero.Health = 0;
            }
            battleResult.Hero  = hero;
            battleResult.Robot = robot;
            return(battleResult);
        }
예제 #17
0
    private static BattleResult performBattle(
      Hero hero, 
      Robot robot, 
      DiceService diceService)
    {
      var battleResult = new BattleResult();
      var round = 0;
      while (hero.Health > 0 && robot.Health > 0)
      {
        var battleRound = new BattleRound();

        round++;
        battleRound.RoundNumber = round;

        battleRound.RobotHealthBeginning = robot.Health;
        battleRound.HeroDamageInflicted = robot.Defend(hero.Attack(diceService));
        battleRound.RobotHealth = robot.Health;

        battleRound.HeroHealthBeginning = hero.Health;
        battleRound.RobotDamageInflicted = hero.Defend(robot.Attack(hero, diceService));
        battleRound.HeroHealth = hero.Health;

        battleResult.BattleRounds.Add(battleRound);
      }
      if (hero.Health > 0)
      {
        hero.MovesRemaining--;
        hero.Wins++;
        hero.Health = hero.Health;
        battleResult.CreditsEarned =
          hero.CollectCredits(diceService, robot.Difficulty);

        battleResult.BonusMovesAwarded =
          hero.AwardBonusMoves(diceService, robot.Difficulty);
      }
      else
      {
        hero.MovesRemaining = 0;
        hero.Losses--;
        hero.Health = 0;
      }
      battleResult.Hero = hero;
      battleResult.Robot = robot;
      return battleResult;
    }
예제 #18
0
        public static Robot GetRobot(RobotDifficulty difficulty, DiceService diceService)
        {
            var damageMaximum = 0;
            var healthMaximum = 0;

            switch (difficulty)
            {
            case RobotDifficulty.Difficult:
            {
                damageMaximum = 50;
                healthMaximum = 50;
                break;
            }

            case RobotDifficulty.Moderate:
            {
                damageMaximum = 35;
                healthMaximum = 35;
                break;
            }

            case RobotDifficulty.Easy:
            {
                damageMaximum = 20;
                healthMaximum = 20;
                break;
            }
            }
            var robot = new Robot
            {
                Name          = "Robot",
                Difficulty    = difficulty,
                Health        = diceService.Roll(healthMaximum),
                DamageMaximum = diceService.Roll(damageMaximum)
            };

            return(robot);
        }
예제 #19
0
        public static Result Train(string userId)
        {
            var result = new Result();

            try
            {
                using (var heroContext = new HeroContext())
                {
                    var hero = heroContext.Heroes.Single(p => p.UserId == userId);

                    string validationMessage;
                    if (!ValidationService.Validate(hero, 1, out validationMessage))
                    {
                        throw new Exception(validationMessage);
                    }

                    var dice             = new DiceService();
                    var trainingIncrease = performTraining(hero, dice);
                    heroContext.SaveChanges();

                    var successMessage =
                        string.Format("Training was successful earning you a {0} point training advantage!",
                                      trainingIncrease);

                    result.Message = successMessage;
                    result.Hero    = hero;

                    ActivityService.QueueActivity(userId, EventType.Weapon, successMessage);
                }
            }
            catch (Exception exception)
            {
                Trace.TraceError(exception.Message);
                throw;
            }
            return(result);
        }
예제 #20
0
        public static Result PurchaseArmor(string userId)
        {
            var result = new Result();

            try
            {
                using (var heroContext = new HeroContext())
                {
                    var hero = heroContext.Heroes.Single(p => p.UserId == userId);

                    string validationMessage;
                    if (!ValidationService.Validate(hero, 10, out validationMessage))
                    {
                        throw new Exception(validationMessage);
                    }
                    var dice             = new DiceService();
                    var armorImprovement = improveArmor(hero, dice);

                    heroContext.SaveChanges();

                    var successMessage = string.Format(
                        "Armor upgrade was successful giving you an improvement of {0}!",
                        armorImprovement);

                    result.Message = successMessage;
                    result.Hero    = hero;

                    ActivityService.QueueActivity(userId, EventType.Armor, successMessage);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.Message);
                throw new Exception(ex.Message);
            }
            return(result);
        }
예제 #21
0
    public static Result PurchaseArmor(string userId)
    {
      var result = new Result();
      try
      {
        using (var heroContext = new HeroContext())
        {
          var hero = heroContext.Heroes.Single(p => p.UserId == userId);

          string validationMessage;
          if (!ValidationService.Validate(hero, 10, out validationMessage))
          {
            throw new Exception(validationMessage);
          }
          var dice = new DiceService();
          var armorImprovement = improveArmor(hero, dice);
          
          heroContext.SaveChanges();

          var successMessage = string.Format(
            "Armor upgrade was successful giving you an improvement of {0}!", 
            armorImprovement);

          result.Message = successMessage;
          result.Hero = hero;

          ActivityService.QueueActivity(userId, EventType.Armor, successMessage);
        }
      }
      catch (Exception ex)
      {
        Trace.TraceError(ex.Message);
        throw new Exception(ex.Message);
      }
      return result;
    }
예제 #22
0
    public static Result Train(string userId)
    {
      var result = new Result();
      try
      {
        using (var heroContext = new HeroContext())
        {
          var hero = heroContext.Heroes.Single(p => p.UserId == userId);

          string validationMessage;
          if (!ValidationService.Validate(hero, 1, out validationMessage))
          {
            throw new Exception(validationMessage);
          }

          var dice = new DiceService();
          var trainingIncrease = performTraining(hero, dice);
          heroContext.SaveChanges();

          var successMessage = 
            string.Format("Training was successful earning you a {0} point training advantage!", 
            trainingIncrease);

          result.Message = successMessage;
          result.Hero = hero;

          ActivityService.QueueActivity(userId, EventType.Weapon, successMessage);
        }
      }
      catch (Exception exception)
      {
        Trace.TraceError(exception.Message);
        throw;
      }
      return result;
    }
예제 #23
0
 public int Attack(Hero hero, DiceService diceService)
 {
   var damageMaximum = DamageMaximum - hero.ArmorBonus;
   damageMaximum = (damageMaximum > 10 ? damageMaximum : 10);
   return diceService.Roll(damageMaximum);
 }
예제 #24
0
 public int Attack(DiceService diceService)
 {
   return diceService.Roll(DamageMaximum);
 }