[gbrainy] Makes UpdateGameQuestionEventArgs more generic & fixes scoring



commit cd885fe1204720e329d188103060d0cb8d7852e5
Author: Jordi Mas <jmas softcatala org>
Date:   Fri Nov 27 12:53:02 2009 +0100

    Makes UpdateGameQuestionEventArgs more generic & fixes scoring

 src/Clients/Classical/gbrainy.cs             |   43 ++++++++++++------
 src/Core/Main/Game.cs                        |   61 +++++++++++---------------
 src/Core/Main/GameSession.cs                 |   60 ++++++++++++++-----------
 src/Core/Main/UpdateGameQuestionEventArgs.cs |   37 ----------------
 src/Core/Makefile.am                         |    2 +-
 5 files changed, 89 insertions(+), 114 deletions(-)
---
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index 1fcfbd2..284b0c8 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -82,7 +82,7 @@ namespace gbrainy.Clients.Classical
 			BuildToolBar ();
 			session = new GameSession ();
 			session.DrawRequest += SessionDrawRequest;
-			session.UpdateGameQuestion += SessionUpdateGameQuestion;
+			session.UpdateUIElement += SessionUpdateUIElement;
 			session.SynchronizingObject = new GtkSynchronize ();
 
 			session.GameManager.Difficulty = (Game.Difficulty) Preferences.GetIntValue (Preferences.DifficultyKey);
@@ -127,10 +127,19 @@ namespace gbrainy.Clients.Classical
 		}
 
 		// Gamesession has requested a question refresh
-		public void SessionUpdateGameQuestion (object o, UpdateGameQuestionEventArgs args)
+		public void SessionUpdateUIElement (object o, UpdateUIStateEventArgs args)
 		{
-			UpdateQuestion (args.Question);
-			ActiveInputControls (true);
+			switch (args.EventType) {
+			case UpdateUIStateEventArgs.EventUIType.QuestionText:
+				UpdateQuestion ((string) args.Data);
+				ActiveInputControls (true);
+				break;
+			case UpdateUIStateEventArgs.EventUIType.Time:
+				UpdateStatusBar ();
+				break;
+			default:
+				throw new InvalidOperationException ("Unknow value");
+			}
 		}
 
 		// Gamesession has requested a redraw of the drawingarea
@@ -339,14 +348,11 @@ namespace gbrainy.Clients.Classical
 			if (session.CurrentGame == null)
 				return;
 	
-			if (answer_button.Sensitive == true && session.CurrentGame.CheckAnswer (answer_entry.Text) == true) {
-				session.GamesWon++;
-				session.CurrentGame.Won = true;
+			if (session.ScoreGame (answer_entry.Text) == true)
 				answer = "<span color='#00A000'>" + Catalog.GetString ("Congratulations.") + "</span>";
-			} else
+			else
 				answer = Catalog.GetString ("Incorrect answer.");
 
-			session.ScoreGame ();
 			session.EnableTimer = false;
 			answer_entry.Text = String.Empty;
 			UpdateStatusBar ();
@@ -376,7 +382,7 @@ namespace gbrainy.Clients.Classical
 				return;
 			}
 
-			session.ScoreGame ();
+			session.ScoreGame (String.Empty);
 			GetNextGame ();
 			session.EnableTimer = true;
 		}
@@ -486,25 +492,34 @@ namespace gbrainy.Clients.Classical
 			GameSensitiveUI ();
 			drawing_area.QueueDraw ();
 			ActiveInputControls (false);
-			SetPauseResumeButton (true);
+			SetPauseResumeButtonUI (true);
 		}
 
-		void SetPauseResumeButton (bool pause)
+		void SetPauseResumeButtonUI (bool pause)
 		{
 			if (pause) {
 				pause_tbbutton.StockId = "pause";
 				pause_tbbutton.Label = Catalog.GetString ("Pause");
-	 			session.Resume ();
 				ActiveInputControls (true);
 			} else {
 				pause_tbbutton.StockId = "resume";
 				pause_tbbutton.Label = Catalog.GetString ("Resume");
-				session.Pause ();
 				ActiveInputControls (false);
 			}
 			UpdateStatusBar ();
 		}
 
