Loading almost done

This commit is contained in:
Master Kwoth 2016-04-07 12:30:58 +02:00
parent 4e2820080b
commit 4f898dc653
4 changed files with 59 additions and 42 deletions

View File

@ -44,6 +44,14 @@ namespace NadekoBot.Classes
} }
} }
internal IList<T> FindAll<T>(Expression<Func<T, bool>> p) where T : IDataModel, new()
{
using (var conn = new SQLiteConnection(FilePath))
{
return conn.Table<T>().Where(p).ToList();
}
}
internal void DeleteAll<T>() where T : IDataModel internal void DeleteAll<T>() where T : IDataModel
{ {
using (var conn = new SQLiteConnection(FilePath)) using (var conn = new SQLiteConnection(FilePath))

View File

@ -16,6 +16,10 @@ namespace NadekoBot.Classes.Music
{ {
public string Provider { get; internal set; } public string Provider { get; internal set; }
public MusicType ProviderType { get; internal set; } public MusicType ProviderType { get; internal set; }
/// <summary>
/// Will be set only if the providertype is normal
/// </summary>
public string Query { get; internal set; }
public string Title { get; internal set; } public string Title { get; internal set; }
public string Uri { get; internal set; } public string Uri { get; internal set; }
} }
@ -233,6 +237,7 @@ namespace NadekoBot.Classes.Music
Provider = "SoundCloud", Provider = "SoundCloud",
Uri = svideo.StreamLink, Uri = svideo.StreamLink,
ProviderType = musicType, ProviderType = musicType,
Query = query,
}); });
} }
var links = await SearchHelper.FindYoutubeUrlByKeywords(query); var links = await SearchHelper.FindYoutubeUrlByKeywords(query);
@ -252,6 +257,7 @@ namespace NadekoBot.Classes.Music
Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube" Title = video.Title.Substring(0, video.Title.Length - 10), // removing trailing "- You Tube"
Provider = "YouTube", Provider = "YouTube",
Uri = video.Uri, Uri = video.Uri,
Query = query,
ProviderType = musicType, ProviderType = musicType,
}); });
} }

View File

@ -1,13 +1,11 @@
using SQLite; namespace NadekoBot.Classes._DataModels
namespace NadekoBot.Classes._DataModels
{ {
internal class SongInfo : IDataModel internal class SongInfo : IDataModel
{ {
public string Provider { get; internal set; } public string Provider { get; internal set; }
public int ProviderType { get; internal set; } public int ProviderType { get; internal set; }
public string Title { get; internal set; } public string Title { get; internal set; }
[Unique]
public string Uri { get; internal set; } public string Uri { get; internal set; }
public string Query { get; internal set; }
} }
} }

View File

