Пример #1
0
        public IActionResult PostRace([FromBody] Race value)
        {
            if (value == null)
            {
                return(BadRequest());
            }

            var loginService = new LoginService(this.sqlManager);
            var user         = loginService.GetUserFromContext(HttpContext.User);

            if (user == null)
            {
                return(Unauthorized());
            }

            var petService = new PetService(this.sqlManager);
            var pet        = petService.GetPetById(value.PetId);

            if (pet == null)
            {
                return(NotFound());
            }

            if (user.UserId != pet.UserId)
            {
                return(Unauthorized());
            }

            var result = service.InsertRace(value);

            return(Ok(result));
        }
        public IActionResult PostNewScore(ulong petid, int score)
        {
            var u = loginService.GetUserFromContext(HttpContext.User);

            if (u == null)
            {
                return(Unauthorized());
            }

            // get the pet
            var pet = petService.GetPetById(petid);

            if (pet == null)
            {
                return(NotFound());
            }
            // check ownership
            if (pet.UserId != u.UserId)
            {
                return(Unauthorized());
            }

            if (score <= 0)
            {
                return(BadRequest());
            }

            // TODO: Consider limiting this based on when the last score was inserted and when the last activity for this pet appears
            // could use the activity id as a way to do this.

            RaceService race = new RaceService(this.sqlManager);
            var         r    = race.InsertRace(new Race()
            {
                PetId     = petid,
                RaceId    = 0,
                Score     = score,
                Timestamp = DateTime.UtcNow
            });

            // also post a new activity
            activityService.MakeActivityForPet(petid, ActivityType.Race);

            // if the score was podium
            var rank = race.GetRaceRank(r.RaceId);

            if (rank != -1 && rank < 4)
            {
                activityService.MakeActivityForPet(petid, ActivityType.RaceHighScore);
                activityService.UpdatePetFromActivity(ActivityType.RaceHighScore, petid, petService);

                // new high score was posted
                var n = new NotificationService();
                if (u.PhoneNumber != null)
                {
                    n.SendMessage(u.PhoneNumber, $"Great job {u.Username}, you just placed #{rank} on the leaderboard!");
                }
                n.SendDiscordNotifyHighScore(rank, score, pet, u);
            }
            else
            {
                activityService.UpdatePetFromActivity(ActivityType.Race, petid, petService);
            }

            return(Json(r));
        }