public void UpdateTotalScore(string userId, int langId) { int total = 0; List <TestResults> results = _db.TestResults.Where(r => r.UserId == userId && r.LangId == langId).ToList(); foreach (TestResults testResult in results) { total += testResult.Result; } TotalScores totalScores = null; try { totalScores = _db.TotalScores.First(s => s.UserId == userId && s.LangId == langId); } catch (InvalidOperationException) { Console.WriteLine("New test result"); } catch (Exception e) { Console.WriteLine(e); throw; } if (totalScores != null) { totalScores.Total = total; _db.Entry(totalScores).State = EntityState.Modified; } else { totalScores = new TotalScores(); totalScores.LangId = langId; totalScores.UserId = userId; totalScores.Total = total; _db.TotalScores.Add(totalScores); } _db.SaveChanges(); }
public string TotalScores() //Calculates the total score. { try { foreach (Competitor TotalScores in skiCompetitors.Values) //Foreach competitor in the dictionary we take their score and add it. { string TotalAddedScores = TotalScores.GetScore(); //Create a varabale makes it easier to see what we're doing. Scores = Scores + int.Parse(TotalAddedScores); //Parse the string into an integer. ConvertedAddedScores = Scores.ToString(); //Turn the added scores back into a string. } } catch { } Scores = 0; //Sets the score back to 0. if (ConvertedAddedScores == null) //If the scores total is null we can return a string. { ConvertedAddedScores = "0"; //Returns "0". Makes the application look more consistent. } return(ConvertedAddedScores); //Returns the total scores. }
public async Task <bool> Test([FromBody] DTOTestResultInfo resultInfo) { var isUser = false; string currentUserId = User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (String.IsNullOrEmpty(currentUserId)) { return(isUser); } int idLangLearn = (int)HttpContext.Session.GetInt32("idLangLearn"); DateTime testDate = DateTime.Now; TestResults testResult = new TestResults() { Result = resultInfo.TotalResult, UserId = currentUserId, LangId = idLangLearn, CategoryId = resultInfo.SubCategoryId, TestDate = testDate, TestId = resultInfo.TestNumber }; var testResultList = await _testResults.GetAll().Where(x => x.TestId == resultInfo.TestNumber && x.LangId == idLangLearn && x.CategoryId == resultInfo.SubCategoryId).ToListAsync(); var totalScoresList = await _totalScores.GetAll().Where(x => x.UserId == currentUserId && x.LangId == idLangLearn).ToListAsync(); int maxTestResultBefore = -1; if (testResultList.Any()) { maxTestResultBefore = testResultList.Max(x => x.Result); } _testResults.Create(testResult); if (!totalScoresList.Any() && !testResultList.Any()) { TotalScores totalScore = new TotalScores() { Total = resultInfo.TotalResult, UserId = currentUserId, LangId = idLangLearn }; _totalScores.Create(totalScore); } else if (!testResultList.Any()) { TotalScores totalScore = totalScoresList.First(); totalScore.Total += testResult.Result; _totalScores.Update(totalScore); } else { TotalScores totalScore = totalScoresList.First(); if (Math.Max(resultInfo.TotalResult, maxTestResultBefore) == resultInfo.TotalResult) { totalScore.Total += Math.Abs(resultInfo.TotalResult - maxTestResultBefore); } _totalScores.Update(totalScore); } _testResults.Save(); _totalScores.Save(); isUser = true; return(isUser); }
private static void Main() { const int MAX_POINTS = 35; string playerPosition = string.Empty; char[,] playField = GeneratePlayField(); char[,] bombsPositions = GenerateBombsPositions(); int pointsCounter = 0; bool isDetonated = false; List <TotalScores> topPlayersScores = new List <TotalScores>(6); int row = 0; int column = 0; bool gameNotStarted = true; bool maxPointsReached = false; do { if (gameNotStarted) { Console.WriteLine("Lets play Minesweeper. Try to find fields without detonating a bomb." + " enter 'top' if you want to see the best players results, 'restart' to start the game, 'exit' if you want to quit the game!"); DrawPlayField(playField); gameNotStarted = false; } Console.Write("Write row and column numbers: "); playerPosition = Console.ReadLine().Trim(); if (playerPosition.Length >= 3) { if (int.TryParse(playerPosition[0].ToString(), out row) && int.TryParse(playerPosition[2].ToString(), out column) && row <= playField.GetLength(0) && column <= playField.GetLength(1)) { playerPosition = "turn"; } } switch (playerPosition) { case "top": DrawRanking(topPlayersScores); break; case "restart": playField = GeneratePlayField(); bombsPositions = GenerateBombsPositions(); DrawPlayField(playField); isDetonated = false; gameNotStarted = false; break; case "exit": Console.WriteLine("Goodbye!"); break; case "turn": if (bombsPositions[row, column] != '*') { if (bombsPositions[row, column] == '-') { ChangePlayerTurn(playField, bombsPositions, row, column); pointsCounter++; } if (MAX_POINTS == pointsCounter) { maxPointsReached = true; } else { DrawPlayField(playField); } } else { isDetonated = true; } break; default: Console.WriteLine("\nInvalid Command!\n"); break; } if (isDetonated) { DrawPlayField(bombsPositions); Console.Write("\nYou've died as a hero! Your scores are: {0}. " + "Enter your name: ", pointsCounter); string niknejm = Console.ReadLine(); TotalScores t = new TotalScores(niknejm, pointsCounter); if (topPlayersScores.Count < 5) { topPlayersScores.Add(t); } else { for (int i = 0; i < topPlayersScores.Count; i++) { if (topPlayersScores[i].PlayerScore < t.PlayerScore) { topPlayersScores.Insert(i, t); topPlayersScores.RemoveAt(topPlayersScores.Count - 1); break; } } } topPlayersScores.Sort((TotalScores r1, TotalScores r2) => r2.PlayerName.CompareTo(r1.PlayerName)); topPlayersScores.Sort((TotalScores r1, TotalScores r2) => r2.PlayerScore.CompareTo(r1.PlayerScore)); DrawRanking(topPlayersScores); playField = GeneratePlayField(); bombsPositions = GenerateBombsPositions(); pointsCounter = 0; isDetonated = false; gameNotStarted = true; } if (maxPointsReached) { Console.WriteLine("\nCongrats! You have been opened all cells without dying."); DrawPlayField(bombsPositions); Console.WriteLine("Your name: "); string playerName = Console.ReadLine(); TotalScores playerScore = new TotalScores(playerName, pointsCounter); topPlayersScores.Add(playerScore); DrawRanking(topPlayersScores); playField = GeneratePlayField(); bombsPositions = GenerateBombsPositions(); pointsCounter = 0; maxPointsReached = false; gameNotStarted = true; } }while (playerPosition != "exit"); Console.Read(); }
private void CurrentScore_ScoreChanged(object sender, System.EventArgs e) { var totalScore = TotalScores.First(x => x.PlayerId == CurrentScore.PlayerId); totalScore.TotalPutts = Scores.Where(x => x.PlayerId == CurrentScore.PlayerId).Sum(x => x.Putts); }