AvailableSessionMenuEntry.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // AvailableSessionMenuEntry.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.Net;
  13. using GameStateManagement;
  14. #endregion
  15. namespace CatapultGame
  16. {
  17. /// <summary>
  18. /// Helper class customizes the standard MenuEntry class
  19. /// for displaying AvailableNetworkSession objects.
  20. /// </summary>
  21. class AvailableSessionMenuEntry : MenuEntry
  22. {
  23. #region Fields
  24. AvailableNetworkSession availableSession;
  25. bool gotQualityOfService;
  26. #endregion
  27. #region Properties
  28. /// <summary>
  29. /// Gets the available network session corresponding to this menu entry.
  30. /// </summary>
  31. public AvailableNetworkSession AvailableSession
  32. {
  33. get { return availableSession; }
  34. }
  35. #endregion
  36. #region Initialization
  37. /// <summary>
  38. /// Constructs a menu entry describing an available network session.
  39. /// </summary>
  40. public AvailableSessionMenuEntry(AvailableNetworkSession availableSession)
  41. : base(GetMenuItemText(availableSession))
  42. {
  43. this.availableSession = availableSession;
  44. }
  45. /// <summary>
  46. /// Formats session information to create the menu text string.
  47. /// </summary>
  48. static string GetMenuItemText(AvailableNetworkSession session)
  49. {
  50. int totalSlots = session.CurrentGamerCount +
  51. session.OpenPublicGamerSlots;
  52. return string.Format("{0} ({1}/{2})", session.HostGamertag,
  53. session.CurrentGamerCount,
  54. totalSlots);
  55. }
  56. #endregion
  57. #region Update
  58. /// <summary>
  59. /// Updates the menu item text, adding information about the network
  60. /// quality of service as soon as that becomes available.
  61. /// </summary>
  62. public override void Update(MenuScreen screen, bool isSelected,
  63. GameTime gameTime)
  64. {
  65. base.Update(screen, isSelected, gameTime);
  66. // Quality of service data can take some time to query, so it will not
  67. // be filled in straight away when NetworkSession.Find returns. We want
  68. // to display the list of available sessions straight away, and then
  69. // fill in the quality of service data whenever that becomes available,
  70. // so we keep checking until this data shows up.
  71. if (screen.IsActive && !gotQualityOfService)
  72. {
  73. QualityOfService qualityOfService = availableSession.QualityOfService;
  74. if (qualityOfService.IsAvailable)
  75. {
  76. TimeSpan pingTime = qualityOfService.AverageRoundtripTime;
  77. Text += string.Format(" - {0:0} ms", pingTime.TotalMilliseconds);
  78. gotQualityOfService = true;
  79. }
  80. }
  81. }
  82. #endregion
  83. }
  84. }