[longomatch] Add support for statistics



commit a207e3520c457c0f442a0cb820ddc90cf6e7739b
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sun Jan 15 23:50:47 2012 +0100

    Add support for statistics

 LongoMatch.Core/LongoMatch.Core.mdp      |    6 +
 LongoMatch.Core/Stats/CategoryStats.cs   |   46 +++++++++
 LongoMatch.Core/Stats/PercentualStat.cs  |   41 ++++++++
 LongoMatch.Core/Stats/ProjectStats.cs    |  156 ++++++++++++++++++++++++++++++
 LongoMatch.Core/Stats/Stat.cs            |   53 ++++++++++
 LongoMatch.Core/Stats/SubCategoryStat.cs |   53 ++++++++++
 6 files changed, 355 insertions(+), 0 deletions(-)
---
diff --git a/LongoMatch.Core/LongoMatch.Core.mdp b/LongoMatch.Core/LongoMatch.Core.mdp
index f72e990..f2a9ce3 100644
--- a/LongoMatch.Core/LongoMatch.Core.mdp
+++ b/LongoMatch.Core/LongoMatch.Core.mdp
@@ -89,6 +89,12 @@
     <File subtype="Code" buildaction="Compile" name="Common/Image.cs" />
     <File subtype="Directory" buildaction="Compile" name="Common" />
     <File subtype="Code" buildaction="Compile" name="Common/Color.cs" />
