Skip to content

Commit

Permalink
fix: timelosses (#312)
Browse files Browse the repository at this point in the history
fixes #310
  • Loading branch information
Disservin authored Apr 28, 2024
1 parent 407aee2 commit 8ac850d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/matchmaking/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Player {
explicit Player(UciEngine &uci_enigne)
: engine(uci_enigne), time_control_(uci_enigne.getConfig().limit.tc) {
if (time_control_.fixed_time != 0) {
time_left_ = time_control_.fixed_time;
time_control_.time_left = time_control_.fixed_time;
} else {
time_left_ = time_control_.time;
time_control_.time_left = time_control_.time;
}
}

Expand All @@ -28,29 +28,29 @@ class Player {
return std::chrono::milliseconds(0);
}

return std::chrono::milliseconds(time_left_ + 100) /* margin*/;
return std::chrono::milliseconds(time_control_.time_left + 100) /* margin*/;
}

/// @brief remove the elapsed time from the participant's time
/// @param elapsed_millis
/// @return `false` when out of time
[[nodiscard]] bool updateTime(const int64_t elapsed_millis) {
const auto &tc = engine.getConfig().limit.tc;
auto &tc = time_control_;
if (tc.time == 0) {
return true;
}

time_left_ -= elapsed_millis;
tc.time_left -= elapsed_millis;

if (time_left_ < -tc.timemargin) {
if (tc.time_left < -tc.timemargin) {
return false;
}

if (time_left_ < 0) {
time_left_ = 0;
if (tc.time_left < 0) {
tc.time_left = 0;
}

time_left_ += time_control_.increment;
tc.time_left += time_control_.increment;

return true;
}
Expand Down Expand Up @@ -116,10 +116,7 @@ class Player {
chess::GameResult result = chess::GameResult::NONE;

private:
const TimeControl time_control_;

/// @brief updated time control after each move
int64_t time_left_;
TimeControl time_control_;
};

} // namespace fast_chess
3 changes: 3 additions & 0 deletions src/types/engine_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ struct TimeControl {
int moves = 0;

int timemargin = 0;

/// @brief updated time control after each move
int64_t time_left;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ORDERED_JSON(TimeControl, increment, fixed_time, time, moves,
timemargin)
Expand Down

0 comments on commit 8ac850d

Please sign in to comment.