From 7cd9b9c0c86d71b42aace48c5d5da609fe91965c Mon Sep 17 00:00:00 2001 From: Rob Blanckaert Date: Sun, 27 Jan 2019 09:50:41 -0800 Subject: [PATCH] Unconcede (#3515) --- cockatrice/src/gamescene.cpp | 2 +- cockatrice/src/messagelogwidget.cpp | 6 ++++++ cockatrice/src/messagelogwidget.h | 1 + cockatrice/src/player.cpp | 2 +- cockatrice/src/player.h | 2 +- cockatrice/src/tab_game.cpp | 28 +++++++++++++++++++++++++--- common/pb/command_concede.proto | 7 +++++++ common/pb/context_concede.proto | 6 ++++++ common/pb/game_commands.proto | 2 ++ common/pb/game_event_context.proto | 1 + common/server_game.h | 2 +- common/server_player.cpp | 28 ++++++++++++++++++++++++++++ common/server_player.h | 2 ++ 13 files changed, 82 insertions(+), 7 deletions(-) diff --git a/cockatrice/src/gamescene.cpp b/cockatrice/src/gamescene.cpp index 1f3c8d69..d45d6873 100644 --- a/cockatrice/src/gamescene.cpp +++ b/cockatrice/src/gamescene.cpp @@ -40,7 +40,7 @@ void GameScene::addPlayer(Player *player) players << player; addItem(player); connect(player, SIGNAL(sizeChanged()), this, SLOT(rearrange())); - connect(player, SIGNAL(gameConceded()), this, SLOT(rearrange())); + connect(player, SIGNAL(playerCountChanged()), this, SLOT(rearrange())); } void GameScene::removePlayer(Player *player) diff --git a/cockatrice/src/messagelogwidget.cpp b/cockatrice/src/messagelogwidget.cpp index dc0ac655..82e19568 100644 --- a/cockatrice/src/messagelogwidget.cpp +++ b/cockatrice/src/messagelogwidget.cpp @@ -169,6 +169,12 @@ void MessageLogWidget::logConcede(Player *player) appendHtmlServerMessage(tr("%1 has conceded the game.").arg(sanitizeHtml(player->getName())), true); } +void MessageLogWidget::logUnconcede(Player *player) +{ + soundEngine->playSound("player_concede"); + appendHtmlServerMessage(tr("%1 has unconceded the game.").arg(sanitizeHtml(player->getName())), true); +} + void MessageLogWidget::logConnectionStateChanged(Player *player, bool connectionState) { if (connectionState) { diff --git a/cockatrice/src/messagelogwidget.h b/cockatrice/src/messagelogwidget.h index cf1c4205..449df4aa 100644 --- a/cockatrice/src/messagelogwidget.h +++ b/cockatrice/src/messagelogwidget.h @@ -57,6 +57,7 @@ public slots: void logAlwaysRevealTopCard(Player *player, CardZone *zone, bool reveal); void logAttachCard(Player *player, QString cardName, Player *targetPlayer, QString targetCardName); void logConcede(Player *player); + void logUnconcede(Player *player); void logConnectionStateChanged(Player *player, bool connectionState); void logCreateArrow(Player *player, Player *startPlayer, diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 0f815c37..12e5c7d5 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -3037,8 +3037,8 @@ void Player::setConceded(bool _conceded) setVisible(!conceded); if (conceded) { clear(); - emit gameConceded(); } + emit playerCountChanged(); } void Player::setMirrored(bool _mirrored) diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 229c96c3..cb897dbc 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -137,7 +137,7 @@ signals: void logAlwaysRevealTopCard(Player *player, CardZone *zone, bool reveal); void sizeChanged(); - void gameConceded(); + void playerCountChanged(); public slots: void actUntapAll(); void actRollDie(); diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index 9fdc79b1..580af983 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -620,11 +620,23 @@ void TabGame::actGameInfo() void TabGame::actConcede() { - if (QMessageBox::question(this, tr("Concede"), tr("Are you sure you want to concede this game?"), - QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) + Player *player = players.value(localPlayerId, nullptr); + if (player == nullptr) return; + if (!player->getConceded()) { + if (QMessageBox::question(this, tr("Concede"), tr("Are you sure you want to concede this game?"), + QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) + return; - sendGameCommand(Command_Concede()); + sendGameCommand(Command_Concede()); + } else { + if (QMessageBox::question(this, tr("Unconcede"), + tr("You have already conceded. Do you want to return to this game?"), + QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::Yes) + return; + + sendGameCommand(Command_Unconcede()); + } } void TabGame::actLeaveGame() @@ -1051,6 +1063,16 @@ void TabGame::eventPlayerPropertiesChanged(const Event_PlayerPropertiesChanged & break; } + case GameEventContext::UNCONCEDE: { + messageLog->logUnconcede(player); + player->setConceded(false); + + QMapIterator playerIterator(players); + while (playerIterator.hasNext()) + playerIterator.next().value()->updateZones(); + + break; + } case GameEventContext::DECK_SELECT: { Context_DeckSelect deckSelect = context.GetExtension(Context_DeckSelect::ext); messageLog->logDeckSelect(player, QString::fromStdString(deckSelect.deck_hash()), diff --git a/common/pb/command_concede.proto b/common/pb/command_concede.proto index 5ac74235..8377d958 100644 --- a/common/pb/command_concede.proto +++ b/common/pb/command_concede.proto @@ -5,3 +5,10 @@ message Command_Concede { optional Command_Concede ext = 1017; } } + + +message Command_Unconcede { + extend GameCommand { + optional Command_Unconcede ext = 1032; + } +} \ No newline at end of file diff --git a/common/pb/context_concede.proto b/common/pb/context_concede.proto index 7b82aa31..a266ed2b 100644 --- a/common/pb/context_concede.proto +++ b/common/pb/context_concede.proto @@ -6,3 +6,9 @@ message Context_Concede { optional Context_Concede ext = 1001; } } + +message Context_Unconcede { + extend GameEventContext { + optional Context_Unconcede ext = 1009; + } +} \ No newline at end of file diff --git a/common/pb/game_commands.proto b/common/pb/game_commands.proto index 47d15441..266c218f 100644 --- a/common/pb/game_commands.proto +++ b/common/pb/game_commands.proto @@ -33,6 +33,8 @@ message GameCommand { DECK_SELECT = 1029; SET_SIDEBOARD_LOCK = 1030; CHANGE_ZONE_PROPERTIES = 1031; + UNCONCEDE = 1032; + } extensions 100 to max; } diff --git a/common/pb/game_event_context.proto b/common/pb/game_event_context.proto index 98d5047b..76dccad0 100644 --- a/common/pb/game_event_context.proto +++ b/common/pb/game_event_context.proto @@ -10,6 +10,7 @@ message GameEventContext { PING_CHANGED = 1006; CONNECTION_STATE_CHANGED = 1007; SET_SIDEBOARD_LOCK = 1008; + UNCONCEDE = 1009; } extensions 100 to max; } diff --git a/common/server_game.h b/common/server_game.h index ca704a87..89ad6e31 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -77,7 +77,6 @@ private: Server_Player *playerWhosAsking, bool omniscient, bool withUserInfo); - void sendGameStateToPlayers(); void storeGameInformation(); signals: void sigStartGameIfReady(); @@ -192,6 +191,7 @@ public: prepareGameEvent(const ::google::protobuf::Message &gameEvent, int playerId, GameEventContext *context = 0); GameEventContext prepareGameEventContext(const ::google::protobuf::Message &gameEventContext); + void sendGameStateToPlayers(); void sendGameEventContainer(GameEventContainer *cont, GameEventStorageItem::EventRecipients recipients = GameEventStorageItem::SendToPrivate | GameEventStorageItem::SendToOthers, diff --git a/common/server_player.cpp b/common/server_player.cpp index 22f7f771..b9bde462 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -776,6 +776,30 @@ Server_Player::cmdConcede(const Command_Concede & /*cmd*/, ResponseContainer & / return Response::RespOk; } +Response::ResponseCode +Server_Player::cmdUnconcede(const Command_Unconcede & /*cmd*/, ResponseContainer & /*rc*/, GameEventStorage &ges) +{ + if (spectator) + return Response::RespFunctionNotAllowed; + if (!game->getGameStarted()) + return Response::RespGameNotStarted; + if (!conceded) + return Response::RespContextError; + + setConceded(false); + + Event_PlayerPropertiesChanged event; + event.mutable_player_properties()->set_conceded(false); + ges.enqueueGameEvent(event, playerId); + ges.setGameEventContext(Context_Unconcede()); + + setupZones(); + + game->sendGameStateToPlayers(); + + return Response::RespOk; +} + Response::ResponseCode Server_Player::cmdReadyStart(const Command_ReadyStart &cmd, ResponseContainer & /*rc*/, GameEventStorage &ges) { @@ -1826,6 +1850,10 @@ Server_Player::processGameCommand(const GameCommand &command, ResponseContainer case GameCommand::CHANGE_ZONE_PROPERTIES: return cmdChangeZoneProperties(command.GetExtension(Command_ChangeZoneProperties::ext), rc, ges); break; + case GameCommand::UNCONCEDE: + return cmdUnconcede(command.GetExtension(Command_Unconcede::ext), rc, ges); + break; + default: return Response::RespInvalidCommand; } diff --git a/common/server_player.h b/common/server_player.h index 68193402..e8caa26f 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -46,6 +46,7 @@ class Command_SetCardCounter; class Command_IncCardCounter; class Command_ReadyStart; class Command_Concede; +class Command_Unconcede; class Command_IncCounter; class Command_CreateCounter; class Command_SetCounter; @@ -190,6 +191,7 @@ public: Response::ResponseCode cmdKickFromGame(const Command_KickFromGame &cmd, ResponseContainer &rc, GameEventStorage &ges); Response::ResponseCode cmdConcede(const Command_Concede &cmd, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdUnconcede(const Command_Unconcede &cmd, ResponseContainer &rc, GameEventStorage &ges); Response::ResponseCode cmdReadyStart(const Command_ReadyStart &cmd, ResponseContainer &rc, GameEventStorage &ges); Response::ResponseCode cmdDeckSelect(const Command_DeckSelect &cmd, ResponseContainer &rc, GameEventStorage &ges); Response::ResponseCode