Example #1
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            litAchievements.Text = "";
            List <Player> players = DataAccess.GetPlayers();
            int           g1      = int.Parse(tbxScoreA.Text);
            int           g2      = int.Parse(tbxScoreB.Text);

            Common.EResult t1Result = Common.GetResult(g1, g2);
            Common.EResult t2Result = Common.GetResult(g2, g1);

            Match match = new Match()
            {
                Id = Guid.NewGuid(), Timestamp = DateTime.Now
            };
            TeamMatch tm1 = new TeamMatch()
            {
                Id     = Guid.NewGuid(), MatchId = match.Id,
                Result = (byte)t1Result, GoalsScored = g1, GoalsLost = g2, Timestamp = DateTime.Now
            };
            TeamMatch tm2 = new TeamMatch()
            {
                Id     = Guid.NewGuid(), MatchId = match.Id,
                Result = (byte)t2Result, GoalsScored = g2, GoalsLost = g1, Timestamp = DateTime.Now
            };
            Player        p1Off             = DataAccess.GetPlayer(tbxPlayer1Off.Text);
            Player        p1Def             = DataAccess.GetPlayer(tbxPlayer1Deff.Text);
            Player        p2Off             = DataAccess.GetPlayer(tbxPlayer2Off.Text);
            Player        p2Def             = DataAccess.GetPlayer(tbxPlayer2Deff.Text);
            List <Player> playersInThisGame = new List <Player>()
            {
                p1Off, p1Def, p2Off, p2Def
            };
            TeamMatchPlayer tmp1Off = new TeamMatchPlayer()
            {
                PlayerId    = p1Off.Id,
                Position    = (int)Common.EPosition.Offence,
                Timestamp   = DateTime.Now,
                TeamMatchId = tm1.Id
            };
            TeamMatchPlayer tmp1Def = new TeamMatchPlayer()
            {
                PlayerId    = p1Def.Id,
                Position    = (int)Common.EPosition.Defence,
                Timestamp   = DateTime.Now,
                TeamMatchId = tm1.Id
            };
            TeamMatchPlayer tmp2Off = new TeamMatchPlayer()
            {
                PlayerId    = p2Off.Id,
                Position    = (int)Common.EPosition.Offence,
                Timestamp   = DateTime.Now,
                TeamMatchId = tm2.Id
            };
            TeamMatchPlayer tmp2Def = new TeamMatchPlayer()
            {
                PlayerId    = p2Def.Id,
                Position    = (int)Common.EPosition.Defence,
                Timestamp   = DateTime.Now,
                TeamMatchId = tm2.Id
            };

            double?player1OffPreRating, player1DefPreRating, player2OffPreRating, player2DefPreRating;
            double?player1OffPreRankPosition = null;
            double?player1DefPreRankPosition = null;
            double?player2OffPreRankPosition = null;
            double?player2DefPreRankPosition = null;
            int?   player1OffPostRankPosition, player1DefPostRankPosition, player2OffPostRankPosition, player2DefPostRankPosition;

            using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext())
            {
                var player1Off = context.TeamMatchPlayers.Where(p => p.PlayerId == tmp1Off.PlayerId)
                                 .OrderByDescending(p => p.Timestamp).FirstOrDefault();

                var player1Def = context.TeamMatchPlayers.Where(p => p.PlayerId == tmp1Def.PlayerId)
                                 .OrderByDescending(p => p.Timestamp).FirstOrDefault();

                var player2Off = context.TeamMatchPlayers.Where(p => p.PlayerId == tmp2Off.PlayerId)
                                 .OrderByDescending(p => p.Timestamp).FirstOrDefault();

                var player2Def = context.TeamMatchPlayers.Where(p => p.PlayerId == tmp2Def.PlayerId)
                                 .OrderByDescending(p => p.Timestamp).FirstOrDefault();

                if (player1Off != null)
                {
                    player1OffPreRating       = player1Off.Rating;
                    player1OffPreRankPosition = context.GetPlayerRankPosition(player1Off.PlayerId);
                }
                else
                {
                    player1OffPreRating       = PlayerHelper.INITIAL_RATING;
                    player1OffPreRankPosition = null;
                }

                if (player1Def != null)
                {
                    player1DefPreRating       = player1Def.Rating;
                    player1DefPreRankPosition = context.GetPlayerRankPosition(player1Def.PlayerId);
                }
                else
                {
                    player1DefPreRating       = PlayerHelper.INITIAL_RATING;
                    player1DefPreRankPosition = null;
                }

                if (player2Off != null)
                {
                    player2OffPreRating       = player2Off.Rating;
                    player2OffPreRankPosition = context.GetPlayerRankPosition(player2Off.PlayerId);
                }
                else
                {
                    player2OffPreRating       = PlayerHelper.INITIAL_RATING;
                    player2OffPreRankPosition = null;
                }

                if (player2Def != null)
                {
                    player2DefPreRating       = player2Def.Rating;
                    player2DefPreRankPosition = context.GetPlayerRankPosition(player2Def.PlayerId);
                }
                else
                {
                    player2DefPreRating       = PlayerHelper.INITIAL_RATING;
                    player2DefPreRankPosition = null;
                }

                PlayerHelper.FillPlayersRating(ref tmp1Off, ref tmp1Def, ref tmp2Off, ref tmp2Def,
                                               player1Off,
                                               player1Def,
                                               player2Off,
                                               player2Def,
                                               g1, g2);

                context.Matches.InsertOnSubmit(match);
                context.TeamMatches.InsertOnSubmit(tm1);
                context.TeamMatches.InsertOnSubmit(tm2);
                context.TeamMatchPlayers.InsertOnSubmit(tmp1Def);
                context.TeamMatchPlayers.InsertOnSubmit(tmp2Def);
                context.TeamMatchPlayers.InsertOnSubmit(tmp1Off);
                context.TeamMatchPlayers.InsertOnSubmit(tmp2Off);
                context.SubmitChanges();

                player1OffPostRankPosition = context.GetPlayerRankPosition(tmp1Off.PlayerId);
                player1DefPostRankPosition = context.GetPlayerRankPosition(tmp1Def.PlayerId);
                player2OffPostRankPosition = context.GetPlayerRankPosition(tmp2Off.PlayerId);
                player2DefPostRankPosition = context.GetPlayerRankPosition(tmp2Def.PlayerId);
            }

            double player1OffRatingChange = Math.Round((double)(tmp1Off.Rating - player1OffPreRating), 0);
            double player1DefRatingChange = Math.Round((double)(tmp1Def.Rating - player1DefPreRating), 0);
            double player2OffRatingChange = Math.Round((double)(tmp2Off.Rating - player2OffPreRating), 0);
            double player2DefRatingChange = Math.Round((double)(tmp2Def.Rating - player2DefPreRating), 0);

            int player1OffRankPositionChange = player1OffPreRankPosition.HasValue ? (int)(player1OffPreRankPosition - player1OffPostRankPosition) : -1;
            int player1DefRankPositionChange = player1DefPreRankPosition.HasValue ? (int)(player1DefPreRankPosition - player1DefPostRankPosition) : -1;
            int player2OffRankPositionChange = player2OffPreRankPosition.HasValue ? (int)(player2OffPreRankPosition - player2OffPostRankPosition) : -1;
            int player2DefRankPositionChange = player2DefPreRankPosition.HasValue ? (int)(player2DefPreRankPosition - player2DefPostRankPosition) : -1;

            litTbxPlayer1OffStats.Text  = string.Format(PostSubmittedStats, player1OffPostRankPosition, GetColouredRankPosition(player1OffRankPositionChange), Math.Round((double)tmp1Off.Rating, 0), GetColouredRating(player1OffRatingChange));
            litTbxPlayer1DeffStats.Text = string.Format(PostSubmittedStats, player1DefPostRankPosition, GetColouredRankPosition(player1DefRankPositionChange), Math.Round((double)tmp1Def.Rating, 0), GetColouredRating(player1DefRatingChange));
            litTbxPlayer2OffStats.Text  = string.Format(PostSubmittedStats, player2OffPostRankPosition, GetColouredRankPosition(player2OffRankPositionChange), Math.Round((double)tmp2Off.Rating, 0), GetColouredRating(player2OffRatingChange));
            litTbxPlayer2DeffStats.Text = string.Format(PostSubmittedStats, player2DefPostRankPosition, GetColouredRankPosition(player2DefRankPositionChange), Math.Round((double)tmp2Def.Rating, 0), GetColouredRating(player2DefRatingChange));

            pnlInfo.Controls.Add(new LiteralControl("Mecz został zapisany"));
            tbxScoreA.Text = tbxScoreB.Text = "";
            tbxPlayer1Off.Focus();

            CalculateAchievements(playersInThisGame);
            Cache.Remove(DataAccess.OverallStatsCacheName);
        }
