diff --git a/config/services.yaml b/config/services.yaml index a83924f..6fc8dec 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,6 +23,6 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones - App\Game: - calls: - - subscribe: [ '@App\Observer\XpEarnedObserver' ] +# App\Game: +# calls: +# - subscribe: [ '@App\Observer\XpEarnedObserver' ] diff --git a/src/Command/GameCommand.php b/src/Command/GameCommand.php index 317ce0b..1d8ea19 100644 --- a/src/Command/GameCommand.php +++ b/src/Command/GameCommand.php @@ -8,6 +8,9 @@ use App\Character\CharacterType; use App\Fight; use App\Game; +use App\Observer\XpEarnedObserver; +use App\Service\OutputtingXpCalculator; +use App\Service\XpCalculator; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -25,6 +28,10 @@ public function __construct( protected function execute(InputInterface $input, OutputInterface $output): int { + $xpCalculator = new XpCalculator(); + $xpCalculator = new OutputtingXpCalculator($xpCalculator); + $this->game->subscribe(new XpEarnedObserver($xpCalculator)); + $io = new SymfonyStyle($input, $output); $io->text('Welcome to the game where warriors fight against each other for honor and glory... and 🍕!'); diff --git a/src/Observer/XpEarnedObserver.php b/src/Observer/XpEarnedObserver.php index 7c3190b..ede49fb 100644 --- a/src/Observer/XpEarnedObserver.php +++ b/src/Observer/XpEarnedObserver.php @@ -5,11 +5,11 @@ namespace App\Observer; use App\Fight; -use App\Service\XpCalculator; +use App\Service\XpCalculatorInterface; readonly class XpEarnedObserver implements CanObserverFight { - public function __construct(private XpCalculator $xpCalculator) + public function __construct(private XpCalculatorInterface $xpCalculator) { } diff --git a/src/Service/OutputtingXpCalculator.php b/src/Service/OutputtingXpCalculator.php new file mode 100644 index 0000000..3afa662 --- /dev/null +++ b/src/Service/OutputtingXpCalculator.php @@ -0,0 +1,34 @@ +getLevel(); + + $this->innerCalculator->addXp($winner, $enemyLevel); + + $afterLevel = $winner->getLevel(); + + if ($beforeLevel !== $afterLevel) { + $output = new ConsoleOutput(); + + $output->writeln('--------------------------------'); + $output->writeln('Congratulations! You\'ve leveled up!'); + $output->writeln(sprintf('You are now level "%d"', $winner->getLevel())); + $output->writeln('--------------------------------'); + } + } +} diff --git a/src/Service/XpCalculator.php b/src/Service/XpCalculator.php index 11dc6db..8ff53b4 100644 --- a/src/Service/XpCalculator.php +++ b/src/Service/XpCalculator.php @@ -6,7 +6,7 @@ use App\Character\Character; -class XpCalculator +class XpCalculator implements XpCalculatorInterface { public function addXp(Character $winner, int $enemyLevel): void { diff --git a/src/Service/XpCalculatorInterface.php b/src/Service/XpCalculatorInterface.php new file mode 100644 index 0000000..a1df00f --- /dev/null +++ b/src/Service/XpCalculatorInterface.php @@ -0,0 +1,12 @@ +