Added some direct listeners for logged in user

This commit is contained in:
Enrico Ludwig 2024-09-05 17:39:26 +02:00
parent 5786f65c26
commit 8489d0c25c
2 changed files with 77 additions and 14 deletions

View File

@ -18,6 +18,20 @@ public class TwitchChannel : INotifyPropertyChanged
private string? _category; private string? _category;
private string? _title; private string? _title;
private DateTime? _lastRaided; private DateTime? _lastRaided;
private string? _id;
public string? Id
{
get => _id;
set
{
if (value == _id)
return;
_id = value;
OnPropertyChanged();
}
}
public string? BroadcasterId public string? BroadcasterId
{ {
@ -151,7 +165,8 @@ public class TwitchChannel : INotifyPropertyChanged
var stream = service.TwitchApi.Helix.Streams.GetStreamsAsync(userLogins: [ Name ]).Result.Streams var stream = service.TwitchApi.Helix.Streams.GetStreamsAsync(userLogins: [ Name ]).Result.Streams
.FirstOrDefault(s => s.UserLogin.Equals(Name, StringComparison.CurrentCultureIgnoreCase)); .FirstOrDefault(s => s.UserLogin.Equals(Name, StringComparison.CurrentCultureIgnoreCase));
Id = channel.Id;
BroadcasterId = channel.Id; BroadcasterId = channel.Id;
DisplayName = channel.DisplayName; DisplayName = channel.DisplayName;
ThumbnailUrl = channel.ThumbnailUrl; ThumbnailUrl = channel.ThumbnailUrl;
@ -184,7 +199,6 @@ public class TwitchChannel : INotifyPropertyChanged
if (args.ChannelId != BroadcasterId) if (args.ChannelId != BroadcasterId)
return; return;
ViewerCount = $"{args.Viewers}"; ViewerCount = $"{args.Viewers}";
} }

View File

