[gbrainy/gbrainy-17x] MemoryConfiguration service + langcode for language + fixes



commit b5504b1a1d9ed6b9a17fc73250d0576f5528f1bc
Author: Jordi Mas <jmas softcatala org>
Date:   Sat Feb 12 14:44:47 2011 +0100

    MemoryConfiguration service + langcode for language + fixes

 src/Clients/Classical/gbrainy.cs             |   17 +++++++--
 src/Clients/WebForms/Default.aspx.cs         |   16 ++++-----
 src/Clients/WebForms/Game.aspx.cs            |   18 ++++++---
 src/Clients/WebForms/Global.asax.cs          |   11 ++++++
 src/Clients/WebForms/LanguageSupport.cs      |   32 ++++++++++++++---
 src/Clients/WebForms/Status.aspx             |    8 ++++
 src/Clients/WebForms/Status.aspx.cs          |   45 ++++++++++++++++++++---
 src/Clients/WebForms/Status.aspx.designer.cs |    2 +
 src/Clients/WebForms/TranslationsWeb.cs      |   18 ++++-----
 src/Clients/WebForms/WebSession.cs           |    2 +-
 src/Clients/WebForms/package.sh              |    2 +-
 src/Core/Core.csproj                         |    2 +
 src/Core/Main/Theme.cs                       |   10 +++++
 src/Core/Makefile.am                         |    4 ++-
 src/Core/Services/IConfiguration.cs          |   35 ++++++++++++++++++
 src/Core/Services/MemoryConfiguration.cs     |   51 ++++++++++++++++++++++++++
 16 files changed, 232 insertions(+), 41 deletions(-)
---
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index 43bd21a..db265fb 100755
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -773,6 +773,18 @@ namespace gbrainy.Clients.Classical
 			Process.Start ("http://live.gnome.org/gbrainy/Extending";);
 		}
 
