CreateOrFindSessionScreen.cs 6.2 KB

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