public static LevelResult ToLevelResult(this LevelMetricQuery levelMetricQuery) { var levelResult = new LevelResult { LevelId = levelMetricQuery.GameLevelId, OpeningQuantity = levelMetricQuery.WordPairIdOpenCounts.Sum(s => s.Value), WordsCount = levelMetricQuery.WordPairIdOpenCounts.Count * 2 }; return(levelResult); }
/// <summary> /// This is to get score by level and update information of memorizing user word pairs /// </summary> /// <param name="userId"></param> /// <param name="levelMetricQuery"></param> /// <returns></returns> public async Task <LevelMetricQueryResult> SetResultsAsync(int userId, LevelMetricQuery levelMetricQuery) { // set sore; LevelMetricQueryResult levelMetricQueryResult = gameScoreService.GetLevelScore(levelMetricQuery.ToLevelResult()); // save score to storage await gameScoreService.PostScoreAsync(userId, levelMetricQueryResult).ConfigureAwait(false); // Calculate word metric; IKnowledgeQualifier knowledgeQualifier = new CardGameKnowledge(levelMetricQuery); // update words pairs license in store await knowledgeUpdateService.UpdateKnowledge(userId, knowledgeQualifier).ConfigureAwait(false); return(levelMetricQueryResult); }
public void SerializationTest() { var expected = new LevelMetricQuery { GameLevelId = 1, WordPairIdOpenCounts = new Dictionary <int, int> { { 2, 3 } } }; Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(expected)); // replace when work and remove Add .AddNewtonsoftJson() // just after .AddControllers() / .AddMvc() or any other combination. // remove Microsoft.AspNetCore.Mvc.NewtonsoftJson //string json = JsonSerializer.Serialize(expected); //var actual = JsonSerializer.Deserialize<LevelMetricQuery>(json) //Assert.NotNull(actual); //Assert.Equal(1, actual.GameLevelId); //Assert.NotNull(actual.WordPairIdOpenCounts); //Assert.True(actual.WordPairIdOpenCounts.ContainsKey(2)); //Assert.Equal(3, actual.WordPairIdOpenCounts[2]); }
public async Task <IActionResult> PostScore(LevelMetricQuery levelMetricQuery) { // Convert to new request var classicCardsMetrics = new ClassicCardLevelMetricQuery { UserId = User.GetUserId(), Metrics = new List <ClassicCardLevelMetric>() { new ClassicCardLevelMetric() { GameLevelId = levelMetricQuery.GameLevelId, WordPairIdOpenCounts = levelMetricQuery.WordPairIdOpenCounts } }.ToImmutableArray() }; // Actual code ClassicCardLevelMetricQueryResult result; try { result = await mediator.Send(classicCardsMetrics) .ConfigureAwait(false); } catch (ArgumentOutOfRangeException e) { return(BadRequest(e.Message)); } // convert to obsolete answer var scoreInfo = result.ClassicCardLevelResult.FirstOrDefault(); LevelMetricQueryResult answer = new LevelMetricQueryResult(scoreInfo.LevelId, scoreInfo.Score); return(Ok(answer)); }
public CardGameKnowledge(LevelMetricQuery levelMetricQuery) { this.levelMetricQuery = levelMetricQuery; }