diff --git a/src/client/GameController.cpp b/src/client/GameController.cpp index 74cbdc1b6eadfb9b7326e012788366c6c174d0a8..b1e7b9e3529dea07f9b1545fdfb4b2e64f992116 100644 --- a/src/client/GameController.cpp +++ b/src/client/GameController.cpp @@ -110,9 +110,23 @@ void GameController::updateGameState(game_state* newGameState) { { trick* trick_to_show = new trick(*_currentGameState->get_last_trick()); oldGameState->set_trick(trick_to_show); + player* winner = oldGameState->get_trick()->get_winner(); + // get player of oldGameState that has same id as winner (winner is player of current game state) + for (auto& player_ : oldGameState->get_players()) { + if (player_->get_id() == winner->get_id()) { + winner = player_; + } + } + // add trick to winning player + winner->set_nof_tricks(winner->get_nof_tricks() + 1); + + // make sure that last card is removed from hand + const player* last_player = oldGameState->get_current_player(); + std::string last_player_error = "Card of last player of trick could not be removed from hand"; + last_player->get_hand()->remove_card(trick_to_show->get_cards_and_players().back().first->get_id(), last_player_error); + GameController::_gameWindow->showPanel(GameController::_mainGamePanelWizard); GameController::_mainGamePanelWizard->buildGameState(oldGameState, GameController::_me); - player* winner = oldGameState->get_trick()->get_winner(); showTrickOverMessage(winner); } @@ -272,7 +286,7 @@ void GameController::showGameOverMessage() { // sort players by score std::vector<player*> players = GameController::_currentGameState->get_players(); std::sort(players.begin(), players.end(), [](const player* a, const player* b) -> bool { - return a->get_scores().back()->get_value() < b->get_scores().back()->get_value(); + return a->get_scores().back()->get_value() > b->get_scores().back()->get_value(); }); // list all players diff --git a/src/common/game_state/player/hand.cpp b/src/common/game_state/player/hand.cpp index 533132f5e7e1145c5892142957af507cb98b743f..94285550bba1959ca7240bbae6b3041918d4082f 100644 --- a/src/common/game_state/player/hand.cpp +++ b/src/common/game_state/player/hand.cpp @@ -59,13 +59,6 @@ card* hand::remove_card(const std::vector<card*>::iterator pos) { return nullptr; } -#ifdef WIZARD_SERVER -// state update functions -bool hand::add_card(card* card, std::string &err) { - _cards.push_back(card); - return true; -} - bool hand::remove_card(std::string card_id, std::string &err) { const auto it = std::ranges::find_if(_cards, [&card_id](const card* x) { return x->get_id() == card_id;}); @@ -77,6 +70,13 @@ bool hand::remove_card(std::string card_id, std::string &err) { return false; } } + +#ifdef WIZARD_SERVER +// state update functions +bool hand::add_card(card* card, std::string &err) { + _cards.push_back(card); + return true; +} #endif // serialization interface diff --git a/src/common/game_state/player/hand.h b/src/common/game_state/player/hand.h index b00da16c1c97ad53fd8806a479a6ef55952ef981..def449c3fad75023286ff96e0e9b0766599c9628 100644 --- a/src/common/game_state/player/hand.h +++ b/src/common/game_state/player/hand.h @@ -32,12 +32,13 @@ private: card* remove_card(int idx); /** - * @brief Removes a card from the hand. - * @param card A pointer to the card that should be removed. - * @return A pointer to the removed card. - */ + * @brief Removes a card from the hand. + * @param card A pointer to the card that should be removed. + * @return A pointer to the removed card. + */ card* remove_card(card* card); + public: // constructors and destructor @@ -88,6 +89,14 @@ public: */ bool try_get_card(const std::string& card_id, card*& hand_card) const; + /** + * @brief Removes a card from the hand. + * @param card_id The id of the card that should be removed. + * @param err The error message updated in case something does not work. + * @return A boolean indicating whether removing the card worked or not. + */ + bool remove_card(std::string card_id, std::string& err); + #ifdef WIZARD_SERVER // state update functions /** @@ -98,13 +107,6 @@ public: */ bool add_card(card* card, std::string& err); - /** - * @brief Removes a card from the hand. - * @param card_id The id of the card that should be removed. - * @param err The error message updated in case something does not work. - * @return A boolean indicating whether removing the card worked or not. - */ - bool remove_card(std::string card_id, std::string& err); #endif // serialization interface