CreateOrFindSessionScreen.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CreateOrFindSessionScreen.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Net;
  14. using Microsoft.Xna.Framework.GamerServices;
  15. #endregion
  16. namespace NetworkStateManagement
  17. {
  18. /// <summary>
  19. /// This menu screen lets the user choose whether to create a new
  20. /// network session, or search for an existing session to join.
  21. /// </summary>
  22. class CreateOrFindSessionScreen : MenuScreen
  23. {
  24. #region Fields
  25. NetworkSessionType sessionType;
  26. #endregion
  27. #region Initialization
  28. /// <summary>
  29. /// Constructor fills in the menu contents.
  30. /// </summary>
  31. public CreateOrFindSessionScreen (NetworkSessionType sessionType)
  32. : base(GetMenuTitle(sessionType))
  33. {
  34. this.sessionType = sessionType;
  35. // Create our menu entries.
  36. MenuEntry createSessionMenuEntry = new MenuEntry (Resources.CreateSession);
  37. MenuEntry findSessionsMenuEntry = new MenuEntry (Resources.FindSessions);
  38. MenuEntry backMenuEntry = new MenuEntry (Resources.Back);
  39. // Hook up menu event handlers.
  40. createSessionMenuEntry.Selected += CreateSessionMenuEntrySelected;
  41. findSessionsMenuEntry.Selected += FindSessionsMenuEntrySelected;
  42. backMenuEntry.Selected += OnCancel;
  43. // Add entries to the menu.
  44. MenuEntries.Add (createSessionMenuEntry);
  45. MenuEntries.Add (findSessionsMenuEntry);
  46. MenuEntries.Add (backMenuEntry);
  47. }
  48. /// <summary>
  49. /// Helper chooses an appropriate menu title for the specified session type.
  50. /// </summary>
  51. static string GetMenuTitle (NetworkSessionType sessionType)
  52. {
  53. switch (sessionType) {
  54. case NetworkSessionType.PlayerMatch:
  55. return Resources.PlayerMatch;
  56. case NetworkSessionType.SystemLink:
  57. return Resources.SystemLink;
  58. default:
  59. throw new NotSupportedException ();
  60. }
  61. }
  62. #endregion
  63. #region Event Handlers
  64. /// <summary>
  65. /// Event handler for when the Create Session menu entry is selected.
  66. /// </summary>
  67. void CreateSessionMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  68. {
  69. try {
  70. // Which local profiles should we include in this session?
  71. IEnumerable<SignedInGamer> localGamers =
  72. NetworkSessionComponent.ChooseGamers (sessionType,
  73. ControllingPlayer.Value);
  74. // Begin an asynchronous create network session operation.
  75. IAsyncResult asyncResult = NetworkSession.BeginCreate (
  76. sessionType, localGamers,
  77. NetworkSessionComponent.MaxGamers,
  78. 0, null, null, null);
  79. // Activate the network busy screen, which will display
  80. // an animation until this operation has completed.
  81. NetworkBusyScreen busyScreen = new NetworkBusyScreen (asyncResult);
  82. busyScreen.OperationCompleted += CreateSessionOperationCompleted;
  83. ScreenManager.AddScreen (busyScreen, ControllingPlayer);
  84. } catch (Exception exception) {
  85. NetworkErrorScreen errorScreen = new NetworkErrorScreen (exception);
  86. ScreenManager.AddScreen (errorScreen, ControllingPlayer);
  87. }
  88. }
  89. enum SessionProperty{ GameMode, SkillLevel, ScoreToWin }
  90. enum GameMode{ Practice, Timed, CaptureTheFlag }
  91. enum SkillLevel{ Beginner, Intermediate, Advanced }
  92. /// <summary>
  93. /// Event handler for when the asynchronous create network session
  94. /// operation has completed.
  95. /// </summary>
  96. void CreateSessionOperationCompleted (object sender,
  97. OperationCompletedEventArgs e)
  98. {
  99. try {
  100. // End the asynchronous create network session operation.
  101. NetworkSession networkSession = NetworkSession.EndCreate (e.AsyncResult);
  102. // Create a component that will manage the session we just created.
  103. NetworkSessionComponent.Create (ScreenManager, networkSession);
  104. // Go to the lobby screen. We pass null as the controlling player,
  105. // because the lobby screen accepts input from all local players
  106. // who are in the session, not just a single controlling player.
  107. ScreenManager.AddScreen (new LobbyScreen (networkSession), null);
  108. } catch (Exception exception) {
  109. NetworkErrorScreen errorScreen = new NetworkErrorScreen (exception);
  110. ScreenManager.AddScreen (errorScreen, ControllingPlayer);
  111. }
  112. }
  113. /// <summary>
  114. /// Event handler for when the Find Sessions menu entry is selected.
  115. /// </summary>
  116. void FindSessionsMenuEntrySelected (object sender, PlayerIndexEventArgs e)
  117. {
  118. try {
  119. // Which local profiles should we include in this session?
  120. IEnumerable<SignedInGamer> localGamers =
  121. NetworkSessionComponent.ChooseGamers (sessionType,
  122. ControllingPlayer.Value);
  123. // Begin an asynchronous find network sessions operation.
  124. IAsyncResult asyncResult = NetworkSession.BeginFind (sessionType,
  125. localGamers, null, null, null);
  126. // Activate the network busy screen, which will display
  127. // an animation until this operation has completed.
  128. NetworkBusyScreen busyScreen = new NetworkBusyScreen (asyncResult);
  129. busyScreen.OperationCompleted += FindSessionsOperationCompleted;
  130. ScreenManager.AddScreen (busyScreen, ControllingPlayer);
  131. } catch (Exception exception) {
  132. NetworkErrorScreen errorScreen = new NetworkErrorScreen (exception);
  133. ScreenManager.AddScreen (errorScreen, ControllingPlayer);
  134. }
  135. }
  136. /// <summary>
  137. /// Event handler for when the asynchronous find network sessions
  138. /// operation has completed.
  139. /// </summary>
  140. void FindSessionsOperationCompleted (object sender,
  141. OperationCompletedEventArgs e)
  142. {
  143. GameScreen nextScreen;
  144. try {
  145. // End the asynchronous find network sessions operation.
  146. AvailableNetworkSessionCollection availableSessions =
  147. NetworkSession.EndFind (e.AsyncResult);
  148. if (availableSessions.Count == 0) {
  149. // If we didn't find any sessions, display an error.
  150. availableSessions.Dispose ();
  151. nextScreen = new MessageBoxScreen (Resources.NoSessionsFound, false);
  152. } else {
  153. // If we did find some sessions, proceed to the JoinSessionScreen.
  154. nextScreen = new JoinSessionScreen (availableSessions);
  155. }
  156. } catch (Exception exception) {
  157. nextScreen = new NetworkErrorScreen (exception);
  158. }
  159. ScreenManager.AddScreen (nextScreen, ControllingPlayer);
  160. }
  161. #endregion
  162. }
  163. }