+		static void InitCoreLibraries ()
+		{
+			// Register services
+			ServiceLocator.Instance.RegisterService <ITranslations> (new TranslationsCatalog ());
+			ServiceLocator.Instance.RegisterService <IConfiguration> (new MemoryConfiguration ());
+			
+			// Configuration
+			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.GamesDefinitions, Defines.DATA_DIR);
+			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.GamesGraphics, Defines.DATA_DIR);
+			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.ThemesDir, Defines.DATA_DIR);
+		}
+
 		public static void Main (string [] args)
 		{
 			try {
@@ -785,9 +797,8 @@ namespace gbrainy.Clients.Classical
 
 			DateTime start_time = DateTime.Now;
 
-			// Register services
-			ServiceLocator.Instance.RegisterService <ITranslations> (new TranslationsCatalog ());
-
+			InitCoreLibraries ();
+			
 			GtkClient app = new GtkClient ();
 			CommandLine.Version ();
 
diff --git a/src/Clients/WebForms/Default.aspx.cs b/src/Clients/WebForms/Default.aspx.cs
index 722440f..9fe2661 100644
--- a/src/Clients/WebForms/Default.aspx.cs
+++ b/src/Clients/WebForms/Default.aspx.cs
@@ -27,7 +27,8 @@ namespace gbrainy.Clients.WebForms
 	public partial class Default : System.Web.UI.Page
 	{
 		WebSession web_session;
-		const string CookieName = "Lang";
+		const string CookieName = "Language";
+		const string DefaultLocale = "en_US";
 
 		void Page_Load (object o, EventArgs e)
         	{
@@ -41,30 +42,27 @@ namespace gbrainy.Clients.WebForms
 			for (int i = 0; i <LanguageSupport.Languages.Length; i++)
 			{
 				languages_drop.Items.Add (new ListItem (LanguageSupport.Languages[i].Name,
-					i.ToString ()));
+					LanguageSupport.Languages[i].LangCode));
 			}
 
 			if (Request.Cookies [CookieName] != null)
     				languages_drop.SelectedValue = Request.Cookies[CookieName].Value;
 			else // Default language value
-				languages_drop.SelectedValue = "0";
+				languages_drop.SelectedValue = DefaultLocale;
 
-			Global.Sessions [Session.SessionID].LanguageIndex =
-				Int32.Parse (languages_drop.SelectedValue);
+			Global.Sessions [Session.SessionID].LanguageCode = languages_drop.SelectedValue;
 	        }
 		
-		
 		protected void OnStartGame (Object sender, EventArgs e)
 		{
 			web_session = Global.Sessions [Session.SessionID];
 			web_session.GameState = null;
 
 			// Collect language cookie
-			Global.Sessions [Session.SessionID].LanguageIndex =
-				Int32.Parse (languages_drop.SelectedValue);
+			Global.Sessions [Session.SessionID].LanguageCode = languages_drop.SelectedValue;
 			
 			Response.Cookies[CookieName].Value = languages_drop.SelectedValue;
-			Response.Cookies[CookieName].Expires =  DateTime.Now.AddYears (1);
+			Response.Cookies[CookieName].Expires = DateTime.Now.AddYears (1);
 
 			Logger.Debug ("Default.OnStartGame. Start game button click");
 			Response.Redirect ("Game.aspx");
diff --git a/src/Clients/WebForms/Game.aspx.cs b/src/Clients/WebForms/Game.aspx.cs
index 3fe4ba8..74d7537 100644
--- a/src/Clients/WebForms/Game.aspx.cs
+++ b/src/Clients/WebForms/Game.aspx.cs
@@ -40,9 +40,11 @@ namespace gbrainy.Clients.WebForms
 		gbrainy.Core.Main.GameSession session;
 		WebSession web_session;
 
-
 		static public GameManager CreateManager ()
 		{
+			if (manager != null)
+				return manager;
+			
 			manager = new GameManager ();
 			manager.LoadAssemblyGames (Defines.GAME_ASSEMBLY);
 			manager.LoadVerbalAnalogies (System.IO.Path.Combine ("data/", Defines.VERBAL_ANALOGIES));
@@ -63,9 +65,9 @@ namespace gbrainy.Clients.WebForms
 			}
 		}
 
-		int GetLanguageIndexFromSessionHandler ()
+		string GetLanguageFromSessionHandler ()
 		{
-			return WebSession.LanguageIndex;
+			return WebSession.LanguageCode;
 		}
 
 		// Page Life-Cycle Events
@@ -140,7 +142,7 @@ namespace gbrainy.Clients.WebForms
 		void InitPage ()
 		{
 			TranslationsWeb service = (TranslationsWeb) ServiceLocator.Instance.GetService <ITranslations> ();
-			service.GetLanguageIndexFromSession = GetLanguageIndexFromSessionHandler;
+			service.GetLanguageFromSession = GetLanguageFromSessionHandler;
 
 			game_image.Width = image_width;
 			game_image.Height = image_height;
@@ -230,12 +232,12 @@ namespace gbrainy.Clients.WebForms
 
 			try
 			{
-				cairo_image = new Cairo.ImageSurface (Cairo.Format.ARGB32, 400, 400);
+				cairo_image = new Cairo.ImageSurface (Cairo.Format.ARGB32, image_width, image_height);
 				cr = new gbrainy.Core.Main.CairoContextEx (cairo_image, "sans 12", 96);
 				file = GetImageFileName (_session.Session.SessionID);
 
 				// Draw Image
-				_session.GameState.Draw (cr, 400, 400, false);
+				_session.GameState.Draw (cr, image_width, image_height, false);
 				cairo_image.WriteToPng (file);
 
 				if (File.Exists (file) == false)
@@ -299,6 +301,10 @@ namespace gbrainy.Clients.WebForms
 
 			if (session != null)
 				session.End ();
+			
+			Global.TotalGamesSessions++;
+			Global.TotalGames += session.History.GamesPlayed;
+			Global.TotalTimeSeconds += session.GameTime.Seconds;
 
 			Response.Redirect ("Game.aspx");
 		}
diff --git a/src/Clients/WebForms/Global.asax.cs b/src/Clients/WebForms/Global.asax.cs
index 9b0f2ae..ca55153 100644
--- a/src/Clients/WebForms/Global.asax.cs
+++ b/src/Clients/WebForms/Global.asax.cs
@@ -14,10 +14,20 @@ namespace gbrainy.Clients.WebForms
 {
 	public class Global : System.Web.HttpApplication
 	{
+		// Application counters
+		static public int TotalSessions { get; set; }
+		static public int TotalGamesSessions { get; set; }
+		static public int TotalGames { get; set; }
+		static public int TotalTimeSeconds { get; set; }
+		static public DateTime Started { get; set; }
+		
+		
 		static public Dictionary <string, WebSession> Sessions = new Dictionary <string, WebSession> ();
 
 		protected virtual void Application_Start (Object sender, EventArgs e)
 		{
+			 Started = DateTime.Now;
+			
 			// Init log system
 			if (String.Compare (Environment.GetEnvironmentVariable ("GBRAINY_DEBUG"), "false", false) != 0)
 			{
@@ -48,6 +58,7 @@ namespace gbrainy.Clients.WebForms
 			}
 			else
 			{
+				TotalSessions++;
 				Sessions.Add (Session.SessionID, details);
 			}
 		}
diff --git a/src/Clients/WebForms/LanguageSupport.cs b/src/Clients/WebForms/LanguageSupport.cs
index 4a9f626..1b2448b 100644
--- a/src/Clients/WebForms/LanguageSupport.cs
+++ b/src/Clients/WebForms/LanguageSupport.cs
@@ -18,6 +18,7 @@
  */
 
 using System;
+using System.Collections.Generic;
 using Mono.Unix;
 
 namespace gbrainy.Clients.WebForms
@@ -27,15 +28,36 @@ namespace gbrainy.Clients.WebForms
 		public class Language
 		{
 			public string Name { get; set; }
-			public string LangCode { get; set; }
+			public string LocaleLanguage { get; set; }
+			public string LangCode  { get; set; }
 
 			public Language (string name, string code)
 			{
 				Name = name;
-				LangCode = code;
+				LocaleLanguage = code;
 			}
 		};
-
+		
+		// code -> language
+		static Dictionary <string, Language> langmap;
+		
+		static LanguageSupport ()
+		{
+			string code;
+			int idx;
+			
+			langmap = new Dictionary <string, Language> ();
+			foreach (Language language in languages)
+			{
+				idx = language.LocaleLanguage.IndexOf (".");
+				code =  language.LocaleLanguage.Substring (0, idx);
+				language.LangCode = code;
+				langmap.Add (code, language);
+			}
+			
+		}
+				
+		// List of exposed locales
 		static Language [] languages =
 		{
 			new Language ("English", "en_US.utf8"),
@@ -64,9 +86,9 @@ namespace gbrainy.Clients.WebForms
 
 		}
 
-		static public Language GetFromIndex (int i)
+		static public Language GetFromCode (string code)
 		{
-			return languages [i];
+			return langmap [code];
 		}
 	}
 }
diff --git a/src/Clients/WebForms/Status.aspx b/src/Clients/WebForms/Status.aspx
index 214c66f..5dd3574 100644
--- a/src/Clients/WebForms/Status.aspx
+++ b/src/Clients/WebForms/Status.aspx
@@ -21,6 +21,14 @@
 				<asp:TableHeaderCell Runat="server">Time Started</asp:TableHeaderCell>
 			</asp:TableHeaderRow>
 		</asp:Table>
+		<br/><br/>
+		<asp:Table id="application_table" GridLines="Both" CellPadding="5" CellSpacing="5" Runat="server">
+			<asp:TableHeaderRow Runat="server">
+				<asp:TableHeaderCell Runat="server">Indicator</asp:TableHeaderCell>
+				<asp:TableHeaderCell Runat="server">Value</asp:TableHeaderCell>
+			</asp:TableHeaderRow>
+		</asp:Table>
+		
 		<br/>
     		<asp:Label runat ="server" ID="total_label"/>
     		<br/><br/>
diff --git a/src/Clients/WebForms/Status.aspx.cs b/src/Clients/WebForms/Status.aspx.cs
index 0284811..2d1cea7 100644
--- a/src/Clients/WebForms/Status.aspx.cs
+++ b/src/Clients/WebForms/Status.aspx.cs
@@ -47,9 +47,14 @@ namespace gbrainy.Clients.WebForms
 	{
 		new PerfCounter ("Mono Memory", "Allocated Objects"),
 		new PerfCounter ("ASP.NET", "Requests Total"),
-
 	};
-
+		
+	void AddCell (TableRow r, string s)
+	{
+		TableCell c = new TableCell ();
+	        c.Controls.Add (new LiteralControl (s));
+	        r.Cells.Add (c);
+	}
 
         public void Page_Load (object sender, EventArgs e)
 	{
@@ -72,8 +77,37 @@ namespace gbrainy.Clients.WebForms
 
 			sessions_table.Rows.Add (r);
 		}
-
-            	total_label.Text = "Total sessions: " + Global.Sessions.Count;
+				
+		// Application counters		
+		{
+			TableRow r = new TableRow ();
+				
+			AddCell (r, "Server started");
+			AddCell (r, Global.Started.ToString());
+			application_table.Rows.Add (r);	
+			
+			r = new TableRow ();
+			AddCell (r, "Total sessions");
+			AddCell (r, Global.TotalSessions.ToString ());
+			application_table.Rows.Add (r);
+				
+			r = new TableRow ();
+			AddCell (r, "Total games played");
+			AddCell (r, Global.TotalGames.ToString ());
+			application_table.Rows.Add (r);
+
+			r = new TableRow ();
+			AddCell (r, "Total seconds played");
+			AddCell (r, Global.TotalTimeSeconds.ToString ());
+			application_table.Rows.Add (r);
+				
+			r = new TableRow ();
+			AddCell (r, "Memory used");
+			AddCell (r,  GC.GetTotalMemory(false).ToString ());
+			application_table.Rows.Add (r);
+		}	
+
+            	total_label.Text = "Total active sessions: " + Global.Sessions.Count;
 
 		// Games
 		string text = Game.CreateManager ().GetGamesSummary ();
@@ -99,7 +133,7 @@ namespace gbrainy.Clients.WebForms
 			assemblies_table.Rows.Add (r);
 		}
 
-		// Performace counters
+		// Performance counters
 		foreach (PerfCounter perf in PerfCounters)
 		{
 			TableRow r = new TableRow ();
@@ -121,6 +155,7 @@ namespace gbrainy.Clients.WebForms
 
 			counters_table.Rows.Add (r);
 		}
+			
         }
 
 	string ReadCounter (string category, string counter)
