KaraokePC/FirebaseKaraoke/FirebaseController.cs
Matt Bruce e2d1859a8f refactored favorites to mostplayed
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
2016-09-05 19:18:41 -05:00

168 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using FireSharp.Interfaces;
using FireSharp.Config;
using FireSharp;
using Newtonsoft.Json;
using System.Configuration;
using Herse.Models;
namespace KaraokePlayer.Classes
{
public class FirebaseController
{
private IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
};
private PlayerState _state = PlayerState.Playing;
private IFirebaseClient _client;
private ControllerStateChangedEventHandler _stateChanged;
private ControllerSongChangedEventHandler _songChanged;
public Settings Settings { get; set; }
private string SettingsPath
{
get { return string.Format("controllers/{0}/player/settings", this.Id); }
}
private string QueuePath
{
get { return string.Format("controllers/{0}/player/queue", this.Id); }
}
private string StatePath
{
get { return string.Format("controllers/{0}/player/state/", this.Id); }
}
private string CurrentSongPath
{
get { return string.Format("controllers/{0}/player/queue/0/", this.Id); }
}
private string MostPlayedPath
{
get { return string.Format("controllers/{0}/mostPlayed/", this.Id); }
}
public string Id { get; set; }
public Song CurrentSong { get; set; }
public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged)
{
Id = clientId;
_stateChanged = stateChanged;
_songChanged = songChanged;
_client = new FirebaseClient(config);
SetupListener();
Settings = _client.Get(SettingsPath).ResultAs<Settings>();
}
private void SetupListener()
{
_client.OnAsync(CurrentSongPath,
added: (s, args, obj) =>
{
Console.WriteLine("added " + args.Path + " " + args.Data);
CurrentSongDidChange();
},
removed: (s, args, obj) =>
{
//TODO: the current song was removed from the queue
}
);
_client.OnAsync(StatePath,
added: (s, args, obj) =>
{
// do we need this? don't think so
},
changed: (s, args, obj) =>
{
Console.WriteLine("state changed " + args.Data);
PlayerState newstate = (PlayerState)Enum.Parse(typeof(PlayerState), args.Data, true);
StateDidChange(newstate);
},
removed: null
);
}
/// <summary>
/// called by the player to end the current song in firebase
/// </summary>
public void EndSong()
{
SetState(PlayerState.Stopped);
List<QueueItem> queue = _client.Get(QueuePath).ResultAs<List<QueueItem>>();
queue.RemoveAt(0);
_client.Set(QueuePath, queue);
}
/// <summary>
/// called by the player to set the state in firebase
/// </summary>
/// <param name="state"></param>
public void SetState(PlayerState state)
{
_state = state;
_client.SetAsync(StatePath, "Playing");
}
/// <summary>
/// called by firebase to change the current song
/// </summary>
private void CurrentSongDidChange()
{
var response = _client.Get(CurrentSongPath);
var item = response.ResultAs<QueueItem>();
if (item == null) return;
if (CurrentSong == null || CurrentSong.Path != item.Song.Path)
{
CurrentSong = item.Song;
_songChanged(new ControllerSongChangedEventArgs(item.Song));
UpdateMostPlayed(item.Song);
}
}
/// <summary>
/// called by firebase to change the current state
/// </summary>
/// <param name="newstate"></param>
private void StateDidChange(PlayerState newstate)
{
if (newstate == _state) return;
_state = newstate;
_stateChanged(new ControllerStateChangedEventArgs(newstate));
}
private void UpdateMostPlayed(Song song)
{
//get MostPlayed
MostPlayedSong mostPlayed = null;
List<MostPlayedSong> mostPlays = _client.Get(MostPlayedPath).ResultAs<List<MostPlayedSong>>();
if (mostPlays == null) mostPlays = new List<MostPlayedSong>();
//unique song is based on the path
mostPlayed = mostPlays.SingleOrDefault(f => f.Path == song.Path);
if (mostPlayed == null)
{
mostPlayed = new MostPlayedSong();
mostPlayed.Artist = song.Artist;
mostPlayed.Title = song.Title;
mostPlayed.Genre = song.Genre;
mostPlayed.Path = song.Path;
mostPlayed.Year = song.Year;
mostPlayed.Count = 1;
mostPlays.Add(mostPlayed);
}
else {
mostPlayed.Count += 1;
}
//take the top 100 songs
var top100 = mostPlays.OrderByDescending(f => f.Count).Take(100);
_client.Set(MostPlayedPath, top100);
}
}
}