SearchResultsScreen.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SearchResultsScreen.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 Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.GamerServices;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Net;
  15. #endregion
  16. namespace NetRumble
  17. {
  18. /// <summary>
  19. /// The search-results screen shows the results of a network-session
  20. /// search, allowing the player to pick the game to join.
  21. /// </summary>
  22. public class SearchResultsScreen : MenuScreen
  23. {
  24. #region Constants
  25. /// <summary>
  26. /// The maximum number of session results to display.
  27. /// </summary>
  28. const int maximumSessions = 8;
  29. #endregion
  30. #region Networking Data
  31. /// <summary>
  32. /// The type of networking session that was requested.
  33. /// </summary>
  34. private NetworkSessionType sessionType;
  35. /// <summary>
  36. /// The collection of search results.
  37. /// </summary>
  38. private AvailableNetworkSessionCollection availableSessions = null;
  39. #endregion
  40. #region Initialization
  41. /// <summary>
  42. /// Constructor fills in the menu contents.
  43. /// </summary>
  44. /// <param name="sessionType">The type of session searched for.</param>
  45. public SearchResultsScreen(NetworkSessionType sessionType) : base()
  46. {
  47. // apply the parameters
  48. this.sessionType = sessionType;
  49. // set the transition times
  50. TransitionOnTime = TimeSpan.FromSeconds(1.0);
  51. TransitionOffTime = TimeSpan.FromSeconds(0.0);
  52. }
  53. #endregion
  54. #region Updating Methods
  55. /// <summary>
  56. /// Updates the screen. This method checks the GameScreen.IsActive
  57. /// property, so the game will stop updating when the pause menu is active,
  58. /// or if you tab away to a different application.
  59. /// </summary>
  60. public override void Update(Microsoft.Xna.Framework.GameTime gameTime,
  61. bool otherScreenHasFocus, bool coveredByOtherScreen)
  62. {
  63. bool signedIntoLive = false;
  64. if (Gamer.SignedInGamers.Count > 0)
  65. {
  66. foreach (SignedInGamer signedInGamer in Gamer.SignedInGamers)
  67. {
  68. if (signedInGamer.IsSignedInToLive)
  69. {
  70. signedIntoLive = true;
  71. break;
  72. }
  73. }
  74. if (!signedIntoLive &&
  75. ((sessionType == NetworkSessionType.PlayerMatch) ||
  76. (sessionType == NetworkSessionType.Ranked)) && !IsExiting)
  77. {
  78. ExitScreen();
  79. }
  80. }
  81. else if (!IsExiting)
  82. {
  83. ExitScreen();
  84. }
  85. if (coveredByOtherScreen && !IsExiting)
  86. {
  87. ExitScreen();
  88. }
  89. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  90. }
  91. /// <summary>
  92. /// Responds to user menu selections.
  93. /// </summary>
  94. protected override void OnSelectEntry(int entryIndex)
  95. {
  96. if ((availableSessions != null) && (entryIndex >= 0) &&
  97. (entryIndex < availableSessions.Count))
  98. {
  99. // start to join
  100. try
  101. {
  102. IAsyncResult asyncResult = NetworkSession.BeginJoin(
  103. availableSessions[entryIndex], null, null);
  104. // create the busy screen
  105. NetworkBusyScreen busyScreen = new NetworkBusyScreen(
  106. "Joining the session...", asyncResult);
  107. busyScreen.OperationCompleted += LoadLobbyScreen;
  108. ScreenManager.AddScreen(busyScreen);
  109. }
  110. catch (NetworkException ne)
  111. {
  112. const string message = "Failed joining the session.";
  113. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  114. messageBox.Accepted += FailedMessageBox;
  115. messageBox.Cancelled += FailedMessageBox;
  116. ScreenManager.AddScreen(messageBox);
  117. System.Console.WriteLine("Failed to join session: " +
  118. ne.Message);
  119. }
  120. catch (GamerPrivilegeException gpe)
  121. {
  122. const string message =
  123. "You do not have permission to join a session.";
  124. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  125. messageBox.Accepted += FailedMessageBox;
  126. messageBox.Cancelled += FailedMessageBox;
  127. ScreenManager.AddScreen(messageBox);
  128. System.Console.WriteLine(
  129. "Insufficient privilege to join session: " + gpe.Message);
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// When the user cancels the screen.
  135. /// </summary>
  136. protected override void OnCancel()
  137. {
  138. if (availableSessions != null)
  139. {
  140. ExitScreen();
  141. }
  142. }
  143. #endregion
  144. #region Drawing Methods
  145. /// <summary>
  146. /// Draw the screen.
  147. /// </summary>
  148. public override void Draw(GameTime gameTime)
  149. {
  150. string alternateString = String.Empty;
  151. // set an alternate string if there are no search results yet
  152. if (availableSessions == null)
  153. {
  154. alternateString = "Searching...";
  155. }
  156. else if (availableSessions.Count <= 0)
  157. {
  158. alternateString = "No sessions found.";
  159. }
  160. if (String.IsNullOrEmpty(alternateString))
  161. {
  162. base.Draw(gameTime);
  163. }
  164. else
  165. {
  166. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  167. Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
  168. Vector2 position = new Vector2(0f, viewportSize.Y * 0.65f);
  169. // Make the menu slide into place during transitions, using a
  170. // power curve to make things look more interesting (this makes
  171. // the movement slow down as it nears the end).
  172. float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
  173. if (ScreenState == ScreenState.TransitionOn)
  174. position.Y += transitionOffset * 256;
  175. else
  176. position.Y += transitionOffset * 512;
  177. // Draw each menu entry in turn.
  178. ScreenManager.SpriteBatch.Begin();
  179. Vector2 origin = new Vector2(0, ScreenManager.Font.LineSpacing / 2);
  180. Vector2 size = ScreenManager.Font.MeasureString(alternateString);
  181. position.X = viewportSize.X / 2f - size.X / 2f;
  182. ScreenManager.SpriteBatch.DrawString(ScreenManager.Font,
  183. alternateString, position,
  184. Color.White, 0, origin, 1.0f,
  185. SpriteEffects.None, 0);
  186. ScreenManager.SpriteBatch.End();
  187. }
  188. }
  189. #endregion
  190. #region Networking Methods
  191. /// <summary>
  192. /// Callback to receive the network-session search results.
  193. /// </summary>
  194. internal void SessionsFound(object sender, OperationCompletedEventArgs e)
  195. {
  196. try
  197. {
  198. availableSessions = NetworkSession.EndFind(e.AsyncResult);
  199. }
  200. catch (NetworkException ne)
  201. {
  202. const string message = "Failed searching for the session.";
  203. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  204. messageBox.Accepted += FailedMessageBox;
  205. messageBox.Cancelled += FailedMessageBox;
  206. ScreenManager.AddScreen(messageBox);
  207. System.Console.WriteLine("Failed to search for session: " +
  208. ne.Message);
  209. }
  210. catch (GamerPrivilegeException gpe)
  211. {
  212. const string message =
  213. "You do not have permission to search for a session. ";
  214. MessageBoxScreen messageBox = new MessageBoxScreen(message + gpe.Message);
  215. messageBox.Accepted += FailedMessageBox;
  216. messageBox.Cancelled += FailedMessageBox;
  217. ScreenManager.AddScreen(messageBox);
  218. System.Console.WriteLine(
  219. "Insufficient privilege to search for session: " + gpe.Message);
  220. }
  221. MenuEntries.Clear();
  222. if (availableSessions != null)
  223. {
  224. foreach (AvailableNetworkSession availableSession in
  225. availableSessions)
  226. {
  227. if (availableSession.CurrentGamerCount < World.MaximumPlayers)
  228. {
  229. MenuEntries.Add(availableSession.HostGamertag + " (" +
  230. availableSession.CurrentGamerCount.ToString() + "/" +
  231. World.MaximumPlayers.ToString() + ")");
  232. }
  233. if (MenuEntries.Count >= maximumSessions)
  234. {
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// Callback to load the lobby screen with the new session.
  242. /// </summary>
  243. private void LoadLobbyScreen(object sender, OperationCompletedEventArgs e)
  244. {
  245. NetworkSession networkSession = null;
  246. try
  247. {
  248. networkSession = NetworkSession.EndJoin(e.AsyncResult);
  249. }
  250. catch (NetworkException ne)
  251. {
  252. const string message = "Failed joining session.";
  253. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  254. messageBox.Accepted += FailedMessageBox;
  255. messageBox.Cancelled += FailedMessageBox;
  256. ScreenManager.AddScreen(messageBox);
  257. System.Console.WriteLine("Failed joining session: " + ne.Message);
  258. }
  259. catch (GamerPrivilegeException gpe)
  260. {
  261. const string message =
  262. "You do not have permission to join a session.";
  263. MessageBoxScreen messageBox = new MessageBoxScreen(message);
  264. messageBox.Accepted += FailedMessageBox;
  265. messageBox.Cancelled += FailedMessageBox;
  266. ScreenManager.AddScreen(messageBox);
  267. System.Console.WriteLine(
  268. "Insufficient privilege to join session: " + gpe.Message);
  269. }
  270. if (networkSession != null)
  271. {
  272. LobbyScreen lobbyScreen = new LobbyScreen(networkSession);
  273. lobbyScreen.ScreenManager = this.ScreenManager;
  274. ScreenManager.AddScreen(lobbyScreen);
  275. }
  276. }
  277. /// <summary>
  278. /// Event handler for when the user selects ok on the "are you sure
  279. /// you want to exit" message box.
  280. /// </summary>
  281. private void FailedMessageBox(object sender, EventArgs e)
  282. {
  283. ExitScreen();
  284. }
  285. #endregion
  286. }
  287. }