~ubuntu-branches/ubuntu/lucid/gbrainy/lucid

« back to all changes in this revision

Viewing changes to src/Core/Main/GameManager.cs

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-01-12 11:21:24 UTC
  • mfrom: (13.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100112112124-o4ztomxa0xfh2ulj
Tags: 1.30-1ubuntu1
* debian/control:
  - Revert build-depends to lucid versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007-2009 Jordi Mas i Hernàndez <jmas@softcatala.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of the
 
7
 * License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public
 
15
 * License along with this program; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
using System;
 
21
using System.IO;
 
22
using System.Collections;
 
23
using System.Collections.Generic;
 
24
using System.Reflection;
 
25
using Mono.Unix;
 
26
 
 
27
#if MONO_ADDINS
 
28
using Mono.Addins;
 
29
using Mono.Addins.Setup;
 
30
#endif
 
31
 
 
32
using gbrainy.Core.Main.Verbal;
 
33
 
 
34
namespace gbrainy.Core.Main
 
35
{
 
36
        public class GameManager
 
37
        {
 
38
                // Serves analogies as their container class is still not exhausted
 
39
                // This is used to make sure that the analogies are not repeated within a game session
 
40
                public class AnalogiesManager
 
41
                {
 
42
                        List <Analogies> analogies;
 
43
 
 
44
                        public AnalogiesManager (Type [] types)
 
45
                        {
 
46
                                analogies = new List <Analogies> ();
 
47
                        
 
48
                                foreach (Type type in types)
 
49
                                {
 
50
                                        Analogies analogy;
 
51
 
 
52
                                        analogy = (Analogies) Activator.CreateInstance (type, true);
 
53
                                        analogies.Add (analogy);
 
54
                                }
 
55
                        }
 
56
 
 
57
                        public void Initialize ()
 
58
                        {
 
59
                                foreach (Analogies analogy in analogies)
 
60
                                        analogy.CurrentIndex = 0;
 
61
                        }
 
62
 
 
63
                        public bool IsExhausted {
 
64
                                get {
 
65
                                        foreach (Analogies analogy in analogies)
 
66
                                        {
 
67
                                                if (analogy.IsExhausted == false)
 
68
                                                        return false;
 
69
                                        }
 
70
                                        return true;
 
71
                                }
 
72
                        }
 
73
                }
 
74
 
 
75
                static Type[] VerbalAnalogiesInternal = new Type[] 
 
76
                {
 
77
                        typeof (AnalogiesQuestionAnswer),
 
78
                        typeof (AnalogiesMultipleOptions),
 
79
                        typeof (AnalogiesPairOfWordsOptions),
 
80
                        typeof (AnalogiesPairOfWordsCompare),
 
81
                };
 
82
 
 
83
                bool once;
 
84
                GameSession.Types game_type;
 
85
                ArrayListIndicesRandom list;
 
86
                IEnumerator enumerator;
 
87
                List <Type> games;
 
88
                Game.Difficulty difficulty;
 
89
                List <Type> LogicPuzzles;
 
90
                List <Type> CalculationTrainers;
 
91
                List <Type> MemoryTrainers;
 
92
                List <Type> VerbalAnalogies;
 
93
                AnalogiesManager analogies_manager;
 
94
        
 
95
                public GameManager ()
 
96
                {
 
97
                        game_type = GameSession.Types.None;
 
98
                        difficulty = Game.Difficulty.Medium;
 
99
                        games = new List <Type> ();
 
100
                        VerbalAnalogies = new List <Type> (VerbalAnalogiesInternal);
 
101
 
 
102
                        LoadAssemblyGame ();
 
103
 
 
104
                        if (LogicPuzzles == null)
 
105
                                LogicPuzzles = new List <Type> ();
 
106
 
 
107
                        if (MemoryTrainers == null)
 
108
                                MemoryTrainers = new List <Type> ();
 
109
 
 
110
                        if (CalculationTrainers == null)
 
111
                                CalculationTrainers = new List <Type> ();
 
112
 
 
113
                        LoadPlugins ();
 
114
 
 
115
                        if (once == false) {
 
116
                                once = true;
 
117
                                Console.WriteLine (Catalog.GetString ("Games registered: {0}: {1} logic puzzles, {2} calculation trainers, {3} memory trainers, {4} verbal analogies"), 
 
118
                                        LogicPuzzles.Count + CalculationTrainers.Count + MemoryTrainers.Count + VerbalAnalogies.Count,
 
119
                                        LogicPuzzles.Count, CalculationTrainers.Count, MemoryTrainers.Count, VerbalAnalogies.Count);
 
120
                        }
 
121
 
 
122
                        analogies_manager = new AnalogiesManager (VerbalAnalogiesInternal);
 
123
                        //GeneratePDF ();
 
124
                }
 
125
 
 
126
                public GameSession.Types GameType {
 
127
                        get {return game_type; }
 
128
                        set {
 
129
                                if (game_type == value)
 
130
                                        return;
 
131
                        
 
132
                                game_type = value;
 
133
                                BuildGameList ();
 
134
                        }
 
135
                }
 
136
 
 
137
                public Game.Difficulty Difficulty {
 
138
                        set {
 
139
                                difficulty = value;
 
140
                                BuildGameList ();
 
141
                        }
 
142
                        get {
 
143
                                return difficulty;
 
144
                        }
 
145
                }
 
146
 
 
147
                // Used from CustomGameDialog only
 
148
                public Type[] CustomGames {
 
149
                        get { 
 
150
                                Type[] list = new Type [LogicPuzzles.Count + CalculationTrainers.Count + MemoryTrainers.Count + VerbalAnalogies.Count];
 
151
                                int idx = 0;
 
152
 
 
153
                                for (int i = 0; i < LogicPuzzles.Count; i++, idx++)
 
154
                                        list[idx] = LogicPuzzles [i];
 
155
 
 
156
                                for (int i = 0; i < CalculationTrainers.Count; i++, idx++)
 
157
                                        list[idx] = CalculationTrainers [i];
 
158
 
 
159
                                for (int i = 0; i < MemoryTrainers.Count; i++, idx++)
 
160
                                        list[idx] = MemoryTrainers [i];
 
161
 
 
162
                                for (int i = 0; i < VerbalAnalogies.Count; i++, idx++)
 
163
                                        list[idx] = VerbalAnalogies [i];
 
164
 
 
165
                                return list;
 
166
                        }
 
167
                        set {
 
168
                                games = new List <Type> (value.Length);
 
169
                                for (int i = 0; i < value.Length; i++)
 
170
                                        games.Add (value[i]);
 
171
 
 
172
                                list = new ArrayListIndicesRandom (games.Count);
 
173
                                Initialize ();
 
174
                        }
 
175
                }
 
176
 
 
177
                // Dynamic load of the gbrainy.Games.Dll assembly
 
178
                void LoadAssemblyGame ()
 
179
                {
 
180
                        const string ASSEMBLY = "gbrainy.Games.dll";
 
181
                        const string CLASS = "gbrainy.Games.GameList";
 
182
                        const string LOGIC_METHOD = "LogicPuzzles";
 
183
                        const string CALCULATION_METHOD = "CalculationTrainers";
 
184
                        const string MEMORY_METHOD = "MemoryTrainers";
 
185
 
 
186
                        Assembly asem;
 
187
                        Type type = null;
 
188
                        PropertyInfo prop;
 
189
                        object obj;
 
190
 
 
191
                        try
 
192
                        {
 
193
                                // Expects the assembly to be in the same dir than this assembly
 
194
                                Assembly asm = Assembly.GetExecutingAssembly ();
 
195
                                string asm_dir = System.IO.Path.GetDirectoryName (asm.Location);
 
196
 
 
197
                                asem = Assembly.LoadFrom (Path.Combine (asm_dir, ASSEMBLY));
 
198
 
 
199
                                foreach (Type t in asem.GetTypes()) 
 
200
                                {
 
201
                                        if (t.FullName == CLASS)
 
202
                                        {
 
203
                                                type = t;
 
204
                                                break;
 
205
                                        }
 
206
                                }
 
207
 
 
208
                                obj = Activator.CreateInstance (type);
 
209
 
 
210
                                prop = type.GetProperty (LOGIC_METHOD);
 
211
                                LogicPuzzles = new List <Type> ((Type []) prop.GetValue (obj, null));
 
212
 
 
213
                                prop = type.GetProperty (MEMORY_METHOD);
 
214
                                MemoryTrainers = new List <Type> ((Type []) prop.GetValue (obj, null));
 
215
 
 
216
                                prop = type.GetProperty (CALCULATION_METHOD);
 
217
                                CalculationTrainers = new List <Type> ((Type []) prop.GetValue (obj, null));
 
218
                        }
 
219
 
 
220
                        catch (Exception e)
 
221
                        {
 
222
                                Console.WriteLine ("GameManager.LoadAssemblyGame. Exception: {0}", e);
 
223
                        }
 
224
                }
 
225
 
 
226
                void LoadPlugins ()
 
227
                {
 
228
 
 
229
        #if MONO_ADDINS
 
230
                        try {
 
231
                                ExtensionNodeList addins;
 
232
                                Game game;
 
233
                                string dir = System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "gbrainy");
 
234
                
 
235
                                AddinManager.Initialize (dir);
 
236
                                Console.WriteLine ("Pluggin database:" + dir);
 
237
                                AddinManager.Registry.Update (null);
 
238
                                new SetupService (AddinManager.Registry);
 
239
 
 
240
                                addins = AddinManager.GetExtensionNodes ("/gbrainy/games/logic");
 
241
                                foreach (TypeExtensionNode node in addins) {
 
242
                                        game = (Game) node.CreateInstance ();
 
243
                                        Console.WriteLine ("Loading external logic game: {0}", game);
 
244
                                        LogicPuzzles.Add (game.GetType ());
 
245
                                }
 
246
                
 
247
                                addins = AddinManager.GetExtensionNodes ("/gbrainy/games/memory");
 
248
                                foreach (TypeExtensionNode node in addins) {
 
249
                                        game = (Game) node.CreateInstance ();
 
250
                                        Console.WriteLine ("Loading external memory game: {0}", game);
 
251
                                        MemoryTrainers.Add (game.GetType ());
 
252
                                }
 
253
 
 
254
                                addins = AddinManager.GetExtensionNodes ("/gbrainy/games/calculation");
 
255
                                foreach (TypeExtensionNode node in addins) {
 
256
                                        game = (Game) node.CreateInstance ();
 
257
                                        Console.WriteLine ("Loading external calculation game: {0}", game);
 
258
                                        CalculationTrainers.Add (game.GetType ());
 
259
                                }
 
260
 
 
261
                                addins = AddinManager.GetExtensionNodes ("/gbrainy/games/verbal");
 
262
                                foreach (TypeExtensionNode node in addins) {
 
263
                                        game = (Game) node.CreateInstance ();
 
264
                                        Console.WriteLine ("Loading external verbal analogy game: {0}", game);
 
265
                                        VerbalAnalogies.Add (game.GetType ());
 
266
                                }
 
267
                        }
 
268
                        catch (Exception e)
 
269
                        {
 
270
                                Console.WriteLine (String.Format ("Exception {0} when loading the plugins", e));
 
271
                        }
 
272
        #endif
 
273
                }
 
274
 
 
275
                void BuildGameList ()
 
276
                {
 
277
                        analogies_manager.Initialize ();
 
278
 
 
279
                        if (GameType == GameSession.Types.Custom)
 
280
                                return;
 
281
                
 
282
                        games.Clear ();
 
283
                        Random random = new Random ();
 
284
 
 
285
                        // For all games, 1/4 of the total are logic, 1/4 Memory, 1/4 calculation, 1/4 verbal analogies
 
286
                        if ((game_type & GameSession.Types.AllGames) == GameSession.Types.AllGames) {
 
287
                        
 
288
                                int idx_cal = 0, idx_mem = 0, idx_verb = 0;
 
289
                                ArrayListIndicesRandom idx_logic = new ArrayListIndicesRandom (LogicPuzzles.Count);
 
290
                                ArrayListIndicesRandom idx_memory = new ArrayListIndicesRandom (MemoryTrainers.Count);
 
291
                                ArrayListIndicesRandom idx_calculation = new ArrayListIndicesRandom (CalculationTrainers.Count);
 
292
                                ArrayListIndicesRandom idx_verbal = new ArrayListIndicesRandom (VerbalAnalogies.Count);
 
293
 
 
294
                                games.Clear ();
 
295
                                idx_memory.Initialize ();
 
296
                                idx_logic.Initialize ();
 
297
                                idx_calculation.Initialize ();
 
298
                                idx_verbal.Initialize ();
 
299
 
 
300
                                for (int i = 0; i < LogicPuzzles.Count; i++, idx_mem++, idx_cal++, idx_verb++) {
 
301
 
 
302
                                        if (idx_cal == CalculationTrainers.Count) {
 
303
                                                idx_cal = 0;
 
304
                                                idx_calculation.Initialize ();
 
305
                                        }
 
306
 
 
307
                                        if (idx_mem == MemoryTrainers.Count) {
 
308
                                                idx_mem = 0;
 
309
                                                idx_memory.Initialize ();
 
310
                                        }
 
311
 
 
312
                                        if (idx_verb == VerbalAnalogies.Count) {
 
313
                                                idx_verb = 0;
 
314
                                                idx_verbal.Initialize ();
 
315
                                        }
 
316
 
 
317
                                        switch (random.Next (3)) {
 
318
                                        case 0:
 
319
                                                games.Add (CalculationTrainers [idx_calculation[idx_cal]]);
 
320
                                                games.Add (LogicPuzzles [idx_logic[i]]);
 
321
                                                games.Add (MemoryTrainers [idx_memory[idx_mem]]);
 
322
                                                games.Add (VerbalAnalogies [idx_verbal[idx_verb]]);
 
323
                                                break;
 
324
                                        case 1:
 
325
                                                games.Add (MemoryTrainers [idx_memory[idx_mem]]);
 
326
                                                games.Add (CalculationTrainers [idx_calculation[idx_cal]]);
 
327
                                                games.Add (VerbalAnalogies [idx_verbal[idx_verb]]);
 
328
                                                games.Add (LogicPuzzles [idx_logic[i]]);
 
329
                                                break;
 
330
                                        case 2:
 
331
                                                games.Add (CalculationTrainers [idx_calculation[idx_cal]]);
 
332
                                                games.Add (VerbalAnalogies [idx_verbal[idx_verb]]);
 
333
                                                games.Add (MemoryTrainers [idx_memory[idx_mem]]);
 
334
                                                games.Add (LogicPuzzles [idx_logic[i]]);
 
335
                                                break;
 
336
                                        }
 
337
                                }
 
338
                        } else {
 
339
 
 
340
                                if ((game_type & GameSession.Types.LogicPuzzles) == GameSession.Types.LogicPuzzles) {
 
341
                                        for (int i = 0; i < LogicPuzzles.Count; i++)
 
342
                                                games.Add (LogicPuzzles [i]);
 
343
                                }
 
344
 
 
345
                                if ((game_type & GameSession.Types.CalculationTrainers) == GameSession.Types.CalculationTrainers) {
 
346
                                        for (int i = 0; i < CalculationTrainers.Count; i++)
 
347
                                                games.Add (CalculationTrainers [i]);
 
348
                                }
 
349
 
 
350
                                if ((game_type & GameSession.Types.MemoryTrainers) == GameSession.Types.MemoryTrainers) {
 
351
                                        for (int i = 0; i < MemoryTrainers.Count; i++)
 
352
                                                games.Add (MemoryTrainers [i]);
 
353
                                }
 
354
 
 
355
                                if ((game_type & GameSession.Types.VerbalAnalogies) == GameSession.Types.VerbalAnalogies) {
 
356
                                        for (int i = 0; i < VerbalAnalogies.Count; i++)
 
357
                                                games.Add (VerbalAnalogies [i]);
 
358
                                }
 
359
 
 
360
                        }
 
361
 
 
362
                        list = new ArrayListIndicesRandom (games.Count);
 
363
                        Initialize ();
 
364
                }
 
365
 
 
366
                void Initialize ()
 
367
                {
 
368
                        if ((game_type & GameSession.Types.AllGames) == GameSession.Types.AllGames) { // The game list has been already randomized
 
369
                                list.Clear ();
 
370
                                for (int i = 0; i < games.Count; i++)
 
371
                                        list.Add (i);
 
372
                        } else
 
373
                                list.Initialize ();
 
374
 
 
375
                        enumerator = list.GetEnumerator ();
 
376
                }
 
377
        
 
378
                public Game GetPuzzle ()
 
379
                {
 
380
                        Game puzzle, first = null;
 
381
 
 
382
                        while (true) {
 
383
 
 
384
                                if (enumerator.MoveNext () == false) { // All the games have been played, restart again 
 
385
                                        Initialize ();
 
386
                                        enumerator.MoveNext ();
 
387
 
 
388
                                        if (analogies_manager.IsExhausted == true)
 
389
                                                analogies_manager.Initialize ();
 
390
                                }
 
391
                                puzzle =  (Game) Activator.CreateInstance ((Type) games [(int) enumerator.Current], true);
 
392
                                //puzzle =  (Game) Activator.CreateInstance (LogicPuzzles [37], true);
 
393
 
 
394
                                if (first != null && first.GetType () == puzzle.GetType ())
 
395
                                        break;
 
396
 
 
397
                                if (puzzle.IsPlayable == false)
 
398
                                        continue;
 
399
                
 
400
                                if ((Preferences.GetBoolValue (Preferences.ColorBlindKey) == true) && puzzle.UsesColors == true)
 
401
                                        continue;
 
402
 
 
403
                                Analogies analogy = puzzle as Analogies;
 
404
                                if (analogy != null && analogy.IsExhausted == true)
 
405
                                        continue;
 
406
                                
 
407
                                if (first == null)
 
408
                                        first = puzzle;
 
409
 
 
410
                                if ((puzzle.GameDifficulty & difficulty) == difficulty)
 
411
                                        break;
 
412
                        }
 
413
 
 
414
                        puzzle.CurrentDifficulty = Difficulty;
 
415
                        return puzzle;
 
416
                }
 
417
 
 
418
        #if _PDF_
 
419
                // Generates a single PDF document with all the puzzles contained in gbrainy (4 games per page)
 
420
                public void GeneratePDF ()
 
421
                {
 
422
                        int width = 400, height = 400, margin = 20, x, y, cnt, games_page = 4;
 
423
                        Game puzzle;
 
424
                        game_type = GameSession.Types.AllGames;
 
425
                        Type [] allgames = CustomGames;
 
426
                
 
427
                        for (int i = 0; i < allgames.Length; i++)
 
428
                                games.Add (allgames [i]);
 
429
 
 
430
                        PdfSurface pdf = new PdfSurface ("games.pdf", (width + margin) * 2, (height + margin) * games_page / 2);
 
431
                        x = y = cnt = 0;
 
432
                        CairoContextEx cr = new CairoContextEx (pdf);
 
433
                        for (int game = 0; game < games.Count; game++)
 
434
                        {
 
435
                                puzzle =  (Game) Activator.CreateInstance ((Type) games [game], true);
 
436
                                puzzle.Initialize ();
 
437
                                cnt++;
 
438
                                cr.Save ();
 
439
                                cr.Translate (x, y);
 
440
                                cr.Rectangle (0, 0, width, height);;    
 
441
                                cr.Clip ();
 
442
                                cr.Save ();
 
443
                                puzzle.DrawPreview (cr, width, height);
 
444
                                x += width + margin;
 
445
                                if (x > width + margin) {
 
446
                                        x = 0;
 
447
                                        y += height + margin;
 
448
                                }
 
449
                                cr.Restore ();
 
450
                                cr.MoveTo (50,  height - 10);
 
451
                                cr.ShowText (String.Format ("Game: {0} / D:{1}", puzzle.Name, puzzle.GameDifficulty));
 
452
                                cr.Stroke ();
 
453
                                cr.Restore ();
 
454
 
 
455
                                if (cnt >= games_page) {
 
456
                                        cr.ShowPage ();
 
457
                                        cnt = x = y = 0;
 
458
                                }
 
459
                        }
 
460
                        pdf.Finish ();
 
461
                        ((IDisposable)cr).Dispose();
 
462
                        return;
 
463
                }
 
464
        #endif
 
465
        }
 
466
}