9
9
using Microsoft . Extensions . Options ;
10
10
using System ;
11
11
using System . Collections . Generic ;
12
+ using System . IO ;
12
13
using System . Linq ;
13
14
using System . Threading ;
14
15
using System . Threading . Tasks ;
@@ -21,50 +22,95 @@ public class TwitchPubSubService : IHostedService
21
22
private IServiceProvider _ServiceProvider ;
22
23
private CancellationToken _Token ;
23
24
private Task _BackgroundTask ;
25
+ private static string [ ] _OldManSoundFx ;
26
+ public static short _OldManCount = 0 ;
24
27
private readonly Twitch . PubSub . Proxy _Proxy ;
25
28
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 > > > ( ) ;
26
31
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 )
28
33
{
29
34
_ServiceProvider = serviceProvider ;
30
35
_Proxy = proxy ;
31
36
_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 ) ) ;
32
46
}
33
47
34
48
public Task StartAsync ( CancellationToken cancellationToken )
35
49
{
36
50
_Proxy . OnChannelPointsRedeemed += _Proxy_OnChannelPointsRedeemed ;
37
51
_Token = cancellationToken ;
38
52
_BackgroundTask = _Proxy . StartAsync ( new TwitchTopic [ ] { TwitchTopic . ChannelPoints ( _Configuration . UserId ) } , _Token ) ;
53
+ IdentifyOldManAudio ( ) ;
39
54
return Task . CompletedTask ;
40
55
}
41
56
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
+
42
66
private void _Proxy_OnChannelPointsRedeemed ( object sender , ChannelRedemption e )
43
67
{
44
68
using ( var scope = _ServiceProvider . CreateScope ( ) )
45
69
{
46
70
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
49
76
{
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
+ }
61
93
}
62
94
}
63
95
64
96
public Task StopAsync ( CancellationToken cancellationToken )
65
97
{
66
98
_Proxy . Dispose ( ) ;
67
99
return Task . CompletedTask ;
100
+
68
101
}
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
+
69
115
}
70
116
}
0 commit comments