2
0

CreateOrFindSessionScreen.cs 6.6 KB

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