Skip to content

Commit f304e36

Browse files
committed
Added a generic sound effect trigger for #284
1 parent 2c57315 commit f304e36

File tree

5 files changed

+114
-20
lines changed

5 files changed

+114
-20
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,6 @@ __pycache__/
292292
*.btm.cs
293293
*.odx.cs
294294
*.xsd.cs
295+
296+
# Ignore mp3 files
297+
*.mp3

Fritz.Chatbot/AttentionHub.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,44 @@
77

88
namespace Fritz.StreamTools.Hubs
99
{
10-
public interface IAttentionHubClient
10+
public interface IAttentionHubClient
11+
{
12+
13+
// Cheer 200 parithon 12/18/2018
14+
// Cheer 500 pharewings 12/18/2018
15+
Task AlertFritz();
16+
Task ClientConnected(string connectionId);
17+
Task SummonScott();
18+
19+
Task PlaySoundEffect(string fileName);
20+
21+
}
22+
23+
public class AttentionHub : Hub<IAttentionHubClient>, IAttentionClient
24+
{
25+
public override Task OnConnectedAsync()
1126
{
27+
return this.Clients.Others.ClientConnected(this.Context.ConnectionId);
28+
}
1229

13-
// Cheer 200 parithon 12/18/2018
14-
// Cheer 500 pharewings 12/18/2018
15-
Task AlertFritz();
16-
Task ClientConnected(string connectionId);
17-
Task SummonScott();
30+
public Task AlertFritz()
31+
{
32+
return this.Clients.Others.AlertFritz();
1833
}
1934

20-
public class AttentionHub : Hub<IAttentionHubClient>, IAttentionClient
35+
public Task SummonScott()
2136
{
22-
public override Task OnConnectedAsync()
23-
{
24-
return this.Clients.Others.ClientConnected(this.Context.ConnectionId);
25-
}
2637

27-
public Task AlertFritz()
28-
{
29-
return this.Clients.Others.AlertFritz();
30-
}
38+
return this.Clients.Others.SummonScott();
3139

32-
public Task SummonScott()
33-
{
40+
}
3441

35-
return this.Clients.Others.SummonScott();
42+
public Task PlaySoundEffect(string fileName)
43+
{
3644

37-
}
45+
return this.Clients.Others.PlaySoundEffect(fileName);
3846

3947
}
48+
49+
}
4050
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Fritz.StreamLib.Core;
6+
using Fritz.StreamTools.Hubs;
7+
using Microsoft.AspNetCore.SignalR;
8+
9+
namespace Fritz.Chatbot.Commands
10+
{
11+
public class SoundFxCommand : IExtendedCommand
12+
{
13+
14+
public SoundFxCommand(IHubContext<AttentionHub, IAttentionHubClient> hubContext)
15+
{
16+
this.HubContext = hubContext;
17+
}
18+
19+
20+
public IHubContext<AttentionHub, IAttentionHubClient> HubContext { get; }
21+
22+
public string Name => "SoundFxCommand";
23+
public string Description => "Play a fun sound effect in the stream";
24+
public int Order => 1;
25+
public bool Final => true;
26+
public TimeSpan? Cooldown => TimeSpan.FromSeconds(0);
27+
28+
private static readonly Dictionary<string, (string text, string fileName, TimeSpan cooldown)> Effects = new Dictionary<string, (string text, string fileName, TimeSpan cooldown)>
29+
{
30+
{ "ohmy", ("Oh my... something strange is happening", "ohmy.mp3", TimeSpan.FromSeconds(30) ) },
31+
{ "andthen", ("... and then ...", "andthen1.mp3", TimeSpan.FromSeconds(120) ) },
32+
{ "andthen2", ("... and then ...", "andthen3.mp3", TimeSpan.FromSeconds(120) )},
33+
{ "andthen3", ("... and then ...", "andthen4.mp3", TimeSpan.FromSeconds(120) )},
34+
{ "andthen4", ("... and then ...", "andthen5.mp3", TimeSpan.FromSeconds(120) )},
35+
{ "andthen5", ("... and then ...", "andthen6.mp3", TimeSpan.FromSeconds(120) )},
36+
{ "andthen6", ("... and then ...", "andthen7.mp3", TimeSpan.FromSeconds(120) )},
37+
{ "javascript", ("Horses LOVE JavaScript!", "javascript.mp3", TimeSpan.FromSeconds(30) ) },
38+
};
39+
40+
private static readonly Dictionary<string, DateTime> SoundCooldowns = new Dictionary<string, DateTime>();
41+
42+
public bool CanExecute(string userName, string fullCommandText)
43+
{
44+
if (!fullCommandText.StartsWith("!")) return false;
45+
var cmd = fullCommandText.Substring(1).ToLowerInvariant();
46+
if (!Effects.ContainsKey(cmd)) return false;
47+
48+
if (!SoundCooldowns.ContainsKey(cmd)) return true;
49+
var cooldown = Effects[cmd].cooldown;
50+
return (SoundCooldowns[cmd].Add(cooldown) < DateTime.Now);
51+
52+
}
53+
54+
public Task Execute(IChatService chatService, string userName, string fullCommandText)
55+
{
56+
57+
var cmdText = fullCommandText.Substring(1).ToLowerInvariant();
58+
var cmd = Effects[cmdText];
59+
60+
SoundCooldowns[cmdText] = DateTime.Now;
61+
62+
var soundTask = this.HubContext.Clients.All.PlaySoundEffect(cmd.fileName);
63+
var textTask = chatService.SendMessageAsync($"@{userName} - {cmd.text}");
64+
65+
return Task.WhenAll(soundTask, textTask);
66+
67+
}
68+
}
69+
}

Fritz.StreamTools/Views/Attention/Index.cshtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<script>
1616
var audio = new Audio('@Url.Content(@"~/contents/hey_listen.mp3")');
1717
var scottAudio = new Audio('@Url.Content(@"~/contents/Scott!!.mp3")');
18+
var baseFolder = '@Url.Content(@"~/contents")';
1819
1920
var hub = new AttentionHub();
2021
hub.onAlertFritz = () => {
@@ -25,6 +26,10 @@
2526
scottAudio.play();
2627
}
2728
29+
hub.onPlaySoundEffect = (fileName) => {
30+
new Audio(baseFolder + '/' + fileName).play();
31+
}
32+
2833
function alertFritz() {
2934
audio.play();
3035
}

Fritz.StreamTools/wwwroot/js/attentionhub.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
class AttentionHub {
33
constructor() {
44
this.onAlertFritz = null;
5-
this.onSummonScott = null;
5+
this.onSummonScott = null;
6+
this.onPlaySoundEffect = null;
67
this.debug = true;
78
this._hub = null;
89
}
@@ -40,6 +41,12 @@ class AttentionHub {
4041
if (this.onSummonScott) this.onSummonScott();
4142
});
4243

44+
this._hub.on("PlaySoundEffect", (fileName) => {
45+
if (this.debug) console.debug(`Playing file: ${fileName}`);
46+
if (this.onPlaySoundEffect) this.onPlaySoundEffect(fileName);
47+
});
48+
49+
4350
return this._hub.start();
4451
}
4552

0 commit comments

Comments
 (0)