Skip to content

Commit 1eedf82

Browse files
authored
refactor: group score together (#946)
1 parent ba7805a commit 1eedf82

File tree

5 files changed

+57
-53
lines changed

5 files changed

+57
-53
lines changed

app/src/engine/uci_engine.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,6 @@ std::vector<std::string> UciEngine::lastInfo() const {
439439
return str_utils::splitString(last_info, ' ');
440440
}
441441

442-
ScoreType UciEngine::lastScoreType() const {
443-
auto score = str_utils::findElement<std::string>(lastInfo(), "score").value_or("ERR");
444-
445-
if (score == "ERR") return ScoreType::ERR;
446-
if (score == "cp") return ScoreType::CP;
447-
if (score == "mate") return ScoreType::MATE;
448-
449-
return ScoreType::ERR;
450-
}
451-
452442
std::chrono::milliseconds UciEngine::lastTime() const {
453443
std::vector<std::string> last_reported_time_info;
454444

@@ -473,12 +463,26 @@ std::chrono::milliseconds UciEngine::lastTime() const {
473463
return std::chrono::milliseconds(time);
474464
}
475465

476-
int64_t UciEngine::lastScore() const {
477-
const auto score = lastScoreType();
466+
Score UciEngine::lastScore() const {
467+
const auto info = lastInfo();
468+
469+
Score score;
470+
471+
score.value = 0;
472+
score.type = [this, &info]() {
473+
auto type_str = str_utils::findElement<std::string>(info, "score").value_or("ERR");
474+
475+
if (type_str == "cp") return ScoreType::CP;
476+
if (type_str == "mate") return ScoreType::MATE;
477+
478+
return ScoreType::ERR;
479+
}();
480+
481+
if (score.type == ScoreType::ERR) return score;
478482

479-
if (score == ScoreType::ERR) return 0;
483+
score.value = str_utils::findElement<int64_t>(info, score.type == ScoreType::CP ? "cp" : "mate").value_or(0);
480484

481-
return str_utils::findElement<int64_t>(lastInfo(), lastScoreType() == ScoreType::CP ? "cp" : "mate").value_or(0);
485+
return score;
482486
}
483487

484488
bool UciEngine::outputIncludesBestmove() const {

app/src/engine/uci_engine.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace fastchess::engine {
2121

2222
enum class ScoreType { CP, MATE, ERR };
2323

24+
struct Score {
25+
ScoreType type;
26+
int64_t value;
27+
};
28+
2429
class UciEngine {
2530
public:
2631
explicit UciEngine(const EngineConfiguration &config, bool realtime_logging);
@@ -106,14 +111,10 @@ class UciEngine {
106111
// Get the last info from the last output.
107112
[[nodiscard]] std::vector<std::string> lastInfo() const;
108113

109-
// Get the last score type from the last output. cp or mate.
110-
[[nodiscard]] ScoreType lastScoreType() const;
111-
112114
[[nodiscard]] std::chrono::milliseconds lastTime() const;
113115

114-
// Get the last score from the last output. Becareful, mate scores are not converted. So
115-
// the score might 1, while it's actually mate 1. Always check lastScoreType() first.
116-
[[nodiscard]] int64_t lastScore() const;
116+
// Get the last score from the last output.
117+
[[nodiscard]] Score lastScore() const;
117118

118119
// returns false if the output doesnt include a bestmove
119120
[[nodiscard]] bool outputIncludesBestmove() const;

app/src/matchmaking/match/match.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ void Match::addMoveData(const Player& player, int64_t measured_time_ms, int64_t
141141
}
142142

143143
// extract last info line
144-
const auto score_type = player.engine.lastScoreType();
145-
const auto info = player.engine.lastInfo();
144+
const auto score = player.engine.lastScore();
145+
const auto info = player.engine.lastInfo();
146146

147147
move_data.nps = str_utils::findElement<uint64_t>(info, "nps").value_or(0);
148148
move_data.hashfull = str_utils::findElement<int64_t>(info, "hashfull").value_or(0);
@@ -151,11 +151,11 @@ void Match::addMoveData(const Player& player, int64_t measured_time_ms, int64_t
151151
move_data.seldepth = str_utils::findElement<int64_t>(info, "seldepth").value_or(0);
152152
move_data.nodes = str_utils::findElement<uint64_t>(info, "nodes").value_or(0);
153153
move_data.pv = str_utils::join(extractPvFromInfo(info).value_or(std::vector<std::string>{}), " ");
154-
move_data.score = player.engine.lastScore();
154+
move_data.score = score.value;
155155
move_data.timeleft = timeleft;
156156
move_data.latency = latency;
157157

158-
move_data.score_string = Match::convertScoreToString(move_data.score, score_type);
158+
move_data.score_string = Match::convertScoreToString(move_data.score, score.type);
159159

160160
if (!config::TournamentConfig->pgn.additional_lines_rgx.empty()) {
161161
for (const auto& rgx : config::TournamentConfig->pgn.additional_lines_rgx) {
@@ -401,10 +401,9 @@ bool Match::playMove(Player& us, Player& them) {
401401
// pgn and epd adjudication. fastchess fixes this by using the fullmove counter from the board
402402
// object directly
403403
auto score = us.engine.lastScore();
404-
auto type = us.engine.lastScoreType();
405404

406-
draw_tracker_.update(score, type, board_.halfMoveClock());
407-
resign_tracker_.update(score, type, ~board_.sideToMove());
405+
draw_tracker_.update(score, board_.halfMoveClock());
406+
resign_tracker_.update(score, ~board_.sideToMove());
408407
maxmoves_tracker_.update();
409408

410409
return true;
@@ -634,7 +633,7 @@ bool Match::adjudicate(Player& us, Player& them) noexcept {
634633
}
635634
}
636635

637-
if (config::TournamentConfig->resign.enabled && resign_tracker_.resignable() && us.engine.lastScore() < 0) {
636+
if (config::TournamentConfig->resign.enabled && resign_tracker_.resignable() && us.engine.lastScore().value < 0) {
638637
us.setLost();
639638
them.setWon();
640639

app/src/matchmaking/match/match.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class DrawTracker {
1919
DrawTracker(config::DrawAdjudication draw_adjudication)
2020
: DrawTracker(draw_adjudication.move_number, draw_adjudication.move_count, draw_adjudication.score) {}
2121

22-
void update(const int score, engine::ScoreType score_type, const int hmvc) noexcept {
22+
void update(const engine::Score& score, const int hmvc) noexcept {
2323
if (hmvc == 0) draw_moves_ = 0;
2424

2525
if (move_count_ > 0) {
26-
if (std::abs(score) <= draw_score_ && score_type == engine::ScoreType::CP) {
26+
if (std::abs(score.value) <= draw_score_ && score.type == engine::ScoreType::CP) {
2727
draw_moves_++;
2828
} else {
2929
draw_moves_ = 0;
@@ -54,18 +54,18 @@ class ResignTracker {
5454
ResignTracker(config::ResignAdjudication resign_adjudication)
5555
: ResignTracker(resign_adjudication.score, resign_adjudication.move_count, resign_adjudication.twosided) {}
5656

57-
void update(const int score, engine::ScoreType score_type, chess::Color color) noexcept {
57+
void update(const engine::Score& score, chess::Color color) noexcept {
5858
if (twosided_) {
59-
if ((std::abs(score) >= resign_score && score_type == engine::ScoreType::CP) ||
60-
score_type == engine::ScoreType::MATE) {
59+
if ((std::abs(score.value) >= resign_score && score.type == engine::ScoreType::CP) ||
60+
score.type == engine::ScoreType::MATE) {
6161
resign_moves++;
6262
} else {
6363
resign_moves = 0;
6464
}
6565
} else {
6666
int& counter = (color == chess::Color::BLACK) ? resign_moves_black : resign_moves_white;
67-
if ((score <= -resign_score && score_type == engine::ScoreType::CP) ||
68-
(score < 0 && score_type == engine::ScoreType::MATE)) {
67+
if ((score.value <= -resign_score && score.type == engine::ScoreType::CP) ||
68+
(score.value < 0 && score.type == engine::ScoreType::MATE)) {
6969
counter++;
7070
} else {
7171
counter = 0;

app/tests/match_test.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,71 +18,71 @@ TEST_SUITE("Match Test") {
1818
TEST_CASE("DrawTracker") {
1919
SUBCASE("adjudicates when conditions met") {
2020
DrawTracker dt(10, 3, 50);
21-
for (int i = 0; i < 6; ++i) dt.update(40, engine::ScoreType::CP, 1);
21+
for (int i = 0; i < 6; ++i) dt.update({engine::ScoreType::CP, 40}, 1);
2222
CHECK(dt.adjudicatable(10));
2323
}
2424

2525
SUBCASE("does not adjudicate before move_number") {
2626
DrawTracker dt(10, 3, 50);
27-
for (int i = 0; i < 6; ++i) dt.update(40, engine::ScoreType::CP, 1);
27+
for (int i = 0; i < 6; ++i) dt.update({engine::ScoreType::CP, 40}, 1);
2828
CHECK_FALSE(dt.adjudicatable(9));
2929
}
3030

3131
SUBCASE("hmvc resets counter") {
3232
DrawTracker dt(10, 3, 50);
33-
dt.update(40, engine::ScoreType::CP, 0);
34-
dt.update(40, engine::ScoreType::CP, 1);
33+
dt.update({engine::ScoreType::CP, 40}, 0);
34+
dt.update({engine::ScoreType::CP, 40}, 1);
3535
CHECK_FALSE(dt.adjudicatable(10));
3636
}
3737

3838
SUBCASE("non-CP score does not count") {
3939
DrawTracker dt(10, 1, 0);
40-
dt.update(0, engine::ScoreType::MATE, 1);
40+
dt.update({engine::ScoreType::MATE, 0}, 1);
4141
CHECK_FALSE(dt.adjudicatable(10));
4242
}
4343

4444
SUBCASE("default parameters") {
4545
DrawTracker dt(0, 1, 0);
46-
dt.update(0, engine::ScoreType::CP, 1);
47-
dt.update(0, engine::ScoreType::CP, 1);
46+
dt.update({engine::ScoreType::CP, 0}, 1);
47+
dt.update({engine::ScoreType::CP, 0}, 1);
4848
CHECK(dt.adjudicatable(0));
4949
}
5050
}
5151

5252
TEST_CASE("ResignTracker") {
5353
SUBCASE("twosided resigns after move_count") {
5454
ResignTracker rt(100, 2, true);
55-
rt.update(150, engine::ScoreType::CP, chess::Color::WHITE);
56-
rt.update(-150, engine::ScoreType::CP, chess::Color::BLACK);
57-
rt.update(150, engine::ScoreType::CP, chess::Color::WHITE);
58-
rt.update(-150, engine::ScoreType::CP, chess::Color::BLACK);
55+
rt.update({engine::ScoreType::CP, 150}, chess::Color::WHITE);
56+
rt.update({engine::ScoreType::CP, -150}, chess::Color::BLACK);
57+
rt.update({engine::ScoreType::CP, 150}, chess::Color::WHITE);
58+
rt.update({engine::ScoreType::CP, -150}, chess::Color::BLACK);
5959
CHECK(rt.resignable());
6060
}
6161

6262
SUBCASE("twosided resets on non-qualifying score") {
6363
ResignTracker rt(100, 2, true);
64-
rt.update(150, engine::ScoreType::CP, chess::Color::WHITE);
65-
rt.update(50, engine::ScoreType::CP, chess::Color::BLACK);
64+
rt.update({engine::ScoreType::CP, 150}, chess::Color::WHITE);
65+
rt.update({engine::ScoreType::CP, 50}, chess::Color::BLACK);
6666
CHECK_FALSE(rt.resignable());
6767
}
6868

6969
SUBCASE("non-twosided white resigns") {
7070
ResignTracker rt(100, 2, false);
71-
rt.update(-150, engine::ScoreType::CP, chess::Color::WHITE);
72-
rt.update(-150, engine::ScoreType::CP, chess::Color::WHITE);
71+
rt.update({engine::ScoreType::CP, -150}, chess::Color::WHITE);
72+
rt.update({engine::ScoreType::CP, -150}, chess::Color::WHITE);
7373
CHECK(rt.resignable());
7474
}
7575

7676
SUBCASE("non-twosided black resigns") {
7777
ResignTracker rt(100, 2, false);
78-
rt.update(-150, engine::ScoreType::CP, chess::Color::BLACK);
79-
rt.update(-150, engine::ScoreType::CP, chess::Color::BLACK);
78+
rt.update({engine::ScoreType::CP, -150}, chess::Color::BLACK);
79+
rt.update({engine::ScoreType::CP, -150}, chess::Color::BLACK);
8080
CHECK(rt.resignable());
8181
}
8282

8383
SUBCASE("mate score triggers resign") {
8484
ResignTracker rt(0, 1, false);
85-
rt.update(-1, engine::ScoreType::MATE, chess::Color::WHITE);
85+
rt.update({engine::ScoreType::MATE, -1}, chess::Color::WHITE);
8686
CHECK(rt.resignable());
8787
}
8888
}

0 commit comments

Comments
 (0)