[longomatch] Include time played in the player stats



commit 8861f266a17d1a2fae4532cf5c53b1de73a8d72e
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Nov 20 17:46:36 2014 +0100

    Include time played in the player stats

 LongoMatch.Core/Stats/PlayerEventTypeStats.cs |    4 +-
 LongoMatch.Core/Stats/PlayerStats.cs          |   71 +++++++++++++++++++++++++
 LongoMatch.Core/Store/Project.cs              |   21 +++++---
 LongoMatch.Core/Store/TimeNode.cs             |    9 +++
 4 files changed, 95 insertions(+), 10 deletions(-)
---
diff --git a/LongoMatch.Core/Stats/PlayerEventTypeStats.cs b/LongoMatch.Core/Stats/PlayerEventTypeStats.cs
index 4dbedfd..fe8f672 100644
--- a/LongoMatch.Core/Stats/PlayerEventTypeStats.cs
+++ b/LongoMatch.Core/Stats/PlayerEventTypeStats.cs
@@ -53,9 +53,10 @@ namespace LongoMatch.Core.Stats
                {
                        events = filter.VisiblePlays.Where (e => e.Players.Contains (player) && 
e.EventType.Equals (EventType)).ToList ();
                        
+                       SubcategoriesStats = new List<SubCategoryStat> ();
+
                        if (EventType is ScoreEventType) {
                                TotalCount = events.Sum (e => (e as ScoreEvent).Score.Points);
-                               SubcategoriesStats = new List<SubCategoryStat> ();
                                SubCategoryStat substat = new SubCategoryStat (Catalog.GetString ("Score"));
                                foreach (Score score in project.Scores) {
                                        int count;
@@ -71,7 +72,6 @@ namespace LongoMatch.Core.Stats
                                SubcategoriesStats.Add (substat);
                        } else if (EventType is PenaltyCardEventType) {
                                TotalCount = events.Count;
-                               SubcategoriesStats = new List<SubCategoryStat> ();
                                SubCategoryStat substat = new SubCategoryStat (Catalog.GetString 
("Penalties"));
                                foreach (PenaltyCard penalty in project.PenaltyCards) {
                                        var penalties = events.Where (e => (e as 
PenaltyCardEvent).PenaltyCard == penalty);
diff --git a/LongoMatch.Core/Stats/PlayerStats.cs b/LongoMatch.Core/Stats/PlayerStats.cs
index 9e0cdb2..3319402 100644
--- a/LongoMatch.Core/Stats/PlayerStats.cs
+++ b/LongoMatch.Core/Stats/PlayerStats.cs
@@ -45,6 +45,7 @@ namespace LongoMatch.Core.Stats
                                        PlayerEventStats.Add (new PlayerEventTypeStats (project, filter, 
player, evtType));
                                }
                        }
+                       UpdateTimePlayed ();
                }
 
                public Player Player {
@@ -52,6 +53,11 @@ namespace LongoMatch.Core.Stats
                        set;
                }
 
+               public Time TimePlayed {
+                       get;
+                       set;
+               }
+
                public List<PlayerEventTypeStats> PlayerEventStats {
                        get;
                        set;
@@ -63,6 +69,71 @@ namespace LongoMatch.Core.Stats
                                stats.Update ();
                        }
                }
