public bool AddHighScore(HighScore highScore) { if (highScore == null || highScore.Name == null || highScore.Name.Length == 0 || highScore.Name.Length > Limits.MAX_HIGHSCORE_NAME || highScore.Score <= 0 || highScore.Lines <= 0 || highScore.Level < 0) { return(false); } if (highScore.Lines / 10 != highScore.Level) { return(false); } var dbContext = GetDbContext(); var highScores = dbContext.DbTetrisHighScore.OrderByDescending(sc => sc.Score).ToList(); if (highScores.Count >= 10) { if (highScores[9].Score >= highScore.Score) { return(false); } } var h = new DbTetrisHighScore { Created = DateTime.UtcNow, Name = highScore.Name, Level = highScore.Level, Lines = highScore.Lines, Score = highScore.Score }; dbContext.DbTetrisHighScore.Add(h); dbContext.SaveChanges(); return(true); }
public List <HighScore> GetHighScores() { var opt = GetOptions(); var ret = new List <HighScore>(); var del = new List <DbTetrisHighScore>(); var dbContext = GetDbContext(); var highScores = dbContext.DbTetrisHighScore.OrderByDescending(sc => sc.Score); foreach (var hs in highScores) { var highScore = new HighScore { Name = hs.Name, Created = DbMynaContext.GetUtcDateTime(hs.Created).Value, Level = hs.Level, Lines = hs.Lines, Score = hs.Score }; var diff = DateTime.UtcNow - highScore.Created; if (diff.TotalDays > opt.KeepHighscores) { del.Add(hs); continue; } ret.Add(highScore); if (ret.Count >= 10) { break; } } if (del.Any()) { dbContext.DbTetrisHighScore.RemoveRange(del); dbContext.SaveChanges(); } return(ret); }
public IActionResult AddHighScore([FromBody] HighScore value) { return(new JsonResult(TetrisService.AddHighScore(value))); }