protected function loop() { $this->output->writeln('Starting...'); while (true) { try { $offers = $this->exchangeModule->scan($this->buy); $this->pingLoader(); $accounts = array(ExchangeModule::CURRENCY => $offers->getCurrencyAmount(), ExchangeModule::GOLD => $offers->getGoldAmount()); foreach ($offers as $offer) { if ($offer->rate <= $this->priceLimit) { $this->output->writeln("[ID: {$offer->id}][Amount: {$offer->amount}]" . "[Rate: {$offer->rate}][{$offer->sellerId}:{$offer->sellerName}]"); $toBuy = $offer->amount; if ($this->buy == ExchangeModule::GOLD && $toBuy > 10) { $toBuy = 10; } if ($toBuy * $offer->rate > $accounts[$this->sell]) { $toBuy = round($accounts[$this->sell] / $offer->rate, 2) - 0.01; } if ($toBuy > 0) { $this->buy($offer, $toBuy); } } } } catch (\Exception $e) { if ($this->output->isVerbose()) { $this->output->writeln('<error>' . $e->getMessage() . '</error>'); } } usleep($this->scanInterval * 1000000); } }
/** * Return a function that can be used by Symfony's process to show the output * of a process live on our screen * * @param Output $output The console output * * @return null|\Closure */ protected function getBufferFunction($output) { if (!$output->isVerbose()) { return null; } return function ($type, $buffer) { if (Process::ERR === $type) { echo 'ERR > ' . $buffer; } else { echo 'OUT > ' . $buffer; } }; }
/** * @param CommunityInterface $community * @param Output $output */ private function updateCommunityMembers(CommunityInterface $community, Output $output) { $this->logger->addInfo(sprintf('Checking community "%s" with %s', $community->getName(), $community->getId())); $today = new \DateTime('-4 hour'); $loopTimeStart = microtime(true); $failsCount = 0; $members = $community->getMembers(); if ($output->isVerbose()) { $this->logger->addInfo(sprintf('Found %s members', count($members))); } foreach ($members as $player) { $memberTimeStart = microtime(true); $this->logger->addInfo(sprintf('Checking user "%s" with id "%s"', $player->getNick(), $player->getId())); $alreadyParseCheckTime = microtime(true); if (count($player->getDateStat()) && $player->getDateStat()->first()->getDate()->format('Y:m:d') == $today->format('Y:m:d')) { $this->logger->addInfo(sprintf('Player "%s" with id %s already parsed today', $player->getNick(), $player->getId())); if ($output->isVerbose()) { $this->logger->addInfo(sprintf('Already parse check take %s sec to process', microtime(true) - $alreadyParseCheckTime)); } continue; } if ($output->isVerbose()) { $this->logger->addInfo(sprintf('Already parse check take %s sec to process', microtime(true) - $alreadyParseCheckTime)); } $playerUpdateTimeStart = microtime(true); try { $this->statService->updatePlayer($player->getId()); } catch (RuntimeException $e) { if ($e->getCode() == 403) { $failsCount++; if ($failsCount >= 10) { throw $e; } $this->logger->addWarning(sprintf('Fail to update player "%s" with id %s', $player->getNick(), $player->getId())); } else { throw $e; } } if ($output->isVerbose()) { $this->logger->addInfo(sprintf('Player parse take %s sec to process', microtime(true) - $playerUpdateTimeStart)); } $sleepMicroTime = rand(500, 1500) * 1000; if ($output->isVerbose()) { $this->logger->addInfo(sprintf('Member take %s sec to process', microtime(true) - $memberTimeStart)); $this->logger->addInfo(sprintf('Sleep for %s sec', $sleepMicroTime / 1000 / 1000)); } usleep($sleepMicroTime); } if ($output->isVerbose()) { $this->logger->addInfo(sprintf('Loop take %s sec to process', microtime(true) - $loopTimeStart)); } }