Skip to content

Commit

Permalink
Fix web logger and adjust timing
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeybohm committed Sep 13, 2024
1 parent dfd8ba3 commit a6fa3c0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
17 changes: 10 additions & 7 deletions engine/src/move_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace wisdom

auto MoveTimer::isTriggered() -> bool
{
if (!my_timer_state.my_started_time.has_value())
if (!my_timer_state.started_time.has_value())
return false;

if (my_timer_state.triggered || my_timer_state.cancelled)
Expand All @@ -25,31 +25,34 @@ namespace wisdom
}

steady_clock::time_point check_time = steady_clock::now();
auto diff_time = check_time - *my_timer_state.my_started_time;
auto diff_time = check_time - *my_timer_state.started_time;

// adjust the next iteration if less than 250ms:
if (my_timer_state.my_last_check_time.has_value())
if (my_timer_state.last_check_time.has_value())
{
auto last_time = *my_timer_state.my_last_check_time;
auto last_time = *my_timer_state.last_check_time;
auto check_time_diff = check_time - last_time;

auto& timing = *my_timing_adjustment;
if (
check_time_diff < chrono::milliseconds(250) && timing.current_iterations < Max_Iterations_Before_Checking
check_time_diff < Lower_Bound_Timer_Check &&
timing.current_iterations < Max_Iterations_Before_Checking
) {
timing.current_iterations = narrow<int> (std::floor (timing.current_iterations * 1.5));
if (timing.current_iterations > Max_Iterations_Before_Checking)
timing.current_iterations = Max_Iterations_Before_Checking;

} else if (
check_time_diff > chrono::milliseconds (500) && timing.current_iterations > Min_Iterations_Before_Checking
check_time_diff > Upper_Bound_Timer_Check &&
timing.current_iterations > Min_Iterations_Before_Checking
) {
timing.current_iterations /= 2;
if (timing.current_iterations < Min_Iterations_Before_Checking)
timing.current_iterations = Min_Iterations_Before_Checking;
}
}

my_timer_state.my_last_check_time = check_time;
my_timer_state.last_check_time = check_time;

if (diff_time >= my_seconds)
{
Expand Down
11 changes: 8 additions & 3 deletions engine/src/move_timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace wisdom
inline constexpr int Min_Iterations_Before_Checking = 10'000;
inline constexpr int Max_Iterations_Before_Checking = 1'000'000;

inline constexpr chrono::milliseconds Lower_Bound_Timer_Check =
chrono::milliseconds { 100 };
inline constexpr chrono::milliseconds Upper_Bound_Timer_Check =
chrono::milliseconds { 150 };

struct MoveTimer;

struct TimingAdjustment
Expand All @@ -16,8 +21,8 @@ namespace wisdom

struct TimerState
{
optional<chrono::steady_clock::time_point> my_started_time {};
optional<chrono::steady_clock::time_point> my_last_check_time {};
optional<chrono::steady_clock::time_point> started_time {};
optional<chrono::steady_clock::time_point> last_check_time {};

int check_calls = 0;
bool triggered = false;
Expand Down Expand Up @@ -51,7 +56,7 @@ namespace wisdom
{
// Reset the state but preserve a few values:
my_timer_state = TimerState {};
my_timer_state.my_started_time = chrono::steady_clock::now();
my_timer_state.started_time = chrono::steady_clock::now();
}

[[nodiscard]] auto
Expand Down
10 changes: 4 additions & 6 deletions ui/wasm/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ EMSCRIPTEN_KEEPALIVE void workerReinitializeGame (int new_game_id)

EMSCRIPTEN_KEEPALIVE void startSearch()
{
const auto& logger = wisdom::worker::getLogger();
auto logger = wisdom::worker::makeLogger();
auto state = GameState::getState();
auto game = GameState::getGame();

Expand All @@ -184,8 +184,8 @@ EMSCRIPTEN_KEEPALIVE void startSearch()
if (new_status != wisdom::GameStatus::Playing)
return;

logger.debug("Going to find best move");
logger.debug("Current turn: " + asString (game->getCurrentTurn()));
logger->debug("Going to find best move");
logger->debug("Current turn: " + asString (game->getCurrentTurn()));

auto move = game->findBestMove(
logger,
Expand All @@ -194,7 +194,7 @@ EMSCRIPTEN_KEEPALIVE void startSearch()
if (!move.has_value())
{
// Could happen if game is paused:
getLogger().debug("No move found.");
logger->debug("No move found.");
return;
}
game->move (*move);
Expand Down Expand Up @@ -232,8 +232,6 @@ EM_JS (void, receiveMoveFromWorker, (int game_id, const char* str),

EMSCRIPTEN_KEEPALIVE void mainThreadReceiveMove (int game_id, int packed_move)
{
auto& logger = wisdom::worker::getLogger();

Move unpacked_move = Move::fromInt (packed_move);
auto state = GameState::getState();
std::string str = asString (unpacked_move);
Expand Down
6 changes: 3 additions & 3 deletions ui/wasm/web_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace wisdom::worker
static void consoleLog (const char* str);
};

[[nodiscard]] inline auto getLogger() -> const WebLogger&
[[nodiscard]] inline auto makeLogger() -> shared_ptr<WebLogger>
{
using namespace wisdom;
static std::unique_ptr<WebLogger> worker_logger = std::make_unique<WebLogger>();
return *worker_logger;
static std::shared_ptr<WebLogger> worker_logger = std::make_shared<WebLogger>();
return worker_logger;
}
}

0 comments on commit a6fa3c0

Please sign in to comment.