Example #2
0
        protected void gvPlayerResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Player p = DataAccess.GetPlayer(Server.HtmlDecode(e.Row.Cells[2].Text));
                if (p != null)
                {
                    e.Row.Cells[2].Text = Server.HtmlDecode(PlayerHelper.GetPlayerNameLink(p));
                }

                if (playersAchievements == null)
                {
                    playersAchievements = DataAccess.GetPlayersWithAchievements();
                }

                PlayersAchievements pa = playersAchievements.FirstOrDefault(w => w.Player.Id == p.Id);
                if (pa != null)
                {
                    List <AchievementWithCurrentLevel> achievements = pa.Achievements;
                    if (achievements.Any())
                    {
                        StringBuilder sb = new StringBuilder("<div class=\"achievementsIcons\">");
                        for (int i = 0; i < pa.Achievements.Count; i++)
                        {
                            if (i > 0 && i % 4 == 0)
                            {
                                sb.Append("</div><div class=\"achievementsIcons\">");
                            }
                            AchievementWithCurrentLevel awcl = pa.Achievements[i];
                            string src   = "";
                            string label = "Poziom {0}: {1}";
                            switch (awcl.Level)
                            {
                            case 1:
                                src   = awcl.Achievement.Level1Icon;
                                label = string.Format(label, Common.GetAchievementsLevelName(1), awcl.Achievement.Level1Label);
                                break;

                            case 2:
                                src   = awcl.Achievement.Level2Icon;
                                label = string.Format(label, Common.GetAchievementsLevelName(2), awcl.Achievement.Level2Label);
                                break;

                            case 3:
                                src   = awcl.Achievement.Level3Icon;
                                label = string.Format(label, Common.GetAchievementsLevelName(3), awcl.Achievement.Level3Label);
                                break;
                            }
                            sb.AppendFormat("<img src=\"{0}\" alt=\"{1}\" title=\"{1}\" height=\"19px\" width=\"19px\" />", src, label);
                        }

                        if (!sb.ToString().EndsWith("</div>"))
                        {
                            sb.Append("</div>");
                        }

                        e.Row.Cells[9].Text = sb.ToString();
                    }
                    else
                    {
                        e.Row.Cells[9].Text = "Brak";
                    }
                }
                else
                {
                    e.Row.Cells[9].Text = "Brak";
                }

                if (Session["UserBasicStatsPlayer"] != null)
                {
                    Player player = (Player)Session["UserBasicStatsPlayer"];
                    if (e.Row.Cells[0].Text == player.Id.ToString())
                    {
                        e.Row.Font.Bold = true;
                    }
                }

                int balance = -1;

                if (int.TryParse(e.Row.Cells[6].Text, out balance))
                {
                    if (balance > 0)
                    {
                        e.Row.Cells[6].Text = "+" + balance.ToString();
                    }
                }


                int pointsChange = -1;

                if (int.TryParse(e.Row.Cells[8].Text, out pointsChange))
                {
                    if (pointsChange > 0)
                    {
                        e.Row.Cells[8].Text = "+" + pointsChange.ToString();
                    }
                }
            }
        }
        private void BindRivalsStats()
        {
            pnlRivals.Controls.Clear();
            Player player = ((Player)Session["UserBasicStatsPlayer"]);

            if (player == null)
            {
                return;
            }

            using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext())
            {
                List <TeamMatchPlayer> rivalMatches = context.TeamMatchPlayers.Where(p =>
                                                                                     !p.TeamMatch.TeamMatchPlayers.Any(x => x.PlayerId == player.Id) &&
                                                                                     p.TeamMatch.Match.TeamMatches.Any(x => x.TeamMatchPlayers.Any(y => y.PlayerId == player.Id)))
                                                      .OrderByDescending(p => p.Timestamp).ToList();
                StringBuilder sb = new StringBuilder();

                if (rivalMatches.Any())
                {
                    //TeamMatchPlayer playerData = lastGames.First().TeamMatchPlayers.First(p => p.PlayerId == player.Id);

                    var rivalGames = rivalMatches
                                     .GroupBy(p => new { Partner = p.Player })
                                     .Select(x => new
                    {
                        //Partner = x.Key.Partner,
                        Name             = PlayerHelper.GetPlayerNameLink(x.Key.Partner),
                        Games            = x.Count(),
                        Won              = x.Count(z => z.TeamMatch.Result == (byte)Common.EResult.Win),
                        Drawn            = x.Count(z => z.TeamMatch.Result == (byte)Common.EResult.Draw),
                        Lost             = x.Count(z => z.TeamMatch.Result == (byte)Common.EResult.Loose),
                        Aggregate        = x.Count(z => z.TeamMatch.Result == (byte)Common.EResult.Win) - x.Count(z => z.TeamMatch.Result == (byte)Common.EResult.Loose),
                        AvgAggregatePerc =
                            100 * (x.Count(z => z.TeamMatch.Result == (byte)Common.EResult.Win))
                            / (x.Count() == 0 ? 1 : x.Count()),
                        AvgGoalsScored = x.Average(z => z.TeamMatch.GoalsScored),
                        AvgGoalsLost   = x.Average(z => z.TeamMatch.GoalsLost)
                    })
                                     .OrderByDescending(p => p.AvgAggregatePerc);
                    gvRivals.DataSource = rivalGames;
                    gvRivals.DataBind();
                    sb.AppendLine(string.Format("Najczęstrzy rywal: {0} ({1})",
                                                rivalGames.OrderByDescending(p => p.Games).First().Name,
                                                rivalGames.OrderByDescending(p => p.Games).First().Games));
                    sb.AppendLine(string.Format("Najlepszy bilans: {0} ({1})",
                                                rivalGames.OrderByDescending(p => p.Aggregate).First().Name,
                                                rivalGames.OrderByDescending(p => p.Aggregate).First().Aggregate < 0
                        ? (rivalGames.OrderByDescending(p => p.Aggregate).First().Aggregate).ToString()
                        : ("+" + rivalGames.OrderByDescending(p => p.Aggregate).First().Aggregate)));
                    sb.AppendLine(string.Format("Najgorszy bilans: {0} ({1})",
                                                rivalGames.OrderBy(p => p.Aggregate).First().Name,
                                                rivalGames.OrderBy(p => p.Aggregate).First().Aggregate < 0
                        ? (rivalGames.OrderBy(p => p.Aggregate).First().Aggregate).ToString()
                        : ("+" + rivalGames.OrderBy(p => p.Aggregate).First().Aggregate)));
                    sb.AppendLine(string.Format("Największy % wygranych: {0} ({1}%)",
                                                rivalGames.OrderByDescending(p => p.AvgAggregatePerc).First().Name,
                                                rivalGames.OrderByDescending(p => p.AvgAggregatePerc).First().AvgAggregatePerc));
                    sb.AppendLine(string.Format("Najmniejszy % wygranych: {0} ({1}%)",
                                                rivalGames.OrderBy(p => p.AvgAggregatePerc).First().Name,
                                                rivalGames.OrderBy(p => p.AvgAggregatePerc).First().AvgAggregatePerc));
                    sb.AppendLine(string.Format("Najwięcej goli zdobytych/mecz: {0} ({1})",
                                                rivalGames.OrderByDescending(p => p.AvgGoalsScored).First().Name,
                                                rivalGames.OrderByDescending(p => p.AvgGoalsScored).First().AvgGoalsScored.ToString("N2")));
                    sb.AppendLine(string.Format("Najmniej goli zdobytych/mecz: {0} ({1})",
                                                rivalGames.OrderBy(p => p.AvgGoalsScored).First().Name,
                                                rivalGames.OrderBy(p => p.AvgGoalsScored).First().AvgGoalsScored.ToString("N2")));
                    sb.AppendLine(string.Format("Najwięcej goli straconych/mecz: {0} ({1})",
                                                rivalGames.OrderByDescending(p => p.AvgGoalsLost).First().Name,
                                                rivalGames.OrderByDescending(p => p.AvgGoalsLost).First().AvgGoalsLost.ToString("N2")));
                    sb.AppendLine(string.Format("Najmniej goli straconych/mecz: {0} ({1})",
                                                rivalGames.OrderBy(p => p.AvgGoalsLost).First().Name,
                                                rivalGames.OrderBy(p => p.AvgGoalsLost).First().AvgGoalsLost.ToString("N2")));
                }

                sb = sb.Replace(Environment.NewLine, "<br/>");

                pnlRivals.Controls.Add(new LiteralControl(sb.ToString()));
            }
        }