private static (int, int) CalculateScore(DAO.Match match)
        {
            int player1 = 0, player2 = 0;

            if (match.Competitor1Set1 > match.Competitor2Set1)
            {
                ++player1;
            }
            else
            {
                ++player2;
            }
            if (match.Competitor1Set2 > match.Competitor2Set2)
            {
                ++player1;
            }
            else
            {
                ++player2;
            }
            if (match.Competitor2Set3 != 0)
            {
                if (match.Competitor1Set3 > match.Competitor2Set3)
                {
                    ++player1;
                }
                else
                {
                    ++player2;
                }
            }
            return(player1, player2);
        }
        public async Task <int> AddResult(Domain.Result.Result result, string userId)
        {
            string[] resultScoreset1 = result.Set1.Split(":");
            string[] resultScoreset2 = result.Set2.Split(":");
            string[] resultScoreset3 = null;
            if (!CheckResult(resultScoreset1) || !CheckResult(resultScoreset2))
            {
                return(-1);
            }
            if (result.Set3 != null)
            {
                resultScoreset3 = result.Set3.Split(":");
                if (!CheckResult(resultScoreset3))
                {
                    return(-1);
                }
            }
            var player1 = await _context.CompetitorData.Where(x => x.UserId == userId).FirstOrDefaultAsync();

            var player2 = await _context.CompetitorData.Where(x => x.CompetitorDataId == result.Enemy).FirstOrDefaultAsync();

            if (result.League != 0 && (player1.LeagueId != result.League || player2.LeagueId != result.League))
            {
                return(-2);
            }
            DAO.Match match = new DAO.Match
            {
                Competitor1Set1 = Int32.Parse(resultScoreset1[0]),
                Competitor2Set1 = Int32.Parse(resultScoreset1[1]),
                Competitor1Set2 = Int32.Parse(resultScoreset2[0]),
                Competitor2Set2 = Int32.Parse(resultScoreset2[1]),
                Competitor1     = player1.CompetitorDataId,
                Competitor2     = player2.CompetitorDataId,
                Confirmation    = false,
                Protest         = false,
                AddingTime      = DateTime.Now
            };
            if (resultScoreset3 != null)
            {
                match.Competitor1Set3 = Int32.Parse(resultScoreset3[0]);
                match.Competitor2Set3 = Int32.Parse(resultScoreset3[1]);
            }
            await _context.AddAsync(match);

            await _context.SaveChangesAsync();

            UpdateTable(match);
            return(0);
        }
예제 #3
0
        private async void ChangeRankingPosition(DAO.Match match)
        {
            var score = CalculateScore(match);

            if (score.Item1 > score.Item2)
            {
                match.Winner = match.Competitor1;
            }
            else
            {
                match.Winner = match.Competitor2;
            }
            await _context.SaveChangesAsync();

            var winner = await _context.RankingTable.Where(x => x.CompetitorDataId == match.Winner).FirstOrDefaultAsync();

            var looser = await _context.RankingTable.Where(x => (x.CompetitorDataId == match.Competitor1 || x.CompetitorDataId == match.Competitor2) && x.CompetitorDataId != match.Winner).FirstOrDefaultAsync();

            if (winner.Position == looser.Position + 1)
            {
                var LastMatch = await _context.Match.Where(x => x.MatchId != match.MatchId && match.LeagueId == 0 && x.Competitor1 == winner.CompetitorDataId).OrderByDescending(x => x.MatchId).FirstOrDefaultAsync();

                DAO.RankingTable PlayerTwoPositionHiger = null;
                if (LastMatch != null)
                {
                    PlayerTwoPositionHiger = await _context.RankingTable.Where(x => x.Position - 2 == winner.Position).FirstOrDefaultAsync();
                }
                if ((LastMatch != null && (PlayerTwoPositionHiger != null &&
                                           (LastMatch.Competitor1 == PlayerTwoPositionHiger.CompetitorDataId ||
                                            LastMatch.Competitor2 == PlayerTwoPositionHiger.CompetitorDataId))) &&
                    LastMatch.Winner == match.Winner)
                {
                    looser.Position += 1;
                    winner.Position -= 2;
                    PlayerTwoPositionHiger.Position += 1;
                }
                else
                {
                    looser.Position += 1;
                    winner.Position -= 1;
                }

                await _context.SaveChangesAsync();
            }
        }
        private async void UpdateTable(DAO.Match match)
        {
            var competitor1 = await _context.LeagueTable.Where(x => x.CompetitorDataId == match.Competitor1).FirstOrDefaultAsync();

            var competitor2 = await _context.LeagueTable.Where(x => x.CompetitorDataId == match.Competitor2).FirstOrDefaultAsync();

            var score = CalculateScore(match);

            if (score.Item1 > score.Item2)
            {
                competitor1.MatechesWon += 1;
                competitor2.MatchesLoss += 1;
            }
            else
            {
                competitor2.MatechesWon += 1;
                competitor1.MatchesLoss += 1;
            }
            competitor1.SetsWon  += score.Item1;
            competitor1.SetsLoss += score.Item2;
            competitor2.SetsWon  += score.Item2;
            competitor2.SetsLoss += score.Item1;
            competitor1.Points   += score.Item1;
            competitor2.Points   += score.Item2;
            if (score.Item1 > score.Item2)
            {
                match.Winner = competitor1.CompetitorDataId;
            }
            else
            {
                match.Winner = competitor2.CompetitorDataId;
            }

            await _context.SaveChangesAsync();

            var tables = await _context.LeagueTable.Where(x => x.LeagueId == match.CompetitorWinner.LeagueId).OrderBy(x => x.Position).ToListAsync();

            changeTablePosition(tables);
            await _context.SaveChangesAsync();
        }