CreateOrFindSessionScreen.cs 6.9 KB

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