[Slim-Checkins] r11446 - /branches/6.5/server/Plugins/RandomPlay/Plugin.pm
adrian at svn.slimdevices.com
adrian at svn.slimdevices.com
Thu Feb 15 10:00:00 PST 2007
Author: adrian
Date: Thu Feb 15 10:00:00 2007
New Revision: 11446
URL: http://svn.slimdevices.com?rev=11446&view=rev
Log:
Bug: 4724
Description: subset of trunk change 11445 to ensure tracks are added
in a random order
Modified:
branches/6.5/server/Plugins/RandomPlay/Plugin.pm
Modified: branches/6.5/server/Plugins/RandomPlay/Plugin.pm
URL: http://svn.slimdevices.com/branches/6.5/server/Plugins/RandomPlay/Plugin.pm?rev=11446&r1=11445&r2=11446&view=diff
==============================================================================
--- branches/6.5/server/Plugins/RandomPlay/Plugin.pm (original)
+++ branches/6.5/server/Plugins/RandomPlay/Plugin.pm Thu Feb 15 10:00:00 2007
@@ -81,30 +81,38 @@
# Restrict by the genre's we've selected.
my $rs = Slim::Schema->rs($type)->search($find, { 'join' => \@joins });
- my @idList;
+ my @results;
if ($limit) {
- # Get a fixed selection of random keys, make sure they don't duplicate.
- my @allIds = $rs->distinct->get_column('me.id')->all;
-
- for (my $i = 0; $i < $limit && @allIds; ++$i) {
-
- push @idList, (splice @allIds, rand @allIds, 1);
- }
+ # Get ids for all results from find
+ my @idList = $rs->distinct->get_column('me.id')->all;
+
+ # Get a random selection
+ my @randomIds;
+
+ for (my $i = 0; $i < $limit && scalar @idList; ++$i) {
+
+ push @randomIds, (splice @idList, rand @idList, 1);
+ }
+
+ # Turn ids into tracks, note this will reorder ids so needs use of RAND() in SQL statement to maintain randomness
+ @results = Slim::Schema->rs($type)->search({ 'id' => { 'in' => \@randomIds } }, { 'order_by' => \'RAND()' })->all;
} else {
- @idList = $rs->distinct->get_column('me.id')->all;
-
- Slim::Player::Playlist::fischer_yates_shuffle(\@idList);
- }
-
- $::d_plugins && msgf("RandomPlay: Find returned %i items\n", scalar @idList);
+ # We want all results from the result set, but need to randomise them
+ my @all = $rs->all;
+
+ while (@all) {
+
+ push @results, (splice @all, rand @all, 1);
+ }
+ }
+
+ $::d_plugins && msgf("RandomPlay: Find returned %i items\n", scalar @results);
# Pull the first track off to add / play it if needed.
- my @results = Slim::Schema->rs($type)->search({ 'id' => { 'in' => \@idList } })->all;
-
my $obj = shift @results;
if (!$obj || !ref($obj)) {
More information about the checkins
mailing list