From ac8ade46a2f0099537c577b494037b4415b6538c Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Tue, 22 Mar 2016 21:02:43 +0100 Subject: [PATCH] observing streams are now in serverspecificconfig --- .gitignore | 3 +- NadekoBot/Classes/JSONModels/Configuration.cs | 26 ------------ NadekoBot/Classes/ServerSpecificConfig.cs | 41 +++++++++++++++++++ NadekoBot/Commands/StreamNotifications.cs | 22 ++++++---- NadekoBot/bin/Debug/data/config_example.json | 1 - 5 files changed, 57 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 06f8bff4..da881a0e 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ Tests/bin # Uncomment if necessary however generally it will be regenerated when needed #!**/packages/repositories.config NadekoBot/bin/Debug/data/nadekobot.sqlite -NadekoBot/bin/Debug/data/config.json \ No newline at end of file +NadekoBot/bin/Debug/data/config.json +NadekoBot/bin/Debug/data/ServerSpecificConfigs.json \ No newline at end of file diff --git a/NadekoBot/Classes/JSONModels/Configuration.cs b/NadekoBot/Classes/JSONModels/Configuration.cs index 1df4a8f0..dcd8920f 100644 --- a/NadekoBot/Classes/JSONModels/Configuration.cs +++ b/NadekoBot/Classes/JSONModels/Configuration.cs @@ -13,9 +13,6 @@ namespace NadekoBot.Classes.JSONModels { [JsonIgnore] public List Quotes { get; set; } = new List(); - public HashSet ObservingStreams { get; set; } = - new HashSet(); - public List RotatingStatuses { get; set; } = new List(); public CommandPrefixesModel CommandPrefixes { get; set; } = new CommandPrefixesModel(); public HashSet ServerBlacklist { get; set; } = new HashSet(); @@ -113,29 +110,6 @@ namespace NadekoBot.Classes.JSONModels { public static bool IsUserBlacklisted(ulong id) => NadekoBot.Config.UserBlacklist.Contains(id); } - public class StreamNotificationConfig : IEquatable { - public string Username { get; set; } - public StreamType Type { get; set; } - public ulong ServerId { get; set; } - public ulong ChannelId { get; set; } - public bool LastStatus { get; set; } - - public enum StreamType { - Twitch, - Hitbox, - YoutubeGaming - } - - public bool Equals(StreamNotificationConfig other) => - this.Username.ToLower().Trim() == other.Username.ToLower().Trim() && - this.Type == other.Type && - this.ServerId == other.ServerId; - - public override int GetHashCode() { - return (int)((int)ServerId + Username.Length + (int)Type); - } - } - public class Quote { public string Author { get; set; } public string Text { get; set; } diff --git a/NadekoBot/Classes/ServerSpecificConfig.cs b/NadekoBot/Classes/ServerSpecificConfig.cs index f71e7671..bf2e76e3 100644 --- a/NadekoBot/Classes/ServerSpecificConfig.cs +++ b/NadekoBot/Classes/ServerSpecificConfig.cs @@ -5,6 +5,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; +using NadekoBot.Classes.JSONModels; using Newtonsoft.Json; namespace NadekoBot.Classes { @@ -34,6 +35,8 @@ namespace NadekoBot.Classes { private readonly ConcurrentDictionary configs; + public IEnumerable AllConfigs => configs.Values; + public ServerSpecificConfig Of(ulong id) => configs.GetOrAdd(id, _ => new ServerSpecificConfig()); @@ -82,8 +85,23 @@ namespace NadekoBot.Classes { } } + [JsonIgnore] + private ObservableCollection observingStreams; + public ObservableCollection ObservingStreams { + get { return observingStreams; } + set { + observingStreams = value; + if (value != null) + observingStreams.CollectionChanged += (s, e) => { + if (!SpecificConfigurations.Instantiated) return; + OnPropertyChanged(); + }; + } + } + public ServerSpecificConfig() { ListOfSelfAssignableRoles = new ObservableCollection(); + ObservingStreams = new ObservableCollection(); } public event PropertyChangedEventHandler PropertyChanged = delegate { SpecificConfigurations.Default.Save(); }; @@ -93,4 +111,27 @@ namespace NadekoBot.Classes { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } + + public class StreamNotificationConfig : IEquatable { + public string Username { get; set; } + public StreamType Type { get; set; } + public ulong ServerId { get; set; } + public ulong ChannelId { get; set; } + public bool LastStatus { get; set; } + + public enum StreamType { + Twitch, + Hitbox, + YoutubeGaming + } + + public bool Equals(StreamNotificationConfig other) => + this.Username.ToLower().Trim() == other.Username.ToLower().Trim() && + this.Type == other.Type && + this.ServerId == other.ServerId; + + public override int GetHashCode() { + return (int)((int)ServerId + Username.Length + (int)Type); + } + } } diff --git a/NadekoBot/Commands/StreamNotifications.cs b/NadekoBot/Commands/StreamNotifications.cs index 70d26049..cec22aaa 100644 --- a/NadekoBot/Commands/StreamNotifications.cs +++ b/NadekoBot/Commands/StreamNotifications.cs @@ -22,8 +22,8 @@ namespace NadekoBot.Commands { checkTimer.Elapsed += async (s, e) => { try { - var streams = NadekoBot.Config.ObservingStreams; - if (streams == null || !streams.Any()) return; + var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); + if (!streams.Any()) return; foreach (var stream in streams) { Tuple data; @@ -108,7 +108,9 @@ namespace NadekoBot.Commands { if (string.IsNullOrWhiteSpace(username)) return; - var toRemove = NadekoBot.Config.ObservingStreams + var config = SpecificConfigurations.Default.Of(e.Server.Id); + + var toRemove = config.ObservingStreams .FirstOrDefault(snc => snc.ChannelId == e.Channel.Id && snc.Username.ToLower().Trim() == username); if (toRemove == null) { @@ -116,7 +118,7 @@ namespace NadekoBot.Commands { return; } - NadekoBot.Config.ObservingStreams.Remove(toRemove); + config.ObservingStreams.Remove(toRemove); ConfigHandler.SaveConfig(); await e.Channel.SendMessage($":ok: Removed `{toRemove.Username}`'s stream from notifications."); }); @@ -126,7 +128,10 @@ namespace NadekoBot.Commands { .Description("Lists all streams you are following on this server." + "\n**Usage**: ~ls") .Do(async e => { - var streams = NadekoBot.Config.ObservingStreams.Where(snc => + + var config = SpecificConfigurations.Default.Of(e.Server.Id); + + var streams = config.ObservingStreams.Where(snc => snc.ServerId == e.Server.Id); var streamsArray = streams as StreamNotificationConfig[] ?? streams.ToArray(); @@ -153,13 +158,15 @@ namespace NadekoBot.Commands { if (string.IsNullOrWhiteSpace(username)) return; + var config = SpecificConfigurations.Default.Of(e.Server.Id); + var stream = new StreamNotificationConfig { ServerId = e.Server.Id, ChannelId = e.Channel.Id, Username = username, Type = type, }; - var exists = NadekoBot.Config.ObservingStreams.Contains(stream); + var exists = config.ObservingStreams.Contains(stream); if (exists) { await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel."); } @@ -182,8 +189,7 @@ namespace NadekoBot.Commands { if (!exists) msg = $":ok: I will notify this channel when status changes.\n{msg}"; await e.Channel.SendMessage(msg); - NadekoBot.Config.ObservingStreams.Add(stream); - ConfigHandler.SaveConfig(); + config.ObservingStreams.Add(stream); }; } } diff --git a/NadekoBot/bin/Debug/data/config_example.json b/NadekoBot/bin/Debug/data/config_example.json index bc5845fb..13198eee 100644 --- a/NadekoBot/bin/Debug/data/config_example.json +++ b/NadekoBot/bin/Debug/data/config_example.json @@ -2,7 +2,6 @@ "DontJoinServers": false, "ForwardMessages": true, "IsRotatingStatus": false, - "ObservingStreams": [], "RotatingStatuses": [], "CommandPrefixes": { "Administration": ".",