Skip to content

Commit

Permalink
refactor(scoreboard): update methods in Score class to return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 29, 2024
1 parent bca9904 commit 9514e7c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 52 deletions.
10 changes: 5 additions & 5 deletions include/endstone/detail/scoreboard/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class EndstoneScore : public Score {
public:
EndstoneScore(std::unique_ptr<EndstoneObjective> objective, ScoreEntry entry);
[[nodiscard]] ScoreEntry getEntry() const override;
[[nodiscard]] int getValue() const override;
void setValue(int score) override;
[[nodiscard]] bool isScoreSet() const override;
[[nodiscard]] Result<int> getValue() const override;
Result<void> setValue(int score) override;
[[nodiscard]] Result<bool> isScoreSet() const override;
[[nodiscard]] Objective &getObjective() const override;
[[nodiscard]] Scoreboard &getScoreboard() const override;

private:
[[nodiscard]] const ScoreboardId &getScoreboardId() const;
[[nodiscard]] const ScoreboardId &getOrCreateScoreboardId() ;
[[nodiscard]] Result<const ScoreboardId *> getScoreboardId() const;
[[nodiscard]] Result<const ScoreboardId *> getOrCreateScoreboardId();

std::unique_ptr<EndstoneObjective> objective_;
ScoreEntry entry_;
Expand Down
7 changes: 4 additions & 3 deletions include/endstone/scoreboard/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include "endstone/scoreboard/score_entry.h"
#include "endstone/util/result.h"

namespace endstone {

Expand All @@ -40,22 +41,22 @@ class Score {
*
* @return the current score
*/
[[nodiscard]] virtual int getValue() const = 0;
[[nodiscard]] virtual Result<int> getValue() const = 0;

/**
* @brief Sets the current score.
*
* @param score New score
*/
virtual void setValue(int score) = 0;
virtual Result<void> setValue(int score) = 0;

/**
* @brief Shows if this score has been set at any point in time.
*
* @return if this score has been set before
*/

[[nodiscard]] virtual bool isScoreSet() const = 0;
[[nodiscard]] virtual Result<bool> isScoreSet() const = 0;

/**
* @brief Gets the Objective being tracked by this Score.
Expand Down
85 changes: 41 additions & 44 deletions src/endstone_core/scoreboard/score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "endstone/detail/scoreboard/objective.h"
#include "endstone/detail/scoreboard/scoreboard.h"
#include "endstone/detail/server.h"
#include "endstone/detail/util/error.h"

namespace endstone::detail {

Expand All @@ -33,47 +34,46 @@ ScoreEntry EndstoneScore::getEntry() const
return entry_;
}

int EndstoneScore::getValue() const
Result<int> EndstoneScore::getValue() const
{
if (objective_->checkState()) {
const auto &id = getScoreboardId();
if (id.isValid() && objective_->objective_.hasScore(id)) {
return objective_->objective_.getPlayerScore(id).value;
}
}
return 0;
return objective_->checkState().and_then([&](const auto *obj) -> Result<int> {
return getScoreboardId().and_then([&](const auto *id) -> Result<int> {
if (id->isValid() && obj->objective_.hasScore(*id)) {
return obj->objective_.getPlayerScore(*id).value;
}
return 0;
});
});
}

void EndstoneScore::setValue(int score)
Result<void> EndstoneScore::setValue(int score)
{
if (objective_->checkState()) {
const auto &id = getOrCreateScoreboardId();
if (!id.isValid()) {
throw std::runtime_error("Invalid scoreboard id");
}

auto &server = entt::locator<EndstoneServer>::value();
if (!objective_->isModifiable()) {
server.getLogger().error("Cannot modify read-only score");
return;
}

bool success = false;
objective_->scoreboard_.board_.modifyPlayerScore(success, id, objective_->objective_, score,
PlayerScoreSetFunction::Set);
if (!success) {
server.getLogger().error("Cannot modify score");
}
}
return objective_->checkState().and_then([&](const auto *obj) -> Result<void> {
return getOrCreateScoreboardId().and_then([&](const auto *id) -> Result<void> {
return obj->isModifiable().and_then([&](bool modifiable) -> Result<void> {
if (!modifiable) {
return nonstd::make_unexpected(make_error("Cannot modify read-only score."));
}

bool success = false;
obj->scoreboard_.board_.modifyPlayerScore(success, *id, obj->objective_, score,
PlayerScoreSetFunction::Set);

if (!success) {
return nonstd::make_unexpected(make_error("Unable to modify score."));
}
return {};
});
});
});
}

bool EndstoneScore::isScoreSet() const
Result<bool> EndstoneScore::isScoreSet() const
{
if (objective_->checkState()) {
const auto &id = getScoreboardId();
return id.isValid() && objective_->objective_.hasScore(id);
}
return false;
return objective_->checkState().and_then([&](const auto *obj) -> Result<bool> {
return getScoreboardId().and_then(
[&](const auto *id) -> Result<bool> { return id->isValid() && obj->objective_.hasScore(*id); });
});
}

Objective &EndstoneScore::getObjective() const
Expand All @@ -86,20 +86,17 @@ Scoreboard &EndstoneScore::getScoreboard() const
return objective_->getScoreboard();
}

const ScoreboardId &EndstoneScore::getScoreboardId() const
Result<const ScoreboardId *> EndstoneScore::getScoreboardId() const
{
if (objective_->checkState()) {
return objective_->scoreboard_.getScoreboardId(entry_);
}
return ScoreboardId::INVALID;
return objective_->checkState().and_then(
[this](const auto *obj) -> Result<const ScoreboardId *> { return &obj->scoreboard_.getScoreboardId(entry_); });
}

const ScoreboardId &EndstoneScore::getOrCreateScoreboardId()
Result<const ScoreboardId *> EndstoneScore::getOrCreateScoreboardId()
{
if (objective_->checkState()) {
return objective_->scoreboard_.getOrCreateScoreboardId(entry_);
}
return ScoreboardId::INVALID;
return objective_->checkState().and_then([this](const auto *obj) -> Result<const ScoreboardId *> {
return &obj->scoreboard_.getOrCreateScoreboardId(entry_);
});
}

} // namespace endstone::detail

0 comments on commit 9514e7c

Please sign in to comment.