diff --git a/app/Browser/Jobs/HellcaseBattles/HellcaseBattleScreenshot.php b/app/Browser/Jobs/HellcaseBattles/HellcaseBattleScreenshot.php new file mode 100644 index 0000000..5dc706e --- /dev/null +++ b/app/Browser/Jobs/HellcaseBattles/HellcaseBattleScreenshot.php @@ -0,0 +1,23 @@ +sendFinishedBattles($browser); + $this->jobRun->addArtifact(new JobArtifact([ + "name" => count($this->battlesSent) . " battailles envoyées", + ])); + $this->createNewBattles(); + $this->jobRun->addArtifact(new JobArtifact([ + "name" => count($this->battlesToAdd) . " nouvelles battailles ajoutées pour surveillage", + ])); + $this->jobRun->success = true; $this->jobRun->save(); @@ -103,9 +113,13 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce $battleLinkButton = $battle->findElement(WebDriverBy::xpath('./div//button[text() = "watch"]')); $battleLinkButton->sendKeys("\n"); sleep(3); - $battleLink = $browser->driver->getCurrentURL(); + try { // If we still are on the casebattle page, it means the battle was cancelled or something else + $browser->waitForLocation("https://hellcase.com/casebattle/", 3); + } catch (Exception $e) { + $battleLink = $browser->driver->getCurrentURL(); - $this->battlesToAdd[$battleLink] = $battleValue; + $this->battlesToAdd[$battleLink] = $battleValue; + } } } @@ -115,11 +129,12 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce if (!array_key_exists($battle->getUrl(), $this->battlesToAdd)) { $browser->visit($battle->getUrl()); - try { - $browser->waitForText("Started at"); + sleep(2); + $browser->waitForText("Case Battle"); + + if ($this->findElementContainingText($browser, "Started at:") != null) { // battle is finished // Send the battle $this->sendBattle($browser, $battle); - } catch (Exception $e) { // Battle is not finished or error (like battle cancelled) } $battle->delete(); @@ -128,7 +143,16 @@ class HellcaseBattlesJob extends HellcaseJob implements ShouldBeUniqueUntilProce } private function sendBattle(Browser $browser, HellcaseBattle $battle) { - AllNotification::send(new JobDebugNotification($this->jobId, "Battle sent" . $battle->getUrl(), screenshotProjectPath:null)); + try { + $battleHeader = $browser->driver->findElement(WebDriverBy::xpath("//*[contains(@class, 'case-battle-game__header')]")); + sleep(2); // Wait for the animations to finish + $battleHeader->takeElementScreenshot(HellcaseBattleScreenshot::getImgFileAbsolutePath()); + } catch (Exception $e) { + $browser->screenshot(JobDebugScreenshot::getFileName($this->jobId)); + AllNotification::send(new JobDebugNotification($this->jobId, "Failed to screenshot battle")); + } + $this->battlesSent[$battle->getUrl()] = $battle->value; + AllNotification::send(new HellcaseBattlesNofication($this->jobId, $battle)); } private function createNewBattles() { diff --git a/app/Browser/Jobs/HellcaseBattles/HellcaseBattlesNofication.php b/app/Browser/Jobs/HellcaseBattles/HellcaseBattlesNofication.php new file mode 100644 index 0000000..5900380 --- /dev/null +++ b/app/Browser/Jobs/HellcaseBattles/HellcaseBattlesNofication.php @@ -0,0 +1,43 @@ +battle = $battle; + + $this->setBody($this->generateBody()); + } + + private function generateBody() { + return new HellcaseBattlesNotificationBody($this->battle); + } + + public function getTitle(): Stringifiable { + return new StringifiableSimpleText("Nouvelle bataille de caisses Hellcase"); + } + + /** + * @inheritDoc + */ + public function getImageProjectPath(): string|null { + return HellcaseBattleScreenshot::getImgFileProjectPath(); + } + + /** + * @inheritDoc + */ + public function getLinkURL(): string|null { + return $this->battle->getUrl(); + } +} diff --git a/app/Browser/Jobs/HellcaseBattles/HellcaseBattlesNotificationBody.php b/app/Browser/Jobs/HellcaseBattles/HellcaseBattlesNotificationBody.php new file mode 100644 index 0000000..9a2eda7 --- /dev/null +++ b/app/Browser/Jobs/HellcaseBattles/HellcaseBattlesNotificationBody.php @@ -0,0 +1,37 @@ +battle = $battle; + } + + /** + * @inheritDoc + */ + public function toMarkdownString(): string { + return " +- Valeur : **{$this->battle->value} €** +- Lien : **{$this->battle->getUrl()}** + "; + } + + /** + * @inheritDoc + */ + public function toString(): string { + return " +- Valeur : {$this->battle->value} € +- Lien : {$this->battle->getUrl()} + "; + } +}