From 1b79e8409f5a1b6d4f050730dce2110e1bc32be3 Mon Sep 17 00:00:00 2001 From: tomangelo2 Date: Wed, 7 Mar 2018 21:25:35 +0100 Subject: [PATCH] Add switch to determine sort type You can set it in scene file with ScoreboardSortType SortBy="Name" or "Points" --- src/level/robotmain.cpp | 36 ++++++++++++++++++++++++++++-------- src/level/scoreboard.cpp | 10 ++++++++++ src/level/scoreboard.h | 13 ++++++++++++- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp index a19879af..d12d8af5 100644 --- a/src/level/robotmain.cpp +++ b/src/level/robotmain.cpp @@ -3563,6 +3563,22 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject) } continue; } + + if (line->GetCommand() == "ScoreboardSortType" && !resetObject) + { + if (line->GetParam("SortBy")->AsString() == "Points") + { + // Sort teams by points + m_scoreboard->SetSortType(SORT_POINTS); + } + else if (line->GetParam("SortBy")->AsString() == "Name") + { + // Sort teams alphabetically + m_scoreboard->SetSortType(SORT_ID); + } + continue; + } + if (line->GetCommand() == "ScoreboardKillRule" && !resetObject) { if (!m_scoreboard) @@ -5912,14 +5928,18 @@ void CRobotMain::UpdateCodeBattleInterface() assert(pw != nullptr); std::set teams = GetAllTeams(); std::vector sortedTeams(teams.begin(), teams.end()); - std::sort(sortedTeams.begin(), sortedTeams.end(), [this](int teamA, int teamB) - { - if (m_scoreboard->GetScore(teamA).points > m_scoreboard->GetScore(teamB).points) return true; //Team A have more points than B? - if (m_scoreboard->GetScore(teamA).points < m_scoreboard->GetScore(teamB).points) return false; //Team A have less points than B? - - if (m_scoreboard->GetScore(teamA).time < m_scoreboard->GetScore(teamB).time) return true; //Team A scored faster than B? - else return false; //Team A scored slower than B? - }); + if(m_scoreboard->GetSortType() == SORT_POINTS) + { + std::sort(sortedTeams.begin(), sortedTeams.end(), [this](int teamA, int teamB) + { + if (m_scoreboard->GetScore(teamA).points > m_scoreboard->GetScore(teamB).points) return true; //Team A have more points than B? + if (m_scoreboard->GetScore(teamA).points < m_scoreboard->GetScore(teamB).points) return false; //Team A have less points than B? + + if (m_scoreboard->GetScore(teamA).time < m_scoreboard->GetScore(teamB).time) return true; //Team A scored faster than B? + else return false; //Team A scored slower than B? + }); + } + int i = 0; for (int team : sortedTeams) { diff --git a/src/level/scoreboard.cpp b/src/level/scoreboard.cpp index 83b74fe1..8db62ff8 100644 --- a/src/level/scoreboard.cpp +++ b/src/level/scoreboard.cpp @@ -112,3 +112,13 @@ void CScoreboard::SetScore(int team, int points) { m_score[team].points = points; } + +SortType CScoreboard::GetSortType() +{ + return m_sorttype; +} + +void CScoreboard::SetSortType(SortType type) +{ + m_sorttype = type; +} diff --git a/src/level/scoreboard.h b/src/level/scoreboard.h index 97477e8b..f691e176 100644 --- a/src/level/scoreboard.h +++ b/src/level/scoreboard.h @@ -42,6 +42,12 @@ struct Score float time = 0; //! Time when points were scored }; +enum SortType +{ + SORT_ID, //Sort by team ID + SORT_POINTS, //Sort by points +}; + /** * \class CScoreboard * \brief Scoreboard used to score complex code battles @@ -55,6 +61,7 @@ struct Score * \section example Usage example * \code{.scene} * Scoreboard enable=true // enable the scoreboard + * ScoreboardSortType SortBy="Name" // sort teams alphabetically, another option is SortBy="Points", sorting teams in order of points * ScoreboardKillRule type=WheeledShooter team=1 score=500 // destruction of team 1's WheeledShooter gives 100 points to the team that destroyed it * ScoreboardKillRule type=TargetBot score=100 // destruction of TargetBot (any team) gives 100 points * ScoreboardEndTakeRule score=1000 // completion of EndMissionTake objectives for any team results in 1000 points for that team @@ -127,9 +134,13 @@ public: Score GetScore(int team); void SetScore(int team, int score); + SortType GetSortType(); + void SetSortType(SortType type); + private: std::vector> m_rulesKill = {}; std::vector> m_rulesEndTake = {}; std::map m_score; int m_finishCounter = 0; -}; \ No newline at end of file + SortType m_sorttype = SORT_ID; +};