public async Task <PlayerModel> createPlayer(PlayerModel player)
        {
            PlayerModel searchPlayer = (await getPlayerByUsername(player.Username));

            if (searchPlayer != null)
            {
                return(searchPlayer);
            }

            Player newPlayer = new Player()
            {
                FirstName = player.FirstName,
                LastName  = player.LastName,
                Username  = player.Username,
                Points    = player.Points,
                Birthday  = player.Birthday
            };

            await _context.AddAsync(newPlayer);

            await saveAsync();

            player.Id = newPlayer.Id;
            return(player);
        }
Ejemplo n.º 2
0
        public async Task <int> createFriendInvite(int playerId, int friendId)
        {
            FriendInviteOutbox fObx = new FriendInviteOutbox()
            {
                InviterId = playerId,
                InvitedId = friendId,
                Date      = DateTimeOffset.Now
            };

            await _context.AddAsync(fObx);

            await saveAsync();

            return(fObx.Id);
        }
        public async Task <int> createMessage(int senderId, int receiverId, string body)
        {
            Message message = new Message()
            {
                FromId = senderId,
                ToId   = receiverId,
                Body   = body,
                Date   = DateTimeOffset.Now
            };
            await _context.AddAsync(message);

            await saveAsync();

            return(message.Id);
        }
        public async Task <Models.GameModel> CreateGame(GameModel game)
        {
            Game newGame = new Game
            {
                GameName       = game.GameName,
                OwnerId        = game.OwnerId,
                StartDate      = DateTime.Now,
                EndDate        = game.EndDate,
                TotalQuestions = game.TotalQuestions,
                IsPublic       = game.IsPublic
            };

            await _context.AddAsync(newGame);

            await _context.SaveChangesAsync();

            var owner = await _context.GamePlayers.ToListAsync();

            var player = new GamePlayer
            {
                GameId   = newGame.Id,
                PlayerId = game.OwnerId
            };

            await _context.AddAsync(player);

            await _context.SaveChangesAsync();

            Models.GameModel appGame = new Models.GameModel
            {
                Id             = newGame.Id,
                GameName       = newGame.GameName,
                OwnerId        = newGame.OwnerId,
                StartDate      = newGame.StartDate,
                EndDate        = newGame.EndDate,
                TotalQuestions = newGame.TotalQuestions,
                IsPublic       = newGame.IsPublic,
                PlayerId       = game.PlayerId
            };

            return(appGame);
        }
Ejemplo n.º 5
0
        public async Task CreatePlayer_PlayerIsValid_Success()
        {
            //arrange
            using var contextfactory        = new TestTriviaGameContextFactory();
            using TriviaRankContext context = contextfactory.CreateContext();

            var insertOwner = new Player
            {
                Username  = "******",
                Password  = "******",
                Birthday  = DateTime.Now,
                Points    = 100,
                FirstName = "Test",
                LastName  = "Player"
            };
            var insertPlayer = new Player
            {
                Username  = "******",
                Password  = "******",
                Birthday  = DateTime.Now,
                Points    = 100,
                FirstName = "TestPlayer",
                LastName  = "Playerlastname"
            };

            await context.AddAsync(insertOwner);

            await context.AddAsync(insertPlayer);

            await context.SaveChangesAsync();

            var insertPlayerModel = new PlayerModel
            {
                Id        = insertPlayer.Id,
                Username  = insertPlayer.Username,
                Password  = insertPlayer.Password,
                Birthday  = insertPlayer.Birthday,
                Points    = insertPlayer.Points,
                FirstName = insertPlayer.FirstName,
                LastName  = insertPlayer.LastName
            };

            var game = new Game
            {
                GameName       = "testgame1",
                GameMode       = true,
                OwnerId        = insertOwner.Id,
                StartDate      = DateTimeOffset.Now,
                EndDate        = DateTimeOffset.Now.AddMinutes(20.0),
                TotalQuestions = 10,
                IsPublic       = true
            };
            await context.AddAsync(game);

            await context.SaveChangesAsync();

            var repo = new GameRepository(context);

            //act

            var insertedId = await repo.AddPlayerToGame(game.Id, insertPlayerModel);

            var gp = await context.GamePlayers.Where(gp => gp.PlayerId == insertPlayer.Id).FirstOrDefaultAsync();

            //assert
            Assert.Equal(insertedId, gp.Id);
        }
Ejemplo n.º 6
0
        public async Task SearchAllGames_GameIsValid_Success()
        {
            //arrange
            using var contextfactory        = new TestTriviaGameContextFactory();
            using TriviaRankContext context = contextfactory.CreateContext();

            var insertOwner = new Player
            {
                Username  = "******",
                Password  = "******",
                Birthday  = DateTime.Now,
                Points    = 100,
                FirstName = "Test",
                LastName  = "Player"
            };

            await context.AddAsync(insertOwner);

            context.SaveChanges();

            var insertedGames = new List <Game>();

            for (var i = 1; i < 5; i++)
            {
                var game = new Game
                {
                    GameName       = "CoolGameDude" + i,
                    GameMode       = true,
                    OwnerId        = 1,
                    StartDate      = DateTimeOffset.Now,
                    EndDate        = DateTimeOffset.Now.AddMinutes(20.0),
                    TotalQuestions = i + 10,
                    IsPublic       = true
                };
                await context.AddAsync(game);
            }

            for (var i = 1; i < 5; i++)
            {
                var game = new Game
                {
                    GameName       = "CoolGameDude" + i,
                    GameMode       = true,
                    OwnerId        = 1,
                    StartDate      = DateTimeOffset.Now,
                    EndDate        = DateTimeOffset.Now,
                    TotalQuestions = i + 10,
                    IsPublic       = true
                };
                await context.AddAsync(game);
            }

            context.SaveChanges();

            var repo    = new GameRepository(context);
            var dbGames = new List <GameModel>();

            //act
            dbGames = await repo.SearchAllGames();

            //assert
            Assert.NotEmpty(dbGames);
        }