-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
239 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
build/ | ||
.history/ | ||
.vs-code/ | ||
.vscode/ | ||
.clangd/ | ||
|
||
|
||
.env | ||
compile_commands.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ if (NOT CONAN_ONLY) | |
session.cpp | ||
player.cpp | ||
world.cpp | ||
scoreboard.cpp | ||
) | ||
target_link_libraries( | ||
server | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "scoreboard.h" | ||
#include "player.h" | ||
|
||
namespace sd { | ||
|
||
const score_t& scoreboard_t::lowest_score() const | ||
{ | ||
return scores_.back(); | ||
} | ||
|
||
void scoreboard_t::push_score(const score_t& score) | ||
{ | ||
if (!scores_.empty() && score.score <= lowest_score().score) { | ||
return; | ||
} | ||
|
||
bool inserted = false; | ||
for (auto it = begin(scores_); it < end(scores_); ++it) { | ||
if (!inserted) { | ||
if (score.score > it->score) { | ||
inserted = true; | ||
|
||
if (score.id == it->id) { | ||
// just replace and bail out | ||
*it = score; | ||
break; | ||
} | ||
else { | ||
// insert, shift the other scores | ||
scores_.insert(it, score); | ||
} | ||
} | ||
if (score.id == it->id) { | ||
// same player already has a better score | ||
break; | ||
} | ||
} | ||
else { | ||
if (score.id == it->id) { | ||
// remove previous highest score | ||
scores_.erase(it--); | ||
} | ||
} | ||
} | ||
|
||
if (!inserted && scores_.size() < max_size) { | ||
scores_.push_back(score); | ||
} | ||
|
||
while (scores_.size() > max_size) { | ||
scores_.pop_back(); | ||
} | ||
} | ||
|
||
void scoreboard_t::push_score(const player_t& player) | ||
{ | ||
if (scores_.empty() || player.score() > lowest_score().score) { | ||
return push_score({player.id(), player.name(), player.score()}); | ||
} | ||
} | ||
|
||
} // sd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "config.h" | ||
|
||
namespace sd { | ||
|
||
struct score_t { | ||
player_id_t id; | ||
std::string name; | ||
double score; | ||
}; | ||
|
||
class scoreboard_t { | ||
public: | ||
static constexpr auto max_size = 10; | ||
|
||
const std::vector<score_t>& scores() const { return scores_; } | ||
void push_score(const score_t& score); | ||
void push_score(const player_t& player); | ||
|
||
private: | ||
const score_t& lowest_score() const; | ||
|
||
std::vector<score_t> scores_; | ||
}; | ||
|
||
} // sd |
Oops, something went wrong.