C

Qt Quick Ultralite swipe_game Demo

/****************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Ultralite module. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see http://www.qt.io/terms-conditions. For further ** information use the contact form at http://www.qt.io/contact-us. ** ** $QT_END_LICENSE$ ** ******************************************************************************/
#include "HighscoreModel.h" #include <algorithm> HighscoreModel::HighscoreModel() {} int HighscoreModel::count() const { return std::count_if(m_highscores.begin(), m_highscores.end(), [](auto score) { return (score.time != 0 && score.tries != 0); }); } Highscore HighscoreModel::data(int index) const { assert(index >= 0 && index < m_highscores.size()); return (index >= 0 && index < m_highscores.size()) ? m_highscores[index] : Highscore(); } void HighscoreModel::addEntry(int time, int tries, int score) { auto newScore = Highscore{time, tries, score}; *back() = (count() < m_highscores.size()) ? newScore : std::max(*back(), newScore); std::sort(m_highscores.begin(), m_highscores.begin() + count(), [](const Highscore &lhs, const Highscore &rhs) { return lhs > rhs; }); modelReset(); } HighscoreModel::scoreContainer::iterator HighscoreModel::back() { auto lastIter = m_highscores.begin() + count(); if (count() == m_highscores.size()) lastIter--; return lastIter; }