src/EventSubscriber/Game/AttackSubscriber.php line 132

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\Game;
  4. use App\Entity\BattleResult;
  5. use App\Entity\Location\CapturableLocationInterface;
  6. use App\Entity\Location\ConvoyInterceptionPoint;
  7. use App\Entity\Location\Town;
  8. use App\Entity\Mail;
  9. use App\Entity\Relation;
  10. use App\Event\BattleEvent;
  11. use App\Event\ConvoyArrivedEvent;
  12. use App\Model\DiscordTriggerService;
  13. use App\Model\Game\GameUserService;
  14. use App\Model\GameService;
  15. use App\Model\UserService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class AttackSubscriber implements EventSubscriberInterface
  18. {
  19.     public const CONVOY 'game.convoy';
  20.     public const ATTACK 'game.attack';
  21.     /**
  22.      * @var GameUserService
  23.      */
  24.     private $userService;
  25.     /**
  26.      * @var DiscordTriggerService
  27.      */
  28.     private $triggerService;
  29.     /**
  30.      * @var GameService
  31.      */
  32.     private $gameService;
  33.     public function __construct(GameUserService $userServiceDiscordTriggerService $triggerServiceGameService $gameService)
  34.     {
  35.         $this->userService $userService;
  36.         $this->triggerService $triggerService;
  37.         $this->gameService $gameService;
  38.     }
  39.     public function convoyArrival(ConvoyArrivedEvent $event)
  40.     {
  41.         $this->gameService->setArea($event->getArea());
  42.         $convoy $event->getConvoy();
  43.         $owner $convoy->getOwner();
  44.         $target $convoy->getTargetName();
  45.         if (!$convoy->isAutoResolved()) {
  46.             if ($target == $owner->getName()) {
  47.                 $this->gameService->eventService->addTownEvent($owner"An attack convoy has arrived home.");
  48.             } else {
  49.                 $this->gameService->eventService->addTownEvent($owner"An attack convoy heading for $target has arrived.");
  50.                 if ($owner instanceof Town) {
  51.                     $this->userService->setAreaContext($this->gameService->area());
  52.                     $targetUser $this->userService->getUserFromUsername($owner->loginname);
  53.                     $discordUser $this->userService->getUserDiscord($targetUser->getId());
  54.                     if ($discordUser && $discordUser->isVerified() && $discordUser->getSettings() != null && $discordUser->getSettings()->attack_convoy) {
  55.                         $this->triggerService->addTrigger($this->gameService->area(), DiscordTriggerService::TRIGGER_USER_DM$targetUser->getId(), "An attack convoy heading for **$target** has arrived.");
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     public function generateReports(BattleEvent $event)
  62.     {
  63.         $this->gameService->setArea($event->getArea());
  64.         $convoy $event->getConvoy();
  65.         $battleResult $event->getBattleResult();
  66.         // Generate report for attacker
  67.         if ($convoy->getOwner()->isPlayer()) {
  68.             $battleResult->setViewSide('attacker');
  69.             $reportId $this->gameService->mailService->saveBattleReport($convoy->getOwner(), $battleResult$convoy->isAutoResolved() ? Mail::STATUS_NEW Mail::STATUS_READ);
  70.             $battleResult->setOwnerReportId($reportId);
  71.         }
  72.         // Generate report for target owner
  73.         if ($convoy->getTarget()->getOwner()->isPlayer()) {
  74.             $battleResult->setViewSide('defender');
  75.             /** @var Town $targetOwner */
  76.             $targetOwner $convoy->getTarget()->getOwner();
  77.             $reportId $this->gameService->mailService->saveBattleReport($targetOwner$battleResultMail::STATUS_NEW);
  78.             $battleResult->setTargetReportId($reportId);
  79.         }
  80.     }
  81.     public function townEvents(BattleEvent $event)
  82.     {
  83.         $this->gameService->setArea($event->getArea());
  84.         $convoy $event->getConvoy();
  85.         $battleResult $event->getBattleResult();
  86.         // Town event for attacked when convoy is autoresolved
  87.         if ($battleResult->getOwnerReportId()) {
  88.             $link $this->gameService->urlGenerator->generate('game_mail_reports_read', ['reportId' => $battleResult->getOwnerReportId()]);
  89.             if ($convoy->isAutoResolved()) {
  90.                 $this->gameService->eventService->addTownEvent($convoy->getOwner(), "An attack convoy arrived at {$convoy->getTarget()->getName()} and attacked instantly. Your convoy " . ($battleResult->isVictorious() ? "was victorious" "was pushed back") . ". Read the <a href='{$link}'>battle report</a>.");
  91.             }
  92.         }
  93.         // Town event when attacking town or owned acre
  94.         if ($battleResult->getTargetReportId() && $convoy->getTarget()->getOwner()->isPlayer()) {
  95.             /** @var Town $targetOwner */
  96.             $targetOwner $convoy->getTarget()->getOwner();
  97.             $link $this->gameService->urlGenerator->generate('game_mail_reports_read', ['reportId' => $battleResult->getTargetReportId()]);
  98.             if ($convoy->getTarget() instanceof CapturableLocationInterface) {
  99.                 if ($battleResult->isVictorious()) {
  100.                     $this->gameService->eventService->addTownEvent($targetOwner"{$convoy->getTarget()->getName()} got captured by the town of {$convoy->getOwner()->getNameWithTag()}. Read the <a href='{$link}'>battle report</a>.");
  101.                 } else {
  102.                     $this->gameService->eventService->addTownEvent($targetOwner"We managed to defend {$convoy->getTarget()->getName()} against an army from {$convoy->getOwner()->getNameWithTag()}. Read the <a href='{$link}'>battle report</a>.");
  103.                 }
  104.             } elseif ($convoy->getTarget() instanceof ConvoyInterceptionPoint) {
  105.                 if ($battleResult->isVictorious()) {
  106.                     $this->gameService->eventService->addTownEvent($targetOwner"Our convoy got overrun by forces from the town of {$convoy->getOwner()->getNameWithTag()}. Read the <a href='{$link}'>battle report</a>.");
  107.                 } else {
  108.                     $this->gameService->eventService->addTownEvent($targetOwner"We managed to defend our convoy against an attack from {$convoy->getOwner()->getNameWithTag()}. Read the <a href='{$link}'>battle report</a>.");
  109.                 }
  110.             } else {
  111.                 if ($battleResult->isVictorious()) {
  112.                     $this->gameService->eventService->addTownEvent($targetOwner"Our town got overrun by forces from the town of {$convoy->getOwner()->getNameWithTag()}. Read the <a href='{$link}'>battle report</a>.");
  113.                 } else {
  114.                     $this->gameService->eventService->addTownEvent($targetOwner"We managed to defend our town against an attack from {$convoy->getOwner()->getNameWithTag()}. Read the <a href='{$link}'>battle report</a>.");
  115.                 }
  116.             }
  117.         }
  118.     }
  119.     public function discordNotify(BattleEvent $event)
  120.     {
  121.         $this->gameService->setArea($event->getArea());
  122.         // Discord notification
  123.         $convoy $event->getConvoy();
  124.         $target $convoy->getTarget();
  125.         if ($target->getOwner()->isPlayer()) {
  126.             /** @var Town $town */
  127.             $town $target->getOwner();
  128.             $this->userService->setAreaContext($this->gameService->area());
  129.             $user $this->userService->getUserFromUsername($town->loginname);
  130.             if ($user) {
  131.                 $discordUser $this->userService->getUserDiscord($user->getId());
  132.                 if ($discordUser && $discordUser->isVerified() && $discordUser->getSettings() != null && $discordUser->getSettings()->attack) {
  133.                     if ($target instanceof CapturableLocationInterface) {
  134.                         $this->triggerService->addTrigger($event->getArea(), DiscordTriggerService::TRIGGER_USER_DM$user->getId(), "{$target->getName()} has been attacked by {$convoy->getOwner()->getNameWithTag()}. The attacker was " . ($event->getBattleResult()->isVictorious() ? "victorious" "defeated"));
  135.                     } else if ($target instanceof ConvoyInterceptionPoint) {
  136.                         $this->triggerService->addTrigger($event->getArea(), DiscordTriggerService::TRIGGER_USER_DM$user->getId(), "Your convoy has been attacked by {$convoy->getOwner()->getNameWithTag()}." . ($event->getBattleResult()->getResult() == BattleResult::RESULT_VICTORY_DECISIVE " Your convoy was devastated and the remaining troops have turned around." ""));
  137.                     } else {
  138.                         $this->triggerService->addTrigger($event->getArea(), DiscordTriggerService::TRIGGER_USER_DM$user->getId(), "Your town has been attacked by {$convoy->getOwner()->getNameWithTag()}. The attacker was " . ($event->getBattleResult()->isVictorious() ? "victorious" "defeated"));
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.     }
  144.     public function relations(BattleEvent $event)
  145.     {
  146.         $this->gameService->setArea($event->getArea());
  147.         // Auto war declaration
  148.         $convoy $event->getConvoy();
  149.         $target $convoy->getTarget()->getOwner();
  150.         $attacker $convoy->getOwner();
  151.         if ($attacker instanceof Town && $target instanceof Town) {
  152.             $target $this->gameService->townService->getTownById($target->getId());
  153.             if (!$target->isAtWarWith($attacker)) {
  154.                 $this->gameService->eventService->addTownEvent($attacker"The town of {$target->getNameWithTag()} has declared war on us because of our recent aggression!");
  155.                 $this->gameService->relationService->createRelation($target$attackerRelation::WAR);
  156.                 $this->userService->setAreaContext($this->gameService->area());
  157.                 $targetUser $this->userService->getUserFromUsername($target->loginname);
  158.                 $discordUser $this->userService->getUserDiscord($targetUser->getId());
  159.                 if ($discordUser && $discordUser->isVerified() && $discordUser->getSettings() != null && $discordUser->getSettings()->war_declaration) {
  160.                     $this->triggerService->addTrigger($this->gameService->area(), DiscordTriggerService::TRIGGER_USER_DM$targetUser->getId(), "You have declared war on {$attacker->getNameWithTag()}.");
  161.                 }
  162.             }
  163.         }
  164.     }
  165.     /**
  166.      * {@inheritDoc}
  167.      * @codeCoverageIgnore
  168.      */
  169.     public static function getSubscribedEvents(): array
  170.     {
  171.         return [
  172.             self::CONVOY => [
  173.                 ['convoyArrival'0],
  174.             ],
  175.             self::ATTACK => [
  176.                 ['generateReports'50],
  177.                 ['townEvents'0],
  178.                 ['discordNotify'0],
  179.                 ['relations'0],
  180.             ]
  181.         ];
  182.     }
  183. }