/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

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