You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

EntityExtensions.cs 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections.Immutable;
  2. using System.Linq;
  3. namespace Discord.WebSocket
  4. {
  5. internal static class EntityExtensions
  6. {
  7. public static IActivity ToEntity(this API.Game model)
  8. {
  9. // Spotify Game
  10. if (model.SyncId.IsSpecified)
  11. {
  12. var assets = model.Assets.GetValueOrDefault()?.ToEntity();
  13. string albumText = assets?[1]?.Text;
  14. string albumArtId = assets?[1]?.ImageId?.Replace("spotify:","");
  15. var timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null;
  16. return new SpotifyGame
  17. {
  18. Name = model.Name,
  19. SessionId = model.SessionId.GetValueOrDefault(),
  20. TrackId = model.SyncId.Value,
  21. TrackUrl = CDN.GetSpotifyDirectUrl(model.SyncId.Value),
  22. AlbumTitle = albumText,
  23. TrackTitle = model.Details.GetValueOrDefault(),
  24. Artists = model.State.GetValueOrDefault()?.Split(';').Select(x=>x?.Trim()).ToImmutableArray(),
  25. Duration = timestamps?.End - timestamps?.Start,
  26. AlbumArtUrl = albumArtId != null ? CDN.GetSpotifyAlbumArtUrl(albumArtId) : null,
  27. Type = ActivityType.Listening,
  28. Flags = model.Flags.GetValueOrDefault()
  29. };
  30. }
  31. // Rich Game
  32. if (model.ApplicationId.IsSpecified)
  33. {
  34. ulong appId = model.ApplicationId.Value;
  35. var assets = model.Assets.GetValueOrDefault()?.ToEntity(appId);
  36. return new RichGame
  37. {
  38. ApplicationId = appId,
  39. Name = model.Name,
  40. Details = model.Details.GetValueOrDefault(),
  41. State = model.State.GetValueOrDefault(),
  42. SmallAsset = assets?[0],
  43. LargeAsset = assets?[1],
  44. Party = model.Party.IsSpecified ? model.Party.Value.ToEntity() : null,
  45. Secrets = model.Secrets.IsSpecified ? model.Secrets.Value.ToEntity() : null,
  46. Timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null,
  47. Flags = model.Flags.GetValueOrDefault()
  48. };
  49. }
  50. // Stream Game
  51. if (model.StreamUrl.IsSpecified)
  52. {
  53. return new StreamingGame(
  54. model.Name,
  55. model.StreamUrl.Value);
  56. }
  57. // Normal Game
  58. return new Game(model.Name, model.Type.GetValueOrDefault() ?? ActivityType.Playing);
  59. }
  60. // (Small, Large)
  61. public static GameAsset[] ToEntity(this API.GameAssets model, ulong? appId = null)
  62. {
  63. return new GameAsset[]
  64. {
  65. model.SmallImage.IsSpecified ? new GameAsset
  66. {
  67. ApplicationId = appId,
  68. ImageId = model.SmallImage.GetValueOrDefault(),
  69. Text = model.SmallText.GetValueOrDefault()
  70. } : null,
  71. model.LargeImage.IsSpecified ? new GameAsset
  72. {
  73. ApplicationId = appId,
  74. ImageId = model.LargeImage.GetValueOrDefault(),
  75. Text = model.LargeText.GetValueOrDefault()
  76. } : null,
  77. };
  78. }
  79. public static GameParty ToEntity(this API.GameParty model)
  80. {
  81. // Discord will probably send bad data since they don't validate anything
  82. long current = 0, cap = 0;
  83. if (model.Size?.Length == 2)
  84. {
  85. current = model.Size[0];
  86. cap = model.Size[1];
  87. }
  88. return new GameParty
  89. {
  90. Id = model.Id,
  91. Members = current,
  92. Capacity = cap,
  93. };
  94. }
  95. public static GameSecrets ToEntity(this API.GameSecrets model)
  96. {
  97. return new GameSecrets(model.Match, model.Join, model.Spectate);
  98. }
  99. public static GameTimestamps ToEntity(this API.GameTimestamps model)
  100. {
  101. return new GameTimestamps(model.Start.ToNullable(), model.End.ToNullable());
  102. }
  103. }
  104. }