UPD: Enhance test and output logs

This commit is contained in:
sebastian 2026-08-05 07:10:27 +02:00
parent befd8b3cd9
commit d4dbcc85fb
3 changed files with 43 additions and 7 deletions

View File

@ -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());
}
}
}

View File

@ -7,6 +7,8 @@
#include <algorithm>
#include <cassert>
#include "spdlog/spdlog.h"
void ludo::LudoGameMode::sendCommand(const std::variant<MovePieceCommand, RollDiceCommand> &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<int>(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});

View File

@ -6,14 +6,33 @@
#include "ludo/LudoGameMode.h"
TEST(LudoGameMode, SecondSixMovesOutOfBlockedEntry) {
ludo::LudoGameMode mode({0}); // ein Spieler reicht zum Isolieren
ludo::LudoGameMode mode({0});
mode.setDiceSource(std::make_unique<SequenceDiceSource>(std::vector<int>{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<ludo::PieceMovedEvent>(&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);
}