From d4dbcc85fb14d7554965e4ae9a676a12118f1acd Mon Sep 17 00:00:00 2001 From: sebastian Date: Wed, 5 Aug 2026 07:10:27 +0200 Subject: [PATCH] UPD: Enhance test and output logs --- game/src/GameLayer.cpp | 3 +++ game/src/ludo/LudoGameMode.cpp | 18 ++++++++++++++++-- tests/LoduGameMode.cpp | 29 ++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/game/src/GameLayer.cpp b/game/src/GameLayer.cpp index ddef115..439c0a7 100644 --- a/game/src/GameLayer.cpp +++ b/game/src/GameLayer.cpp @@ -78,7 +78,10 @@ void GameLayer::onUpdate(float deltaTime) { if (auto entity = m_pickingSystem.update(getMouse(), m_entityRegistry)) { if (auto pieceIndex = m_piecePresenter.getPieceIndex(*entity)) { + spdlog::info("Sende MovePieceCommand fuer pieceIndex={}", *pieceIndex); m_gameMode->sendCommand(ludo::MovePieceCommand{m_localPlayer, *pieceIndex}); + } else { + spdlog::warn("Entity {} getroffen, aber keine bekannte Spielfigur (getPieceIndex = nullopt)", entity->value()); } } } diff --git a/game/src/ludo/LudoGameMode.cpp b/game/src/ludo/LudoGameMode.cpp index ff9747d..06b8e1b 100644 --- a/game/src/ludo/LudoGameMode.cpp +++ b/game/src/ludo/LudoGameMode.cpp @@ -7,6 +7,8 @@ #include #include +#include "spdlog/spdlog.h" + void ludo::LudoGameMode::sendCommand(const std::variant &command) { std::visit([this](const auto& cmd) { handle(cmd);}, command); } @@ -124,10 +126,22 @@ ludo::PlayerColor ludo::LudoGameMode::getColorOf(engine::PlayerID player) const } void ludo::LudoGameMode::handle(const MovePieceCommand &command) { - if (!isCurrentPlayer(command.player) || m_gameState.m_phase != TurnPhase::AwaitingPieceSelection) return; + if (!isCurrentPlayer(command.player)) { + spdlog::warn("MovePieceCommand abgewiesen: {} ist nicht am Zug (aktuell: {})", command.player, getCurrentPlayer()); + return; + } + if (m_gameState.m_phase != TurnPhase::AwaitingPieceSelection) { + spdlog::warn("MovePieceCommand abgewiesen: falsche Phase ({})", static_cast(m_gameState.m_phase)); + return; + } auto movable = getMoveablePieceIndices(getColorOf(command.player), m_gameState.m_diceValue); - if (std::find(movable.begin(), movable.end(), command.pieceIndex) == movable.end()) return; // ungültiger Zug, abweisen + if (std::find(movable.begin(), movable.end(), command.pieceIndex) == movable.end()) { + spdlog::warn("MovePieceCommand abgewiesen: pieceIndex={} nicht in movable-Liste", command.pieceIndex); + return; + } + // ungültiger Zug, abweisen + spdlog::info("MovePieceCommand akzeptiert: pieceIndex={}", command.pieceIndex); auto result = movePieceInternal(command.pieceIndex, m_gameState.m_diceValue); m_pendingEvents.emplace_back(PieceMovedEvent{command.pieceIndex, result.fromPosition, result.toPosition, result.captured}); diff --git a/tests/LoduGameMode.cpp b/tests/LoduGameMode.cpp index 5f8028b..c396a1b 100644 --- a/tests/LoduGameMode.cpp +++ b/tests/LoduGameMode.cpp @@ -6,14 +6,33 @@ #include "ludo/LudoGameMode.h" TEST(LudoGameMode, SecondSixMovesOutOfBlockedEntry) { - ludo::LudoGameMode mode({0}); // ein Spieler reicht zum Isolieren - mode.setDiceSource(std::make_unique(std::vector{6,6})); + ludo::LudoGameMode mode({0}); + mode.setDiceSource(std::make_unique(std::vector{6, 6})); mode.sendCommand(ludo::RollDiceCommand{0}); - ASSERT_EQ(mode.getCurrentPlayer(), 0u) << "Spieler sollte nach Extra-Turn gleich bleiben"; + mode.pollEvents(); // Events des ersten Wurfs verwerfen, nicht Teil dieser Prüfung + ASSERT_EQ(mode.getCurrentPlayer(), 0u); ASSERT_EQ(mode.getState().getPhase(), ludo::TurnPhase::AwaitingRoll); mode.sendCommand(ludo::RollDiceCommand{0}); - EXPECT_EQ(mode.getState().getPhase(), ludo::TurnPhase::AwaitingPieceSelection) - << "Erwartet: Figur-Auswahl, da Entry blockiert"; + mode.pollEvents(); // Events des zweiten Wurfs verwerfen + ASSERT_EQ(mode.getState().getPhase(), ludo::TurnPhase::AwaitingPieceSelection); + + auto movable = mode.getMoveablePieceIndices(ludo::PlayerColor::Red, mode.getState().getDiceValue()); + ASSERT_EQ(movable.size(), 1u); + ASSERT_EQ(movable[0], 0); + + mode.sendCommand(ludo::MovePieceCommand{0, 0}); + + auto events = mode.pollEvents(); // jetzt NUR die Events dieses einen Commands + ASSERT_FALSE(events.empty()); + auto* moved = std::get_if(&events.front()); + ASSERT_NE(moved, nullptr); + EXPECT_EQ(moved->fromPosition, 0); + EXPECT_EQ(moved->toPosition, 6); + EXPECT_FALSE(moved->captured); + + const auto& piece = mode.getState().getPieces()[0]; + EXPECT_EQ(piece.state, ludo::PieceState::OnTrack); + EXPECT_EQ(piece.position, 6); } \ No newline at end of file