diff --git a/game/arbitration.cpp b/game/arbitration.cpp index ca7f458..9990a18 100644 --- a/game/arbitration.cpp +++ b/game/arbitration.cpp @@ -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"); diff --git a/game/arbitration.h b/game/arbitration.h index dc613a1..f880068 100644 --- a/game/arbitration.h +++ b/game/arbitration.h @@ -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(); diff --git a/game/player_event_cmd.cpp b/game/player_event_cmd.cpp index 612c690..7043ea1 100644 --- a/game/player_event_cmd.cpp +++ b/game/player_event_cmd.cpp @@ -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"); } diff --git a/game/public_game.cpp b/game/public_game.cpp index 41c1bb7..dc7f18d 100644 --- a/game/public_game.cpp +++ b/game/public_game.cpp @@ -263,6 +263,7 @@ void PublicGame::arbitrationSetRackManual(const wstring &iLetters) getTypedGame(m_game).setRackManual(iLetters); } + void PublicGame::arbitrationSearch(LimitResults &oResults) { return getTypedGame(m_game).search(oResults); @@ -275,6 +276,7 @@ Move PublicGame::arbitrationCheckWord(const wstring &iWord, return getTypedGame(m_game).checkWord(iWord, iCoords); } + void PublicGame::arbitrationToggleWarning(unsigned iPlayerId) { Arbitration &game = getTypedGame(m_game); @@ -284,11 +286,32 @@ void PublicGame::arbitrationToggleWarning(unsigned iPlayerId) game.addWarning(iPlayerId); } + +bool PublicGame::arbitrationHasWarning(unsigned iPlayerId) const +{ + return getTypedGame(m_game).hasWarning(iPlayerId); +} + + +void PublicGame::arbitrationAddPenalty(unsigned iPlayerId, int iPoints) +{ + Arbitration &game = getTypedGame(m_game); + game.addPenalty(iPlayerId, iPoints); +} + + +int PublicGame::arbitrationGetPenalty(unsigned iPlayerId) const +{ + return getTypedGame(m_game).getPenalty(iPlayerId); +} + + void PublicGame::arbitrationAssign(unsigned iPlayerId, const Move &iMove) { getTypedGame(m_game).assignMove(iPlayerId, iMove); } + void PublicGame::arbitrationFinalizeTurn() { getTypedGame(m_game).finalizeTurn(); diff --git a/game/public_game.h b/game/public_game.h index 1ed01fd..75b433e 100644 --- a/game/public_game.h +++ b/game/public_game.h @@ -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(); diff --git a/game/settings.cpp b/game/settings.cpp index 9d5462b..8e0c839 100644 --- a/game/settings.cpp +++ b/game/settings.cpp @@ -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(tmpConf, *m_conf, "duplicate.reject-invalid"); copySetting(tmpConf, *m_conf, "freegame.reject-invalid"); copySetting(tmpConf, *m_conf, "arbitration.search-limit"); + copySetting(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 }