[gbrainy] Random tips during different sessions



commit 2422eb98dd20c553b56b541ed8ad162c33abed0d
Author: Jordi Mas <jmas softcatala org>
Date:   Tue Dec 22 16:02:31 2009 +0100

    Random tips during different sessions

 src/Core/Main/GameSession.cs |    7 +++++++
 src/Core/Main/GameTips.cs    |    4 ++--
 src/Core/Views/FinishView.cs |   19 ++++++++++++++++++-
 3 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/src/Core/Main/GameSession.cs b/src/Core/Main/GameSession.cs
index 1d0f361..4c141f3 100644
--- a/src/Core/Main/GameSession.cs
+++ b/src/Core/Main/GameSession.cs
@@ -84,12 +84,14 @@ namespace gbrainy.Core.Main
 		private ISynchronizeInvoke synchronize;
 		private PlayerHistory history;
 		private Statistics [] statistics;
+		private int id;
 
 		public event EventHandler DrawRequest;
 		public event EventHandler <UpdateUIStateEventArgs> UpdateUIElement;
 	
 		public GameSession ()
 		{
+			id = 0;
 			game_manager = new GameManager ();
 			game_time = TimeSpan.Zero;
 
@@ -107,6 +109,10 @@ namespace gbrainy.Core.Main
 			history = new PlayerHistory ();
 		}
 
+		public int ID {
+			get {return id;}
+		}
+
 		public PlayerHistory PlayerHistory { 
 			set { history = value; }
 			get { return history; }
@@ -245,6 +251,7 @@ namespace gbrainy.Core.Main
 	
 		public void NewSession ()
 		{
+			id++;
 			if (Status != SessionStatus.NotPlaying)
 				EndSession ();
 
diff --git a/src/Core/Main/GameTips.cs b/src/Core/Main/GameTips.cs
index 0438f1b..03fb763 100644
--- a/src/Core/Main/GameTips.cs
+++ b/src/Core/Main/GameTips.cs
@@ -35,12 +35,12 @@ namespace gbrainy.Core.Main
 		// Gets a random tip from the list
 		static public string Tip {
 			get {
-				if (idx + 1 > Count || random_indices == null) {
+				if (idx + 1 >= Count || random_indices == null) {
 					random_indices = new ArrayListIndicesRandom (Count);
 					random_indices.Initialize ();
 					idx = 0;
 				}
-				return GetTip (idx++); 
+				return GetTip (random_indices [idx++]);
 			}
 		}
 
diff --git a/src/Core/Views/FinishView.cs b/src/Core/Views/FinishView.cs
index de8260b..874f525 100644
--- a/src/Core/Views/FinishView.cs
+++ b/src/Core/Views/FinishView.cs
@@ -32,9 +32,15 @@ namespace gbrainy.Core.Views
 		GameSession session;
 		const int tips_shown = 4;
 
+		// Caching mechanism to use always the same tips during different redraws of the same view
+		int cached_sessionid;
+		List <string> tips;
+
 		public FinishView (GameSession session)
 		{
 			this.session = session;
+			tips = new List <string> ();
+			cached_sessionid = -1;
 		}
 
 		static void DrawBand (CairoContextEx gr, double x, double y)
@@ -164,19 +170,30 @@ namespace gbrainy.Core.Views
 			gr.MoveTo (x, y);		
 
 			if (records.Count == 0) {
+				bool caching = cached_sessionid != session.ID;
+	
 				gr.ShowPangoText (Catalog.GetString ("Tips for your next games"), false, -1, 0);
 				DrawBand (gr, 0.03, y - 0.01);
 
 				y += 0.08;
 
+				if (caching)
+					tips.Clear ();
+
 				for (int i = 0; i < tips_shown; i++)
 				{
-					y = gr.DrawStringWithWrapping (x, y,  "- " + GameTips.Tip);
+					if (caching)
+						tips.Add (GameTips.Tip);
+						
+					y = gr.DrawStringWithWrapping (x, y,  "- " + tips [i]);
 					if (y > 0.88)
 						break;
 
 					y += space_small;
 				}
+
+				if (caching)
+					cached_sessionid = session.ID;
 			} 
 			else  {
 				gr.ShowPangoText (Catalog.GetString ("Congratulations! New personal record"), false, -1, 0);



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