Handle penalties in the core

This commit is contained in:
Olivier Teulière 2012-03-23 08:12:03 +01:00
parent 84e46bef85
commit e017e73cd9
6 changed files with 78 additions and 0 deletions

View file

@ -135,6 +135,45 @@ bool Arbitration::hasWarning(unsigned iPlayerId) const
}
void Arbitration::addPenalty(unsigned iPlayerId, int iPoints)
{
ASSERT(iPlayerId < getNPlayers(), "Wrong player number");
if (iPoints == 0)
{
// Retrieve the default value of the penalty
iPoints = Settings::Instance().getInt("arbitration.default-penalty");
}
LOG_INFO("Giving a penalty of " << iPoints << " to player " << iPlayerId);
// If an existing penalty exists, merge it with the new one
const PlayerEventCmd *cmd = getPlayerEvent(iPlayerId, PlayerEventCmd::PENALTY);
if (cmd == 0)
{
Command *pCmd = new PlayerEventCmd(*m_players[iPlayerId],
PlayerEventCmd::PENALTY, iPoints);
accessNavigation().insertCommand(pCmd);
}
else
{
Command *pCmd = new PlayerEventCmd(*m_players[iPlayerId],
PlayerEventCmd::PENALTY,
iPoints + cmd->getPoints());
accessNavigation().replaceCommand(*cmd, pCmd);
}
}
int Arbitration::getPenalty(unsigned iPlayerId) const
{
ASSERT(iPlayerId < getNPlayers(), "Wrong player number");
const PlayerEventCmd *cmd = getPlayerEvent(iPlayerId, PlayerEventCmd::PENALTY);
if (cmd == 0)
return 0;
return cmd->getPoints();
}
void Arbitration::assignMove(unsigned int iPlayerId, const Move &iMove)
{
ASSERT(iPlayerId < getNPlayers(), "Wrong player number");

View file

@ -59,6 +59,9 @@ public:
void removeWarning(unsigned iPlayerId);
bool hasWarning(unsigned iPlayerId) const;
void addPenalty(unsigned iPlayerId, int iPoints);
int getPenalty(unsigned iPlayerId) const;
void assignMove(unsigned int iPlayerId, const Move &iMove);
void finalizeTurn();

View file

@ -22,6 +22,7 @@
#include "player_event_cmd.h"
#include "player.h"
#include "debug.h"
INIT_LOGGER(game, PlayerEventCmd);
@ -30,6 +31,8 @@ INIT_LOGGER(game, PlayerEventCmd);
PlayerEventCmd::PlayerEventCmd(Player &ioPlayer, EventType iEvent, int iPoints)
: m_player(ioPlayer), m_eventType(iEvent), m_points(iPoints)
{
// This check is valid for the 3 event types
ASSERT(iPoints >= 0, "Negative points not allowed");
}

View file

@ -263,6 +263,7 @@ void PublicGame::arbitrationSetRackManual(const wstring &iLetters)
getTypedGame<Arbitration>(m_game).setRackManual(iLetters);
}
void PublicGame::arbitrationSearch(LimitResults &oResults)
{
return getTypedGame<Arbitration>(m_game).search(oResults);
@ -275,6 +276,7 @@ Move PublicGame::arbitrationCheckWord(const wstring &iWord,
return getTypedGame<Arbitration>(m_game).checkWord(iWord, iCoords);
}
void PublicGame::arbitrationToggleWarning(unsigned iPlayerId)
{
Arbitration &game = getTypedGame<Arbitration>(m_game);
@ -284,11 +286,32 @@ void PublicGame::arbitrationToggleWarning(unsigned iPlayerId)
game.addWarning(iPlayerId);
}
bool PublicGame::arbitrationHasWarning(unsigned iPlayerId) const
{
return getTypedGame<Arbitration>(m_game).hasWarning(iPlayerId);
}
void PublicGame::arbitrationAddPenalty(unsigned iPlayerId, int iPoints)
{
Arbitration &game = getTypedGame<Arbitration>(m_game);
game.addPenalty(iPlayerId, iPoints);
}
int PublicGame::arbitrationGetPenalty(unsigned iPlayerId) const
{
return getTypedGame<Arbitration>(m_game).getPenalty(iPlayerId);
}
void PublicGame::arbitrationAssign(unsigned iPlayerId, const Move &iMove)
{
getTypedGame<Arbitration>(m_game).assignMove(iPlayerId, iMove);
}
void PublicGame::arbitrationFinalizeTurn()
{
getTypedGame<Arbitration>(m_game).finalizeTurn();

View file

@ -266,6 +266,10 @@ public:
const wstring &iCoords) const;
void arbitrationToggleWarning(unsigned iPlayerId);
bool arbitrationHasWarning(unsigned iPlayerId) const;
void arbitrationAddPenalty(unsigned iPlayerId, int iPoints);
int arbitrationGetPenalty(unsigned iPlayer) const;
void arbitrationAssign(unsigned playerId, const Move &iMove);
void arbitrationFinalizeTurn();

View file

@ -180,6 +180,9 @@ Settings::Settings()
// ============== Arbitration mode options ==============
Setting &arbitration = m_conf->getRoot().add("arbitration", Setting::TypeGroup);
// Default value of a penalty
arbitration.add("default-penalty", Setting::TypeInt) = 5;
// Number of search results kept in a search
arbitration.add("search-limit", Setting::TypeInt) = 100;
@ -197,6 +200,7 @@ Settings::Settings()
copySetting<bool>(tmpConf, *m_conf, "duplicate.reject-invalid");
copySetting<bool>(tmpConf, *m_conf, "freegame.reject-invalid");
copySetting<int>(tmpConf, *m_conf, "arbitration.search-limit");
copySetting<int>(tmpConf, *m_conf, "arbitration.default-penalty");
}
catch (...)
{
@ -281,6 +285,8 @@ int Settings::getInt(const string &iName) const
return 10;
else if (iName == "arbitration.search-limit")
return 100;
else if (iName == "arbitration.default-penalty")
return 5;
return 0;
#endif
}