Example #1
0
 /// <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);
     }
 }
Example #2
0
 /// <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);
     }
 }
Example #3
0
        /// <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);
            }
        }
Example #4
0
        /// <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);
            }
        }
Example #5
0
        /// <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);
            }
        }
Example #6
0
        /// <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);
            }
        }
Example #7
0
        /// <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);
            }
        }
Example #8
0
        /// <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);
            }
        }
Example #9
0
        /// <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);
            }
        }