/// <summary>
 /// You make a get request to this method with an integer(1:Wood, 2:Copper, 3:Silver, 4:Gold), it creates a question in the database using the id as the max value(inclusive) for the numbers and returns that question. Parameter name is "id" because I am lazy.
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 // GET api/values/5
 public IHttpActionResult Get(int id)
 {
     try
     {
         using (var db = new CarpmaContext())
         {
             int maxValue = GetMaxValue(id, db);
             var rnd = new Random();
             int number1 = rnd.Next(2, maxValue);
             int number2 = rnd.Next(2, maxValue);
             var questionToCreateAndSend = new Question
             {
                 CreateDate = DateTime.Now,
                 IsChecked = false,
                 Max = maxValue,
                 Number1 = number1,
                 Number2 = number2,
                 Result = number1 * number2
             };
             db.Questions.Add(questionToCreateAndSend);
             db.SaveChanges();
             return Created<Question>("", questionToCreateAndSend);
         }
     }
     catch (Exception e)
     {
         return InternalServerError(e);
     }
 }
 public ActionResult SetScoreIntervals(ScoreSetting scoreSetting)
 {
     if (InputOrder(scoreSetting))
     {
         try
         {
             using (var db = new CarpmaContext())
             {
                 var currentSetting = db.ScoreSettings.First();
                 currentSetting.Wood = scoreSetting.Wood;
                 currentSetting.Copper = scoreSetting.Copper;
                 currentSetting.Silver = scoreSetting.Silver;
                 currentSetting.Gold = scoreSetting.Gold;
                 db.SaveChanges();
                 ViewBag.ValidationStatus = "success";
                 return View("SetScoreIntervals");
             }
         }
         catch (Exception)
         {
             ViewBag.ValidationStatus = "error";
             return View("SetScoreIntervals");
         }                
     }
     else
     {
         ViewBag.ValidationStatus = "failure";
         return View("SetScoreIntervals");
     }            
 }
 public ActionResult ResetScores()
 {
     Score score;
     using (var db = new CarpmaContext())
     {
         score = db.Scores.First();
     }
     return View(score);
 }
 public ActionResult SetScoreIntervals()
 {
     ScoreSetting scoreSetting;
     using (var db = new CarpmaContext())
     {
         scoreSetting = db.ScoreSettings.First();
     }            
     return View(scoreSetting);
 }
 public ActionResult Index()
 {
     List<Question> solvedQuestions;
     using (var db = new CarpmaContext())
     {
         solvedQuestions = db.Questions.Where(p => p.SolveDate != null).OrderByDescending(p => p.SolveDate).ToList();
     }
     return View(solvedQuestions);
 }
 /// <summary>
 /// Returns all the records in the database ordered(descending) by solve date
 /// </summary>
 /// <returns></returns>
 public IHttpActionResult Get()
 {
     List<Question> allRecords;
     try
     {
         using (var db = new CarpmaContext())
         {
             allRecords = db.Questions.OrderByDescending(p => p.SolveDate).ToList();
         }
         return Ok(allRecords);
     }
     catch (Exception e)
     {
         return InternalServerError(e);
     }
 }
 /// <summary>
 /// This method will be used to wake up web service
 /// </summary>
 /// <returns></returns>
 public IHttpActionResult Get()
 {
     Score score;
     try
     {
         using (var db = new CarpmaContext())
         {
             score = db.Scores.First();
         }
         return Ok(score);
     }
     catch (Exception e)
     {
         return InternalServerError(e);
     }
 }
 private int GetMaxValue(int id, CarpmaContext db)
 {
     var scoreSetting = db.ScoreSettings.First();
     switch (id)
     {
         case 1:
             return scoreSetting.Wood;
         case 2:
             return scoreSetting.Copper;
         case 3:
             return scoreSetting.Silver;
         case 4:
             return scoreSetting.Gold;
         default:
             return 100;
     }
 }
 public ActionResult ResetScore()
 {
     try
     {
         using (var db = new CarpmaContext())
         {
             var score = db.Scores.First();
             score.Wood = 0;
             score.Copper = 0;
             score.Silver = 0;
             score.Gold = 0;
             db.SaveChanges();
             ViewBag.ResetStatus = "success";
             return View("ResetScores");
         }
     }
     catch (Exception)
     {
         ViewBag.ResetStatus = "error";
         return View("ResetScores");
     }
 }
 public ActionResult ShowUnsolvedQuestions()
 {
     List<Question> unsolvedQuestions;
     using (var db = new CarpmaContext())
     {
         unsolvedQuestions = db.Questions.Where(p => p.SolveDate == null).OrderByDescending(p => p.CreateDate).ToList();
     }
     return View(unsolvedQuestions);
 }
        /// <summary>
        /// Send your answered question in the body. Use question id as url parameter. It will return the updated score. Method is POST instead of PUT because of Unity3d's WWW class limitations.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        // POST api/values/5
        public IHttpActionResult Post(int id, [FromBody]Question value)
        {
            Score score;
            ScoreSetting scoreSetting;
            Question questionToUpdate;
            try
            {
                using (var db = new CarpmaContext())
                {
                    scoreSetting = db.ScoreSettings.First();
                    score = db.Scores.First();
                    questionToUpdate = db.Questions.First(p => p.Id == id);
                    questionToUpdate.SolveDate = value.SolveDate;
                    questionToUpdate.SolveSeconds = value.SolveSeconds;

                    if (value.Max <= scoreSetting.Wood)
                    {
                        score.Wood++;
                    }
                    else if (value.Max <= scoreSetting.Copper)
                    {
                        score.Copper++;
                    }
                    else if (value.Max <= scoreSetting.Silver)
                    {
                        score.Silver++;
                    }
                    else
                    {
                        score.Gold++;
                    }
                    db.SaveChanges();
                }
                return Ok(score);
            }
            catch (Exception e)
            {
                return InternalServerError(e);
            }
        }