diff --git a/src/Clients/WebForms/Status.aspx.designer.cs b/src/Clients/WebForms/Status.aspx.designer.cs
index f1eea7c..b2872d9 100644
--- a/src/Clients/WebForms/Status.aspx.designer.cs
+++ b/src/Clients/WebForms/Status.aspx.designer.cs
@@ -19,6 +19,8 @@ namespace gbrainy.Clients.WebForms {
 		
 		protected System.Web.UI.WebControls.Table sessions_table;
 		
+		protected System.Web.UI.WebControls.Table application_table;
+		
 		protected System.Web.UI.WebControls.Label total_label;
 		
 		protected System.Web.UI.WebControls.Label games_label;
diff --git a/src/Clients/WebForms/TranslationsWeb.cs b/src/Clients/WebForms/TranslationsWeb.cs
index 007cd9e..36e0c2b 100644
--- a/src/Clients/WebForms/TranslationsWeb.cs
+++ b/src/Clients/WebForms/TranslationsWeb.cs
@@ -27,15 +27,13 @@ namespace gbrainy.Clients.WebForms
 {
 	public class TranslationsWeb : ITranslations
 	{
-		public delegate int GetLanguageIndexFromSessionHandler ();
+		public delegate string GetLanguageFromSessionHandler ();
 		static readonly object sync = new object ();
 
-		public GetLanguageIndexFromSessionHandler GetLanguageIndexFromSession;
+		public GetLanguageFromSessionHandler GetLanguageFromSession;
 
 		public void Init (string package, string localedir)
 		{
-			string s = Directory.GetCurrentDirectory ();
-			
 			Catalog.Init (package, localedir);
 		}
 
@@ -45,8 +43,8 @@ namespace gbrainy.Clients.WebForms
 
 			lock (sync)
 			{
-				int index = GetLanguageIndexFromSession ();
-				SetContext (index);
+				string code = GetLanguageFromSession ();
+				SetContext (code);
 				str = Catalog.GetString (s);
 			}
 			return str;
@@ -58,16 +56,16 @@ namespace gbrainy.Clients.WebForms
 
 			lock (sync)
 			{
-				int index = GetLanguageIndexFromSession ();
-				SetContext (index);
+				string code = GetLanguageFromSession ();
+				SetContext (code);
 				str = Catalog.GetPluralString (s, p, n);
 			}
 			return str;
 		}
 
-		void SetContext (int index)
+		void SetContext (string code)
 		{
-			string langcode = LanguageSupport.GetFromIndex (index).LangCode;
+			string langcode = LanguageSupport.GetFromCode (code).LocaleLanguage;
 			Environment.SetEnvironmentVariable ("LANGUAGE", langcode);
 			Init ("gbrainy", Defines.LOCALE_DIR);
 		}
diff --git a/src/Clients/WebForms/WebSession.cs b/src/Clients/WebForms/WebSession.cs
index 6722874..4cf2cc9 100644
--- a/src/Clients/WebForms/WebSession.cs
+++ b/src/Clients/WebForms/WebSession.cs
@@ -10,7 +10,7 @@ namespace gbrainy.Clients.WebForms
 		public HttpSessionState Session { get; set; }
 		public DateTime Started { get; set; }
 		public gbrainy.Core.Main.GameSession GameState { get; set; }
-		public int LanguageIndex  { get; set; }
+		public string LanguageCode  { get; set; }
 
 		public WebSession (HttpSessionState session)
 		{
diff --git a/src/Clients/WebForms/package.sh b/src/Clients/WebForms/package.sh
index 25639ca..ca5ee47 100755
--- a/src/Clients/WebForms/package.sh
+++ b/src/Clients/WebForms/package.sh
@@ -19,7 +19,7 @@ cp ../*.css .
 cp ../*.asax .
 cp ../web.config .
 
-cp ../bin/* bin
+cp ../bin/*.dll bin
 cp ../../../../data/*.xml data
 cp ../../../../data/game-graphics/* images
 cp ../../../../data/app-graphics/* images
diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj
index 6cdf394..3b66de6 100644
--- a/src/Core/Core.csproj
+++ b/src/Core/Core.csproj
@@ -109,6 +109,8 @@
     <Compile Include="Main\Theme.cs" />
     <Compile Include="Main\ThemeManager.cs" />
     <Compile Include="Libraries\TranslationsCatalog.cs" />
+    <Compile Include="Services\IConfiguration.cs" />
+    <Compile Include="Services\MemoryConfiguration.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ProjectExtensions>
diff --git a/src/Core/Main/Theme.cs b/src/Core/Main/Theme.cs
index 9a9040c..c5835b6 100644
--- a/src/Core/Main/Theme.cs
+++ b/src/Core/Main/Theme.cs
@@ -18,12 +18,15 @@
  */
 
 using System;
+using System.IO;
 using System.Xml.Serialization;
 using System.Text.RegularExpressions;
 using System.Globalization;
 
 using Cairo;
 
+using gbrainy.Core.Services;
+
 namespace gbrainy.Core.Main
 {
 	public class Theme
@@ -83,6 +86,13 @@ namespace gbrainy.Core.Main
 				return new Cairo.Color ((double) r / 255d, (double) g / 255d, (double) b / 255d, (double) a / 255d);
 			}
 		}
+		
+		public string GetFullPath (string path)
+		{
+			IConfiguration config = ServiceLocator.Instance.GetService <IConfiguration> ();
+			
+			return System.IO.Path.Combine (config.Get <string> (ConfigurationKeys.ThemesDir), path);
+		}
 	}
 }
 
diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am
index 1010a6f..62979ba 100644
--- a/src/Core/Makefile.am
+++ b/src/Core/Makefile.am
@@ -66,7 +66,9 @@ CSDISTFILES =  \
 		$(srcdir)/Services/IService.cs		\
 		$(srcdir)/Services/ServiceLocator.cs	\
 		$(srcdir)/Services/ITranslations.cs	\
-		$(srcdir)/Libraries/TranslationsCatalog.cs
+		$(srcdir)/Libraries/TranslationsCatalog.cs \
+		$(srcdir)/Services/IConfiguration.cs	\
+		$(srcdir)/Services/MemoryConfiguration.cs
 
 CSFILES = $(CSDISTFILES)	\
 	Main/Defines.cs	\
diff --git a/src/Core/Services/IConfiguration.cs b/src/Core/Services/IConfiguration.cs
new file mode 100644
index 0000000..694165b
--- /dev/null
+++ b/src/Core/Services/IConfiguration.cs
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2011 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * 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., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+namespace gbrainy.Core.Services
+{
+	public enum ConfigurationKeys
+	{
+		GamesDefinitions,	// Analogies.xml and games.xml
+		GamesGraphics,
+		ThemesDir,
+	};
+
+	public interface IConfiguration : IService
+	{
+		T Get <T> (ConfigurationKeys key);
+		void Set <T> (ConfigurationKeys key, T val);
+	}
+}
+
diff --git a/src/Core/Services/MemoryConfiguration.cs b/src/Core/Services/MemoryConfiguration.cs
new file mode 100644
index 0000000..1c1c07c
--- /dev/null
+++ b/src/Core/Services/MemoryConfiguration.cs
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2011 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * 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., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System.Collections.Generic;
+
+namespace gbrainy.Core.Services
+{
+	public class MemoryConfiguration : IConfiguration
+	{
+		Dictionary <ConfigurationKeys, object> keys;
+
+		public MemoryConfiguration ()
+		{
+			keys = new Dictionary <ConfigurationKeys, object> ();
+		}
+
+		public T Get <T> (ConfigurationKeys key)
+		{
+			return (T) keys [key];
+		}
+
+		public void Set <T> (ConfigurationKeys key, T val)
+		{
+			if (keys.ContainsKey (key) == false)
+			{
+				keys.Add (key, val);
+			}
+			else
+			{
+				keys[key] = val;
+			}
+		}
+	}
+}
+



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