Arbitration: allow removing penalties

This commit is contained in:
Olivier Teulière 2012-05-20 22:12:42 +02:00
parent 89892c622c
commit 2b6b85cce3
7 changed files with 29 additions and 18 deletions

View file

@ -139,18 +139,18 @@ bool Arbitration::hasWarning(unsigned iPlayerId) const
void Arbitration::addPenalty(unsigned iPlayerId, int iPoints) void Arbitration::addPenalty(unsigned iPlayerId, int iPoints)
{ {
ASSERT(iPlayerId < getNPlayers(), "Wrong player number"); 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) if (iPoints == 0)
{ {
// Retrieve the default value of the penalty // Retrieve the default value of the penalty
iPoints = Settings::Instance().getInt("arbitration.default-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); 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 // If an existing penalty exists, merge it with the new one
const PlayerEventCmd *cmd = getPlayerEvent(iPlayerId, PlayerEventCmd::PENALTY); const PlayerEventCmd *cmd = getPlayerEvent(iPlayerId, PlayerEventCmd::PENALTY);
if (cmd == 0) if (cmd == 0)
@ -161,6 +161,7 @@ void Arbitration::addPenalty(unsigned iPlayerId, int iPoints)
} }
else else
{ {
// When the resulting value is 0, instead of merging we drop the existing one
Command *pCmd = new PlayerEventCmd(*m_players[iPlayerId], Command *pCmd = new PlayerEventCmd(*m_players[iPlayerId],
PlayerEventCmd::PENALTY, PlayerEventCmd::PENALTY,
iPoints + cmd->getPoints()); 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 int Arbitration::getPenalty(unsigned iPlayerId) const
{ {
ASSERT(iPlayerId < getNPlayers(), "Wrong player number"); ASSERT(iPlayerId < getNPlayers(), "Wrong player number");

View file

@ -60,7 +60,8 @@ public:
void removeWarning(unsigned iPlayerId); void removeWarning(unsigned iPlayerId);
bool hasWarning(unsigned iPlayerId) const; 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; int getPenalty(unsigned iPlayerId) const;
void assignMove(unsigned int iPlayerId, const Move &iMove); void assignMove(unsigned int iPlayerId, const Move &iMove);

View file

@ -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<Arbitration>(m_game); Arbitration &game = getTypedGame<Arbitration>(m_game);
game.addPenalty(iPlayerId, iPoints); if (game.getPenalty(iPlayerId) != 0)
game.removePenalty(iPlayerId);
else
game.addPenalty(iPlayerId);
} }

View file

@ -271,7 +271,7 @@ public:
void arbitrationToggleWarning(unsigned iPlayerId); void arbitrationToggleWarning(unsigned iPlayerId);
bool arbitrationHasWarning(unsigned iPlayerId) const; bool arbitrationHasWarning(unsigned iPlayerId) const;
void arbitrationAddPenalty(unsigned iPlayerId, int iPoints); void arbitrationTogglePenalty(unsigned iPlayerId);
int arbitrationGetPenalty(unsigned iPlayer) const; int arbitrationGetPenalty(unsigned iPlayer) const;
void arbitrationAssign(unsigned playerId, const Move &iMove); void arbitrationAssign(unsigned playerId, const Move &iMove);

View file

@ -85,7 +85,7 @@ ArbitAssignments::ArbitAssignments(QWidget *parent, PublicGame *iGame)
filter = new KeyEventFilter(this, Qt::Key_P); filter = new KeyEventFilter(this, Qt::Key_P);
QObject::connect(filter, SIGNAL(keyPressed(int, int)), QObject::connect(filter, SIGNAL(keyPressed(int, int)),
this, SLOT(addPenalty())); this, SLOT(addRemovePenalty()));
treeViewPlayers->installEventFilter(filter); treeViewPlayers->installEventFilter(filter);
// Display a preview of the master word when clicked // 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->setStatusTip(_q("Give a penalty to the selected player(s)"));
penaltyAction->setShortcut(Qt::Key_P); penaltyAction->setShortcut(Qt::Key_P);
QObject::connect(penaltyAction, SIGNAL(triggered()), QObject::connect(penaltyAction, SIGNAL(triggered()),
this, SLOT(addPenalty())); this, SLOT(addRemovePenalty()));
iMenu.addAction(penaltyAction); iMenu.addAction(penaltyAction);
} }
@ -591,7 +591,7 @@ void ArbitAssignments::addRemoveWarning()
} }
void ArbitAssignments::addPenalty() void ArbitAssignments::addRemovePenalty()
{ {
QSet<unsigned int> playersIdSet = getSelectedPlayers(); QSet<unsigned int> playersIdSet = getSelectedPlayers();
if (playersIdSet.isEmpty()) if (playersIdSet.isEmpty())
@ -599,7 +599,7 @@ void ArbitAssignments::addPenalty()
BOOST_FOREACH(unsigned int id, playersIdSet) BOOST_FOREACH(unsigned int id, playersIdSet)
{ {
m_game->arbitrationAddPenalty(id, 0); m_game->arbitrationTogglePenalty(id);
} }
emit gameUpdated(); emit gameUpdated();
} }

View file

@ -79,7 +79,7 @@ private slots:
void assignTopMove(); void assignTopMove();
void suppressMove(); void suppressMove();
void addRemoveWarning(); void addRemoveWarning();
void addPenalty(); void addRemovePenalty();
void endTurn(); void endTurn();
private: private:

View file

@ -359,11 +359,9 @@ void helpArbitration()
printf(" t [] : changer le tirage\n"); printf(" t [] : changer le tirage\n");
printf(" j j [] {} : jouer le mot [] aux coordonnées {} pour le joueur j\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(" 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(" w -- avertissement\n");
printf(" p -- pénalité\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(" f : finaliser le tour courant\n");
printf(" s [] : sauver la partie en cours dans le fichier []\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"); 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); unsigned id = parsePlayerId(tokens, 1, iGame);
wchar_t type = parseCharInList(tokens, 2, L"wp"); wchar_t type = parseCharInList(tokens, 2, L"wp");
int value = parseNum(tokens, 3);
if (type == L'w') if (type == L'w')
iGame.arbitrationToggleWarning(id); iGame.arbitrationToggleWarning(id);
else if (type == 'p') else if (type == 'p')
iGame.arbitrationAddPenalty(id, value); iGame.arbitrationTogglePenalty(id);
// else if (type == 's') // else if (type == 's')
// iGame.arbitrationSetSolo(id, value); // iGame.arbitrationSetSolo(id, value);
} }