+
+               void UpdateTimePlayed ()
+               {
+                       LineupEvent lineup = project.Lineup;
+                       List<SubstitutionEvent> subs;
+                       Time start;
+                       List<TimeNode> timenodes, playingTimeNodes;
+                       TimeNode last;
+                       
+                       subs = project.EventsByType (project.SubstitutionsEventType).
+                               Where (s => !(s is LineupEvent) && ((s as SubstitutionEvent).In == Player ||
+                                                                   (s as SubstitutionEvent).Out == Player))
+                               .OrderBy (e => e.EventTime).Select (e => e as SubstitutionEvent).ToList ();
+
+                       if (lineup.AwayStartingPlayers.Contains (Player) ||
+                               lineup.HomeStartingPlayers.Contains (Player)) {
+                               start = lineup.EventTime;
+                       } else {
+                               SubstitutionEvent sub = subs.Where (s => s.In == Player).FirstOrDefault ();
+                               if (sub == null) {
+                                       TimePlayed = new Time (0);
+                                       return;
+                               } else {
+                                       start = sub.EventTime;
+                               }
+                       }
+
+                       timenodes = new List<TimeNode> ();
+                       /* Find the sequences of playing time */
+                       last = new TimeNode { Start = start };
+                       timenodes.Add (last);
+                       if (subs.Count == 0) {
+                               last.Stop = project.Description.FileSet.Duration;
+                       } else {
+                               foreach (SubstitutionEvent sub in subs) {
+                                       if (last.Stop == null) {
+                                               if (sub.Out == Player) {
+                                                       last.Stop = sub.EventTime;
+                                               }
+                                       } else {
+                                               if (sub.In == Player) {
+                                                       last = new TimeNode { Start = sub.EventTime };
+                                                       timenodes.Add (last);
+                                               }
+                                       }
+                               }
+                       }
+
+                       playingTimeNodes = new List<TimeNode> ();
+                       /* Get the real playing time intersecting with the periods */
+                       foreach (TimeNode timenode in timenodes) {
+                               foreach (Period p in project.Periods) {
+                                       if (p.PeriodNode.Intersect (timenode) != null) {
+                                               foreach (TimeNode ptn in p.Nodes) {
+                                                       TimeNode res = ptn.Intersect (timenode);
+                                                       if (res != null) {
+                                                               playingTimeNodes.Add (res);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       
+                       TimePlayed = new Time (playingTimeNodes.Sum (t => t.Duration.MSeconds));
+               }
        }
 }
 
diff --git a/LongoMatch.Core/Store/Project.cs b/LongoMatch.Core/Store/Project.cs
index b93bc00..ccca01c 100644
--- a/LongoMatch.Core/Store/Project.cs
+++ b/LongoMatch.Core/Store/Project.cs
@@ -191,6 +191,17 @@ namespace LongoMatch.Core.Store
                        }
                }
 
+               [JsonIgnore]
+               public LineupEvent Lineup {
+                       get {
+                               LineupEvent lineup = Timeline.OfType <LineupEvent> ().FirstOrDefault ();
+                               if (lineup == null) {
+                                       lineup = CreateLineupEvent ();
+                               }
+                               return lineup;
+                       }
+               }
+
                #endregion
 
                #region Public Methods
@@ -339,16 +350,10 @@ namespace LongoMatch.Core.Store
                                         out List<Player> awayBenchPlayers)
                {
                        TeamTemplate homeTeam, awayTeam;
-                       LineupEvent lineup;
                        List<Player> homeTeamPlayers, awayTeamPlayers;
 
-                       lineup = Timeline.OfType <LineupEvent> ().FirstOrDefault ();
-                       if (lineup == null) {
-                               lineup = CreateLineupEvent ();
-                       }
-
-                       homeTeamPlayers = lineup.HomeStartingPlayers.Concat (lineup.HomeBenchPlayers).ToList 
();
-                       awayTeamPlayers = lineup.AwayStartingPlayers.Concat (lineup.AwayBenchPlayers).ToList 
();
+                       homeTeamPlayers = Lineup.HomeStartingPlayers.Concat (Lineup.HomeBenchPlayers).ToList 
();
+                       awayTeamPlayers = Lineup.AwayStartingPlayers.Concat (Lineup.AwayBenchPlayers).ToList 
();
 
                        foreach (SubstitutionEvent ev in Timeline.OfType<SubstitutionEvent> ().
                                 Where (e => e.EventTime <= currentTime)) {
diff --git a/LongoMatch.Core/Store/TimeNode.cs b/LongoMatch.Core/Store/TimeNode.cs
index 345c453..b6ab13b 100644
--- a/LongoMatch.Core/Store/TimeNode.cs
+++ b/LongoMatch.Core/Store/TimeNode.cs
@@ -131,6 +131,15 @@ namespace LongoMatch.Core.Store
                                Start = new Time (Math.Min (Start.MSeconds, tn.Start.MSeconds)),
                                Stop = new Time (Math.Max (Stop.MSeconds, tn.Stop.MSeconds))};
                }
+               
+               public TimeNode Intersect (TimeNode tn) {
+                       if (tn.Stop < Start || tn.Start > Stop)
+                               return null;
+                       else
+                               return new TimeNode {
+                               Start = new Time (Math.Max (Start.MSeconds, tn.Start.MSeconds)),
+                               Stop = new Time (Math.Min (Stop.MSeconds, tn.Stop.MSeconds))};
+               }
 
                #endregion
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]