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 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. namespace Discord.WebSocket
  2. {
  3. internal static class EntityExtensions
  4. {
  5. public static IActivity ToEntity(this API.Game model)
  6. {
  7. // Rich Game
  8. if (model.ApplicationId.IsSpecified)
  9. {
  10. ulong appId = model.ApplicationId.Value;
  11. var assets = model.Assets.GetValueOrDefault()?.ToEntity(appId);
  12. return new RichGame
  13. {
  14. ApplicationId = appId,
  15. Name = model.Name,
  16. Details = model.Details.GetValueOrDefault(),
  17. State = model.State.GetValueOrDefault(),
  18. SmallAsset = assets?[0],
  19. LargeAsset = assets?[1],
  20. Party = model.Party.IsSpecified ? model.Party.Value.ToEntity() : null,
  21. Secrets = model.Secrets.IsSpecified ? model.Secrets.Value.ToEntity() : null,
  22. Timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null
  23. };
  24. }
  25. // Stream Game
  26. if (model.StreamUrl.IsSpecified)
  27. {
  28. return new StreamingGame(
  29. model.Name,
  30. model.StreamUrl.Value);
  31. }
  32. // Normal Game
  33. return new Game(model.Name, model.Type.GetValueOrDefault() ?? ActivityType.Playing);
  34. }
  35. // (Small, Large)
  36. public static GameAsset[] ToEntity(this API.GameAssets model, ulong appId)
  37. {
  38. return new GameAsset[]
  39. {
  40. model.SmallImage.IsSpecified ? new GameAsset
  41. {
  42. ApplicationId = appId,
  43. ImageId = model.SmallImage.GetValueOrDefault(),
  44. Text = model.SmallText.GetValueOrDefault()
  45. } : null,
  46. model.LargeImage.IsSpecified ? new GameAsset
  47. {
  48. ApplicationId = appId,
  49. ImageId = model.LargeImage.GetValueOrDefault(),
  50. Text = model.LargeText.GetValueOrDefault()
  51. } : null,
  52. };
  53. }
  54. public static GameParty ToEntity(this API.GameParty model)
  55. {
  56. // Discord will probably send bad data since they don't validate anything
  57. long current = 0, cap = 0;
  58. if (model.Size?.Length == 2)
  59. {
  60. current = model.Size[0];
  61. cap = model.Size[1];
  62. }
  63. return new GameParty
  64. {
  65. Id = model.Id,
  66. Members = current,
  67. Capacity = cap,
  68. };
  69. }
  70. public static GameSecrets ToEntity(this API.GameSecrets model)
  71. {
  72. return new GameSecrets(model.Match, model.Join, model.Spectate);
  73. }
  74. public static GameTimestamps ToEntity(this API.GameTimestamps model)
  75. {
  76. return new GameTimestamps(model.Start.ToNullable(), model.End.ToNullable());
  77. }
  78. }
  79. }