Skip to content

Commit

Permalink
modified room cleanup logic, added extra leave room button to interna…
Browse files Browse the repository at this point in the history
…l/index
  • Loading branch information
tomzorz committed May 3, 2021
1 parent 48739e3 commit dba855f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
2 changes: 2 additions & 0 deletions sources/PugetSound/PugetSound.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=authz/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
19 changes: 6 additions & 13 deletions sources/PugetSound/PugetSound/Logic/PartyRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ public class PartyRoom
private readonly StatisticsService _statisticsService;
private readonly DevicePersistenceService _devicePersistenceService;
private DateTimeOffset _handledUntil;
private DateTimeOffset _timeSinceEmpty;
private DateTimeOffset _timeSinceLastSongPlayed;
private int _currentDjNumber;
private FullTrack _currentTrack;

private readonly DateTimeOffset _customFutureDateTimeOffset = new DateTimeOffset(9999, 1, 1, 0, 0, 0, TimeSpan.Zero);

private readonly List<Func<Task>> _roomRetries;

public DateTimeOffset TimeSinceEmpty => _timeSinceEmpty;
public DateTimeOffset TimeSinceLastSongPlayed => _timeSinceLastSongPlayed;

public string RoomId { get; }

Expand All @@ -54,7 +52,7 @@ public PartyRoom(string roomId, ILogger logger, SpotifyAccessService spotifyAcce
RoomId = roomId;
_members = new List<RoomMember>();
_roomEvents = new List<IRoomEvent>();
_timeSinceEmpty = _customFutureDateTimeOffset;
_timeSinceLastSongPlayed = DateTimeOffset.Now;
_roomRetries = new List<Func<Task>>();

_handledUntil = DateTimeOffset.Now;
Expand Down Expand Up @@ -90,8 +88,6 @@ public void MemberJoin(RoomMember member)
ToggleDj(member, false);

if (_currentTrack != null) StartSongForMemberUgly(member);

_timeSinceEmpty = _customFutureDateTimeOffset;
}

public void TryFixPlaybackForMember(RoomMember member)
Expand Down Expand Up @@ -201,12 +197,6 @@ public void MemberLeave(RoomMember member)
OnRoomMembersChanged?.Invoke(this, member.UserName);

UpdateReactionTotals();

// this was the last member to leave
if (!_members.Any())
{
_timeSinceEmpty = DateTimeOffset.Now;
}
}

public async Task<RoomState> TryPlayNext(bool force = false)
Expand Down Expand Up @@ -304,6 +294,9 @@ public async Task<RoomState> TryPlayNext(bool force = false)
_roomEvents.Add(new SongPlayedEvent(nextPlayer.UserName, nextPlayer.FriendlyName,
$"{CurrentRoomState.CurrentSongArtist} - {CurrentRoomState.CurrentSongTitle}", song.Id, song.Uri));

// successfully played song, so change last song played
_timeSinceLastSongPlayed = _handledUntil;

return CurrentRoomState;
}

Expand Down
49 changes: 39 additions & 10 deletions sources/PugetSound/PugetSound/Logic/RoomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,14 @@ public bool TryKickUserFromRoom(string roomName, string userName)

public async Task ProcessRoomsAsync()
{
var roomsForCleanup = new List<string>();
var roomsForCleanup = new List<PartyRoom>();

foreach (var partyRoom in _rooms)
{
// everyone left for a day, remove room
if (partyRoom.Value.TimeSinceEmpty + TimeSpan.FromHours(24) < DateTimeOffset.Now)
// no song played in the last 2 hours, schedule room for removal
if (partyRoom.Value.TimeSinceLastSongPlayed + TimeSpan.FromHours(2) < DateTimeOffset.Now)
{
partyRoom.Value.OnRoomMembersChanged -= Room_OnRoomMembersChanged;
partyRoom.Value.OnRoomNotification -= Room_OnRoomNotification;
partyRoom.Value.OnRoomCurrentReactionsChanged -= Room_OnRoomCurrentReactionsChanged;
roomsForCleanup.Add(partyRoom.Value.RoomId);
roomsForCleanup.Add(partyRoom.Value);
continue;
}

Expand Down Expand Up @@ -222,11 +219,43 @@ public async Task ProcessRoomsAsync()
}

// clean up old rooms
foreach (var roomId in roomsForCleanup)
foreach (var room in roomsForCleanup)
{
_rooms.Remove(roomId);
// remove members
var members = room.Members.ToList();
foreach (var roomMember in members)
{
try
{
room.MemberLeave(roomMember);

await _roomHubContext.Clients.Client(roomMember.ConnectionId).ForcedRoomLeave();

_logger.Log(LogLevel.Information, "Kicked {Username} as their room is closing down.", roomMember.UserName);
}
catch (Exception e)
{
_logger.Log(LogLevel.Warning, "Error during removing member {Username} because {@Exception}", roomMember.UserName, e);
}
}

// ensure stats consistency (shouldn't really happen but eh)
var rc = room.Members.Count;
if (rc > 0)
{
for (int i = 0; i < rc; i++)
{
_statisticsService.DecrementUserCount();
}
}

// remove room
room.OnRoomMembersChanged -= Room_OnRoomMembersChanged;
room.OnRoomNotification -= Room_OnRoomNotification;
room.OnRoomCurrentReactionsChanged -= Room_OnRoomCurrentReactionsChanged;
_rooms.Remove(room.RoomId);
_statisticsService.DecrementRoomCount();
_logger.Log(LogLevel.Information, "Cleaned up empty room {Room}", roomId);
_logger.Log(LogLevel.Information, "Cleaned up silent room {Room}", room.RoomId);
}
}

Expand Down
1 change: 0 additions & 1 deletion sources/PugetSound/PugetSound/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace PugetSound
* - add room history who voted to skip song
* - add progress bar / votes required to vote skip song button
* - fetch upcoming 1-3 songs for every user on queue listing, show them as upcoming
* - allow cleaning up of rooms where there are only listeners and no song has been played for X time
*
* BUGS
* - spotify refresh token shenanigans
Expand Down
6 changes: 3 additions & 3 deletions sources/PugetSound/PugetSound/Revision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public static class Revision
{
private const int Year = 2021;

private const int Month = 04;
private const int Month = 05;

private const int Day = 29;
private const int Day = 03;

private const string DailyRevision = "alpha";
private const string DailyRevision = "beta";

public static string CssQuery { get; } = $"{Year}{Month:D2}{Day:D2}{DailyRevision}";

Expand Down
8 changes: 7 additions & 1 deletion sources/PugetSound/PugetSound/Views/Internal/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
<div class="alert alert-info">
<p>Heads up! 👀</p>
<p><strong>It looks like you're still in 🎶@Model.RoomName!</strong> This room has been pre-filled so that you can back to your music - just press [Enter room] to continue.</p>
<p class="mb-0">If you enter a different room name you'll leave your current one.</p>
<p class="mb-0">If you enter a different room name you'll leave your current one. Alternatively you can press the [Leave room] button below to leave this room.</p>
<br />
<form method="post" asp-action="LeaveRoom" class="form-room-leave">
<input type="hidden" asp-for="UserName" value="@Model.UserName" />
<input type="hidden" asp-for="RoomName" value="@Model.RoomName" />
<input type="submit" class="redButton" id="leaveRoomButton" value="Leave Room" />
</form>
</div>
}
else if (!string.IsNullOrWhiteSpace(Model.RoomName))
Expand Down

0 comments on commit dba855f

Please sign in to comment.