Skip to content

Small C wrapper compatibility fixes #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Game::Game(
tetrion_index](InputEvent event, SimulationStep simulation_step_index) {
spdlog::debug("event: {} (step {})", magic_enum::enum_name(event), simulation_step_index);

recording_writer->add_event(tetrion_index, simulation_step_index, event);
//TODO(Totto): Remove all occurrences of std::ignore, where we shouldn't ignore this return value
std::ignore = recording_writer->add_record(tetrion_index, simulation_step_index, event);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/game/tetrion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void Tetrion::spawn_next_tetromino(const helper::TetrominoType type, const Simul
spdlog::info("game over");
if (m_recording_writer.has_value()) {
spdlog::info("writing snapshot");
m_recording_writer.value()->add_snapshot(m_tetrion_index, simulation_step_index, core_information());
std::ignore = m_recording_writer.value()->add_snapshot(simulation_step_index, core_information());
}
m_active_tetromino = {};
m_ghost_tetromino = {};
Expand Down Expand Up @@ -456,7 +456,7 @@ void Tetrion::lock_active_tetromino(const SimulationStep simulation_step_index)
#if !defined(NDEBUG)
if (m_recording_writer) {
spdlog::debug("adding snapshot at step {}", simulation_step_index);
(*m_recording_writer)->add_snapshot(m_tetrion_index, simulation_step_index, core_information());
std::ignore = (*m_recording_writer)->add_snapshot(simulation_step_index, core_information());
}
#endif
}
Expand Down
24 changes: 17 additions & 7 deletions src/libs/recordings/utility/recording_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ recorder::RecordingWriter::RecordingWriter(RecordingWriter&& old) noexcept
helper::expected<recorder::RecordingWriter, std::string> recorder::RecordingWriter::get_writer(
const std::filesystem::path& path,
std::vector<TetrionHeader>&& tetrion_headers,
AdditionalInformation&& information
AdditionalInformation&& information,
bool overwrite
) {
auto mode = std::ios::out | std::ios::binary;
if (overwrite) {
if (std::filesystem::exists(path)) {
return helper::unexpected<std::string>{
fmt::format("file already exists, not overwriting it: \"{}\"", path.string())
};
}
} else {
mode |= std::ios::trunc;
}


auto output_file = std::ofstream{ path, std::ios::out | std::ios::binary };
auto output_file = std::ofstream{ path, mode };

if (not output_file) {
return helper::unexpected<std::string>{ fmt::format("failed to open output file \"{}\"", path.string()) };
Expand Down Expand Up @@ -74,7 +86,7 @@ helper::expected<recorder::RecordingWriter, std::string> recorder::RecordingWrit
return RecordingWriter{ std::move(output_file), std::move(tetrion_headers), std::move(information) };
}

helper::expected<void, std::string> recorder::RecordingWriter::add_event(
helper::expected<void, std::string> recorder::RecordingWriter::add_record(
const u8 tetrion_index, // NOLINT(bugprone-easily-swappable-parameters)
const u64 simulation_step_index,
const InputEvent event
Expand Down Expand Up @@ -108,7 +120,6 @@ helper::expected<void, std::string> recorder::RecordingWriter::add_event(
}

helper::expected<void, std::string> recorder::RecordingWriter::add_snapshot(
const u8 tetrion_index,
const u64 simulation_step_index,
std::unique_ptr<TetrionCoreInformation> information
) {
Expand All @@ -121,9 +132,8 @@ helper::expected<void, std::string> recorder::RecordingWriter::add_snapshot(
return helper::unexpected<std::string>{ result.error() };
}

const auto snapshot = TetrionSnapshot{ tetrion_index, information->level,
information->score, information->lines_cleared,
simulation_step_index, information->mino_stack };
const auto snapshot = TetrionSnapshot{ information->tetrion_index, information->level, information->score,
information->lines_cleared, simulation_step_index, information->mino_stack };

const auto bytes = snapshot.to_bytes();
result = helper::writer::write_vector_to_file(m_output_file, bytes);
Expand Down
9 changes: 5 additions & 4 deletions src/libs/recordings/utility/recording_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ namespace recorder {
static helper::expected<RecordingWriter, std::string> get_writer(
const std::filesystem::path& path,
std::vector<TetrionHeader>&& tetrion_headers,
AdditionalInformation&& information
AdditionalInformation&& information,
bool overwrite = false
);

helper::expected<void, std::string> add_event(
[[nodiscard]] helper::expected<void, std::string> add_record(
u8 tetrion_index, // NOLINT(bugprone-easily-swappable-parameters)
u64 simulation_step_index,
InputEvent event
);
helper::expected<void, std::string>
add_snapshot(u8 tetrion_index, u64 simulation_step_index, std::unique_ptr<TetrionCoreInformation> information);
[[nodiscard]] helper::expected<void, std::string>
add_snapshot(u64 simulation_step_index, std::unique_ptr<TetrionCoreInformation> information);

private:
static helper::expected<void, std::string>
Expand Down
30 changes: 16 additions & 14 deletions src/lobby/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ namespace lobby {
// lobby commit used: https://github.com/OpenBrickProtocolFoundation/lobby/commit/2e0c8d05592f4e4d08437e6cb754a30f02c4e97c
static constexpr StaticString supported_version{ "0.1.0" };

helper::expected<void, std::string> check_compatibility();
[[nodiscard]] helper::expected<void, std::string> check_compatibility();

helper::expected<void, std::string> check_reachability();
[[nodiscard]] helper::expected<void, std::string> check_reachability();

explicit Client(const std::string& api_url);

Expand All @@ -66,30 +66,32 @@ namespace lobby {

~Client();

helper::expected<Client, std::string> static get_client(const std::string& url);
[[nodiscard]] helper::expected<Client, std::string> static get_client(const std::string& url);


bool is_authenticated();
[[nodiscard]] bool is_authenticated();

bool authenticate(const Credentials& credentials);
[[nodiscard]] bool authenticate(const Credentials& credentials);

helper::expected<std::vector<LobbyInfo>, std::string> get_lobbies();
[[nodiscard]] helper::expected<std::vector<LobbyInfo>, std::string> get_lobbies();

helper::expected<void, std::string> join_lobby(int lobby_id);
[[nodiscard]] helper::expected<void, std::string> join_lobby(int lobby_id);

helper::expected<LobbyDetail, std::string> get_lobby_detail(int lobby_id);
[[nodiscard]] helper::expected<LobbyDetail, std::string> get_lobby_detail(int lobby_id);

helper::expected<void, std::string> delete_lobby(int lobby_id);
[[nodiscard]] helper::expected<void, std::string> delete_lobby(int lobby_id);

helper::expected<void, std::string> leave_lobby(int lobby_id);
[[nodiscard]] helper::expected<void, std::string> leave_lobby(int lobby_id);

helper::expected<void, std::string> start_lobby(int lobby_id);
[[nodiscard]] helper::expected<void, std::string> start_lobby(int lobby_id);

helper::expected<LobbyCreateResponse, std::string> create_lobby(const CreateLobbyRequest& arguments);
[[nodiscard]] helper::expected<LobbyCreateResponse, std::string> create_lobby(
const CreateLobbyRequest& arguments
);

helper::expected<std::vector<PlayerInfo>, std::string> get_users();
[[nodiscard]] helper::expected<std::vector<PlayerInfo>, std::string> get_users();

helper::expected<void, std::string> register_user(const RegisterRequest& register_request);
[[nodiscard]] helper::expected<void, std::string> register_user(const RegisterRequest& register_request);
};


Expand Down