PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/Hnefatafl/Controllers/SelectVariationController.cs

https://bitbucket.org/trisi/hnefatafl-monotouch
C# | 150 lines | 129 code | 21 blank | 0 comment | 14 complexity | 930301fe9034f7fbad5457c013eb1702 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using MonoTouch.Foundation;
  5using MonoTouch.UIKit;
  6using MonoTouch.GameKit;
  7using Core;
  8
  9namespace Hnefatafl
 10{
 11	public class SelectVariationController : UIViewController
 12	{
 13		SelectVariationView view;
 14		GameController gameController;
 15		Game game;
 16		GameType gameType;
 17		NSUserDefaults defaults;
 18		bool isTablet;
 19
 20		public SelectVariationController (GameType gameType)
 21		{
 22			this.gameType = gameType;
 23			defaults = NSUserDefaults.StandardUserDefaults;
 24			isTablet = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad;
 25		}
 26
 27		public void btnHnefatafl_Pushed ()
 28		{
 29			game = new Game (Variation.Hnefatafl, view.swWeak.On, isTablet);
 30
 31			switch (gameType) {
 32			case GameType.Asynchronous:
 33				GCHelper.findMatchWithViewController (this, Variation.Hnefatafl, false, true, 
 34				                                      view.swWeak.On);
 35				break;
 36			case GameType.PassNPlay:
 37				if (defaults.DataForKey ("pnpHnefatafl") != null)
 38					game = (Game) GCHelper.ByteArrayToObject (defaults.DataForKey ("pnpHnefatafl").
 39					                                          ToArray ());
 40				addSubview ();
 41				break;
 42			default:
 43				break;
 44			}
 45		}
 46
 47		public void btnArdRi_Pushed ()
 48		{
 49			game = new Game (Variation.ArdRi, view.swWeak.On, isTablet);
 50			
 51			switch (gameType) {
 52			case GameType.Asynchronous:
 53				GCHelper.findMatchWithViewController (this, Variation.Hnefatafl, true, true, 
 54				                                      view.swWeak.On);
 55				break;
 56			case GameType.PassNPlay:
 57				if (defaults.DataForKey ("pnpArdRi") != null)
 58					game = (Game) GCHelper.ByteArrayToObject (defaults.DataForKey ("pnpArdRi").
 59					                                          ToArray ());
 60				addSubview ();
 61				break;
 62			default:
 63				break;
 64			}
 65		}
 66
 67		public void btnTablut_Pushed ()
 68		{
 69			game = new Game (Variation.Tablut, view.swWeak.On, isTablet);
 70			
 71			switch (gameType) {
 72			case GameType.Asynchronous:
 73				GCHelper.findMatchWithViewController (this, Variation.Hnefatafl, false, true, 
 74				                                      view.swWeak.On);
 75				break;
 76			case GameType.PassNPlay:
 77				if (defaults.DataForKey ("pnpTablut") != null)
 78					game = (Game) GCHelper.ByteArrayToObject (defaults.DataForKey ("pnpTablut").
 79					                                          ToArray ());
 80				addSubview ();
 81				break;
 82			default:
 83				break;
 84			}
 85		}
 86
 87		public void btnMenu_Pushed ()
 88		{
 89			NavigationController.PopViewControllerAnimated (true);
 90		}
 91
 92		void addSubview ()
 93		{
 94			if (gameType == GameType.PassNPlay)
 95				gameController = new PassNPlayController (game);
 96			else
 97				gameController = new OnlineController (game);
 98
 99			NavigationController.PushViewController (gameController, true);
100		}
101
102		public void EnterNewGame (GKTurnBasedMatch match)
103		{
104			GCHelper.LoadPlayersForCurrentMatch ();
105			addSubview ();
106		}
107
108		public void TakeTurn (GKTurnBasedMatch match)
109		{
110			byte[] dataBytes = match.MatchData.ToArray ();
111			byte[] decompressed = GCHelper.Decompress (dataBytes);
112			game = (Game) GCHelper.ByteArrayToObject (decompressed);
113
114			if (game != null) {
115				GCHelper.LoadPlayersForCurrentMatch ();
116				addSubview ();
117			}
118		}
119
120		public void SendNotice (string notice, GKTurnBasedMatch match)
121		{
122			GKNotificationBanner.Show (notice, match.Message, null);
123		}
124
125		public override void LoadView()
126		{
127			view = new SelectVariationView ();
128
129			view.btnHnefatafl_Pushed = btnHnefatafl_Pushed;
130			view.btnArdRi_Pushed = btnArdRi_Pushed;
131			view.btnTablut_Pushed = btnTablut_Pushed;
132			view.btnMenu_Pushed = btnMenu_Pushed;
133
134			View = view;
135		}
136		
137		public override void ViewWillAppear (bool animated)
138		{
139			NavigationController.NavigationBar.Hidden = true;
140		}
141
142		public override void ViewDidLoad ()
143		{
144			GCHelper.EnterNewGame = EnterNewGame;
145			GCHelper.TakeTurn = TakeTurn;
146			GCHelper.SendNotice = SendNotice;
147		}
148	}
149}
150