media: prevent same track showing twice in random list

- Random list now avoids showing the same track more than once
- Even if a track appears in multiple zones, it will only show once
This commit is contained in:
Sattva
2025-05-19 04:27:14 +03:00
parent 4fc3576211
commit 6290aaf3d0
+27 -17
View File
@@ -15493,15 +15493,12 @@ function LeaPlusLC:RunOnce()
local function MarkCurrentTrackListened() local function MarkCurrentTrackListened()
if LastFolder == L["Random"] and LastPlayed and trackStartTime and trackStartTime > 0 then if LastFolder == L["Random"] and LastPlayed and trackStartTime and trackStartTime > 0 then
print("last folder ok")
local elapsed = GetTime() - trackStartTime local elapsed = GetTime() - trackStartTime
if elapsed >= 30 then if elapsed >= 15 then
print("time ok (" .. math.floor(elapsed) .. "s)")
local id = GetTrackIDFromPath(LastPlayed) local id = GetTrackIDFromPath(LastPlayed)
if id and id ~= "" then if id and id ~= "" then
print("id ok")
LeaPlusDB["ListenedTracks"][id] = true LeaPlusDB["ListenedTracks"][id] = true
LeaPlusLC:Print("Added track to listened: " .. id) --LeaPlusLC:Print("Added track to listened: " .. id)
end end
end end
end end
@@ -15552,7 +15549,7 @@ function LeaPlusLC:RunOnce()
-- Function to play a track and show the static highlight bar -- Function to play a track and show the static highlight bar
local function PlayTrack() local function PlayTrack()
-- Зафиксировать предыдущий трек, если слушали >=30 сек -- On Click new track - mark previous
MarkCurrentTrackListened() MarkCurrentTrackListened()
-- Остановить текущее воспроизведение (если было) -- Остановить текущее воспроизведение (если было)
StopMusic() StopMusic()
@@ -15593,7 +15590,7 @@ function LeaPlusLC:RunOnce()
end end
if trackTime then if trackTime then
LeaPlusLC.TrackTimer = LibCompat.NewTimer(trackTime + 1, function() LeaPlusLC.TrackTimer = LibCompat.NewTimer(trackTime + 1, function()
-- По окончании: фиксируем текущий трек и переходим к следующему -- Automatic mark track after listened fully
MarkCurrentTrackListened() MarkCurrentTrackListened()
StopMusic() StopMusic()
if tracknumber > #playlist then if tracknumber > #playlist then
@@ -15827,6 +15824,8 @@ function LeaPlusLC:RunOnce()
ListData[4] = "|cffffd800" .. L["Selection of music tracks"] -- Must be lower case |c ListData[4] = "|cffffd800" .. L["Selection of music tracks"] -- Must be lower case |c
-- Populate list data until it contains desired number of tracks -- Populate list data until it contains desired number of tracks
local attempt = 0 local attempt = 0
-- локальный set, чтобы за один проход random не добавлял один и тот же trackID снова
local randHash = {}
while #ListData < 50 and attempt < 2000 do while #ListData < 50 and attempt < 2000 do
attempt = attempt + 1 attempt = attempt + 1
-- Get random category -- Get random category
@@ -15835,22 +15834,32 @@ function LeaPlusLC:RunOnce()
local rZone = random(1, #ZoneList[rCategory]) local rZone = random(1, #ZoneList[rCategory])
-- Get random track within zone -- Get random track within zone
local rTrack = ZoneList[rCategory][rZone].tracks[random(1, #ZoneList[rCategory][rZone].tracks)] local rTrack = ZoneList[rCategory][rZone].tracks[random(1, #ZoneList[rCategory][rZone].tracks)]
-- Only allow tracks that are real mp3 (exclude SoundKit/non-file)
-- гарантируем, что подтаблица существует
LeaPlusDB["ListenedTracks"] = LeaPlusDB["ListenedTracks"] or {}
if rTrack and rTrack ~= "" and strfind(rTrack, "#") and strfind(rTrack:lower(), ".mp3") then if rTrack and rTrack ~= "" and strfind(rTrack, "#") and strfind(rTrack:lower(), ".mp3") then
local trackID = GetTrackIDFromPath(rTrack) local trackID = GetTrackIDFromPath(rTrack)
-- гарантируем, что подтаблица существует (на всякий случай)
if not LeaPlusDB["ListenedTracks"] then -- пропускаем, если уже слушали или уже попал в текущий random
LeaPlusDB["ListenedTracks"] = {} if not LeaPlusDB["ListenedTracks"][trackID] and not randHash[trackID] then
end
-- база инициализирована заранее, используем квадратные скобки
if not LeaPlusDB["ListenedTracks"][trackID] then
local zoneLabel = "|Cffffffaa" .. ZoneList[rCategory][rZone].zone .. " |r" .. rTrack local zoneLabel = "|Cffffffaa" .. ZoneList[rCategory][rZone].zone .. " |r" .. rTrack
if not tContains(ListData, zoneLabel) if not tContains(randomBannedList, L[ZoneList[rCategory][rZone].zone])
and not tContains(randomBannedList, L[ZoneList[rCategory][rZone].zone])
and not tContains(randomBannedList, rTrack) and not tContains(randomBannedList, rTrack)
then then
tinsert(ListData, zoneLabel) if not tContains(ListData, zoneLabel) then
tinsert(ListData, zoneLabel)
randHash[trackID] = true -- фиксируем, чтобы не повторить в этой же подборке
end
end end
else
---- Печать причины пропуска
--if LeaPlusDB["ListenedTracks"][trackID] then
-- print("skipped (already listened):", trackID)
--elseif randHash[trackID] then
-- print("skipped (already added in this random):", trackID)
--end
end end
end end
end end
@@ -15858,6 +15867,7 @@ function LeaPlusLC:RunOnce()
if #ListData <= 4 then if #ListData <= 4 then
tinsert(ListData, "|cff999999(You have listened it all!)|r") tinsert(ListData, "|cff999999(You have listened it all!)|r")
end end
print("Final random count:", #ListData - 4) -- вычитаем заголовки
-- Refresh the track listing -- Refresh the track listing
UpdateList() UpdateList()
-- Set track listing to top -- Set track listing to top