/// <summary>
 /// Returns all games associated with player id
 /// </summary>
 /// <param name="playerId"></param>
 /// <returns></returns>
 public static IEnumerable<Game> GetGamesByPlayerId(int playerId)
 {
     IPingPongRepository<Game> _repo = new PingPongRepository<Game>();
     var games = _repo.FindBy(x => x.ChallengerId == playerId || x.DefenderId == playerId)
                 .ToList();
     return games;
 }
 // GET: Games/Details/5
 public ActionResult Details(int id)
 {
     if (String.IsNullOrEmpty(User.Identity.Name))
     {
         return RedirectToAction("Login", "Account");
     }
     var repo = new PingPongRepository<Game>();
     var game = repo.FindBy(x => x.GameId == id).SingleOrDefault();
     return View(game);
 }
 /// <summary>
 /// Finds latest game by user id
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Game FindLatestGameByUserId(int id)
 {
     try
     {
         IPingPongRepository<Game> _repo = new PingPongRepository<Game>();
         var games = _repo.FindBy(x => x.ChallengerId == id || x.DefenderId == id).ToList();
         var latestGameId = games.Max(x => x.GameId);
         return _repo.FindBy(x => x.GameId == latestGameId).SingleOrDefault();
     }
     catch (Exception)
     {
         throw new Exception("You have no existing games");
     }
     
 }
 /// <summary>
 /// Gets second latest gamne
 /// </summary>
 /// <param name="userId"></param>
 /// <returns></returns>
 public static Game GetSecondToLastGame(int userId)
 {
     IPingPongRepository<Game> _repo = new PingPongRepository<Game>();
     //This needs to be refactored. Looks terrible
     var games = _repo.FindBy(x => x.ChallengerId == userId || x.DefenderId == userId).ToList();
     var latestGame = games.OrderByDescending(x => x.GameId).First();
     games.Remove(latestGame);
     return games.OrderByDescending(x => x.GameId).FirstOrDefault();
 }
 /// <summary>
 /// Returns game by identification number
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Game GetGameById(int id)
 {
     IPingPongRepository<Game> _repo = new PingPongRepository<Game>();
     var game = _repo.FindBy(x => x.GameId == id).SingleOrDefault();
     return game;
 }
 public static int GetNumberOfGamesForPlayer(int id)
 {
     IPingPongRepository<Game> _repo = new PingPongRepository<Game>();
     var gameCount = _repo.FindBy(x => x.ChallengerId == id || x.DefenderId == id).Count();
     return gameCount;
 }
 /// <summary>
 /// Returns player based on password
 /// </summary>
 /// <param name="password">Player's password</param>
 /// <returns></returns>
 public static Player GetPlayerByPassword(string password)
 {
     IPingPongRepository<Player> _repo = new PingPongRepository<Player>();
     var player = _repo.FindBy(x => x.Password == password)
                  .SingleOrDefault();
     return player;
 }
 /// <summary>
 /// Returns boolean value based on whether password is correct
 /// </summary>
 /// <param name="id"></param>
 /// <param name="password"></param>
 /// <returns></returns>
 public static bool IsCorrectPassword(int id, string password)
 {
     IPingPongRepository<Player> _repo = new PingPongRepository<Player>();
     var pw = _repo.FindBy(x => x.PlayerId == id).Select(x => x.Password).SingleOrDefault();
     return password == pw;
 }
 /// <summary>
 /// Retrieves player by indentification number
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static Player GetPlayerById(int id)
 {
     IPingPongRepository<Player> _repo = new PingPongRepository<Player>();
     var player = _repo.FindBy(x => x.PlayerId == id)
                 .SingleOrDefault();
     return player;
 }
 /// <summary>
 /// Returns player's full name based on identification number
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public static string GetPlayerFullNameById(int id)
 {
     IPingPongRepository<Player> _repo = new PingPongRepository<Player>();
     var fullname = _repo.FindBy(x => x.PlayerId == id)
                     .Select(x => x.FirstName + " " + x.LastName)
                     .SingleOrDefault();
     return fullname;
 }
 /// <summary>
 /// Returns Players full name based on username
 /// </summary>
 /// <param name="username"></param>
 /// <returns></returns>
 public static string GetPlayerFullNameByUsername(string username)
 {
     IPingPongRepository<Player> _repo = new PingPongRepository<Player>();
     var fullname = _repo.FindBy(x => x.LoginName == username)
                    .Select(x => x.FirstName + " " + x.LastName)
                    .SingleOrDefault();
     return fullname;
 }