diff --git a/qtcnet-client/Forms/JackpotSpinForm.cs b/qtcnet-client/Forms/JackpotSpinForm.cs index 30a7d0c..58966c1 100644 --- a/qtcnet-client/Forms/JackpotSpinForm.cs +++ b/qtcnet-client/Forms/JackpotSpinForm.cs @@ -1,4 +1,5 @@ -using System; +using qtcnet_client.Services; +using System; using System.ComponentModel; using System.Windows.Forms; @@ -17,8 +18,10 @@ namespace qtcnet_client.Forms private readonly Random _rndInt = new(); private bool _allowClose = true; - public JackpotSpinForm() + private readonly AudioService _audioService; + public JackpotSpinForm(AudioService audioService) { + _audioService = audioService; InitializeComponent(); FormClosing += JackpotSpinForm_FormClosing; @@ -55,6 +58,7 @@ namespace qtcnet_client.Forms // Start spinning numbers _animationTimer.Start(); + _audioService.PlaySoundEffect("sndTokenSpinLoop"); // Schedule stop _stopTimer.Interval = (int)AnimationLengthMilliseconds; @@ -77,6 +81,9 @@ namespace qtcnet_client.Forms // Show final result lblSpinAnim.Text = $"{CurrencyWon} Q's Won"; + // play done sound + _audioService.PlaySoundEffect("sndTokenWin"); + _allowClose = true; } } diff --git a/qtcnet-client/Forms/MainForm.cs b/qtcnet-client/Forms/MainForm.cs index 11c0eab..0145c8b 100644 --- a/qtcnet-client/Forms/MainForm.cs +++ b/qtcnet-client/Forms/MainForm.cs @@ -42,6 +42,7 @@ namespace qtcnet_client private readonly ClientConfig _config; private readonly ImageFactory _imgFactory; private readonly UpdateService _updateService; + private readonly AudioService _audioService; public MainForm(IApiService apiService, IGatewayService gatewayService, @@ -49,7 +50,8 @@ namespace qtcnet_client CredentialService credentialService, ClientConfig config, ImageFactory imageFactory, - UpdateService updateService) + UpdateService updateService, + AudioService audioService) { _apiService = apiService; _gatewayService = gatewayService; @@ -58,6 +60,7 @@ namespace qtcnet_client _config = config; _imgFactory = imageFactory; _updateService = updateService; + _audioService = audioService; // sub to currentuser updates _apiService.OnCurrentUserUpdate += _apiService_OnCurrentUserUpdate; @@ -402,13 +405,7 @@ namespace qtcnet_client CurrentProfileControl?.Username = _apiService.CurrentUser?.Username ?? "Username"; CurrentProfileControl?.CurrencyCount = _apiService.CurrentUser?.CurrencyAmount ?? 0; - var _pfpRes = await _apiService.GetUserProfilePic(_apiService.CurrentUser!.Id); - if (_pfpRes.Success && _pfpRes.Data != null) - { - using var ms = new MemoryStream(_pfpRes.Data); - Image img = Image.FromStream(ms); - CurrentProfileControl?.ProfileImage = img; - } + CurrentProfileControl?.ProfileImage = await _imgFactory.GetAndCreateProfileImage(_apiService.CurrentUser!.Id, _apiService.CurrentUser!.Status, _apiService.CurrentUser!.ActiveProfileCosmetic); CurrentProfileControl?.RefreshInfo(); } @@ -637,14 +634,14 @@ namespace qtcnet_client // check for daily spin eligibility var now = DateTime.UtcNow; - var lastSpin = _apiService.CurrentUser.LastCurrencySpin; + var lastSpin = DateTime.MinValue; if(lastSpin.Date < now.Date) { Random rnd = new(); - JackpotSpinForm _jackpotSpin = new() + JackpotSpinForm _jackpotSpin = new(_audioService) { - CurrencyWon = rnd.Next(0, 500) + CurrencyWon = rnd.Next(0, 200) }; var _dialogRes = _jackpotSpin.ShowDialog(); if(_dialogRes == DialogResult.OK) @@ -802,6 +799,12 @@ namespace qtcnet_client if(_args.User.Id != "0") await _chatRoom.LoadProfileImage(_msg, _args.User); + + // play sound + if (_args.User.Id != _apiService.CurrentUser?.Id) + _audioService.PlaySoundEffect("sndMessage"); + else + _audioService.PlaySoundEffect("sndSendClick"); } } } diff --git a/qtcnet-client/Program.cs b/qtcnet-client/Program.cs index d028236..2cc579c 100644 --- a/qtcnet-client/Program.cs +++ b/qtcnet-client/Program.cs @@ -41,6 +41,7 @@ namespace qtcnet_client service.AddSingleton(); service.AddSingleton(); service.AddSingleton(); + service.AddSingleton(); service.AddSingleton(provider => GetOrCreateClientConfig()); service.AddSingleton(provider => new ApiService(provider.GetService()!.ServerBaseUri + "/api", provider.GetService()!, provider.GetService()!)); service.AddSingleton(provider => new GatewayService(provider.GetService()!.ServerBaseUri + "/chat", provider.GetService()!, provider.GetService()!)); diff --git a/qtcnet-client/Services/AudioService.cs b/qtcnet-client/Services/AudioService.cs new file mode 100644 index 0000000..c3f762d --- /dev/null +++ b/qtcnet-client/Services/AudioService.cs @@ -0,0 +1,71 @@ +using NAudio.Wave; + +namespace qtcnet_client.Services +{ + public class AudioService : IDisposable + { + public WaveOutEvent? OutputDevice { get; set; } + public AudioFileReader? AudioFileReader { get; set; } + + public event EventHandler? OnSoundEnded; + public void PlaySoundEffect(string soundName) + { + if (!File.Exists($"./Sounds/{soundName}.wav")) return; + + OutputDevice = new WaveOutEvent(); + AudioFileReader = new AudioFileReader($"./Sounds/{soundName}.wav"); + OutputDevice.Init(AudioFileReader); + OutputDevice.Play(); + + return; + } + + public void PlaySoundEffectWithEventString(string soundName, string eventString) + { + if (!File.Exists($"./Sounds/{soundName}.wav")) return; + + OutputDevice = new WaveOutEvent(); + AudioFileReader = new AudioFileReader($"./Sounds/{soundName}.wav"); + OutputDevice.Init(AudioFileReader); + OutputDevice.Play(); + + OutputDevice.PlaybackStopped += (sender, args) => OnSoundEnded?.Invoke(null, new SoundEndedEventArgs { EventString = eventString }); + } + + public void PlaySoundLooped(string soundName, int loopCount) + { + if (!File.Exists($"./Sounds/{soundName}.wav")) return; + + OutputDevice = new WaveOutEvent(); + AudioFileReader = new AudioFileReader($"./Sounds/{soundName}.wav"); + OutputDevice.Init(AudioFileReader); + OutputDevice.Play(); + + int i = 0; + OutputDevice.PlaybackStopped += (sender, args) => + { + i++; + if (i == loopCount) { OutputDevice.PlaybackStopped += null; return; } + + AudioFileReader.Seek(0, SeekOrigin.Begin); + OutputDevice.Play(); + }; + } + + public void Dispose() + { + GC.SuppressFinalize(this); + + OutputDevice?.Dispose(); + OutputDevice = null; + AudioFileReader?.Dispose(); + AudioFileReader = null; + } + + } + + public class SoundEndedEventArgs : EventArgs + { + public string EventString { get; set; } = "UNKNOWN"; + } +} diff --git a/qtcnet-client/Sounds/sndContactRequest.wav b/qtcnet-client/Sounds/sndContactRequest.wav new file mode 100644 index 0000000..8d7dda6 Binary files /dev/null and b/qtcnet-client/Sounds/sndContactRequest.wav differ diff --git a/qtcnet-client/Sounds/sndDirectMsg.wav b/qtcnet-client/Sounds/sndDirectMsg.wav new file mode 100644 index 0000000..dd43f90 Binary files /dev/null and b/qtcnet-client/Sounds/sndDirectMsg.wav differ diff --git a/qtcnet-client/Sounds/sndMessage.wav b/qtcnet-client/Sounds/sndMessage.wav new file mode 100644 index 0000000..4a76cbf Binary files /dev/null and b/qtcnet-client/Sounds/sndMessage.wav differ diff --git a/qtcnet-client/Sounds/sndSendClick.wav b/qtcnet-client/Sounds/sndSendClick.wav new file mode 100644 index 0000000..d4c0538 Binary files /dev/null and b/qtcnet-client/Sounds/sndSendClick.wav differ diff --git a/qtcnet-client/Sounds/sndTTTMoveMade.wav b/qtcnet-client/Sounds/sndTTTMoveMade.wav new file mode 100644 index 0000000..011f3e3 Binary files /dev/null and b/qtcnet-client/Sounds/sndTTTMoveMade.wav differ diff --git a/qtcnet-client/Sounds/sndTokenJackpot.wav b/qtcnet-client/Sounds/sndTokenJackpot.wav new file mode 100644 index 0000000..ad94fb4 Binary files /dev/null and b/qtcnet-client/Sounds/sndTokenJackpot.wav differ diff --git a/qtcnet-client/Sounds/sndTokenSpinLoop.wav b/qtcnet-client/Sounds/sndTokenSpinLoop.wav new file mode 100644 index 0000000..e0b497e Binary files /dev/null and b/qtcnet-client/Sounds/sndTokenSpinLoop.wav differ diff --git a/qtcnet-client/Sounds/sndTokenWin.wav b/qtcnet-client/Sounds/sndTokenWin.wav new file mode 100644 index 0000000..80ecd63 Binary files /dev/null and b/qtcnet-client/Sounds/sndTokenWin.wav differ diff --git a/qtcnet-client/qtcnet-client.csproj b/qtcnet-client/qtcnet-client.csproj index 22e63c2..541882c 100644 --- a/qtcnet-client/qtcnet-client.csproj +++ b/qtcnet-client/qtcnet-client.csproj @@ -18,8 +18,9 @@ - - + + + @@ -41,4 +42,31 @@ + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + \ No newline at end of file