C

Qt Quick Ultralite swipegame 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 { std::uint8_t size = 0; for (const auto el : m_highscores) { if (el) ++size; } return size; } Highscore HighscoreModel::data(int index) const { if (index >= 0 && index < count()) { return *(m_highscores.at(index)); } return Highscore(); } void HighscoreModel::addEntry(int time, int tries, int score) { auto index = (count() == 0) ? count() : count() - 1; auto currentScore = Highscore{time, tries, score}; if (index < m_scoreboardLength - 1) { ++index; } m_highscores[index] = (m_highscores[index]) ? std::max(*m_highscores[index], currentScore) : currentScore; std::sort(m_highscores.begin(), m_highscores.end(), sort_order()); modelReset(); }