+    <File subtype="Directory" buildaction="Compile" name="Stats" />
+    <File subtype="Code" buildaction="Compile" name="Stats/ProjectStats.cs" />
+    <File subtype="Code" buildaction="Compile" name="Stats/CategoryStats.cs" />
+    <File subtype="Code" buildaction="Compile" name="Stats/PercentualStat.cs" />
+    <File subtype="Code" buildaction="Compile" name="Stats/Stat.cs" />
+    <File subtype="Code" buildaction="Compile" name="Stats/SubCategoryStat.cs" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
diff --git a/LongoMatch.Core/Stats/CategoryStats.cs b/LongoMatch.Core/Stats/CategoryStats.cs
new file mode 100644
index 0000000..89d9e6b
--- /dev/null
+++ b/LongoMatch.Core/Stats/CategoryStats.cs
@@ -0,0 +1,46 @@
+// 
+//  Copyright (C) 2012 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using System.Collections.Generic;
+
+using LongoMatch.Interfaces;
+
+namespace LongoMatch.Stats
+{
+	public class CategoryStats: Stat
+	{
+		List <SubCategoryStat> subcatStats;
+		
+		public CategoryStats (string name, int totalCount, int localTeamCount, int visitorTeamCount):
+			base (name, totalCount, localTeamCount, visitorTeamCount)
+		{
+			subcatStats = new List<SubCategoryStat>();
+		}
+		
+		public List<SubCategoryStat> SubcategoriesStats {
+			get {
+				return subcatStats;
+			}
+		}
+		
+		public void AddSubcatStat (SubCategoryStat subcatStat) {
+			subcatStats.Add(subcatStat);
+		}
+	}
+}
+
diff --git a/LongoMatch.Core/Stats/PercentualStat.cs b/LongoMatch.Core/Stats/PercentualStat.cs
new file mode 100644
index 0000000..0a0f44e
--- /dev/null
+++ b/LongoMatch.Core/Stats/PercentualStat.cs
@@ -0,0 +1,41 @@
+// 
+//  Copyright (C) 2012 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using System.Collections.Generic;
+
+
+namespace LongoMatch.Stats
+{
+	public class PercentualStat: Stat
+	{
+		int parentTotal;
+		
+		public PercentualStat (string name, int totalCount, int localTeamCount,
+			int visitorTeamCount, int parentTotal): base (name, totalCount, localTeamCount, visitorTeamCount)
+		{
+			this.parentTotal = parentTotal;
+		}
+		
+		public int TotalPercent {
+			get {
+				return (int) (((float)TotalCount) / parentTotal * 100);
+			}
+		}
+	}
+}
+
diff --git a/LongoMatch.Core/Stats/ProjectStats.cs b/LongoMatch.Core/Stats/ProjectStats.cs
new file mode 100644
index 0000000..d26503c
--- /dev/null
+++ b/LongoMatch.Core/Stats/ProjectStats.cs
@@ -0,0 +1,156 @@
+// 
+//  Copyright (C) 2012 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+using LongoMatch.Common;
+using LongoMatch.Interfaces;
+using LongoMatch.Store;
+using LongoMatch.Store.Templates;
+
+namespace LongoMatch.Stats
+{
+	public class ProjectStats
+	{
+		List<CategoryStats> catStats;
+		
+		public ProjectStats (Project project)
+		{
+			catStats = new List<CategoryStats>(); 
+			
+			UpdateStats (project);
+			
+			ProjectName = project.Description.Title;
+			Date = project.Description.MatchDate;
+			LocalTeam = project.LocalTeamTemplate.TeamName;
+			VisitorTeam = project.VisitorTeamTemplate.TeamName;
+			Competition = project.Description.Competition;
+			Season = project.Description.Season;
+		}
+		
+		public string ProjectName {
+			set;
+			get;
+		}
+		
+		public string Competition {
+			get;
+			set;
+		}
+		
+		public string Season {
+			get;
+			set;
+		}
+		
+		public string LocalTeam {
+			get;
+			set;
+		}
+		
+		public string VisitorTeam {
+			get;
+			set;
+		}
+		
+		public DateTime Date {
+			get;
+			set;
+		}
+		
+		public List<CategoryStats> CategoriesStats {
+			get {
+				return catStats;
+			}
+		}
+		
+		void CountPlaysInTeam (List<Play> plays, out int localTeamCount, out int visitorTeamCount) {
+			localTeamCount = plays.Where(p => p.Team == Team.LOCAL || p.Team == Team.BOTH).Count();
+			visitorTeamCount = plays.Where(p => p.Team == Team.VISITOR || p.Team == Team.BOTH).Count();
+		}
+		
+		void UpdateStats (Project project) {
+			catStats.Clear();
+			
+			foreach (Category cat in project.Categories) {
+				CategoryStats stats;
+				List<Play> plays;
+				int localTeamCount, visitorTeamCount;
+				
+				plays = project.PlaysInCategory (cat);
+				CountPlaysInTeam(plays, out localTeamCount, out visitorTeamCount);
+				stats = new CategoryStats(cat.Name, plays.Count, localTeamCount, visitorTeamCount);
+				catStats.Add (stats);
+				
+				foreach (ISubCategory subcat in cat.SubCategories) {
+					SubCategoryStat subcatStat;
+					
+					if (subcat is PlayerSubCategory)
+						continue;
+						
+					subcatStat = new SubCategoryStat(subcat.Name);
+					stats.AddSubcatStat(subcatStat);
+					
+					 if (subcat is TagSubCategory) {
+						foreach (string option in subcat.ElementsDesc()) {
+							List<Play> subcatPlays;
+							int count;
+							StringTag tag;
+							
+							tag = new StringTag();
+							tag.SubCategory = subcat;
+							tag.Value = option;
+							
+							subcatPlays = plays.Where(p => p.Tags.Tags.Contains(tag)).ToList();
+							count = subcatPlays.Count(); 
+							CountPlaysInTeam(subcatPlays, out localTeamCount, out visitorTeamCount);
+							PercentualStat pStat = new PercentualStat(option, count, localTeamCount,
+								visitorTeamCount, stats.TotalCount);
+							subcatStat.AddOptionStat(pStat);
+						}
+					 } 
+					 
+					 if (subcat is TeamSubCategory) {
+						List<Team> teams = new List<Team>();
+						teams.Add(Team.LOCAL);
+						teams.Add(Team.VISITOR);
+						
+						foreach (Team team in teams) {
+							List<Play> subcatPlays;
+							int count;
+							TeamTag tag;
+							
+							tag = new TeamTag();
+							tag.SubCategory = subcat;
+							tag.Value = team;
+							
+							subcatPlays = plays.Where(p => p.Teams.Tags.Contains(tag)).ToList();
+							count = subcatPlays.Count(); 
+							CountPlaysInTeam(subcatPlays, out localTeamCount, out visitorTeamCount);
+							PercentualStat pStat = new PercentualStat(team.ToString(), count, localTeamCount,
+								visitorTeamCount, stats.TotalCount);
+							subcatStat.AddOptionStat(pStat);
+						}
+					 } 
+				}
+			}
+		}
+	}
+}
+
diff --git a/LongoMatch.Core/Stats/Stat.cs b/LongoMatch.Core/Stats/Stat.cs
new file mode 100644
index 0000000..6c9df44
--- /dev/null
+++ b/LongoMatch.Core/Stats/Stat.cs
@@ -0,0 +1,53 @@
+// 
+//  Copyright (C) 2012 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+
+namespace LongoMatch.Stats
+{
+	public class Stat
+	{
+		public Stat (string name, int totalCount, int localTeamCount, int visitorTeamCount)
+		{
+			Name = name;
+			TotalCount = totalCount;
+			LocalTeamCount = localTeamCount;
+			VisitorTeamCount = visitorTeamCount;
+		}
+		
+		public string Name {
+			get;
+			set;
+		}
+		
+		public int TotalCount {
+			get;
+			set;
+		}
+		
+		public int LocalTeamCount {
+			get;
+			set;
+		}
+		
+		public int VisitorTeamCount {
+			get;
+			set;
+		}
+	}
+}
+
diff --git a/LongoMatch.Core/Stats/SubCategoryStat.cs b/LongoMatch.Core/Stats/SubCategoryStat.cs
new file mode 100644
index 0000000..d1b090c
--- /dev/null
+++ b/LongoMatch.Core/Stats/SubCategoryStat.cs
@@ -0,0 +1,53 @@
+// 
+//  Copyright (C) 2012 Andoni Morales Alastruey
+// 
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 2 of the License, or
+//  (at your option) any later version.
+// 
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//  GNU General Public License for more details.
+//  
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+// 
+using System;
+using System.Collections.Generic;
+
+using LongoMatch.Interfaces;
+using LongoMatch.Store;
+
+namespace LongoMatch.Stats
+{
+	public class SubCategoryStat
+	{
+		
+		List<PercentualStat> optionStats;
+		
+		public SubCategoryStat (string name)
+		{
+			Name = name;
+			optionStats = new List<PercentualStat>();
+		}
+		
+		public string Name {
+			get;
+			set;
+		}
+		
+		public List<PercentualStat> OptionStats {
+			get {
+				return optionStats; 
+			}
+		}
+		
+		public void AddOptionStat (PercentualStat stat) {
+			optionStats.Add(stat);
+		}
+	}
+}
+



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