Beispiel #1
0
 /// <summary>
 /// Establishes a connection with database to register a new account.
 /// </summary>
 /// <param name="account">Account to register</param>
 /// <returns>True if account was registered successfully; False if not</returns>
 public bool SignUp(Account account)
 {
     try {
         using (DostDatabase db = new DostDatabase()) {
             if (db.Account.ToList().Find(accountRegistered => accountRegistered.email == account.Email) != null)
             {
                 return(false);
             }
             var validationCode = Engine.HashWithSHA256(account.Username + DateTime.Now);
             db.Account.Add(new DataAccess.Account {
                 username       = account.Username,
                 password       = Engine.HashWithSHA256(account.Password),
                 email          = account.Email,
                 coins          = 0,
                 creationDate   = DateTime.Now,
                 isVerified     = 0,
                 validationCode = validationCode
             });
             if (db.SaveChanges() != 0)
             {
                 SendSignUpEmail(account.Email, validationCode);
                 return(true);
             }
         }
     } catch (DbUpdateException dbUpdateException) {
         Console.WriteLine("DbUpdateException -> " + dbUpdateException.Message);
     } catch (Exception exception) {
         Console.WriteLine("Exception -> " + exception.Message);
     }
     return(false);
 }
Beispiel #2
0
        /// <summary>
        /// Reduces the timer time of an ingame environment.
        /// </summary>
        /// <param name="guidGame">Game global unique identifier</param>
        /// <param name="guidPlayer">Player global unique identifier</param>
        public void ReduceTime(string guidGame, string guidPlayer)
        {
            if (!gamesClients.ContainsKey(guidGame))
            {
                return;
            }
            if (!gamesClients[guidGame].ContainsKey(guidPlayer))
            {
                return;
            }
            var game = GameService.ActiveGames.Find(playerInGame => playerInGame.ActiveGameGuid == guidGame);

            if (game == null)
            {
                return;
            }
            var player = game.Players.Find(playerInGame => playerInGame.ActivePlayerGuid == guidPlayer);

            if (player == null)
            {
                return;
            }
            if (!GameService.GamesTimer.ContainsKey(game.ActiveGameGuid))
            {
                return;
            }
            using (DostDatabase db = new DostDatabase()) {
                var account = db.Account.Find(player.Account.Id);
                if (account == null)
                {
                    return;
                }
                if (account.coins < GameService.ROUND_REDUCE_TIME_COST)
                {
                    return;
                }
                account.coins -= GameService.ROUND_REDUCE_TIME_COST;
                if (db.SaveChanges() != 1)
                {
                    return;
                }
            }
            GameService.GamesTimer[game.ActiveGameGuid] = GameService.GamesTimer[game.ActiveGameGuid].AddSeconds(-GameService.ROUND_REDUCE_TIME_SECONDS);
            foreach (var playerInGame in gamesClients[guidGame])
            {
                playerInGame.Value.ReduceTime(guidGame);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Establishes a connection with database to try to find an account with username and password given.
        /// If exists and is not verified, tries to verify it with webpage.
        /// </summary>
        /// <param name="username">Account username</param>
        /// <param name="password">Account password</param>
        /// <returns>Account found in database</returns>
        public Account TryLogin(string username, string password)
        {
            string  hashedPassword = Engine.HashWithSHA256(password);
            Account account        = new Account();

            try {
                using (DostDatabase db = new DostDatabase()) {
                    var accountDb = db.Account.ToList().Find(
                        accountInList => accountInList.username == username && accountInList.password == hashedPassword
                        );
                    if (accountDb != null)
                    {
                        account.Id             = accountDb.idaccount;
                        account.Username       = accountDb.username;
                        account.Password       = accountDb.password;
                        account.Email          = accountDb.email;
                        account.Coins          = accountDb.coins;
                        account.CreationDate   = accountDb.creationDate;
                        account.IsVerified     = accountDb.isVerified == 1;
                        account.ValidationCode = accountDb.validationCode;
                        if (!account.IsVerified && TryValidateAccount(account.ValidationCode))
                        {
                            accountDb.isVerified = 1;
                            if (db.SaveChanges() != 0)
                            {
                                account.IsVerified = true;
                            }
                        }
                    }
                }
            } catch (DbUpdateException dbUpdateException) {
                Console.WriteLine("DbUpdateException -> " + dbUpdateException.Message);
            } catch (Exception exception) {
                Console.WriteLine("Exception -> " + exception.Message);
            }
            return(account);
        }
Beispiel #4
0
        /// <summary>
        /// Ends the game in an ingame environment.
        /// </summary>
        /// <param name="guidGame">Game global unique identifier</param>
        public void EndGame(string guidGame)
        {
            if (!gamesClients.ContainsKey(guidGame))
            {
                return;
            }
            var game = GameService.ActiveGames.Find(activeGame => activeGame.ActiveGameGuid == guidGame);

            if (game == null)
            {
                return;
            }
            using (DostDatabase db = new DostDatabase()) {
                var newGame = new DataAccess.Game {
                    date  = game.Date,
                    round = game.Round
                };
                db.Game.Add(newGame);
                if (db.SaveChanges() == 0)
                {
                    return;
                }
                game.Players.ForEach(player => {
                    newGame.Player.Add(new DataAccess.Player {
                        idaccount = player.Account.Id,
                        idgame    = newGame.idgame,
                        isHost    = player.IsHost ? 1 : 0,
                        score     = player.Score
                    });
                });
                if (db.SaveChanges() == 0)
                {
                    return;
                }
                game.GameCategories.ForEach(category => {
                    var newCategory = new DataAccess.GameCategory {
                        idgame = newGame.idgame,
                        name   = category.Name
                    };
                    newGame.GameCategory.Add(newCategory);
                    newGame.Player.ToList().ForEach(player => {
                        var playerCategoryAnswers = category.CategoryPlayerAnswer.Where(categoryAnswer => categoryAnswer.Player.Account.Id == player.idaccount).ToList();
                        playerCategoryAnswers.ForEach(categoryAnswer => {
                            newCategory.CategoryPlayerAnswer.Add(new DataAccess.CategoryPlayerAnswer {
                                idcategory = newCategory.idcategory,
                                idplayer   = player.idplayer,
                                answer     = categoryAnswer.Answer,
                                round      = categoryAnswer.Round
                            });
                        });
                    });
                });
                var playerPlaces = game.Players.ToList();
                playerPlaces = playerPlaces.OrderBy(playerPlace => playerPlace.Score).ToList();
                for (int index = 0; index < playerPlaces.Count; index++)
                {
                    db.Account.Find(playerPlaces[index].Account.Id).coins += GameService.MAX_COINS_PER_GAME_WIN / (index + 1);
                }
                if (db.SaveChanges() == 0)
                {
                    return;
                }
            }
            foreach (var player in gamesClients[guidGame])
            {
                player.Value.EndGame(guidGame);
            }
            System.Threading.Thread.Sleep(2000);
            gamesClients.Remove(guidGame);
        }
Beispiel #5
0
        /// <summary>
        /// Sets a new letter for an active game.
        /// </summary>
        /// <param name="guidGame">Game global unique identifier</param>
        /// <param name="idaccount">Account identifier of letter selector</param>
        /// <param name="selectRandomLetter">True if letter should be selected randomly; False if not</param>
        /// <param name="letter">Letter to be selected if selectRandomLetter parameter has false value</param>
        /// <returns>True if letter was set successfully; False if not</returns>
        public bool SetGameLetter(string guidGame, int idaccount, bool selectRandomLetter, string letter = null)
        {
            var findGame = activeGames.Find(game => game.ActiveGameGuid == guidGame);

            if (findGame == null)
            {
                return(false);
            }
            else if (!selectRandomLetter && string.IsNullOrWhiteSpace(letter))
            {
                return(false);
            }
            else if (!selectRandomLetter)
            {
                using (DostDatabase db = new DostDatabase()) {
                    var account = db.Account.Find(idaccount);
                    if (account == null || account.coins < ROUND_LETTER_SELECTION_COST)
                    {
                        return(false);
                    }
                    account.coins -= ROUND_LETTER_SELECTION_COST;
                    if (db.SaveChanges() != 1)
                    {
                        return(false);
                    }
                }
            }
            int asciiLetterA       = 65;
            int asciiLetterZ       = 90;
            int roundTimeInSeconds = ROUND_TIME + 3;

            findGame.LetterSelected    = selectRandomLetter ? Convert.ToChar(new Random().Next(asciiLetterA, asciiLetterZ)).ToString() : letter;
            findGame.RoundStartingTime = DateTime.Now.Ticks;
            Task.Run(() => {
                if (!GamesTimer.ContainsKey(findGame.ActiveGameGuid))
                {
                    GamesTimer.Add(findGame.ActiveGameGuid, new DateTime(findGame.RoundStartingTime).AddSeconds(roundTimeInSeconds));
                }
                else
                {
                    GamesTimer[findGame.ActiveGameGuid] = new DateTime(findGame.RoundStartingTime).AddSeconds(roundTimeInSeconds);
                }
                while (true)
                {
                    if (!(GamesTimer[findGame.ActiveGameGuid] > DateTime.Now))
                    {
                        break;
                    }
                }
                var gamesClients = InGameService.GamesClients;
                if (!gamesClients.ContainsKey(guidGame))
                {
                    return;
                }
                foreach (var gameClient in gamesClients[guidGame])
                {
                    gameClient.Value.EndRound(guidGame);
                }
            });
            return(true);
        }