+
+		void SetPauseResumeButton (bool pause)
+		{
+			if (pause)
+	 			session.Resume ();
+			else
+				session.Pause ();
+
+			SetPauseResumeButtonUI (pause);
+		}
+
 		void OnPauseGame (object sender, EventArgs args)
 		{
 			SetPauseResumeButton (session.Paused);
diff --git a/src/Core/Main/Game.cs b/src/Core/Main/Game.cs
index bd9a4fa..faac839 100644
--- a/src/Core/Main/Game.cs
+++ b/src/Core/Main/Game.cs
@@ -51,20 +51,18 @@ namespace gbrainy.Core.Main
 		protected string right_answer;
 		protected Random random;
 		private TimeSpan game_time;
-		private bool won;
 		private bool tip_used;
 		private Difficulty difficulty;
 		private ISynchronizeInvoke synchronize;
 
 		public event EventHandler DrawRequest;
-		public event EventHandler <UpdateGameQuestionEventArgs> UpdateGameQuestion;
+		public event EventHandler <UpdateUIStateEventArgs> UpdateUIElement;
 
 		protected Game ()
 		{
 			random = new Random ();
 			draw_answer = false;
 			default_color = new Cairo.Color (0, 0, 0);
-			won = false;
 			tip_used = false;
 			difficulty = Difficulty.Medium;
 		}
@@ -81,10 +79,11 @@ namespace gbrainy.Core.Main
 		// Used by games to request a question repaint
 		protected void UpdateQuestion (string question)
 		{
-			if (UpdateGameQuestion == null)
+			if (UpdateUIElement == null)
 				return;
 
-			UpdateGameQuestion (this, new UpdateGameQuestionEventArgs (question));
+			UpdateUIElement (this, new UpdateUIStateEventArgs (UpdateUIStateEventArgs.EventUIType.QuestionText, 
+				question));
 		}
 
 		public abstract string Question {
@@ -188,11 +187,6 @@ namespace gbrainy.Core.Main
 			set {game_time = value; }
 		}
 
-		public bool Won {
-			get { return won; }
-			set { won = value; }
-		}
-
 		// Average time in seconds that a player is expected to complete this game
 		public int AverageTime {
 			get {
@@ -224,33 +218,30 @@ namespace gbrainy.Core.Main
 		//
 		// Score algorithm return a value between 0 and 10
 		//
-		public virtual int Score {
-			get {
-				double score;
-				double seconds = GameTime.TotalSeconds;
-
-				if (won == false) {
-					score = 0;
-				} else {		
-					score = 10;
-			
-					// Time
-					if (seconds > AverageTime * 3) {
-						score = score * 0.6;
-					}
-					else if (seconds > AverageTime * 2) {
-						score = score * 0.7;
-					} else if (seconds > AverageTime) {
-						score = score * 0.8;
-					}
-		
-					if (tip_used) {
-						score = score * 0.8;
-					}
-				}
+		public virtual int Score (string answer)
+		{
+			double score;
+			double seconds = GameTime.TotalSeconds;
 
-				return (int) score;
+			if (CheckAnswer (answer) == false)
+				return 0;
+
+			score = 10;
+	
+			// Time
+			if (seconds > AverageTime * 3) {
+				score = score * 0.6;
+			}
+			else if (seconds > AverageTime * 2) {
+				score = score * 0.7;
+			} else if (seconds > AverageTime) {
+				score = score * 0.8;
 			}
+
+			if (tip_used)
+				score = score * 0.8;
+
+			return (int) score;
 		}
 	
 		public abstract void Initialize ();
diff --git a/src/Core/Main/GameSession.cs b/src/Core/Main/GameSession.cs
index 60e1485..0ffcde1 100644
--- a/src/Core/Main/GameSession.cs
+++ b/src/Core/Main/GameSession.cs
@@ -79,7 +79,7 @@ namespace gbrainy.Core.Main
 		private PlayerHistory history;
 
 		public event EventHandler DrawRequest;
-		public event EventHandler <UpdateGameQuestionEventArgs> UpdateGameQuestion;
+		public event EventHandler <UpdateUIStateEventArgs> UpdateUIElement;
 	
 		public GameSession ()
 		{
@@ -238,10 +238,15 @@ namespace gbrainy.Core.Main
 			if (Status != SessionStatus.NotPlaying)
 				EndSession ();
 
+			current_time = TimeSpanToStr (game_time);
+			scores = new int [(int) ScoresType.Last];
+			games = new int [(int) ScoresType.Last];
+			total_score = 0;
 			games_played = 0;
 			games_won = 0;
 			game_time = TimeSpan.Zero;
-			timer.Enabled = true;
+			timer.SynchronizingObject = SynchronizingObject;
+			EnableTimer = true;
 		}
 
 		public void EndSession ()
@@ -251,17 +256,11 @@ namespace gbrainy.Core.Main
 			if (CurrentGame != null)
 				CurrentGame.Finish ();
 
-			timer.Enabled = false;
+			EnableTimer = false;
+			timer.SynchronizingObject = null;
+
 			paused = false;
 			CurrentGame = null;
-			games_played = 0;
-			games_won = 0;
-			game_time = TimeSpan.Zero;
-			current_time = TimeSpanToStr (game_time);
-			scores = new int [(int) ScoresType.Last];
-			games = new int [(int) ScoresType.Last];
-			total_score = 0;
-			scored_game = false;
 			Status = SessionStatus.Finished;
 		}
 
@@ -274,7 +273,7 @@ namespace gbrainy.Core.Main
 			CurrentGame = game_manager.GetPuzzle ();
 			CurrentGame.SynchronizingObject = SynchronizingObject;
 			CurrentGame.DrawRequest += GameDrawRequest;
-			CurrentGame.UpdateGameQuestion += GameUpdateGameQuestion;
+			CurrentGame.UpdateUIElement += GameUpdateUIElement;
 
 			CurrentGame.Initialize ();
 
@@ -285,37 +284,43 @@ namespace gbrainy.Core.Main
 
 		public void Pause ()
 		{
-			timer.Enabled = false;
+			EnableTimer = false;
 			paused = true;
 			current_time = Catalog.GetString ("Paused");
 		}
 
 		public void Resume ()
 		{
-			timer.Enabled = true;
+			EnableTimer = true;
 			paused = false;
 		}
 
-		public void ScoreGame ()
+		public bool ScoreGame (string answer)
 		{
+			int score;
+
 			if (CurrentGame == null || scored_game == true)
-				return;
+				return false;
+
+			score = CurrentGame.Score (answer);
+			if (score > 0)
+				GamesWon++;
 
 			switch (CurrentGame.Type) {
 			case Game.Types.LogicPuzzle:
-				scores [(int) ScoresType.LogicPuzzles] += CurrentGame.Score;
+				scores [(int) ScoresType.LogicPuzzles] += score;
 				games [(int) ScoresType.LogicPuzzles]++;
 				break;
 			case Game.Types.MemoryTrainer:
-				scores [(int) ScoresType.MemoryTrainers] += CurrentGame.Score;
+				scores [(int) ScoresType.MemoryTrainers] += score;
 				games [(int) ScoresType.MemoryTrainers]++;
 				break;
 			case Game.Types.MathTrainer:
-				scores [(int) ScoresType.CalculationTrainers] += CurrentGame.Score;
+				scores [(int) ScoresType.CalculationTrainers] += score;
 				games [(int) ScoresType.CalculationTrainers]++;
 				break;
 			case Game.Types.VerbalAnalogy:
-				scores [(int) ScoresType.VerbalAnalogies] += CurrentGame.Score;
+				scores [(int) ScoresType.VerbalAnalogies] += score;
 				games [(int) ScoresType.VerbalAnalogies]++;
 				break;
 			default:
@@ -329,9 +334,9 @@ namespace gbrainy.Core.Main
 
 			total_score = total_score * 10 / games_played;
 			scored_game = true;
+			return (score > 0) ? true: false;
 		}	
 
-
 		private void TimerUpdater (object source, ElapsedEventArgs e)
 		{
 			lock (this) {
@@ -340,7 +345,10 @@ namespace gbrainy.Core.Main
 				current_time = TimeSpanToStr (game_time);
 			}
 
-			//Application.Invoke (delegate {	app.UpdateStatusBar (); } );
+			if (UpdateUIElement == null)
+				return;
+
+			UpdateUIElement (this, new UpdateUIStateEventArgs (UpdateUIStateEventArgs.EventUIType.Time, null));
 		}
 
 		static private string TimeSpanToStr (TimeSpan time)
@@ -353,24 +361,22 @@ namespace gbrainy.Core.Main
 			return fmt;
 		}
 
-		public void GameUpdateGameQuestion (object o, UpdateGameQuestionEventArgs args)
+		public void GameUpdateUIElement (object obj, UpdateUIStateEventArgs args)
 		{
-			if (UpdateGameQuestion != null)
-				UpdateGameQuestion (this, args);
+			if (UpdateUIElement != null)
+				UpdateUIElement (this, args);
 		}
 
 		// A game has requested a redraw, scale the request to the object
 		// subscribed to GameSession.GameDrawRequest
 		public void GameDrawRequest (object o, EventArgs args)
 		{
-			//Console.WriteLine ("GameSession.GameRequestRedraw {0}", o); 
 			if (DrawRequest != null)
 				DrawRequest (this, EventArgs.Empty);
 		}
 
 		public virtual void Draw (CairoContextEx gr, int width, int height, bool rtl)
 		{
-			//Console.WriteLine ("GameSession.Draw");
 			controler.CurrentView.Draw (gr, width, height, rtl);
 		}
 
diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am
index 778fa22..334129f 100644
--- a/src/Core/Makefile.am
+++ b/src/Core/Makefile.am
@@ -14,7 +14,7 @@ CSFILES =  \
 		Main/Memory.cs			\
 		Main/PlayerHistory.cs		\
 		Main/Preferences.cs		\
-		Main/UpdateGameQuestionEventArgs.cs \
+		Main/UpdateUIStateEventArgs.cs \
 		Main/Verbal/Analogies.cs 	\
 		Main/Verbal/AnalogiesFactory.cs	\
 		Main/Verbal/AnalogiesMultipleOptions.cs	\



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