public async Task <IActionResult> RunTeamScoresForDate(RunTeamDateDto value)
        {
            var updateTeamscores = await _repo.RunTeamScoresForDate(value);

            return(StatusCode(201));
        }
Пример #2
0
        // public async Task<bool> RunTeamScoresForDate(RunTeamDateDto value)
        // {
        //     // Get the current round
        //     var round = await _content.Rounds.FromSql("SELECT * FROM Rounds where CAST(startDate AS INT) <= {0} and CAST(endDate AS INT) >= {0}", value.RunDate).FirstOrDefaultAsync();

        //     // What will be happening is for each userId
        //     var users = await _content.Users.ToListAsync();

        //     foreach(var user in users) {
        //         decimal daysScore = 0;

        //         // Need to get the current rounds score

        //         // Their teamDetails are got
        //         var teamdetails = await _content.TeamDetails.FromSql("SELECT * FROM TeamDetails where userId = {0}", user.Id).ToListAsync();

        //         // Then for each teamdetail record
        //         foreach(var teamdetail in teamdetails) {
        //             // Check to see if there is a score for the player for the gameDate of value
        //             var playerScore = await _content.PlayerScores.FromSql("SELECT * FROM PlayerScores where PlayerId = {0} and GameDate = {1}", teamdetail.PlayerId, value.RunDate).ToListAsync();

        //             if(playerScore.Count > 0) {
        //                 // if yes add the score to the team score after applying any bonuses (C or 6)
        //                 if(teamdetail.Position <= 10) {
        //                     if(teamdetail.Position == teamdetail.Captain) {
        //                         daysScore = daysScore + (playerScore[0].Score * 2);
        //                     } else if (teamdetail.Position == teamdetail.SixthMan) {
        //                         decimal tempValue = (decimal) playerScore[0].Score;
        //                         daysScore = daysScore + tempValue;
        //                     } else {
        //                         daysScore = daysScore + playerScore[0].Score;
        //                     }
        //                 }
        //             }
        //         }

        //         // once all teamDetails are completed for the user, then update entry to the TeamScore table for the day
        //         var teamScore = await _content.TeamScores.FirstOrDefaultAsync(t => t.RoundId == round.RoundNumber && t.UserId == user.Id);

        //         // TeamScore ts = new TeamScore();
        //         teamScore.UserId = user.Id;
        //         teamScore.RoundId = round.RoundNumber;
        //         teamScore.Total = daysScore / 100;

        //         // Now need to update the TeamScore
        //         _content.TeamScores.Update(teamScore);
        //     }
        //     return await _content.SaveChangesAsync() > 0;
        // }

        public async Task <bool> RunTeamScoresForDate(RunTeamDateDto value)
        {
            // Get the current round
            var round = await _content.Rounds.FromSql("SELECT * FROM Rounds where CAST(startDate AS INT) <= {0} and CAST(endDate AS INT) >= {0}", value.RunDate).FirstOrDefaultAsync();

            // What will be happening is for each userId
            var users = await _content.Users.ToListAsync();

            foreach (var user in users)
            {
                decimal daysScore = 0;

                // Need to get the current rounds score

                // Their teamDetails are got
                var teamdetails = await _content.TeamDetails.FromSql("SELECT * FROM TeamDetails where userId = {0}", user.Id).ToListAsync();

                // Then for each teamdetail record
                foreach (var teamdetail in teamdetails)
                {
                    // Check to see if there is a score for the player for the gameDate of value
                    var playerScore = await _content.PlayerScores.FromSql("SELECT * FROM PlayerScores where PlayerId = {0} and GameDate = {1}", teamdetail.PlayerId, value.RunDate).ToListAsync();

                    if (playerScore.Count > 0)
                    {
                        // if yes add the score to the team score after applying any bonuses (C or 6)
                        if (teamdetail.Position <= 10)
                        {
                            if (teamdetail.Position == teamdetail.Captain)
                            {
                                daysScore = daysScore + (playerScore[0].Score * 2);
                            }
                            else if (teamdetail.Position == teamdetail.SixthMan)
                            {
                                decimal tempValue = (decimal)playerScore[0].Score;
                                daysScore = daysScore + tempValue;
                            }
                            else
                            {
                                daysScore = daysScore + playerScore[0].Score;
                            }
                        }
                    }
                }

                // // once all teamDetails are completed for the user, then update entry to the TeamScore table for the day
                // var teamScore = await _content.TeamScores.FirstOrDefaultAsync(t => t.RoundId == round.RoundNumber && t.UserId == user.Id);

                // // TeamScore ts = new TeamScore();
                // // teamScore.UserId = user.Id;
                // // teamScore.RoundId = round.RoundNumber;
                // // teamScore.Total = daysScore / 100;

                // // // Now need to update the TeamScore
                // // _content.TeamScores.Update(teamScore);
                var teamScore = _content.TeamScores.FirstOrDefault(t => t.RoundId == round.RoundNumber && t.UserId == user.Id);

                TeamScore ts = new TeamScore();
                if (teamScore == null)
                {
                    // ts.UserId = user.Id;
                    // ts.RoundId = round.RoundNumber;
                    // ts.Total = (int)daysScore;

                    // // Create a new record
                    // await _content.TeamScores.AddAsync(ts);
                }
                else
                {
                    // ts.Id = teamScore.Id;
                    teamScore.UserId  = user.Id;
                    teamScore.RoundId = round.RoundNumber;
                    teamScore.Total   = (int)daysScore;

                    // Update
                    _content.TeamScores.Update(teamScore);
                }
            }
            return(await _content.SaveChangesAsync() > 0);
        }