@ -2,6 +2,7 @@
using Discord.Commands; using Discord.Commands;
using Discord.Modules; using Discord.Modules;
using NadekoBot.Classes; using NadekoBot.Classes;
using NadekoBot.Classes._DataModels;
using NadekoBot.Classes.Music; using NadekoBot.Classes.Music;
using NadekoBot.Classes.Permissions; using NadekoBot.Classes.Permissions;
using NadekoBot.Extensions; using NadekoBot.Extensions;
@ -10,7 +11,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Timer = System.Timers.Timer;
namespace NadekoBot.Modules namespace NadekoBot.Modules
{ {
@ -20,24 +20,8 @@ namespace NadekoBot.Modules
public static ConcurrentDictionary<Server, MusicPlayer> MusicPlayers = new ConcurrentDictionary<Server, MusicPlayer>(); public static ConcurrentDictionary<Server, MusicPlayer> MusicPlayers = new ConcurrentDictionary<Server, MusicPlayer>();
public static ConcurrentDictionary<ulong, float> DefaultMusicVolumes = new ConcurrentDictionary<ulong, float>(); public static ConcurrentDictionary<ulong, float> DefaultMusicVolumes = new ConcurrentDictionary<ulong, float>();
private readonly Timer setgameTimer = new Timer();
private bool setgameEnabled = false;
public Music() public Music()
{ {
setgameTimer.Interval = 20000;
setgameTimer.Elapsed += (s, e) =>
{
try
{
var num = MusicPlayers.Count(kvp => kvp.Value.CurrentSong != null);
NadekoBot.Client.SetGame($"{num} songs".SnPl(num) + $", {MusicPlayers.Sum(kvp => kvp.Value.Playlist.Count())} queued");
}
catch { }
};
// ready for 1.0 // ready for 1.0
//NadekoBot.Client.UserUpdated += (s, e) => //NadekoBot.Client.UserUpdated += (s, e) =>
//{ //{
@ -449,10 +433,11 @@ namespace NadekoBot.Modules
Provider = s.SongInfo.Provider, Provider = s.SongInfo.Provider,
ProviderType = (int)s.SongInfo.ProviderType, ProviderType = (int)s.SongInfo.ProviderType,
Title = s.SongInfo.Title, Title = s.SongInfo.Title,
Uri = s.SongInfo.Uri Uri = s.SongInfo.Uri,
Query = s.SongInfo.Query
}); });
var playlist = new Classes._DataModels.MusicPlaylist var playlist = new MusicPlaylist
{ {
CreatorId = (long)e.User.Id, CreatorId = (long)e.User.Id,
CreatorName = e.User.Name, CreatorName = e.User.Name,
@ -461,7 +446,7 @@ namespace NadekoBot.Modules
DbHandler.Instance.SaveAll(songInfos); DbHandler.Instance.SaveAll(songInfos);
DbHandler.Instance.Save(playlist); DbHandler.Instance.Save(playlist);
DbHandler.Instance.InsertMany(songInfos.Select(s => new Classes._DataModels.PlaylistSongInfo DbHandler.Instance.InsertMany(songInfos.Select(s => new PlaylistSongInfo
{ {
PlaylistId = playlist.Id, PlaylistId = playlist.Id,
SongInfoId = s.Id SongInfoId = s.Id
@ -472,39 +457,59 @@ namespace NadekoBot.Modules
}); });
cgb.CreateCommand("load") 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`") .Description("Loads a playlist under a certain name. \n**Usage**: `!m load classical-1`")
.Parameter("name", ParameterType.Unparsed) .Parameter("name", ParameterType.Unparsed)
.Do(async e => .Do(async e =>
{ {
var voiceCh = e.User.VoiceChannel;
var textCh = e.Channel;
if (voiceCh == null || voiceCh.Server != textCh.Server)
{
await textCh.SendMessage("💢 You need to be in a voice channel on this server.\n If you are already in a voice channel, try rejoining.");
return;
}
var name = e.GetArg("name")?.Trim(); var name = e.GetArg("name")?.Trim();
if (string.IsNullOrWhiteSpace(name) || if (string.IsNullOrWhiteSpace(name))
name.Length > 20 ||
name.Contains("-"))
return; return;
MusicPlayer musicPlayer; var parts = name.Split('-');
if (!MusicPlayers.TryGetValue(e.Server, out musicPlayer)) if (parts.Length != 2)
return;
var playlistName = parts[0];
int playlistNumber;
if (!int.TryParse(parts[1], out playlistNumber))
return; return;
//to avoid concurrency issues var playlist = DbHandler.Instance.FindOne<MusicPlaylist>(
var currentPlaylist = new List<Song>(musicPlayer.Playlist); p => p.Name.ToLower() == name);
if (!currentPlaylist.Any()) if (playlist == null)
{
await e.Channel.SendMessage("Can't find playlist under that name.");
return; return;
}
var psis = DbHandler.Instance.FindAll<PlaylistSongInfo>(psi =>
psi.PlaylistId == playlist.Id);
var songInfos = psis.Select(psi => DbHandler.Instance
.FindOne<Classes._DataModels.SongInfo>(si => si.Id == psi.SongInfoId));
await e.Channel.SendMessage($"`Attempting to load {songInfos.Count()} songs`");
foreach (var si in songInfos)
{
try
{
await QueueSong(textCh, voiceCh, si.Query, true, (MusicType)si.ProviderType);
}
catch (Exception ex)
{
Console.WriteLine($"Failed QueueSong in load playlist. {ex}");
}
}
}); });
//cgb.CreateCommand("debug")
// .Description("Does something magical. **BOT OWNER ONLY**")
// .AddCheck(Classes.Permissions.SimpleCheckers.OwnerOnly())
// .Do(e => {
// var inactivePlayers =
// Console.WriteLine("");
// });
}); });
} }