@ -38,6 +38,7 @@ public interface ITwitchService
public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INotifyPropertyChanging public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INotifyPropertyChanging
{ {
private bool _isRaidStarted; private bool _isRaidStarted;
private int _raidParticipants;
private TwitchChannel? _userChannel; private TwitchChannel? _userChannel;
private readonly List<TwitchChannel> _registeredChannels; private readonly List<TwitchChannel> _registeredChannels;
private User? _user; private User? _user;
@ -72,7 +73,13 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot
public TwitchAPI TwitchApi { get; } public TwitchAPI TwitchApi { get; }
public TwitchPubSub TwitchEvents { get; } public TwitchPubSub TwitchEvents { get; }
public int RaidParticipants
{
get => _raidParticipants;
set => SetField(ref _raidParticipants, value);
}
public TwitchService() public TwitchService()
{ {
_registeredChannels = []; _registeredChannels = [];
@ -100,20 +107,21 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot
Console.WriteLine($"[INFO][{nameof(TwitchService)}] Connecting to Twitch Events ..."); Console.WriteLine($"[INFO][{nameof(TwitchService)}] Connecting to Twitch Events ...");
TwitchEvents.OnRaidGo += OnUserRaidGo; TwitchEvents.OnRaidGo += OnUserRaidGo;
TwitchEvents.OnRaidUpdateV2 += OnUserRaidUpdate; TwitchEvents.OnRaidUpdate += OnUserRaidUpdate;
TwitchEvents.OnStreamUp += OnUserStreamUp; TwitchEvents.OnStreamUp += OnUserStreamUp;
TwitchEvents.OnStreamDown += OnUserStreamDown; TwitchEvents.OnStreamDown += OnUserStreamDown;
TwitchEvents.OnViewCount += OnViewCount;
TwitchEvents.OnLog += (sender, args) => Console.WriteLine($"[INFO][{nameof(TwitchService)}] {args.Data}");
TwitchEvents.OnPubSubServiceError += (sender, args) => Console.WriteLine($"[ERROR][{nameof(TwitchService)}] {args.Exception.Message}");
TwitchEvents.OnPubSubServiceConnected += (sender, args) => Console.WriteLine($"[INFO][{nameof(TwitchService)}] Connected to Twitch PubSub.");
TwitchEvents.OnPubSubServiceClosed += (sender, args) => Console.WriteLine($"[INFO][{nameof(TwitchService)}] Disconnected from Twitch PubSub.");
TwitchEvents.ListenToRaid(UserChannel.BroadcasterId);
TwitchEvents.ListenToVideoPlayback(UserChannel.BroadcasterId); TwitchEvents.ListenToVideoPlayback(UserChannel.BroadcasterId);
TwitchEvents.ListenToRaid(UserChannel.BroadcasterId);
TwitchEvents.SendTopics(token); TwitchEvents.SendTopics(token);
TwitchEvents.Connect(); TwitchEvents.Connect();
RegisterForEvents(UserChannel);
Console.WriteLine($"[INFO][{nameof(TwitchService)}] Connected to Twitch Events.");
await Task.CompletedTask; await Task.CompletedTask;
} }
@ -210,11 +218,13 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot
public void RegisterForEvents(TwitchChannel channel) public void RegisterForEvents(TwitchChannel channel)
{ {
Console.WriteLine($"[DEBUG][{nameof(TwitchService)}] Registering for events for {channel.Name} ...");
TwitchEvents.OnStreamUp += channel.OnStreamUp; TwitchEvents.OnStreamUp += channel.OnStreamUp;
TwitchEvents.OnStreamDown += channel.OnStreamDown; TwitchEvents.OnStreamDown += channel.OnStreamDown;
TwitchEvents.OnViewCount += channel.OnViewCount; TwitchEvents.OnViewCount += channel.OnViewCount;
TwitchEvents.ListenToVideoPlayback(channel.BroadcasterId); TwitchEvents.ListenToVideoPlayback(channel.Id);
TwitchEvents.SendTopics(AccessToken); TwitchEvents.SendTopics(AccessToken);
@ -227,7 +237,7 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot
TwitchEvents.OnStreamDown -= channel.OnStreamDown; TwitchEvents.OnStreamDown -= channel.OnStreamDown;
TwitchEvents.OnViewCount -= channel.OnViewCount; TwitchEvents.OnViewCount -= channel.OnViewCount;
TwitchEvents.ListenToVideoPlayback(channel.BroadcasterId); TwitchEvents.ListenToVideoPlayback(channel.Id);
TwitchEvents.SendTopics(AccessToken, true); TwitchEvents.SendTopics(AccessToken, true);
@ -247,8 +257,6 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot
public void StartRaid(string from, string to) public void StartRaid(string from, string to)
{ {
// TODO: Also check, if the logged in user is live
TwitchApi.Helix.Raids.StartRaidAsync(from, to); TwitchApi.Helix.Raids.StartRaidAsync(from, to);
IsRaidStarted = true; IsRaidStarted = true;
} }
@ -299,24 +307,65 @@ public sealed class TwitchService : ITwitchService, INotifyPropertyChanged, INot
Tools.OpenUrl(url); Tools.OpenUrl(url);
} }
private void OnUserRaidUpdate(object? sender, OnRaidUpdateV2Args e) // TODO Not called while raid is ongoing
private void OnUserRaidUpdate(object? sender, OnRaidUpdateArgs e)
{ {
//if (e.ChannelId != UserChannel?.BroadcasterId)
// return;
RaidParticipants = e.ViewerCount;
Console.WriteLine($"[INFO][{nameof(TwitchService)}] Raid participants: {RaidParticipants}");
}
private void OnViewCount(object? sender, OnViewCountArgs e)
{
if (UserChannel == null)
return;
if (e.ChannelId != UserChannel.Id)
return;
UserChannel.OnViewCount(sender, e);
} }
private void OnUserRaidGo(object? sender, OnRaidGoArgs e) private void OnUserRaidGo(object? sender, OnRaidGoArgs e)
{ {
if (e.ChannelId != UserChannel?.Id)
return;
Console.WriteLine($"[INFO][{nameof(TwitchService)}] Raid started.");
IsRaidStarted = false; IsRaidStarted = false;
} }
private void OnUserStreamDown(object? sender, OnStreamDownArgs e) private void OnUserStreamDown(object? sender, OnStreamDownArgs e)
{ {
if (UserChannel == null)
return;
if (e.ChannelId != UserChannel?.Id)
return;
Console.WriteLine($"[INFO][{nameof(TwitchService)}] Stream down.");
IsRaidStarted = false; IsRaidStarted = false;
UserChannel.IsLive = false;
} }
private void OnUserStreamUp(object? sender, OnStreamUpArgs e) private void OnUserStreamUp(object? sender, OnStreamUpArgs e)
{ {
if (UserChannel == null)
return;
if (e.ChannelId != UserChannel?.Id)
return;
Console.WriteLine($"[INFO][{nameof(TwitchService)}] Stream up.");
IsRaidStarted = false; IsRaidStarted = false;
UserChannel.IsLive = true;
} }
public event PropertyChangingEventHandler? PropertyChanging; public event PropertyChangingEventHandler? PropertyChanging;