private static void UpdateSpotifyList(Model.Game game) { List <string> trackIds = new List <string>(); Dictionary <string, int> tracksCountPerPlayer = new Dictionary <string, int>(); int maxTracksPerPlayer = (int)Math.Floor(100.0 / game.Players.Count); foreach (Nominee nominee in game.Nominees .Where(nominee => nominee.UpVotes.Count + nominee.NominatingPlayerIds.Count > nominee.DownVotes.Count) .OrderByDescending( nominee => nominee.UpVotes.Count + nominee.NominatingPlayerIds.Count - nominee.DownVotes.Count)) { string nominatingPlayer = nominee.NominatingPlayerIds.FirstOrDefault(); if (tracksCountPerPlayer.ContainsKey(nominatingPlayer)) { if (tracksCountPerPlayer[nominatingPlayer] < maxTracksPerPlayer) { tracksCountPerPlayer[nominatingPlayer]++; trackIds.Add(nominee.TrackId); } } else { tracksCountPerPlayer[nominatingPlayer] = 1; trackIds.Add(nominee.TrackId); } } SpotifyServices.ReOrderPlaylist(game, trackIds.ToArray()); }
protected string RenderSpotifyAuthUrl() { Regex portFinder = new Regex(":\\d+"); string absoluteUrl = Request.Url.AbsoluteUri; string modifiedUrl = portFinder.Replace(absoluteUrl, ""); return(SpotifyServices.GetAuthUrl(modifiedUrl)); }
private static async Task <ServiceResult <string> > CurrentPlayingsong(SpotifyWebAPI api) => await SpotifyServices.CurrentPlayingsong(api);
public ConnectedController(SpotifyServices spot, SessionStateProvider baseSession) : base() { _spotService = spot; _mySession = new AppSession(baseSession); }
public ChooseFavouriteSongsController(SpotifyServices spot, SessionStateProvider baseSession) : base() { _spotService = spot; _mySession = new AppSession(baseSession); }
public ConnectController(SpotifyServices spot) { _spotService = spot; }
protected void Page_Load(object sender, EventArgs e) { string newGameId = null; bool newGameIdVerified = false; int idLength = 4; while (!newGameIdVerified) { newGameId = GetRandomString(idLength); var previousGame = StateManager.Db.GetGame(newGameId); if (previousGame == null) { newGameIdVerified = true; } else { idLength++; } } var createFromListId = Request.QueryString["createfromlistid"]; var createFromListName = Request.Form["ListName"]; if (!string.IsNullOrWhiteSpace(createFromListId) || !string.IsNullOrWhiteSpace(createFromListName)) { using (var user = StateManager.CurrentUser) { Playlist playlist = null; Playlist selectedList = null; string listNameToCreate = createFromListName; if (!string.IsNullOrWhiteSpace(createFromListId)) { selectedList = SpotifyServices.GetPlaylists(user.SpotifyAuthTokens).PlayLists.FirstOrDefault(playList => playList.Id == createFromListId); if (selectedList != null && string.IsNullOrWhiteSpace(listNameToCreate)) { listNameToCreate = "DemocraticDj - " + selectedList.Name; } } playlist = SpotifyServices.CreatePlayList(user.SpotifyAuthTokens, user.SpotifyUser.Id, listNameToCreate); if (playlist != null) { var game = new Model.Game { GameId = newGameId, SpotifyPlaylistId = playlist.Id, SpotifyPlaylistUri = playlist.Uri, UserId = user.UserId, GameName = playlist.Name }; if (selectedList != null) { foreach (var track in SpotifyServices.GetTracksFromPlaylist(selectedList, user.SpotifyAuthTokens)) { game.Nominees.Add(new Nominee { TrackId = track.Id, NominatingPlayerIds = new List <string> { user.UserId } }); } } StateManager.Db.SaveGame(game); Response.Redirect("/Game.aspx?gameid=" + game.GameId); } } } DataBind(); }
protected void Page_Load(object sender, EventArgs e) { var authResponseCode = Request.QueryString["code"]; var responseState = Request.QueryString["state"]; if (!string.IsNullOrEmpty(authResponseCode)) { var tokens = SpotifyServices.ProcessAuthCode(authResponseCode, responseState); if (tokens != null) { tokens.ReceivedTime = DateTime.UtcNow; Model.Spotify.SpotifyUser spotifyUser = SpotifyServices.GetAuthenticatedUser(tokens); var spotifyAuthenticatedUser = StateManager.Db.FindExistingUser(spotifyUser); if (spotifyAuthenticatedUser != null) { using (var session = StateManager.CurrentSession) { session.UserId = spotifyAuthenticatedUser.UserId; } } using (User currentUser = StateManager.CurrentUser) { currentUser.SpotifyAuthTokens = tokens; currentUser.SpotifyUser = spotifyUser; } if (!string.IsNullOrWhiteSpace(responseState)) { Response.Redirect(responseState); } Response.Redirect("UserManagement.aspx"); } } using (User user = StateManager.CurrentUser) { if (user.IsLoggedIn) { KnownUser.Visible = true; UnknownUser.Visible = false; var verifyingEmailId = Request.QueryString["emailverification"]; if (!string.IsNullOrWhiteSpace(verifyingEmailId)) { var userEmailToVerify = user.Emails.FirstOrDefault(item => item.PendingVerificationId == verifyingEmailId); if (userEmailToVerify != null) { userEmailToVerify.IsVerified = true; Response.Redirect(SpotifyServices.RedirectUrl, false); return; } } var useIcon = Request.QueryString["useIcon"]; if (!string.IsNullOrEmpty(useIcon)) { int parsedIconIdndex; if (int.TryParse(useIcon, out parsedIconIdndex) && GravatarOptions.Count() > parsedIconIdndex) { user.AvatarUrl = GravatarOptions.Skip(parsedIconIdndex).First().Url; Response.Redirect(SpotifyServices.RedirectUrl, false); return; } } string gameToDelete = Request.QueryString["deletegameid"]; if (!string.IsNullOrWhiteSpace(gameToDelete)) { StateManager.Db.DeleteGame(gameToDelete); } if (IsPostBack) { if (!string.IsNullOrWhiteSpace(NameBox.Value) && NameBox.Value != user.DisplayName) { user.DisplayName = NameBox.Value; } var newPassword = PasswordUpdate.Value; var repeatPassword = PasswordRepeat.Value; if (!string.IsNullOrWhiteSpace(newPassword) && !string.IsNullOrWhiteSpace(repeatPassword) && newPassword == repeatPassword) { user.Password = newPassword; } string newEmail = (Request.Form["newemail"] ?? string.Empty).ToLower(); if (!string.IsNullOrWhiteSpace(newEmail)) { var existingUser = StateManager.Db.FindExistingUser(newEmail); if (existingUser != null) { existingUser.ShouldAutoSave = false; } else { user.Emails.Add(new UserEmail { Address = newEmail }); } } } else { if (string.IsNullOrWhiteSpace(user.UserName)) { UserNameAsLabelWrapper.Visible = false; } else { UserNameAsLabel.Text = user.UserName; } NameBox.Value = user.DisplayName; } if (user.SpotifyAuthTokens != null && !string.IsNullOrWhiteSpace(user.SpotifyAuthTokens.AccessToken)) { SpotifyAuthLink.Visible = false; SpotifyInfo.Visible = true; } else { SpotifyAuthLink.Visible = true; SpotifyInfo.Visible = false; } } else { KnownUser.Visible = false; UnknownUser.Visible = true; var existingUserEmailOrName = Request.Form["existingUserEmailOrUsername"]; var existingUserPassword = Request.Form["existingUserPassword"]; if (!string.IsNullOrWhiteSpace(existingUserEmailOrName) && !string.IsNullOrWhiteSpace(existingUserPassword)) { var foundUser = StateManager.Db.FindExistingUser(existingUserEmailOrName); if (foundUser != null && foundUser.Password == existingUserPassword) { using (var session = StateManager.CurrentSession) { session.UserId = foundUser.UserId; } Response.Redirect("/"); } else { //TODO: print error stating that login failed } } var newUserEmail = Request.Form["newUserEmail"]; var newUserName = Request.Form["newUserName"]; var newPassword = Request.Form["newPassword"]; if (!string.IsNullOrWhiteSpace(newUserEmail) && !string.IsNullOrWhiteSpace(newUserName) && !string.IsNullOrWhiteSpace(newPassword)) { newUserEmail = newUserEmail.ToLower(); var existingUser = StateManager.Db.FindExistingUser(newUserEmail) ?? StateManager.Db.FindExistingUser(newUserName); if (existingUser != null) { //TODO: say that the email or username is taken } else { user.UserName = newUserName; user.Password = newPassword; user.Emails.Add(new UserEmail { Address = newUserEmail }); RestServicesController controller = new RestServicesController(); controller.VerifyEmail(new VerifyEmailRequest { Email = newUserEmail }); } } } } DataBind(); }
public RecommendationsController(SpotifyServices spot, SessionStateProvider baseSession) : base() { _spotService = spot; _mySession = new AppSession(baseSession); }