99using Microsoft . Extensions . Options ;
1010using System ;
1111using System . Collections . Generic ;
12+ using System . IO ;
1213using System . Linq ;
1314using System . Threading ;
1415using System . Threading . Tasks ;
@@ -21,50 +22,95 @@ public class TwitchPubSubService : IHostedService
2122 private IServiceProvider _ServiceProvider ;
2223 private CancellationToken _Token ;
2324 private Task _BackgroundTask ;
25+ private static string [ ] _OldManSoundFx ;
26+ public static short _OldManCount = 0 ;
2427 private readonly Twitch . PubSub . Proxy _Proxy ;
2528 private readonly ConfigurationSettings _Configuration ;
29+ private readonly IHostEnvironment _HostEnvironment ;
30+ private readonly Dictionary < string , Action < IHubContext < AttentionHub , IAttentionHubClient > > > _ChannelPointActions = new Dictionary < string , Action < IHubContext < AttentionHub , IAttentionHubClient > > > ( ) ;
2631
27- public TwitchPubSubService ( IServiceProvider serviceProvider , Twitch . PubSub . Proxy proxy , IOptions < ConfigurationSettings > settings )
32+ public TwitchPubSubService ( IServiceProvider serviceProvider , Twitch . PubSub . Proxy proxy , IHostEnvironment hostEnvironment , IOptions < ConfigurationSettings > settings )
2833 {
2934 _ServiceProvider = serviceProvider ;
3035 _Proxy = proxy ;
3136 _Configuration = settings . Value ;
37+ _HostEnvironment = hostEnvironment ;
38+
39+ InitializeChannelPointActions ( ) ;
40+
41+ }
42+
43+ private void InitializeChannelPointActions ( )
44+ {
45+ _ChannelPointActions . Add ( "get off my lawn!" , ( c ) => TwitchPubSubService . OldManDeveloper ( c ) ) ;
3246 }
3347
3448 public Task StartAsync ( CancellationToken cancellationToken )
3549 {
3650 _Proxy . OnChannelPointsRedeemed += _Proxy_OnChannelPointsRedeemed ;
3751 _Token = cancellationToken ;
3852 _BackgroundTask = _Proxy . StartAsync ( new TwitchTopic [ ] { TwitchTopic . ChannelPoints ( _Configuration . UserId ) } , _Token ) ;
53+ IdentifyOldManAudio ( ) ;
3954 return Task . CompletedTask ;
4055 }
4156
57+ private void IdentifyOldManAudio ( )
58+ {
59+
60+ var oldManPath = Path . Combine ( _HostEnvironment . ContentRootPath , "wwwroot" , "contents" , "oldman" ) ;
61+ var di = new DirectoryInfo ( oldManPath ) ;
62+ _OldManSoundFx = di . GetFiles ( ) . Select ( f => f . Name ) . OrderBy ( x => Guid . NewGuid ( ) ) . ToArray ( ) ;
63+
64+ }
65+
4266 private void _Proxy_OnChannelPointsRedeemed ( object sender , ChannelRedemption e )
4367 {
4468 using ( var scope = _ServiceProvider . CreateScope ( ) )
4569 {
4670 var context = scope . ServiceProvider . GetRequiredService < IHubContext < AttentionHub , IAttentionHubClient > > ( ) ;
47- //context.Clients.All.PlaySoundEffect("pointsredeemed.mp3");
48- var redemption = new ChannelPointRedemption
71+
72+ if ( _ChannelPointActions . ContainsKey ( e . redemption . reward . title . ToLowerInvariant ( ) ) ) {
73+ _ChannelPointActions [ e . redemption . reward . title . ToLowerInvariant ( ) ] ( context ) ;
74+ }
75+ else
4976 {
50- RedeemingUserName = e . redemption . user . display_name ,
51- RedeemingUserId = e . redemption . user . id ,
52- RewardName = e . redemption . reward . title ,
53- RewardValue = e . redemption . reward . cost ,
54- RewardPrompt = e . redemption . user_input ,
55- BackgroundColor = e . redemption . reward . background_color ,
56- Image_1x = new Uri ( e . redemption . reward . image ? . url_1x ?? e . redemption . reward . default_image . url_1x ) ,
57- Image_2x = new Uri ( e . redemption . reward . image ? . url_2x ?? e . redemption . reward . default_image . url_2x ) ,
58- Image_4x = new Uri ( e . redemption . reward . image ? . url_4x ?? e . redemption . reward . default_image . url_4x )
59- } ;
60- context . Clients . All . NotifyChannelPoints ( redemption ) ;
77+
78+ var redemption = new ChannelPointRedemption
79+ {
80+ RedeemingUserName = e . redemption . user . display_name ,
81+ RedeemingUserId = e . redemption . user . id ,
82+ RewardName = e . redemption . reward . title ,
83+ RewardValue = e . redemption . reward . cost ,
84+ RewardPrompt = e . redemption . user_input ,
85+ BackgroundColor = e . redemption . reward . background_color ,
86+ Image_1x = new Uri ( e . redemption . reward . image ? . url_1x ?? e . redemption . reward . default_image . url_1x ) ,
87+ Image_2x = new Uri ( e . redemption . reward . image ? . url_2x ?? e . redemption . reward . default_image . url_2x ) ,
88+ Image_4x = new Uri ( e . redemption . reward . image ? . url_4x ?? e . redemption . reward . default_image . url_4x )
89+ } ;
90+ context . Clients . All . NotifyChannelPoints ( redemption ) ;
91+
92+ }
6193 }
6294 }
6395
6496 public Task StopAsync ( CancellationToken cancellationToken )
6597 {
6698 _Proxy . Dispose ( ) ;
6799 return Task . CompletedTask ;
100+
68101 }
102+
103+ #region Channel Point Actions
104+
105+ public static void OldManDeveloper ( IHubContext < AttentionHub , IAttentionHubClient > hubContext )
106+ {
107+
108+ var fx = _OldManSoundFx [ _OldManCount ++ % _OldManSoundFx . Length ] ;
109+ hubContext . Clients . All . PlaySoundEffect ( $ "oldman/{ fx } ") ;
110+
111+ }
112+
113+ #endregion
114+
69115 }
70116}
0 commit comments