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); }
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); }
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); }
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); }