Skip to content

Commit

Permalink
better sorting of songs
Browse files Browse the repository at this point in the history
  • Loading branch information
FlafyDev committed Aug 25, 2024
1 parent 252811f commit 4739323
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
7 changes: 7 additions & 0 deletions include/nong.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,17 @@ class JUKEBOX_DLL Nong final {

~Nong();

enum class Type {
Local,
YT,
Hosted,
};

SongMetadata* metadata() const;
std::optional<std::filesystem::path> path() const;
std::optional<std::string> indexID() const;
geode::Result<Nongs> toNongs() const;
Type type() const;

template <typename ReturnType>
ReturnType visit(
Expand Down
31 changes: 28 additions & 3 deletions src/managers/index_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "index_manager.hpp"

#include <Geode/utils/web.hpp>
#include <filesystem>
#include <matjson.hpp>

#include "../../include/nong.hpp"
Expand Down Expand Up @@ -102,7 +103,7 @@ Result<> IndexManager::loadIndex(std::filesystem::path path) {
ytNong["name"].as_string(),
ytNong["artist"].as_string(),
std::nullopt,
ytNong["startOffset"].as_int()
ytNong.contains("startOffset") ? ytNong["startOffset"].as_int() : 0
),
ytNong["ytID"].as_string(),
index.m_id,
Expand All @@ -128,7 +129,7 @@ Result<> IndexManager::loadIndex(std::filesystem::path path) {
hostedNong["name"].as_string(),
hostedNong["artist"].as_string(),
std::nullopt,
hostedNong["startOffset"].as_int()
hostedNong.contains("startOffset") ? hostedNong["startOffset"].as_int() : 0
),
hostedNong["url"].as_string(),
index.m_id,
Expand Down Expand Up @@ -309,11 +310,35 @@ Result<std::vector<Nong>> IndexManager::getNongs(int gdSongID) {
}
}

std::sort(nongs.begin(), nongs.end(), [defaultUniqueID = localNongs.value()->defaultSong()->metadata()->m_uniqueID](const Nong& a, const Nong& b) {
std::unordered_map<Nong::Type, int> sortedNongType = {
{ Nong::Type::Local, 1 },
{ Nong::Type::Hosted, 2 },
{ Nong::Type::YT, 3 }
};

std::sort(nongs.begin(), nongs.end(), [&sortedNongType, defaultUniqueID = localNongs.value()->defaultSong()->metadata()->m_uniqueID](const Nong& a, const Nong& b) {
// Place the object with isDefault == true at the front
if (a.metadata()->m_uniqueID == defaultUniqueID) return true;
if (b.metadata()->m_uniqueID == defaultUniqueID) return false;

// Next, those without an index
if (!a.indexID().has_value() && b.indexID().has_value()) return true;
if (a.indexID().has_value() && !b.indexID().has_value()) return false;

// Next, compare whether path exists or not
if (a.path().has_value() && std::filesystem::exists(a.path().value())) {
if (!b.path().has_value() || !std::filesystem::exists(b.path().value())) {
return true;
}
} else if (b.path().has_value() && std::filesystem::exists(b.path().value())) {
return false;
}

// Next, compare by type
if (a.type() != b.type()) {
return sortedNongType.at(a.type()) < sortedNongType.at(b.type());
}

// Next, compare whether indexID exists or not (std::nullopt should be first)
if (a.indexID().has_value() != b.indexID().has_value()) {
return !a.indexID().has_value() && b.indexID().has_value();
Expand Down
20 changes: 11 additions & 9 deletions src/nong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,6 @@ class Nong::Impl {
private:
friend class Nong;

enum class Type {
Local,
YT,
Hosted,
};

Type m_type;
std::unique_ptr<LocalSong> m_localSong = nullptr;
std::unique_ptr<YTSong> m_ytSong = nullptr;
Expand Down Expand Up @@ -759,6 +753,10 @@ class Nong::Impl {
}
}
}

Type type() const {
return m_type;
}
};

// Explicit template instantiation
Expand Down Expand Up @@ -799,11 +797,11 @@ Nong::Nong(const Nong& other)

Nong& Nong::operator=(const Nong& other) {
m_impl->m_type = other.m_impl->m_type;
if (other.m_impl->m_type == Impl::Type::Local) {
if (other.m_impl->m_type == Type::Local) {
m_impl->m_localSong = std::make_unique<LocalSong>(*other.m_impl->m_localSong);
} else if (other.m_impl->m_type == Impl::Type::YT) {
} else if (other.m_impl->m_type == Type::YT) {
m_impl->m_ytSong = std::make_unique<YTSong>(*other.m_impl->m_ytSong);
} else if (other.m_impl->m_type == Impl::Type::Hosted) {
} else if (other.m_impl->m_type == Type::Hosted) {
m_impl->m_hostedSong = std::make_unique<HostedSong>(*other.m_impl->m_hostedSong);
}
return *this;
Expand All @@ -825,6 +823,10 @@ std::optional<std::string> Nong::indexID() const {
return m_impl->indexID();
}

Nong::Type Nong::type() const {
return m_impl->type();
}

template <typename ReturnType>
ReturnType Nong::visit(
std::function<ReturnType(LocalSong*)> local,
Expand Down

0 comments on commit 4739323

Please sign in to comment.