/// <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 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 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 happened after every step at the game, also send the last score of the user. /// </summary> public void ReportMove(string fromUserName, Move move, string toUserName, MoveResult moveResult, int score) { try { if (_clients.ContainsKey(toUserName) && (_playingClients.Contains(Tuple.Create(fromUserName, toUserName)) || _playingClients.Contains(Tuple.Create(toUserName, fromUserName)))) { Thread updateOtherPlayerMoveThread = new Thread(() => { _clients[toUserName].OtherPlayerReportMove(move, moveResult, score); } ); updateOtherPlayerMoveThread.Start(); } else { EndGame(fromUserName, 1000, toUserName, 0); var opponentDisconnected = new OpponentDisconnectedFault { Details = ": " + toUserName + " Disconnected From The Game!\n" + "You Automatically Win!!!" }; throw new FaultException <OpponentDisconnectedFault>(opponentDisconnected); } } catch (FaultException <OpponentDisconnectedFault> fault) { throw fault; } 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); } }