Ejemplo n.º 1
0
        public async Task PostScore(string nickname, string game, [FromBody] int points)
        {
            // Lookup gamer based on nickname
            Gamer gamer = await context.Gamers
                          .FirstOrDefaultAsync(g => g.Nickname.ToLower() == nickname.ToLower());

            if (gamer == null)
            {
                return;
            }

            // Find highest score for game
            var score = await context.Scores
                        .Where(s => s.Game == game && s.Gamer == gamer)
                        .OrderByDescending(s => s.Points)
                        .FirstOrDefaultAsync();

            if (score == null)
            {
                score = new Score()
                {
                    Gamer = gamer, Points = points, Game = game
                };
                await context.Scores.AddAsync(score);
            }
            else
            {
                if (score.Points > points)
                {
                    return;
                }
                score.Points = points;
            }
            await context.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task <bool> UpdateAsync(PlayerDto playerDto)
        {
            leaderboardContext.PlayerDtos.Attach(playerDto);
            leaderboardContext.Entry(playerDto).State = EntityState.Modified;

            return(await leaderboardContext.SaveChangesAsync() > 0);
        }
Ejemplo n.º 3
0
        public async Task <BoardEntryDto> Post(BoardEntryDto boardEntryDto)
        {
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(new HttpResponseMessage
                {
                    ReasonPhrase = "Invalid data",
                    StatusCode   = HttpStatusCode.BadRequest
                });
            }

            using (var dbContext = new LeaderboardContext())
            {
                var boardEntry = new BoardEntry
                {
                    Id        = Guid.NewGuid(),
                    Failed    = boardEntryDto.Failed,
                    Passed    = boardEntryDto.Passed,
                    Skipped   = boardEntryDto.Skipped,
                    Username  = boardEntryDto.Username,
                    Submitted = DateTime.UtcNow
                };

                // remove existing entry before adding new one
                // should probably have a password or something
                dbContext.Leaderboard.RemoveRange(dbContext.Leaderboard.Where(l => l.Username == boardEntryDto.Username));

                // add new entry
                var result = dbContext.Leaderboard.Add(boardEntry);
                await dbContext.SaveChangesAsync();

                if (dbContext.TopTenEntries.Any(be => be.Id == result.Id))
                {
                    var clients = GlobalHost.ConnectionManager.GetHubContext <LeaderboardHub>().Clients;
                    clients.All.leaderboardUpdate(await dbContext.TopTenEntries.Select(entry => new BoardEntryDto
                    {
                        Id        = entry.Id,
                        Failed    = entry.Failed,
                        Passed    = entry.Passed,
                        Skipped   = entry.Skipped,
                        Username  = entry.Username,
                        Submitted = entry.Submitted
                    }).ToListAsync());
                }

                return(new BoardEntryDto
                {
                    Failed = result.Failed,
                    Id = result.Id,
                    Passed = result.Passed,
                    Skipped = result.Skipped,
                    Username = result.Username,
                    Submitted = result.Submitted
                });
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> AddOrReplaceScore(int gameId, string leaderboardMoniker, [FromBody] AddOrUpdateScoreRequest req)
        {
            if (req == null)
            {
                return(BadRequest("Empty body"));
            }

            var game = await _ctx.Games.FirstOrDefaultAsync(x => x.Id == gameId);

            if (game == null)
            {
                return(BadRequest($"Game with id {gameId} doesn't exist"));
            }

            var leaderboard = await _ctx.Leaderboards.Include(nameof(Leaderboard.Entries)).FirstOrDefaultAsync(x => x.Name == leaderboardMoniker);

            if (leaderboard == null)
            {
                return(BadRequest($"Leaderboard with moniker {leaderboardMoniker} doesn't exist"));
            }

            if (leaderboard.Entries == null)
            {
                return(NotFound("null entries"));
            }

            var existing = leaderboard.Entries.FirstOrDefault(x => x.DeviceID == req.DeviceId);

            if (existing != null)
            {
                if (existing.PlayerName != req.PlayerName)
                {
                    existing.PlayerName = req.PlayerName;
                }

                existing.ScoreValue += req.NewScore;
            }
            else
            {
                var entry = new LeaderboardEntry()
                {
                    DeviceID   = req.DeviceId,
                    ScoreValue = req.NewScore,
                    PlayerName = req.PlayerName,
                };

                leaderboard.Entries.Add(entry);
            }

            await _ctx.SaveChangesAsync();

            return(Ok());
        }
Ejemplo n.º 5
0
        public async Task Delete()
        {
            using (var dbContext = new LeaderboardContext())
            {
                dbContext.Leaderboard.RemoveRange(dbContext.Leaderboard);
                await dbContext.SaveChangesAsync();

                var clients = GlobalHost.ConnectionManager.GetHubContext <LeaderboardHub>().Clients;
                clients.All.leaderboardUpdate(await dbContext.TopTenEntries.Select(entry => new BoardEntryDto
                {
                    Id        = entry.Id,
                    Failed    = entry.Failed,
                    Passed    = entry.Passed,
                    Skipped   = entry.Skipped,
                    Username  = entry.Username,
                    Submitted = entry.Submitted
                }).ToListAsync());
            }
        }