From 4e2820080bb9bd6f10872ea94eaa805837b571ff Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Thu, 7 Apr 2016 11:39:16 +0200 Subject: [PATCH] playlist saving done --- NadekoBot/Classes/DBHandler.cs | 20 ++++++ NadekoBot/Classes/Music/MusicControls.cs | 2 + .../Classes/_DataModels/MusicPlaylist.cs | 22 +------ .../Classes/_DataModels/PlaylistSongInfo.cs | 8 +++ NadekoBot/Classes/_DataModels/SongInfo.cs | 13 ++++ NadekoBot/Modules/Music.cs | 61 ++++++++++++++++++- NadekoBot/NadekoBot.csproj | 2 + 7 files changed, 104 insertions(+), 24 deletions(-) create mode 100644 NadekoBot/Classes/_DataModels/PlaylistSongInfo.cs create mode 100644 NadekoBot/Classes/_DataModels/SongInfo.cs diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index a3063b38..11d33e18 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -29,6 +29,9 @@ namespace NadekoBot.Classes conn.CreateTable(); conn.CreateTable(); conn.CreateTable(); + conn.CreateTable(); + conn.CreateTable(); + conn.CreateTable(); conn.Execute(Queries.TransactionTriggerQuery); } } @@ -125,6 +128,23 @@ namespace NadekoBot.Classes } } + /// + /// Updates an existing object or creates a new one + /// + internal void SaveAll(IEnumerable ocol) where T : IDataModel, new() + { + using (var conn = new SQLiteConnection(FilePath)) + { + conn.RunInTransaction(() => + { + foreach (var o in ocol) + { + conn.InsertOrReplace(o, typeof(T)); + } + }); + } + } + internal T GetRandom(Expression> p) where T : IDataModel, new() { using (var conn = new SQLiteConnection(FilePath)) diff --git a/NadekoBot/Classes/Music/MusicControls.cs b/NadekoBot/Classes/Music/MusicControls.cs index f83ac3e8..c7005134 100644 --- a/NadekoBot/Classes/Music/MusicControls.cs +++ b/NadekoBot/Classes/Music/MusicControls.cs @@ -125,6 +125,8 @@ namespace NadekoBot.Classes.Music { playlist.Clear(); CurrentSong = null; + RepeatPlaylist = false; + RepeatSong = false; if (!SongCancelSource.IsCancellationRequested) SongCancelSource.Cancel(); } diff --git a/NadekoBot/Classes/_DataModels/MusicPlaylist.cs b/NadekoBot/Classes/_DataModels/MusicPlaylist.cs index ff998690..ff5f2199 100644 --- a/NadekoBot/Classes/_DataModels/MusicPlaylist.cs +++ b/NadekoBot/Classes/_DataModels/MusicPlaylist.cs @@ -1,29 +1,9 @@ -using SQLite; -using System.Collections.Generic; - -namespace NadekoBot.Classes._DataModels +namespace NadekoBot.Classes._DataModels { internal class MusicPlaylist : IDataModel { - [Unique] public string Name { get; set; } public long CreatorId { get; set; } public string CreatorName { get; set; } - public List Songs { get; set; } - } - - [System.Serializable] - internal class SongInfo - { - public string Name { get; set; } - public string Link { get; set; } - public SongType Type { get; set; } - } - - internal enum SongType - { - Local, - Radio, - Query } } diff --git a/NadekoBot/Classes/_DataModels/PlaylistSongInfo.cs b/NadekoBot/Classes/_DataModels/PlaylistSongInfo.cs new file mode 100644 index 00000000..8db63993 --- /dev/null +++ b/NadekoBot/Classes/_DataModels/PlaylistSongInfo.cs @@ -0,0 +1,8 @@ +namespace NadekoBot.Classes._DataModels +{ + internal class PlaylistSongInfo : IDataModel + { + public int PlaylistId { get; set; } + public int SongInfoId { get; set; } + } +} diff --git a/NadekoBot/Classes/_DataModels/SongInfo.cs b/NadekoBot/Classes/_DataModels/SongInfo.cs new file mode 100644 index 00000000..324885b5 --- /dev/null +++ b/NadekoBot/Classes/_DataModels/SongInfo.cs @@ -0,0 +1,13 @@ +using SQLite; + +namespace NadekoBot.Classes._DataModels +{ + internal class SongInfo : IDataModel + { + public string Provider { get; internal set; } + public int ProviderType { get; internal set; } + public string Title { get; internal set; } + [Unique] + public string Uri { get; internal set; } + } +} diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index b324d3f2..1e39a14a 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -418,9 +418,61 @@ namespace NadekoBot.Modules await e.Channel.SendMessage($"🎵🔁`Repeat playlist {(currentValue ? "enabled" : "disabled")}`"); }); - cgb.CreateCommand("pls") - .Alias("playlistsave") - .Description("Saves a playlist under a certain name. Name must be no longer than 20 characters and mustn't contain dashes.\n**Usage**: `!m pls classical1`") + cgb.CreateCommand("save") + .Description("Saves a playlist under a certain name. Name must be no longer than 20 characters and mustn't contain dashes.\n**Usage**: `!m save classical1`") + .Parameter("name", ParameterType.Unparsed) + .Do(async e => + { + var name = e.GetArg("name")?.Trim(); + + if (string.IsNullOrWhiteSpace(name) || + name.Length > 20 || + name.Contains("-")) + return; + + MusicPlayer musicPlayer; + if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer)) + return; + + //to avoid concurrency issues + var currentPlaylist = new List(musicPlayer.Playlist); + var curSong = musicPlayer.CurrentSong; + if (curSong != null) + currentPlaylist.Insert(0, curSong); + + if (!currentPlaylist.Any()) + return; + + + var songInfos = currentPlaylist.Select(s => new Classes._DataModels.SongInfo + { + Provider = s.SongInfo.Provider, + ProviderType = (int)s.SongInfo.ProviderType, + Title = s.SongInfo.Title, + Uri = s.SongInfo.Uri + }); + + var playlist = new Classes._DataModels.MusicPlaylist + { + CreatorId = (long)e.User.Id, + CreatorName = e.User.Name, + Name = name, + }; + + DbHandler.Instance.SaveAll(songInfos); + DbHandler.Instance.Save(playlist); + DbHandler.Instance.InsertMany(songInfos.Select(s => new Classes._DataModels.PlaylistSongInfo + { + PlaylistId = playlist.Id, + SongInfoId = s.Id + })); + + await e.Channel.SendMessage($"🎵 `Saved playlist as {name}-{playlist.Id}`"); + + }); + + cgb.CreateCommand("load") + .Description("Loads a playlist under a certain name. Name must be no longer than 20 characters and mustn't contain dashes.\n**Usage**: `!m load classical1`") .Parameter("name", ParameterType.Unparsed) .Do(async e => { @@ -442,7 +494,10 @@ namespace NadekoBot.Modules return; + + }); + //cgb.CreateCommand("debug") // .Description("Does something magical. **BOT OWNER ONLY**") // .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly()) diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index e628c037..a68a6b52 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -152,9 +152,11 @@ + +