public static List <PlayersAchievements> GetPlayersWithAchievements() { List <PlayersAchievements> players = new List <PlayersAchievements>(); if (HttpContext.Current.Cache[DataAccess.PlayersAchievementsCacheName] == null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { foreach (Player p in DataAccess.GetPlayers()) { var achievements = (from ac in context.AchievementPlayer join a in context.Achievements on ac.AchievementId equals a.Id where ac.PlayerId == p.Id && ac.LevelOfAchievement >= 1 select new AchievementWithCurrentLevel(a, ac.LevelOfAchievement)).ToList(); PlayersAchievements pa = new PlayersAchievements(p, achievements); players.Add(pa); } } HttpContext.Current.Cache.Insert(DataAccess.PlayersAchievementsCacheName, players, null, DateTime.Now.AddSeconds(DataAccess.CacheTimeInSeconds), TimeSpan.Zero); } else { players = (List <PlayersAchievements>)HttpContext.Current.Cache[DataAccess.PlayersAchievementsCacheName]; } return(players); }
public static List<PlayersAchievements> GetPlayersWithAchievements() { List<PlayersAchievements> players = new List<PlayersAchievements>(); if (HttpContext.Current.Cache[DataAccess.PlayersAchievementsCacheName] == null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { foreach (Player p in DataAccess.GetPlayers()) { var achievements = (from ac in context.AchievementPlayer join a in context.Achievements on ac.AchievementId equals a.Id where ac.PlayerId == p.Id && ac.LevelOfAchievement >= 1 select new AchievementWithCurrentLevel(a, ac.LevelOfAchievement)).ToList(); PlayersAchievements pa = new PlayersAchievements(p, achievements); players.Add(pa); } } HttpContext.Current.Cache.Insert(DataAccess.PlayersAchievementsCacheName, players, null, DateTime.Now.AddSeconds(DataAccess.CacheTimeInSeconds), TimeSpan.Zero); } else { players = (List<PlayersAchievements>)HttpContext.Current.Cache[DataAccess.PlayersAchievementsCacheName]; } return players; }
private string GetTeamString(TeamMatch tm, Player p, TrambambuleDBContextDataContext context) { var offence = tm.TeamMatchPlayers.First(x => x.Position == (byte)Common.EPosition.Offence); var defence = tm.TeamMatchPlayers.First(x => x.Position == (byte)Common.EPosition.Defence); int offenceRatingChange = 0; int defenceRatingChange = 0; if (offence.RatingChange.HasValue) { offenceRatingChange = (int)offence.RatingChange.Value; } else { offenceRatingChange = (int)Math.Round((double)(offence.Rating - PlayerHelper.INITIAL_RATING), 0); } if (defence.RatingChange.HasValue) { defenceRatingChange = (int)defence.RatingChange.Value; } else { defenceRatingChange = (int)Math.Round((double)(defence.Rating - PlayerHelper.INITIAL_RATING), 0); } return(string.Format("{0} ({2})<br/>{1} ({3})", GetPlayerNameString(offence.Player, p), GetPlayerNameString(defence.Player, p), GetRatingChangeString(offenceRatingChange), GetRatingChangeString(defenceRatingChange))); }
private void BindLastGames() { tblResults.Rows.Clear(); Player player = (Player)Session["UserBasicStatsPlayer"]; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List<Match> playerMatches = null; if (player != null) { playerMatches = context.Matches.Where(p => p.TeamMatches.Any(x => x.TeamMatchPlayers.Any(z => z.PlayerId == player.Id))) .OrderByDescending(p => p.Timestamp).Take(50).ToList(); } else { playerMatches = context.Matches .OrderByDescending(p => p.Timestamp).Take(50).ToList(); } if (!playerMatches.Any()) return; foreach (Match m in playerMatches) AddRow(m, player, context); } }
private void BindLastGames() { tblResults.Rows.Clear(); Player player = (Player)Session["UserBasicStatsPlayer"]; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List <Match> playerMatches = null; if (player != null) { playerMatches = context.Matches.Where(p => p.TeamMatches.Any(x => x.TeamMatchPlayers.Any(z => z.PlayerId == player.Id))) .OrderByDescending(p => p.Timestamp).Take(50).ToList(); } else { playerMatches = context.Matches .OrderByDescending(p => p.Timestamp).Take(50).ToList(); } if (!playerMatches.Any()) { return; } foreach (Match m in playerMatches) { AddRow(m, player, context); } } }
private void BindPlayerRatingStats() { pnlRanking.Controls.Clear(); Player player = ((Player)Session["UserBasicStatsPlayer"]); if (player == null) { return; } using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List <TeamMatchPlayer> playerMatches = context.TeamMatchPlayers.Where(p => p.PlayerId == player.Id) .OrderByDescending(p => p.Timestamp).ToList(); if (playerMatches.Any()) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Obecna pozycja w rankingu: " + playerMatches.First().RankPosition); sb.AppendLine("Najwyższa pozycja w rankingu: " + playerMatches.Min(p => p.RankPosition)); sb.AppendLine("Najniższa pozycja w rankingu: " + playerMatches.Max(p => p.RankPosition)); sb.AppendLine("Najwięcej punktów rankingowych: " + playerMatches.Max(p => (int)p.Rating.Value)); sb.AppendLine("Najmniej punktów rankingowych: " + playerMatches.Min(p => (int)p.Rating.Value)); sb.AppendLine("Notowany w rankingu od: " + playerMatches.Min(p => p.Timestamp)); sb = sb.Replace(Environment.NewLine, "<br/>"); pnlRanking.Controls.Add(new LiteralControl(sb.ToString())); } } }
private void BindPlayerRankChart() { lcRanking.Series.Clear(); lcRanking.Visible = false; Player player = ((Player)Session["UserBasicStatsPlayer"]); if (player == null) { return; } using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List <TeamMatchPlayer> hist = context.TeamMatchPlayers.Where(p => p.PlayerId == player.Id) .OrderByDescending(p => p.Timestamp).Take(20).OrderBy(p => p.Timestamp).ToList(); if (!hist.Any() || hist.Count < 2) { return; } lcRanking.CategoriesAxis = string.Join(",", hist.Select(p => p.Timestamp.ToString("dd-MM-yyyy HH:mm:ss")).ToArray()); AjaxControlToolkit.LineChartSeries lcs = new AjaxControlToolkit.LineChartSeries(); lcs.Data = hist.Where(p => p.RankPosition.HasValue).Select(p => (decimal)p.RankPosition.Value).ToArray(); lcs.Name = "Historia pozycji rankingowej"; lcRanking.Series.Add(lcs); lcRanking.Visible = true; } }
private void BindGoalsStats() { pnlGoals.Controls.Clear(); Player player = ((Player)Session["UserBasicStatsPlayer"]); if (player == null) { return; } using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List <TeamMatch> playerMatches = context.TeamMatches.Where(p => p.TeamMatchPlayers.Any(x => x.PlayerId == player.Id)).ToList(); StringBuilder sb = new StringBuilder(); //sb.AppendLine("Rozegranych: " + playerMatches.Select(p => new { Mid = p.MatchId }).Distinct().Count()); sb.AppendLine("Bilans bramek: " + playerMatches.Select(p => new { Mid = p.MatchId, GoalsScored = p.GoalsScored }).Distinct().Sum(p => p.GoalsScored) + " / " + playerMatches.Select(p => new { Mid = p.MatchId, GoalsLost = p.GoalsLost }).Distinct().Sum(p => p.GoalsLost)); sb.Append("<hr/>"); sb.AppendLine("Bilans bramek [atak]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Sum(p => p.GoalsScored) + " / " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Sum(p => p.GoalsLost)); try { sb.AppendLine("Średnia goli strzelonych [atak]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Average(p => p.GoalsScored).ToString("n2")); sb.AppendLine("Średnia goli straconych [atak]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Average(p => p.GoalsLost).ToString("n2")); } catch { } sb.AppendLine("Bilans bramek [obrona]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Sum(p => p.GoalsScored) + " / " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Sum(p => p.GoalsLost)); try { sb.AppendLine("Średnia goli strzelonych [obrona]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Average(p => p.GoalsScored).ToString("n2")); sb.AppendLine("Średnia goli straconych [obrona]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Average(p => p.GoalsLost).ToString("n2")); } catch { } sb = sb.Replace(Environment.NewLine, "<br/>"); pnlGoals.Controls.Add(new LiteralControl(sb.ToString())); } }
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 Page_LoadComplete(object sender, EventArgs e) { if (Session["UserBasicStatsPlayer"] == null) { if (Request.QueryString["userId"] != null && Request.QueryString["userId"] != "") { int.TryParse(Request.QueryString["userId"], out selectedUserId); } } else { selectedUserId = ((Player)Session["UserBasicStatsPlayer"]).Id; } List <AchievementsWithPlayers> awpList = new List <AchievementsWithPlayers>(); if (Cache[DataAccess.AllAchievementsCacheName] == null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { var achievements = context.Achievements.ToList(); foreach (Achievement achievement in achievements) { var players = (from ap in context.AchievementPlayer join p in context.Players on ap.PlayerId equals p.Id where ap.AchievementId == achievement.Id && ap.LevelOfAchievement > 0 select new PlayerWithLevelOfAchievement(p, ap.LevelOfAchievement, ap.ToNextLevelLabel)).ToList(); AchievementsWithPlayers awp = new AchievementsWithPlayers(achievement, players); awpList.Add(awp); } } Cache.Insert(DataAccess.AllAchievementsCacheName, awpList, null, DateTime.Now.AddSeconds(DataAccess.CacheTimeInSeconds), TimeSpan.Zero); } else { awpList = (List <AchievementsWithPlayers>)HttpContext.Current.Cache[DataAccess.AllAchievementsCacheName]; } if (awpList.Count > 0) { repAchievements.DataSource = awpList; repAchievements.DataBind(); } }
public static List<Player> GetPlayers() { List<Player> playersList = new List<Player>(); if (HttpContext.Current.Cache[PlayersListCacheName] == null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { playersList = context.Players.ToList(); } HttpContext.Current.Cache.Insert(DataAccess.PlayersListCacheName, playersList, null, DateTime.Now.AddSeconds(CacheTimeInSeconds), TimeSpan.Zero); } else { playersList = (List<Player>)HttpContext.Current.Cache[PlayersListCacheName]; } return playersList; }
protected void btnAddNewPlayer_Click(object sender, EventArgs e) { if (Page.IsValid) { string firstName = txtNewPlayerFirstName.Text.Trim(); string lastName = txtNewPlayerLastName.Text.Trim(); if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && firstName.Length > 0 && firstName.Length <= 100 && lastName.Length > 0 && lastName.Length <= 100) { int location = int.Parse(ddlLocation.SelectedValue); string nick = (firstName.Length < 2 ? "Jakiś" : firstName.Substring(0, 2)) + (lastName.Length < 2 ? "Pseudonim" : lastName.Substring(0, 2)); using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { try { Player p = new Player(); p.FirstName = firstName; p.LastName = lastName; p.Location = location; p.Nickname = nick; p.Timestamp = DateTime.Now; context.Players.InsertOnSubmit(p); context.SubmitChanges(); Cache.Remove(DataAccess.PlayersListCacheName); Cache.Remove(DataAccess.PlayersNamesAndSurnamesWithoutPolishCharsCacheName); Cache.Remove(DataAccess.PlayersAchievementsCacheName); Cache.Remove(DataAccess.AllAchievementsCacheName); Cache.Remove(DataAccess.OverallStatsCacheName); new AchievementsService(new List <Player>() { p }).RecalculateAchievements(true); litStatus.Text = "<div class=\"validator\">Użytkownik został dodany</div>"; txtNewPlayerFirstName.Text = ""; txtNewPlayerLastName.Text = ""; ddlLocation.SelectedIndex = 0; } catch { litStatus.Text = "<div class=\"validator\">Wystąpił błąd podczas dodawania nowego użytkownika</div>"; } } } } }
public static List <Player> GetPlayers() { List <Player> playersList = new List <Player>(); if (HttpContext.Current.Cache[PlayersListCacheName] == null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { playersList = context.Players.ToList(); } HttpContext.Current.Cache.Insert(DataAccess.PlayersListCacheName, playersList, null, DateTime.Now.AddSeconds(CacheTimeInSeconds), TimeSpan.Zero); } else { playersList = (List <Player>)HttpContext.Current.Cache[PlayersListCacheName]; } return(playersList); }
protected void Page_Load(object sender, EventArgs e) { try { bool achievementsExists = true; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { achievementsExists = context.AchievementPlayer.Any(); } if (!achievementsExists) { List<AchievementTextWithIds> achievementsData = new AchievementsService(DataAccess.GetPlayers().ToList()).RecalculateAchievements(true); litStatus.Text = "Osiągnięcia zostały przyznane"; //List<AchievementsWithPlayers> awpList = new List<AchievementsWithPlayers>(); //using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) //{ // var achievements = context.Achievements.ToList(); // foreach (Achievement achievement in achievements) // { // var players = (from ap in context.AchievementPlayer // join p in context.Players // on ap.PlayerId equals p.Id // where ap.AchievementId == achievement.Id // select new PlayerWithLevelOfAchievement(p, ap.LevelOfAchievement, ap.ToNextLevelLabel)).ToList(); // AchievementsWithPlayers awp = new AchievementsWithPlayers(achievement, players); // awpList.Add(awp); // } //} Cache.Remove(DataAccess.PlayersListCacheName); Cache.Remove(DataAccess.PlayersNamesAndSurnamesWithoutPolishCharsCacheName); Cache.Remove(DataAccess.PlayersAchievementsCacheName); Cache.Remove(DataAccess.AllAchievementsCacheName); Cache.Remove(DataAccess.OverallStatsCacheName); } } catch (Exception ex) { litStatus.Text = ex.Message + " " + ex.StackTrace; } }
protected void Page_Load(object sender, EventArgs e) { try { bool achievementsExists = true; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { achievementsExists = context.AchievementPlayer.Any(); } if (!achievementsExists) { List <AchievementTextWithIds> achievementsData = new AchievementsService(DataAccess.GetPlayers().ToList()).RecalculateAchievements(true); litStatus.Text = "Osiągnięcia zostały przyznane"; //List<AchievementsWithPlayers> awpList = new List<AchievementsWithPlayers>(); //using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) //{ // var achievements = context.Achievements.ToList(); // foreach (Achievement achievement in achievements) // { // var players = (from ap in context.AchievementPlayer // join p in context.Players // on ap.PlayerId equals p.Id // where ap.AchievementId == achievement.Id // select new PlayerWithLevelOfAchievement(p, ap.LevelOfAchievement, ap.ToNextLevelLabel)).ToList(); // AchievementsWithPlayers awp = new AchievementsWithPlayers(achievement, players); // awpList.Add(awp); // } //} Cache.Remove(DataAccess.PlayersListCacheName); Cache.Remove(DataAccess.PlayersNamesAndSurnamesWithoutPolishCharsCacheName); Cache.Remove(DataAccess.PlayersAchievementsCacheName); Cache.Remove(DataAccess.AllAchievementsCacheName); Cache.Remove(DataAccess.OverallStatsCacheName); } } catch (Exception ex) { litStatus.Text = ex.Message + " " + ex.StackTrace; } }
protected void btnAddNewPlayer_Click(object sender, EventArgs e) { if (Page.IsValid) { string firstName = txtNewPlayerFirstName.Text.Trim(); string lastName = txtNewPlayerLastName.Text.Trim(); if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && firstName.Length > 0 && firstName.Length <= 100 && lastName.Length > 0 && lastName.Length <= 100) { int location = int.Parse(ddlLocation.SelectedValue); string nick = (firstName.Length < 2 ? "Jakiś" : firstName.Substring(0, 2)) + (lastName.Length < 2 ? "Pseudonim" : lastName.Substring(0, 2)); using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { try { Player p = new Player(); p.FirstName = firstName; p.LastName = lastName; p.Location = location; p.Nickname = nick; p.Timestamp = DateTime.Now; context.Players.InsertOnSubmit(p); context.SubmitChanges(); Cache.Remove(DataAccess.PlayersListCacheName); Cache.Remove(DataAccess.PlayersNamesAndSurnamesWithoutPolishCharsCacheName); Cache.Remove(DataAccess.PlayersAchievementsCacheName); Cache.Remove(DataAccess.AllAchievementsCacheName); Cache.Remove(DataAccess.OverallStatsCacheName); new AchievementsService(new List<Player>() { p }).RecalculateAchievements(true); litStatus.Text = "<div class=\"validator\">Użytkownik został dodany</div>"; txtNewPlayerFirstName.Text = ""; txtNewPlayerLastName.Text = ""; ddlLocation.SelectedIndex = 0; } catch { litStatus.Text = "<div class=\"validator\">Wystąpił błąd podczas dodawania nowego użytkownika</div>"; } } } } }
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())); } }
private void GetStatsBeforeSubmitting() { Player p1Off = DataAccess.GetPlayer(tbxPlayer1Off.Text); Player p1Def = DataAccess.GetPlayer(tbxPlayer1Deff.Text); Player p2Off = DataAccess.GetPlayer(tbxPlayer2Off.Text); Player p2Def = DataAccess.GetPlayer(tbxPlayer2Deff.Text); if (p1Off != null || p1Def != null || p2Off != null || p2Def != null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { if (p1Off != null) { var player1Off = context.TeamMatchPlayers.Where(p => p.PlayerId == p1Off.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player1Off == null) litTbxPlayer1OffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); else litTbxPlayer1OffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player1Off.PlayerId), Math.Round((double)player1Off.Rating, 0)); } if (p1Def != null) { var player1Def = context.TeamMatchPlayers.Where(p => p.PlayerId == p1Def.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player1Def == null) litTbxPlayer1DeffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); else litTbxPlayer1DeffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player1Def.PlayerId), Math.Round((double)player1Def.Rating, 0)); } if (p2Off != null) { var player2Off = context.TeamMatchPlayers.Where(p => p.PlayerId == p2Off.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player2Off == null) litTbxPlayer2OffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); else litTbxPlayer2OffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player2Off.PlayerId), Math.Round((double)player2Off.Rating, 0)); } if (p2Def != null) { var player2Def = context.TeamMatchPlayers.Where(p => p.PlayerId == p2Def.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player2Def == null) litTbxPlayer2DeffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); else litTbxPlayer2DeffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player2Def.PlayerId), Math.Round((double)player2Def.Rating, 0)); } } } }
private string GetTeamString(TeamMatch tm, Player p, TrambambuleDBContextDataContext context) { var offence = tm.TeamMatchPlayers.First(x => x.Position == (byte)Common.EPosition.Offence); var defence = tm.TeamMatchPlayers.First(x => x.Position == (byte)Common.EPosition.Defence); int offenceRatingChange = 0; int defenceRatingChange = 0; if (offence.RatingChange.HasValue) offenceRatingChange = (int)offence.RatingChange.Value; else offenceRatingChange = (int)Math.Round((double)(offence.Rating - PlayerHelper.INITIAL_RATING), 0); if (defence.RatingChange.HasValue) defenceRatingChange = (int)defence.RatingChange.Value; else defenceRatingChange = (int)Math.Round((double)(defence.Rating - PlayerHelper.INITIAL_RATING), 0); return string.Format("{0} ({2})<br/>{1} ({3})", GetPlayerNameString(offence.Player, p), GetPlayerNameString(defence.Player, p), GetRatingChangeString(offenceRatingChange), GetRatingChangeString(defenceRatingChange)); }
protected void Page_LoadComplete(object sender, EventArgs e) { if (Session["UserBasicStatsPlayer"] == null) { if (Request.QueryString["userId"] != null && Request.QueryString["userId"] != "") { int.TryParse(Request.QueryString["userId"], out selectedUserId); } } else { selectedUserId = ((Player)Session["UserBasicStatsPlayer"]).Id; } List<AchievementsWithPlayers> awpList = new List<AchievementsWithPlayers>(); if (Cache[DataAccess.AllAchievementsCacheName] == null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { var achievements = context.Achievements.ToList(); foreach (Achievement achievement in achievements) { var players = (from ap in context.AchievementPlayer join p in context.Players on ap.PlayerId equals p.Id where ap.AchievementId == achievement.Id && ap.LevelOfAchievement > 0 select new PlayerWithLevelOfAchievement(p, ap.LevelOfAchievement, ap.ToNextLevelLabel)).ToList(); AchievementsWithPlayers awp = new AchievementsWithPlayers(achievement, players); awpList.Add(awp); } } Cache.Insert(DataAccess.AllAchievementsCacheName, awpList, null, DateTime.Now.AddSeconds(DataAccess.CacheTimeInSeconds), TimeSpan.Zero); } else { awpList = (List<AchievementsWithPlayers>)HttpContext.Current.Cache[DataAccess.AllAchievementsCacheName]; } if (awpList.Count > 0) { repAchievements.DataSource = awpList; repAchievements.DataBind(); } }
private void BindGoalsStats() { pnlGoals.Controls.Clear(); Player player = ((Player)Session["UserBasicStatsPlayer"]); if (player == null) return; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List<TeamMatch> playerMatches = context.TeamMatches.Where(p => p.TeamMatchPlayers.Any(x => x.PlayerId == player.Id)).ToList(); StringBuilder sb = new StringBuilder(); //sb.AppendLine("Rozegranych: " + playerMatches.Select(p => new { Mid = p.MatchId }).Distinct().Count()); sb.AppendLine("Bilans bramek: " + playerMatches.Select(p => new { Mid = p.MatchId, GoalsScored = p.GoalsScored }).Distinct().Sum(p => p.GoalsScored) + " / " + playerMatches.Select(p => new { Mid = p.MatchId, GoalsLost = p.GoalsLost }).Distinct().Sum(p => p.GoalsLost)); sb.Append("<hr/>"); sb.AppendLine("Bilans bramek [atak]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Sum(p => p.GoalsScored) + " / " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Sum(p => p.GoalsLost)); try { sb.AppendLine("Średnia goli strzelonych [atak]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Average(p => p.GoalsScored).ToString("n2")); sb.AppendLine("Średnia goli straconych [atak]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Offence).Average(p => p.GoalsLost).ToString("n2")); } catch { } sb.AppendLine("Bilans bramek [obrona]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Sum(p => p.GoalsScored) + " / " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Sum(p => p.GoalsLost)); try { sb.AppendLine("Średnia goli strzelonych [obrona]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Average(p => p.GoalsScored).ToString("n2")); sb.AppendLine("Średnia goli straconych [obrona]: " + playerMatches.Where(p => p.TeamMatchPlayers.First(x => x.PlayerId == player.Id).Position == (byte)Common.EPosition.Defence).Average(p => p.GoalsLost).ToString("n2")); } catch { } sb = sb.Replace(Environment.NewLine, "<br/>"); pnlGoals.Controls.Add(new LiteralControl(sb.ToString())); } }
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); }
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())); } }
private void BindPlayerRatingStats() { pnlRanking.Controls.Clear(); Player player = ((Player)Session["UserBasicStatsPlayer"]); if (player == null) return; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List<TeamMatchPlayer> playerMatches = context.TeamMatchPlayers.Where(p => p.PlayerId == player.Id) .OrderByDescending(p => p.Timestamp).ToList(); if (playerMatches.Any()) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Obecna pozycja w rankingu: " + playerMatches.First().RankPosition); sb.AppendLine("Najwyższa pozycja w rankingu: " + playerMatches.Min(p => p.RankPosition)); sb.AppendLine("Najniższa pozycja w rankingu: " + playerMatches.Max(p => p.RankPosition)); sb.AppendLine("Najwięcej punktów rankingowych: " + playerMatches.Max(p => (int)p.Rating.Value)); sb.AppendLine("Najmniej punktów rankingowych: " + playerMatches.Min(p => (int)p.Rating.Value)); sb.AppendLine("Notowany w rankingu od: " + playerMatches.Min(p => p.Timestamp)); sb = sb.Replace(Environment.NewLine, "<br/>"); pnlRanking.Controls.Add(new LiteralControl(sb.ToString())); } } }
private void BindPlayerRatingChart() { lcRating.Series.Clear(); lcRating.Visible = false; Player player = ((Player)Session["UserBasicStatsPlayer"]); if (player == null) return; using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { List<TeamMatchPlayer> hist = context.TeamMatchPlayers.Where(p => p.PlayerId == player.Id) .OrderByDescending(p => p.Timestamp).Take(20).OrderBy(p => p.Timestamp).ToList(); if (!hist.Any() || hist.Count < 2) return; lcRating.CategoriesAxis = string.Join(",", hist.Select(p => p.Timestamp.ToString("dd-MM-yyyy HH:mm:ss")).ToArray()); AjaxControlToolkit.LineChartSeries lcs = new AjaxControlToolkit.LineChartSeries(); lcs.Data = hist.Select(p => (decimal)((int) (p.Rating.Value - hist.Last().Rating.Value) )).ToArray(); lcs.Name = "Rożn. pkt. rank. z chwilą obecną"; lcRating.Series.Add(lcs); lcRating.Visible = true; } }
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); }
private void GetStatsBeforeSubmitting() { Player p1Off = DataAccess.GetPlayer(tbxPlayer1Off.Text); Player p1Def = DataAccess.GetPlayer(tbxPlayer1Deff.Text); Player p2Off = DataAccess.GetPlayer(tbxPlayer2Off.Text); Player p2Def = DataAccess.GetPlayer(tbxPlayer2Deff.Text); if (p1Off != null || p1Def != null || p2Off != null || p2Def != null) { using (TrambambuleDBContextDataContext context = new TrambambuleDBContextDataContext()) { if (p1Off != null) { var player1Off = context.TeamMatchPlayers.Where(p => p.PlayerId == p1Off.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player1Off == null) { litTbxPlayer1OffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); } else { litTbxPlayer1OffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player1Off.PlayerId), Math.Round((double)player1Off.Rating, 0)); } } if (p1Def != null) { var player1Def = context.TeamMatchPlayers.Where(p => p.PlayerId == p1Def.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player1Def == null) { litTbxPlayer1DeffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); } else { litTbxPlayer1DeffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player1Def.PlayerId), Math.Round((double)player1Def.Rating, 0)); } } if (p2Off != null) { var player2Off = context.TeamMatchPlayers.Where(p => p.PlayerId == p2Off.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player2Off == null) { litTbxPlayer2OffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); } else { litTbxPlayer2OffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player2Off.PlayerId), Math.Round((double)player2Off.Rating, 0)); } } if (p2Def != null) { var player2Def = context.TeamMatchPlayers.Where(p => p.PlayerId == p2Def.Id) .OrderByDescending(p => p.Timestamp).FirstOrDefault(); if (player2Def == null) { litTbxPlayer2DeffStats.Text = string.Format(PreSubmittedStats, "-", PlayerHelper.INITIAL_RATING); } else { litTbxPlayer2DeffStats.Text = string.Format(PreSubmittedStats, context.GetPlayerRankPosition(player2Def.PlayerId), Math.Round((double)player2Def.Rating, 0)); } } } } }