Beispiel #1
0
        public static IActivity ToEntity(this API.Game model)
        {
            // Spotify Game
            if (model.SyncId.IsSpecified)
            {
                var    assets     = model.Assets.GetValueOrDefault()?.ToEntity();
                string albumText  = assets?[1]?.Text;
                string albumArtId = assets?[1]?.ImageId?.Replace("spotify:", "");
                var    timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null;
                return(new SpotifyGame
                {
                    Name = model.Name,
                    SessionId = model.SessionId.GetValueOrDefault(),
                    TrackId = model.SyncId.Value,
                    TrackUrl = CDN.GetSpotifyDirectUrl(model.SyncId.Value),
                    AlbumTitle = albumText,
                    TrackTitle = model.Details.GetValueOrDefault(),
                    Artists = model.State.GetValueOrDefault()?.Split(';').Select(x => x?.Trim()).ToImmutableArray(),
                    Duration = timestamps?.End - timestamps?.Start,
                    AlbumArtUrl = albumArtId != null?CDN.GetSpotifyAlbumArtUrl(albumArtId) : null,
                                      Type = ActivityType.Listening,
                                      Flags = model.Flags.GetValueOrDefault(),
                });
            }

            // Rich Game
            if (model.ApplicationId.IsSpecified)
            {
                ulong appId  = model.ApplicationId.Value;
                var   assets = model.Assets.GetValueOrDefault()?.ToEntity(appId);
                return(new RichGame
                {
                    ApplicationId = appId,
                    Name = model.Name,
                    Details = model.Details.GetValueOrDefault(),
                    State = model.State.GetValueOrDefault(),
                    SmallAsset = assets?[0],
                    LargeAsset = assets?[1],
                    Party = model.Party.IsSpecified ? model.Party.Value.ToEntity() : null,
                    Secrets = model.Secrets.IsSpecified ? model.Secrets.Value.ToEntity() : null,
                    Timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null,
                    Flags = model.Flags.GetValueOrDefault()
                });
            }
            // Stream Game
            if (model.StreamUrl.IsSpecified)
            {
                return(new StreamingGame(
                           model.Name,
                           model.StreamUrl.Value)
                {
                    Flags = model.Flags.GetValueOrDefault(),
                    Details = model.Details.GetValueOrDefault()
                });
            }
            // Normal Game
            return(new Game(model.Name, model.Type.GetValueOrDefault() ?? ActivityType.Playing,
                            model.Flags.IsSpecified ? model.Flags.Value : ActivityProperties.None,
                            model.Details.GetValueOrDefault()));
        }
        public static IActivity ToEntity(this API.Game model)
        {
            #region  Custom Status Game
            if (model.Id.IsSpecified && model.Id.Value == "custom")
            {
                return(new CustomStatusGame()
                {
                    Type = ActivityType.CustomStatus,
                    Name = model.Name,
                    State = model.State.IsSpecified ? model.State.Value : null,
                    Emote = model.Emoji.IsSpecified ? model.Emoji.Value.ToIEmote() : null,
                    CreatedAt = DateTimeOffset.FromUnixTimeMilliseconds(model.CreatedAt.Value),
                });
            }
            #endregion

            #region Spotify Game
            if (model.SyncId.IsSpecified)
            {
                var    assets     = model.Assets.GetValueOrDefault()?.ToEntity();
                string albumText  = assets?[1]?.Text;
                string albumArtId = assets?[1]?.ImageId?.Replace("spotify:", "");
                var    timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null;
                return(new SpotifyGame
                {
                    Name = model.Name,
                    SessionId = model.SessionId.GetValueOrDefault(),
                    TrackId = model.SyncId.Value,
                    TrackUrl = CDN.GetSpotifyDirectUrl(model.SyncId.Value),
                    AlbumTitle = albumText,
                    TrackTitle = model.Details.GetValueOrDefault(),
                    Artists = model.State.GetValueOrDefault()?.Split(';').Select(x => x?.Trim()).ToImmutableArray(),
                    StartedAt = timestamps?.Start,
                    EndsAt = timestamps?.End,
                    Duration = timestamps?.End - timestamps?.Start,
                    AlbumArtUrl = albumArtId != null?CDN.GetSpotifyAlbumArtUrl(albumArtId) : null,
                                      Type = ActivityType.Listening,
                                      Flags = model.Flags.GetValueOrDefault(),
                });
            }
            #endregion

            #region Rich Game
            if (model.ApplicationId.IsSpecified)
            {
                ulong appId  = model.ApplicationId.Value;
                var   assets = model.Assets.GetValueOrDefault()?.ToEntity(appId);
                return(new RichGame
                {
                    ApplicationId = appId,
                    Name = model.Name,
                    Details = model.Details.GetValueOrDefault(),
                    State = model.State.GetValueOrDefault(),
                    SmallAsset = assets?[0],
                    LargeAsset = assets?[1],
                    Party = model.Party.IsSpecified ? model.Party.Value.ToEntity() : null,
                    Secrets = model.Secrets.IsSpecified ? model.Secrets.Value.ToEntity() : null,
                    Timestamps = model.Timestamps.IsSpecified ? model.Timestamps.Value.ToEntity() : null,
                    Flags = model.Flags.GetValueOrDefault()
                });
            }
            #endregion

            #region  Stream Game
            if (model.StreamUrl.IsSpecified)
            {
                return(new StreamingGame(
                           model.Name,
                           model.StreamUrl.Value)
                {
                    Flags = model.Flags.GetValueOrDefault(),
                    Details = model.Details.GetValueOrDefault()
                });
            }
            #endregion

            #region  Normal Game
            return(new Game(model.Name, model.Type.GetValueOrDefault() ?? ActivityType.Playing,
                            model.Flags.IsSpecified ? model.Flags.Value : ActivityProperties.None,
                            model.Details.GetValueOrDefault()));

            #endregion
        }