private void AddRow(Match m, Player p, TrambambuleDBContextDataContext context)
 {
     TableRow row = new TableRow();
     row.Cells.Add(new TableCell()
     {
         HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center,
         Text = m.Timestamp.ToString("dd-MM-yyyy HH:mm:ss")
     });
     //row.Cells.Add(new TableCell()
     //{
     //    HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center,
     //    Text = "ATAK<br/>OBRONA"
     //});
     row.Cells.Add(new TableCell()
     {
         HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center,
         Text = GetTeamString(m.TeamMatches[0], p, context)
     });
     row.Cells.Add(new TableCell()
     {
         HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center,
         Text = m.TeamMatches[0].GoalsScored + " : " + m.TeamMatches[0].GoalsLost
     });
     row.Cells.Add(new TableCell()
     {
         HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center,
         Text = GetTeamString(m.TeamMatches[1], p, context)
     });
     if (p != null)
     {
         Common.EResult result = (Common.EResult)Enum.Parse(typeof(Common.EResult),
             m.TeamMatches.First(x => x.TeamMatchPlayers.Any(z => z.PlayerId == p.Id)).Result.ToString());
         switch (result)
         {
             case Common.EResult.Loose:
                 //row.CssClass = "loose";
                 row.Cells[2].ForeColor = Color.Red;
                 break;
             case Common.EResult.Win:
                 //row.CssClass = "win";
                 row.Cells[2].ForeColor = Color.Green;
                 break;
             case Common.EResult.Draw:
                 //row.CssClass = "draw";
                 row.Cells[2].ForeColor = Color.Blue;
                 break;
         }
     }
     tblResults.Rows.Add(row);
 }
        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);
        }
 partial void DeleteMatch(Match instance);
 partial void UpdateMatch(Match instance);
 partial void InsertMatch(Match instance);