#include "blitz/game/LeaderBoard.h"

namespace blitz {
namespace game {

void LeaderBoard::AddPlayer(Player* player) {
	m_Players.push_back(player);
	Update();
}

void LeaderBoard::RemovePlayer(PlayerID player) {
	auto it = std::find_if(m_Players.begin(), m_Players.end(), [player](game::Player* p) { return p->GetID() == player; });
	if (it != m_Players.end())
		m_Players.erase(it);
}

void LeaderBoard::Update() {
	std::sort(m_Players.begin(), m_Players.end(), [](Player* a, Player* b) {
		std::uint16_t killsA = a->GetStats().m_Kills;
		std::uint16_t killsB = b->GetStats().m_Kills;
		if (killsA == killsB) {
			return a->GetStats().m_Deaths < b->GetStats().m_Deaths;
		}
		return killsA > killsB;
	});
}

}  // namespace game
}  // namespace blitz
