public async void RequestContainsExistedWords() { // arrange int userId = 1; int otherId = 2; using InWordsDataContext context = InWordsDataContextFactory.Create(); await context.AddAccount(userId); await context.AddAccount(otherId); await context.SaveChangesAsync(); context.Add(CreateUserWordPair(userId, "0", "0-0", 1)); context.Add(CreateUserWordPair(userId, "1", "1-1", 2)); context.Add(CreateUserWordPair(otherId, "2", "2-2", 3)); context.Add(CreateUserWordPair(userId, "3", "3-3", 4)); await context.SaveChangesAsync(); // act var service = new GetUserWords(context); var requestData = new GetWordsRequest(); requestData.UserWordpairIds.Add(new int[] { 1, 2, 99, 100 }); var request = new AuthReq <GetWordsRequest, WordsReply>(requestData) { UserId = userId }; var reply = await service.HandleRequest(request, default); // assert Assert.Equal(4, reply.ToAdd[0].UserWordPair); Assert.Equal(new int[] { 99, 100 }, reply.ToDelete.ToArray()); }
public async void GetUserWords() { // arrange int userId = 1; int otherId = 2; using InWordsDataContext context = InWordsDataContextFactory.Create(); await context.AddAccount(userId); await context.AddAccount(otherId); await context.SaveChangesAsync(); context.Add(CreateUserWordPair(userId, "0", "0-0")); context.Add(CreateUserWordPair(userId, "1", "1-1")); context.Add(CreateUserWordPair(userId, "3", "3-3")); context.Add(CreateUserWordPair(otherId, "2", "2-2")); await context.SaveChangesAsync(); // act var service = new GetUserWords(context); var requestData = new GetWordsRequest(); var request = new AuthReq <GetWordsRequest, WordsReply>(requestData) { UserId = userId }; var reply = await service.HandleRequest(request, default); // assert Assert.Equal(context.UserWordPairs.Where(d => d.UserId == userId).Count(), reply.ToAdd.Count()); Assert.Empty(reply.ToDelete); }
public static async Task <int> AddOrGetUserHistoryGame(this InWordsDataContext context, int userId) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var historyGame = (from game in context.Games.Where(d => d.CreatorId == userId) join tag in context.GameTags.Where(d => d.Tags == GameTags.CustomLevelsHistory) on game.GameId equals tag.GameId select game).OrderBy(g => g.GameId).ToArray(); int historyGameId = 0; if (historyGame.Any()) { historyGameId = historyGame[0].GameId; if (historyGame.Length > 1) { var historyCollisions = historyGame.Skip(1); int[] ids = historyCollisions.Select(d => d.GameId).ToArray(); var levels = context.GameLevels.Where(g => ids.Contains(g.GameId)); levels.ForEach(level => { level.GameId = historyGameId; }); await context.SaveChangesAsync().ConfigureAwait(false); context.Remove(historyCollisions); await context.SaveChangesAsync().ConfigureAwait(false); } } else { Game game = new Game { CreatorId = userId }; context.Add(game); await context.SaveChangesAsync().ConfigureAwait(false); GameTag historyGameTag = new GameTag() { GameId = game.GameId, Tags = GameTags.CustomLevelsHistory, UserId = userId }; context.Add(historyGameTag); await context.SaveChangesAsync().ConfigureAwait(false); historyGameId = game.GameId; } return(historyGameId); }
public async void DeleteUserWordPairs_ShouldBeOk() { // arrange int userId = 1; int otherId = 2; await using InWordsDataContext context = InWordsDataContextFactory.Create(); await context.AddAccount(userId); await context.AddAccount(otherId); await context.SaveChangesAsync(); context.Add(new UserWordPair() { UserWordPairId = 1, ForeignWord = "Test1", NativeWord = "Тест1", UserId = userId });; context.Add(new UserWordPair() { UserWordPairId = 2, ForeignWord = "Test2", NativeWord = "Тест2", UserId = userId }); context.Add(new UserWordPair() { UserWordPairId = 3, ForeignWord = "Test3", NativeWord = "Тест3", UserId = userId }); await context.SaveChangesAsync(); DeleteWordsRequest deletewordsRequets = new DeleteWordsRequest(); deletewordsRequets.Delete.AddRange(new int[] { 1, 3 }); // act var requestObject = new AuthReq <DeleteWordsRequest, Empty>(deletewordsRequets) { UserId = userId }; var deleteWords = new DeleteWords(context); Empty response = await deleteWords.HandleRequest(requestObject).ConfigureAwait(false); // assert Assert.Equal(1, context.UserWordPairs.Count()); Assert.Equal("Тест2", context.UserWordPairs.First().NativeWord); }
public async void GetOfficialSets_ShouldBeOk() { // arrange string picture = "testPicture"; string description = "testdes"; string title = "testtitle"; Language language = new Language() { LanguageId = 2, Title = "ru" }; await using InWordsDataContext context = InWordsDataContextFactory.Create(); Game game = new Game() { Picture = picture }; context.Add(language); context.Add(game); await context.SaveChangesAsync(); CreationDescription creationDescription = new CreationDescription() { CreationId = game.GameId, Description = description, LanguageId = 2, Title = title, }; context.Add(creationDescription); GameTag gameTag = new GameTag() { GameId = game.GameId, Tags = GameTags.Official }; context.Add(gameTag); context.SaveChanges(); // act Empty empty = new Empty(); var request = new AuthReq <Empty, WordSetReply>(empty) { UserId = 0 }; var reply = await new GetWordSetsHandler(context).Handle(request); // assert Assert.Single(reply.WordSets); Assert.Equal(description, reply.WordSets[0].Description); Assert.Equal(title, reply.WordSets[0].Title); Assert.Equal(picture, reply.WordSets[0].Picture); }
public async void GetLevelWords() { // arrange int userId = 1; await using InWordsDataContext context = InWordsDataContextFactory.Create(); Game game = new Game() { }; context.Add(game); await context.SaveChangesAsync(); GameLevel gameLevel = new GameLevel() { GameId = game.GameId }; context.Add(gameLevel); await context.SaveChangesAsync(); List <GameLevelWord> gameLevelWords = new List <GameLevelWord> { new GameLevelWord() { GameLevelId = gameLevel.GameLevelId, ForeignWord = "test1", NativeWord = "тест1" }, new GameLevelWord() { GameLevelId = gameLevel.GameLevelId, ForeignWord = "test2", NativeWord = "тест2" }, }; context.GameLevelWords.AddRange(gameLevelWords); await context.SaveChangesAsync(); await context.AddAccount(userId); await context.SaveChangesAsync(); // act var data = new GetLevelWordsRequest() { LevelId = gameLevel.GameLevelId }; var request = new AuthReq <GetLevelWordsRequest, GetLevelWordsReply>(data) { UserId = userId }; GetLevelWordsReply result = await new GetLevelWords(context).HandleRequest(request); // assert Assert.Equal(2, result.Words.Count); }
public async void EstimateTraining_Returns6Stars() { // arrange int userId = 1; using InWordsDataContext context = InWordsDataContextFactory.Create(); Game game = new Game() { }; context.Add(game); context.SaveChanges(); GameLevel gameLevel = new GameLevel() { GameId = game.GameId }; context.Add(gameLevel); context.SaveChanges(); await context.AddAccount(userId); await context.SaveChangesAsync(); // act TrainingDataRequest trainingDataRequest = new TrainingDataRequest(); Training training = new Training { }; training.ClosedCardsMetric = new ClosedCardsMetric(); training.ClosedCardsMetric.WordIdOpenCount.Add(1, 4); training.ClosedCardsMetric.WordIdOpenCount.Add(2, 4); training.ClosedCardsMetric.WordIdOpenCount.Add(3, 2); training.OpenedCardsMetric = new OpenedCardsMetric(); training.OpenedCardsMetric.WordIdOpenCount.Add(1, 3); training.OpenedCardsMetric.WordIdOpenCount.Add(2, 2); training.OpenedCardsMetric.WordIdOpenCount.Add(3, 2); trainingDataRequest.Metrics.Add(training); var requestData = new AuthReq <TrainingDataRequest, TrainingScoreReply>(trainingDataRequest) { UserId = userId }; var requestHandler = new EstimateTraining(context); var result = await requestHandler.HandleRequest(requestData); // assert Assert.Single(result.Scores); Assert.Equal(6, result.Scores.Single().ClosedCards.Score); Assert.Equal(5, result.Scores.Single().OpenedCards.Score); Assert.Equal(6, result.Scores.Single().Score); }
public async void GetLevelsInSet() { // arrange await using InWordsDataContext context = InWordsDataContextFactory.Create(); var game1 = new Game(); context.Add(game1); List <GameLevel> gameLevels = new List <GameLevel> { new GameLevel() { GameId = game1.GameId, }, new GameLevel() { GameId = game1.GameId, } }; context.AddRange(gameLevels); await context.SaveChangesAsync(); // act var data = new GetLevelsRequest() { WordSetId = game1.GameId }; var request = new AuthReq <GetLevelsRequest, GetLevelsReply>(data); var handler = new GetWordSetLevels(context); var result = await handler.Handle(request); // assert Assert.Equal(2, result.Levels.Count); }
public static async Task <int[]> CreateLevels(this InWordsDataContext context, int gameId, int userId, IList <int[]> pairsInLevels) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (pairsInLevels == null) { pairsInLevels = Array.Empty <int[]>(); } // create levles var levels = pairsInLevels.Select(d => { var game = new GameLevel() { GameId = gameId }; game.Historylevel = new Historylevel() { DateTime = DateTime.UtcNow, WordsCount = d.Distinct().Count(), GameLevel = game }; context.Add(game); return(game); }).ToArray(); await context.SaveChangesAsync().ConfigureAwait(false); // fill levels with words for (int i = 0; i < pairsInLevels.Count; i++) { var currentLevel = levels[i]; int[] currentWordsIds = pairsInLevels[i].ToArray(); var currentWords = context.CurrentUserWordPairs(userId) .Where(u => currentWordsIds.Contains(u.UserWordPairId)) .ToArray(); var gameLevelWords = currentWords.Select(w => new GameLevelWord() { ForeignWord = w.ForeignWord, NativeWord = w.NativeWord, GameLevelId = currentLevel.GameLevelId, }).ToArray(); context.GameLevelWords.AddRange(gameLevelWords); } await context.SaveChangesAsync().ConfigureAwait(false); return(levels.Select(level => level.GameLevelId).ToArray()); }
public static IEnumerable <UserGameLevel> AddOrUpdateUserGameLevel( this InWordsDataContext context, List <LevelScore> levelScores, int userId) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (levelScores == null) { return(Array.Empty <UserGameLevel>()); } levelScores.AddRange(GetTotalScores(levelScores)); // select all games var types = levelScores.Select(d => d.GameType).Distinct().ToArray(); var levelIds = levelScores.Select(d => d.GameLevelId).Distinct().ToArray(); var existedLevels = context.UserGameLevels .Where(u => u.UserId == userId) .Where(u => types.Contains(u.GameType)) .Where(u => levelIds.Contains(u.GameLevelId)); // find all users games var usersGames = from gameLevel in existedLevels join usersGame in context.UserGameLevels.Where(s => s.UserId == userId) on gameLevel.GameLevelId equals usersGame.GameLevelId into ug from usersGame in ug.DefaultIfEmpty() select new { gameLevel.GameLevelId, usersGame }; // create if not exist Dictionary <(int, GameType), UserGameLevel> userGameLevels = new Dictionary <(int, GameType), UserGameLevel>(); IList <UserGameLevel> corrupted = new List <UserGameLevel>(); foreach (var ugame in usersGames) { if (ugame == null) { continue; } var currentUserGame = ugame.usersGame; var key = (ugame.GameLevelId, currentUserGame.GameType); if (userGameLevels.ContainsKey(key)) { if (userGameLevels[key].UserGameLevelId < currentUserGame.UserGameLevelId) { int maxScore = Math.Max(userGameLevels[key].UserStars, currentUserGame.UserStars); currentUserGame.UserStars = maxScore; corrupted.Add(userGameLevels[key]); userGameLevels[key] = currentUserGame; } } else { userGameLevels.Add(key, currentUserGame); } } context.RemoveRange(corrupted); // update if currentScore > score in database foreach (var ls in levelScores) { UserGameLevel currentScore; var key = (ls.GameLevelId, ls.GameType); if (userGameLevels.ContainsKey(key)) { currentScore = userGameLevels[key]; } else { currentScore = new UserGameLevel(userId, ls.GameLevelId, ls.Score, ls.GameType); userGameLevels.Add(key, currentScore); context.Add(currentScore); } if (currentScore.UserStars < ls.Score) { currentScore.UserStars = ls.Score; } } return(userGameLevels.Values); }