Skip to content

Commit ba7805a

Browse files
authored
refactor: cleanup code (#945)
- increase some datatypes from int to int64_t - less nesting - more readable MoveData - assert
1 parent c4026f0 commit ba7805a

File tree

5 files changed

+81
-62
lines changed

5 files changed

+81
-62
lines changed

app/src/core/helper.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ template <typename T>
5656
return std::stof(haystack[index + 1]);
5757
else if constexpr (std::is_same_v<T, uint64_t>)
5858
return std::stoull(haystack[index + 1]);
59+
else if constexpr (std::is_same_v<T, int64_t>)
60+
return std::stoll(haystack[index + 1]);
5961
else
6062
return haystack[index + 1];
6163
}

app/src/engine/uci_engine.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,19 +468,17 @@ std::chrono::milliseconds UciEngine::lastTime() const {
468468
break;
469469
}
470470

471-
const auto time = str_utils::findElement<int>(last_reported_time_info, "time").value_or(0);
471+
const auto time = str_utils::findElement<int64_t>(last_reported_time_info, "time").value_or(0);
472472

473473
return std::chrono::milliseconds(time);
474474
}
475475

476-
int UciEngine::lastScore() const {
476+
int64_t UciEngine::lastScore() const {
477477
const auto score = lastScoreType();
478478

479-
if (score == ScoreType::ERR) {
480-
return 0;
481-
}
479+
if (score == ScoreType::ERR) return 0;
482480

483-
return str_utils::findElement<int>(lastInfo(), lastScoreType() == ScoreType::CP ? "cp" : "mate").value_or(0);
481+
return str_utils::findElement<int64_t>(lastInfo(), lastScoreType() == ScoreType::CP ? "cp" : "mate").value_or(0);
484482
}
485483

486484
bool UciEngine::outputIncludesBestmove() const {

app/src/engine/uci_engine.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,17 @@ class UciEngine {
8585
return;
8686
#endif
8787

88-
auto ret = process_.setAffinity(cpus);
89-
90-
if (!ret) {
91-
// turn cpus vector into a string for logging
92-
std::string cpu_str;
93-
for (const auto &cpu : cpus) {
94-
if (!cpu_str.empty()) cpu_str += ", ";
95-
cpu_str += std::to_string(cpu);
96-
}
97-
98-
Logger::print<Logger::Level::WARN>(
99-
"Warning; Failed to set CPU affinity for the engine process to {}. Please restart.", cpu_str);
88+
if (process_.setAffinity(cpus)) return;
89+
90+
// turn cpus vector into a string for error logging
91+
std::string cpu_str;
92+
for (const auto &cpu : cpus) {
93+
if (!cpu_str.empty()) cpu_str += ", ";
94+
cpu_str += std::to_string(cpu);
10095
}
96+
97+
Logger::print<Logger::Level::WARN>(
98+
"Warning; Failed to set CPU affinity for the engine process to {}. Please restart.", cpu_str);
10199
}
102100

103101
// Get the bestmove from the last output.
@@ -115,7 +113,7 @@ class UciEngine {
115113

116114
// Get the last score from the last output. Becareful, mate scores are not converted. So
117115
// the score might 1, while it's actually mate 1. Always check lastScoreType() first.
118-
[[nodiscard]] int lastScore() const;
116+
[[nodiscard]] int64_t lastScore() const;
119117

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

app/src/matchmaking/match/match.cpp

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ Match::Match(const book::Opening& opening)
9999
const auto move = uci::moveToUci(opening_move, board_.chess960());
100100
board_.makeMove<true>(opening_move);
101101

102-
return MoveData(move, "0.00", 0, 0, 0, 0, 0, true, true);
102+
MoveData move_data;
103+
104+
move_data.move = move;
105+
move_data.score_string = "0.00";
106+
move_data.book = true;
107+
move_data.legal = true;
108+
109+
return move_data;
103110
};
104111

105112
std::transform(opening_.moves.begin(), opening_.moves.end(), std::back_inserter(data_.moves), insert_move);
@@ -120,7 +127,12 @@ std::string Match::convertScoreToString(int score, engine::ScoreType score_type)
120127
void Match::addMoveData(const Player& player, int64_t measured_time_ms, int64_t latency, int64_t timeleft, bool legal) {
121128
const auto move = player.engine.bestmove().value_or("<none>");
122129

123-
MoveData move_data = MoveData(move, "0.00", measured_time_ms, 0, 0, 0, 0, legal);
130+
MoveData move_data;
131+
132+
move_data.move = move;
133+
move_data.score_string = "0.00";
134+
move_data.elapsed_millis = measured_time_ms;
135+
move_data.legal = legal;
124136

125137
if (player.engine.output().size() <= 1) {
126138
data_.moves.push_back(move_data);
@@ -133,10 +145,10 @@ void Match::addMoveData(const Player& player, int64_t measured_time_ms, int64_t
133145
const auto info = player.engine.lastInfo();
134146

135147
move_data.nps = str_utils::findElement<uint64_t>(info, "nps").value_or(0);
136-
move_data.hashfull = str_utils::findElement<int>(info, "hashfull").value_or(0);
148+
move_data.hashfull = str_utils::findElement<int64_t>(info, "hashfull").value_or(0);
137149
move_data.tbhits = str_utils::findElement<uint64_t>(info, "tbhits").value_or(0);
138-
move_data.depth = str_utils::findElement<int>(info, "depth").value_or(0);
139-
move_data.seldepth = str_utils::findElement<int>(info, "seldepth").value_or(0);
150+
move_data.depth = str_utils::findElement<int64_t>(info, "depth").value_or(0);
151+
move_data.seldepth = str_utils::findElement<int64_t>(info, "seldepth").value_or(0);
140152
move_data.nodes = str_utils::findElement<uint64_t>(info, "nodes").value_or(0);
141153
move_data.pv = str_utils::join(extractPvFromInfo(info).value_or(std::vector<std::string>{}), " ");
142154
move_data.score = player.engine.lastScore();
@@ -541,35 +553,39 @@ void Match::verifyPvLines(const Player& us) {
541553

542554
const auto illegal_move = std::find(moves.begin(), moves.end(), uci::uciToMove(board, move)) == moves.end();
543555

544-
if (gameover || illegal_move) {
545-
std::string warning;
546-
if (illegal_move) {
547-
warning = "Warning; Illegal PV move - move {} from {}";
548-
} else if (gameoverResult.first == GameResultReason::THREEFOLD_REPETITION) {
549-
warning = "Warning; PV continues after threefold repetition - move {} from {}";
550-
} else if (gameoverResult.first == GameResultReason::FIFTY_MOVE_RULE) {
551-
warning = "Warning; PV continues after fifty-move rule - move {} from {}";
552-
} else if (gameoverResult.first == GameResultReason::CHECKMATE) {
553-
warning = "Warning; PV continues after checkmate - move {} from {}";
554-
} else if (gameoverResult.first == GameResultReason::STALEMATE) {
555-
warning = "Warning; PV continues after stalemate - move {} from {}";
556-
}
556+
if (!gameover && !illegal_move) {
557+
board.makeMove<true>(uci::uciToMove(board, move));
558+
continue;
559+
}
557560

558-
assert(!warning.empty());
561+
// illegal move or gameover reached
559562

560-
auto out = fmt::format(fmt::runtime(warning), move, name);
561-
auto uci_info = fmt::format("Info; {}", info);
562-
auto position = fmt::format("Position; {}", startpos == "startpos" ? "startpos" : ("fen " + startpos));
563-
auto moves = fmt::format("Moves; {}", str_utils::join(uci_moves, " "));
563+
std::string warning;
564564

565-
auto separator = config::TournamentConfig->test_env ? " :: " : "\n";
565+
if (illegal_move) {
566+
warning = "Warning; Illegal PV move - move {} from {}";
567+
} else if (gameoverResult.first == GameResultReason::THREEFOLD_REPETITION) {
568+
warning = "Warning; PV continues after threefold repetition - move {} from {}";
569+
} else if (gameoverResult.first == GameResultReason::FIFTY_MOVE_RULE) {
570+
warning = "Warning; PV continues after fifty-move rule - move {} from {}";
571+
} else if (gameoverResult.first == GameResultReason::CHECKMATE) {
572+
warning = "Warning; PV continues after checkmate - move {} from {}";
573+
} else if (gameoverResult.first == GameResultReason::STALEMATE) {
574+
warning = "Warning; PV continues after stalemate - move {} from {}";
575+
}
566576

567-
Logger::print<Logger::Level::WARN>("{1}{0}{2}{0}{3}{0}{4}", separator, out, uci_info, position, moves);
577+
assert(!warning.empty());
568578

569-
break;
570-
}
579+
auto out = fmt::format(fmt::runtime(warning), move, name);
580+
auto uci_info = fmt::format("Info; {}", info);
581+
auto position = fmt::format("Position; {}", startpos == "startpos" ? "startpos" : ("fen " + startpos));
582+
auto moves = fmt::format("Moves; {}", str_utils::join(uci_moves, " "));
583+
584+
auto separator = config::TournamentConfig->test_env ? " :: " : "\n";
585+
586+
Logger::print<Logger::Level::WARN>("{1}{0}{2}{0}{3}{0}{4}", separator, out, uci_info, position, moves);
571587

572-
board.makeMove<true>(uci::uciToMove(board, move));
588+
break;
573589
}
574590
};
575591

@@ -674,6 +690,7 @@ std::string Match::convertChessReason(const std::string& color, GameResultReason
674690
return Match::FIFTY_MSG;
675691
}
676692

693+
assert(false && "Unhandled GameResultReason in convertChessReason");
677694
return "";
678695
}
679696

app/src/types/match_data.hpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,39 @@
1515
namespace fastchess {
1616

1717
struct MoveData {
18+
MoveData() = default;
1819
MoveData(std::string _move, std::string _score_string, int64_t _elapsed_millis, int _depth, int _seldepth,
1920
int _score, int _nodes, bool _legal = true, bool _book = false, std::string _pv = "")
2021
: move(std::move(_move)),
2122
score_string(std::move(_score_string)),
22-
elapsed_millis(_elapsed_millis),
23+
pv(_pv),
2324
nodes(_nodes),
24-
seldepth(_seldepth),
25+
elapsed_millis(_elapsed_millis),
2526
depth(_depth),
27+
seldepth(_seldepth),
2628
score(_score),
2729
legal(_legal),
28-
book(_book),
29-
pv(_pv) {}
30+
book(_book) {}
3031

3132
std::vector<std::string> additional_lines;
3233
std::string move;
3334
std::string score_string;
35+
std::string pv = "";
36+
37+
uint64_t nodes = 0;
38+
uint64_t nps = 0;
39+
uint64_t tbhits = 0;
40+
3441
int64_t elapsed_millis = 0;
42+
int64_t depth = 0;
43+
int64_t seldepth = 0;
44+
int64_t score = 0;
3545
int64_t timeleft = 0;
3646
int64_t latency = 0;
37-
uint64_t nodes = 0;
38-
int seldepth = 0;
39-
int depth = 0;
40-
int score = 0;
41-
uint64_t nps = 0;
42-
int hashfull = 0;
43-
uint64_t tbhits = 0;
44-
bool legal = true;
45-
bool book = false;
46-
std::string pv = "";
47+
int64_t hashfull = 0;
48+
49+
bool legal = true;
50+
bool book = false;
4751
};
4852

4953
enum class MatchTermination {
@@ -65,7 +69,7 @@ struct MatchData {
6569

6670
MatchData() {}
6771

68-
explicit MatchData(std::string fen) : fen(std::move(fen)) {
72+
explicit MatchData(std::string_view f) : fen(f) {
6973
start_time = time::datetime_iso();
7074
date = time::datetime("%Y.%m.%d").value_or("-");
7175
}

0 commit comments

Comments
 (0)