/// <summary> /// that function is return all register clients of the game in the DB. /// </summary> public List <string> GetAllRegisterClients() { try { using (var ctx = new FourInRowDB_Context()) { return(ctx.Users.Select(user => user.UserName).ToList()); } } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is finding the count of losing games of selected client. /// </summary> private int FindNumberOfLosses(string userName) { using (var ctx = new FourInRowDB_Context()) { return(ctx.HistoryGames.Where(g => g.LossUserName == userName).ToList().Count); } }
/// <summary> /// that function is register the new client to the DB. /// </summary> public bool Register(string userName, string hashedPassword, string emojiIcon) { try { using (var ctx = new FourInRowDB_Context()) { var user = (from u in ctx.Users where u.UserName == userName select u).FirstOrDefault(); if (user == null) { ctx.Users.Add(new User { UserName = userName, HashedPassword = hashedPassword, EmojiIcon = emojiIcon }); ctx.SaveChanges(); } else { var userExists = new UserExistsFault { Details = "User name: " + userName + " already Exits On DB, Try something else!" }; throw new FaultException <UserExistsFault>(userExists); } return(true); } } catch (FaultException <UserExistsFault> fault) { throw; } catch (DbUpdateException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is return all history games that played. /// </summary> public List <HistoryGame> ShowAllHistoryGames() { try { using (var ctx = new FourInRowDB_Context()) { return(ctx.HistoryGames.Select(g => g).ToList()); } } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is call before a new game to get access to clients images form the DB. /// </summary> public List <string> GetImageClients(string userNameOne, string userNameTwo) { try { var result = new List <string>(); using (var ctx = new FourInRowDB_Context()) { var emojiOne = ctx.Users.Where(u => u.UserName == userNameOne) .Select(u => u.EmojiIcon).FirstOrDefault(); var emojiTwo = ctx.Users.Where(u => u.UserName == userNameTwo) .Select(u => u.EmojiIcon).FirstOrDefault(); result.Add(emojiOne); result.Add(emojiTwo); return(result); } } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is update the DB, about live game that end. and update clients as no playing. /// also update the DB about history game. /// </summary> public void EndGame(string winnerUserName, int winnerScore, string loseUserName, int loseScore) { try { using (var ctx = new FourInRowDB_Context()) { var liveGame = ctx.LiveGames.FirstOrDefault(g => (g.UserNameOne == winnerUserName || g.UserNameOne == loseUserName) && (g.UserNameTwo == winnerUserName || g.UserNameTwo == loseUserName)); var isUserOneWin = liveGame != null && winnerUserName == liveGame.UserNameOne; var isWinGame = winnerScore > loseScore; var historyGame = new HistoryGame { GameId = liveGame.GameId, UserNameOne = liveGame.UserNameOne, UserNameTwo = liveGame.UserNameTwo, StartingDateTime = liveGame.StartingDateTime, WinUserName = (isWinGame) ? winnerUserName : "******", LossUserName = (isWinGame) ? loseUserName : "******", UserNameOneScore = (isUserOneWin) ? winnerScore : loseScore, UserNameTwoScore = (isUserOneWin) ? loseScore : winnerScore, EndingDateTime = DateTime.Now }; ctx.HistoryGames.Add(historyGame); ctx.LiveGames.Remove(liveGame); ctx.SaveChanges(); } _playingClients.Remove(Tuple.Create(winnerUserName, loseUserName)); _playingClients.Remove(Tuple.Create(loseUserName, winnerUserName)); } catch (DbUpdateException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is finding the number of score of all games of a selected client. /// </summary> private int FindNumberOfPoints(string userName) { using (var ctx = new FourInRowDB_Context()) { int fq1 = 0, fq2 = 0; var q1 = ctx.HistoryGames.Where(g => g.UserNameOne == userName).ToList(); var q2 = ctx.HistoryGames.Where(g => g.UserNameTwo == userName).ToList(); if (q1.Count > 0) { fq1 = (int)q1.Sum(g => g.UserNameOneScore); } if (q2.Count > 0) { fq2 = (int)q2.Sum(g => g.UserNameTwoScore); } return(fq1 + fq2); } }
/// <summary> /// that function is connect the registered client to the Server. /// </summary> public void Connect(string userName, string hashedPassword) { lock (syncObj) { if (_clients.ContainsKey(userName)) { var userExists = new UserExistsFault { Details = "User name: " + userName + " already Connected, Try something else!" }; throw new FaultException <UserExistsFault>(userExists); } using (var ctx = new FourInRowDB_Context()) { var user = (from u in ctx.Users where u.UserName == userName select u).FirstOrDefault(); if (user == null) { var userNotExists = new UserNotExistsFault { Details = "User name: " + userName + " Not Exits On DB, Please Register First!" }; throw new FaultException <UserNotExistsFault>(userNotExists); } else if (user.HashedPassword != hashedPassword) { var fault = new WrongPasswordFault { Details = "Wrong Password, Please Try Again!" }; throw new FaultException <WrongPasswordFault>(fault); } else { _clients.Add(userName, OperationContext.Current.GetCallbackChannel <IFourRowCallback>()); _liveClients.Add(userName, true); } } } }
/// <summary> /// that function is return all history games that played with the 2 clients. /// </summary> public List <HistoryGame> ShowAllHistoryGamesTwoPlayers(List <string> playersList) { var userNameOne = playersList[0]; var userNameTwo = playersList[1]; try { using (var ctx = new FourInRowDB_Context()) { var player_one = (ctx.HistoryGames .Where(h => (h.UserNameOne == userNameOne || h.UserNameTwo == userNameOne))).ToList(); var player_two = (ctx.HistoryGames .Where(h => (h.UserNameOne == userNameTwo || h.UserNameTwo == userNameTwo))).ToList(); var intersectHistoryGames = player_one.Intersect(player_two).ToList(); if (intersectHistoryGames.Count > 0) { return(intersectHistoryGames); } return(null); } } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is return all date about selected client. /// </summary> public Tuple <List <HistoryGame>, Result> ShowAllPlayerHistoryGames(string userName) { var list = new List <HistoryGame>(); try { using (var ctx = new FourInRowDB_Context()) { list = (ctx.HistoryGames .Where(h => (h.UserNameOne == userName || h.UserNameTwo == userName))) .ToList(); } var result = new Result { UserName = userName, NumberOfGames = FindNumberOfGames(userName), NumberOfVictory = FindNumberOfVictory(userName), NumberOfLosses = FindNumberOfLosses(userName), NumberOfPoints = FindNumberOfPoints(userName) }; return(Tuple.Create(list, result)); } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is update the DB, about new live game and update clients as playing. /// </summary> public void StartGame(string fromUserName, string toUserName) { try { var liveGame = new LiveGame { UserNameOne = fromUserName, UserNameTwo = toUserName, StartingDateTime = DateTime.Now }; _playingClients.Add(Tuple.Create(toUserName, fromUserName)); using (var ctx = new FourInRowDB_Context()) { ctx.LiveGames.Add(liveGame); ctx.SaveChanges(); } } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function sort all register users. /// </summary> public List <Result> SortAllRegisterUsers(string fromUserName, string by) { var list = new List <Result>(); try { using (var ctx = new FourInRowDB_Context()) { var q = (ctx.Users.Select(u => u.UserName)).ToList(); foreach (var userName in q) { list.Add( new Result { UserName = userName, NumberOfGames = FindNumberOfGames(userName), NumberOfVictory = FindNumberOfVictory(userName), NumberOfLosses = FindNumberOfLosses(userName), NumberOfPoints = FindNumberOfPoints(userName) }); } } } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } switch (by) { case "UserName": return(list.OrderBy(r => r.UserName).ToList()); case "NumberOfGames": return(list.OrderBy(r => r.NumberOfGames).ToList()); case "NumberOfVictory": return(list.OrderBy(r => r.NumberOfVictory).ToList()); case "NumberOfLosses": return(list.OrderBy(r => r.NumberOfLosses).ToList()); case "NumberOfPoints": return(list.OrderBy(r => r.NumberOfPoints).ToList()); default: return(null); } }