CreateOrFindSessionScreen.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. using System.Threading.Tasks;
  13. namespace NetworkStateManagement
  14. {
  15. /// <summary>
  16. /// This menu screen lets the user choose whether to create a new
  17. /// network session, or search for an existing session to join.
  18. /// </summary>
  19. class CreateOrFindSessionScreen : MenuScreen
  20. {
  21. NetworkSessionType sessionType;
  22. /// <summary>
  23. /// Constructor fills in the menu contents.
  24. /// </summary>
  25. public CreateOrFindSessionScreen(NetworkSessionType sessionType)
  26. : base(GetMenuTitle(sessionType))
  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, ControllingPlayer.Value);
  66. // Begin an asynchronous create network session operation.
  67. var networkSession = NetworkSession.CreateAsync(
  68. sessionType,
  69. NetworkSessionComponent.MaxLocalGamers,
  70. NetworkSessionComponent.MaxGamers,
  71. 1,
  72. null);
  73. // Activate the network busy screen, which will display
  74. // an animation until this operation has completed.
  75. NetworkBusyScreen<NetworkSession> busyScreen = new NetworkBusyScreen<NetworkSession>(networkSession);
  76. busyScreen.OperationCompleted += CreateSessionOperationCompleted;
  77. ScreenManager.AddScreen(busyScreen, ControllingPlayer);
  78. }
  79. catch (Exception exception)
  80. {
  81. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  82. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  83. }
  84. }
  85. enum SessionProperty { GameMode, SkillLevel, ScoreToWin }
  86. enum GameMode { Practice, Timed, CaptureTheFlag }
  87. enum SkillLevel { Beginner, Intermediate, Advanced }
  88. /// <summary>
  89. /// Event handler for when the asynchronous create network session
  90. /// operation has completed.
  91. /// </summary>
  92. void CreateSessionOperationCompleted(object sender,
  93. OperationCompletedEventArgs e)
  94. {
  95. try
  96. {
  97. // Use the result directly from the event args.
  98. NetworkSession networkSession = e.Result as NetworkSession;
  99. if (networkSession == null)
  100. throw new InvalidOperationException("NetworkSession result was null or invalid.");
  101. // Create a component that will manage the session we just created.
  102. NetworkSessionComponent.Create(ScreenManager, networkSession);
  103. // Go to the lobby screen. We pass null as the controlling player,
  104. // because the lobby screen accepts input from all local players
  105. // who are in the session, not just a single controlling player.
  106. ScreenManager.AddScreen(new LobbyScreen(networkSession), null);
  107. }
  108. catch (Exception exception)
  109. {
  110. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  111. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  112. }
  113. }
  114. /// <summary>
  115. /// Event handler for when the Find Sessions menu entry is selected.
  116. /// </summary>
  117. void FindSessionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
  118. {
  119. try
  120. {
  121. // Which local profiles should we include in this session?
  122. IEnumerable<SignedInGamer> localGamers = NetworkSessionComponent.ChooseGamers(sessionType, ControllingPlayer.Value);
  123. // Begin an asynchronous find network sessions operation.
  124. var availableNetworkSessions = NetworkSession.FindAsync(
  125. sessionType,
  126. NetworkSessionComponent.MaxLocalGamers, null);
  127. // Activate the network busy screen, which will display
  128. // an animation until this operation has completed.
  129. NetworkBusyScreen<AvailableNetworkSessionCollection> busyScreen = new NetworkBusyScreen<AvailableNetworkSessionCollection>(availableNetworkSessions);
  130. busyScreen.OperationCompleted += FindSessionsOperationCompleted;
  131. ScreenManager.AddScreen(busyScreen, ControllingPlayer);
  132. }
  133. catch (Exception exception)
  134. {
  135. NetworkErrorScreen errorScreen = new NetworkErrorScreen(exception);
  136. ScreenManager.AddScreen(errorScreen, ControllingPlayer);
  137. }
  138. }
  139. /// <summary>
  140. /// Event handler for when the asynchronous find network sessions
  141. /// operation has completed.
  142. /// </summary>
  143. void FindSessionsOperationCompleted(object sender, OperationCompletedEventArgs e)
  144. {
  145. GameScreen nextScreen;
  146. try
  147. {
  148. // Use the result directly from the event args.
  149. AvailableNetworkSessionCollection availableSessions = e.Result as AvailableNetworkSessionCollection;
  150. if (availableSessions == null)
  151. throw new InvalidOperationException("AvailableNetworkSessionCollection result was null or invalid.");
  152. if (availableSessions.Count == 0)
  153. {
  154. // If we didn't find any sessions, display an error.
  155. availableSessions.Dispose();
  156. nextScreen = new MessageBoxScreen(Resources.NoSessionsFound, false);
  157. }
  158. else
  159. {
  160. // If we did find some sessions, proceed to the JoinSessionScreen.
  161. nextScreen = new JoinSessionScreen(availableSessions);
  162. }
  163. }
  164. catch (Exception exception)
  165. {
  166. nextScreen = new NetworkErrorScreen(exception);
  167. }
  168. ScreenManager.AddScreen(nextScreen, ControllingPlayer);
  169. }
  170. }
  171. }