UIUtility.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //-----------------------------------------------------------------------------
  2. // UIUtilty.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 System.Text;
  10. namespace CardsFramework
  11. {
  12. public static class UIUtility
  13. {
  14. /// <summary>
  15. /// Indicates if the game is running on a mobile platform.
  16. /// </summary>
  17. public readonly static bool IsMobile = OperatingSystem.IsAndroid() || OperatingSystem.IsIOS();
  18. /// <summary>
  19. /// Indicates if the game is running on a desktop platform.
  20. /// </summary>
  21. public readonly static bool IsDesktop = OperatingSystem.IsMacOS() || OperatingSystem.IsLinux() || OperatingSystem.IsWindows();
  22. /// <summary>
  23. /// Gets the name of a card asset.
  24. /// </summary>
  25. /// <param name="card">The card type for which to get the asset name.</param>
  26. /// <returns>The card's asset name.</returns>
  27. public static string GetCardAssetName(TraditionalCard card)
  28. {
  29. return string.Format("{0}{1}",
  30. ((card.Value | CardValue.FirstJoker) ==
  31. CardValue.FirstJoker ||
  32. (card.Value | CardValue.SecondJoker) ==
  33. CardValue.SecondJoker) ?
  34. "" : card.Type.ToString(), card.Value);
  35. }
  36. /// <summary>
  37. /// Strips the GUID suffix from player names for display purposes.
  38. /// Network player names are in format "PlayerName_12345678" for unique identification,
  39. /// but we only want to show "PlayerName" to the user.
  40. /// </summary>
  41. /// <param name="name">The full player name with potential GUID suffix</param>
  42. /// <returns>The display name without GUID suffix</returns>
  43. public static string StripGuidSuffix(string name)
  44. {
  45. if (string.IsNullOrEmpty(name))
  46. return name;
  47. // Check if name contains underscore followed by 8 hex characters (GUID prefix)
  48. int underscoreIndex = name.LastIndexOf('_');
  49. if (underscoreIndex > 0 && underscoreIndex < name.Length - 1)
  50. {
  51. string suffix = name.Substring(underscoreIndex + 1);
  52. // Verify it looks like a GUID prefix (8 hex characters)
  53. if (suffix.Length >= 8 && IsHexString(suffix.Substring(0, 8)))
  54. {
  55. return name.Substring(0, underscoreIndex);
  56. }
  57. }
  58. return name;
  59. }
  60. /// <summary>
  61. /// Checks if a string contains only hexadecimal characters
  62. /// </summary>
  63. private static bool IsHexString(string str)
  64. {
  65. foreach (char c in str)
  66. {
  67. if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
  68. return false;
  69. }
  70. return true;
  71. }
  72. }
  73. }