// GET: HighScore /* * The HighScoreController Index method access's the Database and uses linq to get the specfic information. * This information is then passed over to the view where it is then displayed inside a table. * The information gotten is from the current month and then it calculates the average score for each player from that month. */ public ActionResult Index() { QuizDBDataContext db = new QuizDBDataContext(); //Creates a instance of the DB DateTime now = DateTime.Now; //The current DateTime var Scores = from Re in db.tblResults //The anonyomous datatype returned from the DB which is all Results from the current month. join Pl in db.tblPlayers on Re.PlayerID equals Pl.PlayerID where Re.ResultDateTime.Month == now.Month select new { Pl.PlayerName, Re.Score, Re.ResultDateTime }; var nwScores = Scores.GroupBy(S => S.PlayerName) //The anonyomous datatype which is the PlayerName, the average score for that player from this month and the GamesPlayed. .Select(a => new { PlayerName = a.Key, Avg = a.Average(av => av.Score), Count = a.Count() }) .OrderByDescending(c => c.Avg); //Orders the data by the Players average Score. List <Average> PlayerAverages = new List <Average>(); foreach (var group in nwScores) //Loops through the variable nwScores { Average nwAvg = new Average(group.PlayerName, group.Avg, group.Count); //Sets each playerName, AverageScore and Games PLayed to a Average object. PlayerAverages.Add(nwAvg); //Adds that current Average object to the PlayerAverages list. } ViewBag.Now = DateTime.Now.ToString("MMMM"); ViewBag.AVG = PlayerAverages; //Passes the PlayerAverages List to the ViewBag so it can be quickly accessed from the view. return(View()); }
public ActionResult Score() { if ((Session["User"] == null) || ((bool)Session["Finshed"] == false)) //Checks that a User is logged in and All the questions have been answered. { return(Redirect("../Home/Index")); //return Redirect used instead of return View because Return View simply displays the View without calling the relevant controller method causing the page to be static. } else { QuestionDB questions = (QuestionDB)Session["Questions"]; //The QuestionDB instance is set from the Session Questions QuizDBDataContext db = new QuizDBDataContext(); //Creates a instance of the Database tblResult result = new tblResult //Creates a new instance of tblResult to be saved into the Database { PlayerID = (int)Session["UserID"], //Sets the PlayerID from the ID stored in the Session Score = questions.QuestionRoundScore(), //Sets the Score for the round as well as the current DateTime stamp ResultDateTime = DateTime.Now }; db.tblResults.InsertOnSubmit(result); try //Try catch used to submit the changes to the Database { db.SubmitChanges(); } catch (Exception e) { Console.WriteLine(e); } ViewBag.TotalScore = questions.TotalScore(); return(View("Score")); } }
public ActionResult Login(string Username, string passWord) { QuizDBDataContext db = new QuizDBDataContext(); //Creates a instance of the DB if (db.tblPlayers.Where(u => u.PlayerName == Username && u.PlayerPassword == passWord).Any()) //Uses linq to check if the Username and password are correct. { var userID = (from pl in db.tblPlayers //Uses linq to loop through and get the Data from tblPlayers for the first specfic user. where string.Equals(pl.PlayerName, Username) && string.Equals(pl.PlayerPassword, passWord) select pl.PlayerID).First(); Session["UserID"] = userID; //Sets the UserID Session to the first Matching UserID from the DB Query Session["User"] = Username; //Sets the User Session to the UserName return(Redirect("../Home/Index")); //return Redirect used instead of return View because Return View simply displays the View without calling the relevant controller method causing the page to be static. } Session["User"] = null; //The User and UserID Sessions are set to null if no user is found in the DB. Session["UserID"] = null; return(View()); }
public ActionResult Register(string Username, string passWord) { QuizDBDataContext db = new QuizDBDataContext(); //Creates a instance of the Database tblPlayer player = new tblPlayer //Creates a new instance of tblPlayer to be added to the DataBase { PlayerName = Username, PlayerPassword = passWord }; db.tblPlayers.InsertOnSubmit(player); try //Try catch statement used to save the new User into the DataBase { db.SubmitChanges(); } catch (Exception e) { Console.WriteLine(e); //Writes the Error to the console } return(Redirect("Login")); //return Redirect used instead of return View because Return View simply displays the View without calling the relevant controller method causing the page to be static. }