From 2b6b85cce3a6008909692f243faf24b02aee578f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 20 May 2012 22:12:42 +0200 Subject: [PATCH] Arbitration: allow removing penalties --- game/arbitration.cpp | 18 ++++++++++++++---- game/arbitration.h | 3 ++- game/public_game.cpp | 7 +++++-- game/public_game.h | 2 +- qt/arbit_assignments.cpp | 8 ++++---- qt/arbit_assignments.h | 2 +- utils/eliottxt.cpp | 7 ++----- 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/game/arbitration.cpp b/game/arbitration.cpp index f00aa8b..9fea40a 100644 --- a/game/arbitration.cpp +++ b/game/arbitration.cpp @@ -139,18 +139,18 @@ bool Arbitration::hasWarning(unsigned iPlayerId) const void Arbitration::addPenalty(unsigned iPlayerId, int iPoints) { ASSERT(iPlayerId < getNPlayers(), "Wrong player number"); - ASSERT(iPoints >= 0, "Expected a positive value for the penalty"); + ASSERT(iPoints <= 0, "Expected a negative value for the penalty"); if (iPoints == 0) { // Retrieve the default value of the penalty iPoints = Settings::Instance().getInt("arbitration.default-penalty"); + + // By convention, use negative values to indicate a penalty + iPoints = -iPoints; } LOG_INFO("Giving a penalty of " << iPoints << " to player " << iPlayerId); - // By convention, use negative values to indicate a penalty - iPoints = -iPoints; - // If an existing penalty exists, merge it with the new one const PlayerEventCmd *cmd = getPlayerEvent(iPlayerId, PlayerEventCmd::PENALTY); if (cmd == 0) @@ -161,6 +161,7 @@ void Arbitration::addPenalty(unsigned iPlayerId, int iPoints) } else { + // When the resulting value is 0, instead of merging we drop the existing one Command *pCmd = new PlayerEventCmd(*m_players[iPlayerId], PlayerEventCmd::PENALTY, iPoints + cmd->getPoints()); @@ -169,6 +170,15 @@ void Arbitration::addPenalty(unsigned iPlayerId, int iPoints) } +void Arbitration::removePenalty(unsigned iPlayerId) +{ + ASSERT(iPlayerId < getNPlayers(), "Wrong player number"); + const PlayerEventCmd *cmd = getPlayerEvent(iPlayerId, PlayerEventCmd::PENALTY); + ASSERT(cmd != 0, "No penalty found for player " << iPlayerId); + accessNavigation().dropCommand(*cmd); +} + + int Arbitration::getPenalty(unsigned iPlayerId) const { ASSERT(iPlayerId < getNPlayers(), "Wrong player number"); diff --git a/game/arbitration.h b/game/arbitration.h index 4d5d6fb..832afc8 100644 --- a/game/arbitration.h +++ b/game/arbitration.h @@ -60,7 +60,8 @@ public: void removeWarning(unsigned iPlayerId); bool hasWarning(unsigned iPlayerId) const; - void addPenalty(unsigned iPlayerId, int iPoints); + void addPenalty(unsigned iPlayerId, int iPoints = 0); + void removePenalty(unsigned iPlayerId); int getPenalty(unsigned iPlayerId) const; void assignMove(unsigned int iPlayerId, const Move &iMove); diff --git a/game/public_game.cpp b/game/public_game.cpp index e232570..a191bd3 100644 --- a/game/public_game.cpp +++ b/game/public_game.cpp @@ -300,10 +300,13 @@ bool PublicGame::arbitrationHasWarning(unsigned iPlayerId) const } -void PublicGame::arbitrationAddPenalty(unsigned iPlayerId, int iPoints) +void PublicGame::arbitrationTogglePenalty(unsigned iPlayerId) { Arbitration &game = getTypedGame(m_game); - game.addPenalty(iPlayerId, iPoints); + if (game.getPenalty(iPlayerId) != 0) + game.removePenalty(iPlayerId); + else + game.addPenalty(iPlayerId); } diff --git a/game/public_game.h b/game/public_game.h index 56fc876..1fd4634 100644 --- a/game/public_game.h +++ b/game/public_game.h @@ -271,7 +271,7 @@ public: void arbitrationToggleWarning(unsigned iPlayerId); bool arbitrationHasWarning(unsigned iPlayerId) const; - void arbitrationAddPenalty(unsigned iPlayerId, int iPoints); + void arbitrationTogglePenalty(unsigned iPlayerId); int arbitrationGetPenalty(unsigned iPlayer) const; void arbitrationAssign(unsigned playerId, const Move &iMove); diff --git a/qt/arbit_assignments.cpp b/qt/arbit_assignments.cpp index f1f7360..47402a9 100644 --- a/qt/arbit_assignments.cpp +++ b/qt/arbit_assignments.cpp @@ -85,7 +85,7 @@ ArbitAssignments::ArbitAssignments(QWidget *parent, PublicGame *iGame) filter = new KeyEventFilter(this, Qt::Key_P); QObject::connect(filter, SIGNAL(keyPressed(int, int)), - this, SLOT(addPenalty())); + this, SLOT(addRemovePenalty())); treeViewPlayers->installEventFilter(filter); // Display a preview of the master word when clicked @@ -285,7 +285,7 @@ void ArbitAssignments::populatePlayersMenu(QMenu &iMenu, const QPoint &iPoint) penaltyAction->setStatusTip(_q("Give a penalty to the selected player(s)")); penaltyAction->setShortcut(Qt::Key_P); QObject::connect(penaltyAction, SIGNAL(triggered()), - this, SLOT(addPenalty())); + this, SLOT(addRemovePenalty())); iMenu.addAction(penaltyAction); } @@ -591,7 +591,7 @@ void ArbitAssignments::addRemoveWarning() } -void ArbitAssignments::addPenalty() +void ArbitAssignments::addRemovePenalty() { QSet playersIdSet = getSelectedPlayers(); if (playersIdSet.isEmpty()) @@ -599,7 +599,7 @@ void ArbitAssignments::addPenalty() BOOST_FOREACH(unsigned int id, playersIdSet) { - m_game->arbitrationAddPenalty(id, 0); + m_game->arbitrationTogglePenalty(id); } emit gameUpdated(); } diff --git a/qt/arbit_assignments.h b/qt/arbit_assignments.h index 3a296c7..88efcca 100644 --- a/qt/arbit_assignments.h +++ b/qt/arbit_assignments.h @@ -79,7 +79,7 @@ private slots: void assignTopMove(); void suppressMove(); void addRemoveWarning(); - void addPenalty(); + void addRemovePenalty(); void endTurn(); private: diff --git a/utils/eliottxt.cpp b/utils/eliottxt.cpp index 02a0274..a845d7b 100644 --- a/utils/eliottxt.cpp +++ b/utils/eliottxt.cpp @@ -359,11 +359,9 @@ void helpArbitration() printf(" t [] : changer le tirage\n"); printf(" j j [] {} : jouer le mot [] aux coordonnées {} pour le joueur j\n"); printf(" m [] {} : définir le master move [] aux coordonnées {}\n"); - printf(" e j [w|p|s] {} : assigner un événement au joueur j :\n"); + printf(" e j [w|p] {} : assigner/supprimer un événement au joueur j :\n"); printf(" w -- avertissement\n"); printf(" p -- pénalité\n"); - printf(" s -- solo\n"); - printf(" {} -- valeur de l'événement\n"); printf(" f : finaliser le tour courant\n"); printf(" s [] : sauver la partie en cours dans le fichier []\n"); printf(" h [p|n|f|l|r] : naviguer dans l'historique (prev, next, first, last, replay)\n"); @@ -772,11 +770,10 @@ void loopArbitration(PublicGame &iGame) { unsigned id = parsePlayerId(tokens, 1, iGame); wchar_t type = parseCharInList(tokens, 2, L"wp"); - int value = parseNum(tokens, 3); if (type == L'w') iGame.arbitrationToggleWarning(id); else if (type == 'p') - iGame.arbitrationAddPenalty(id, value); + iGame.arbitrationTogglePenalty(id); // else if (type == 's') // iGame.arbitrationSetSolo(id, value); }