diff --git a/CHANGELOG.md b/CHANGELOG.md index 85c2d456d..21e95e644 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,15 @@ -## [2.1.1] - 2017-17-23 +## [2.1.2] - 2017-08-09 +### Added +- Storage of the sectors ownership data in Redis + +### Fixed +- Commanders defection when their base is taken +- Display of the incoming attacks in the admiralty +- Sectors ownership data in faction tactical registry +- Ending of recycling missions +- Conversion of the recycling places when there is no resources left + +## [2.1.1] - 2017-07-23 ### Added - Spy and combat active report highlighting in the list - Daily scheduling for notifications cleaning and inactive accounts management diff --git a/system/Classes/Daemon/Server.php b/system/Classes/Daemon/Server.php index 0e82944b6..9b56a977d 100644 --- a/system/Classes/Daemon/Server.php +++ b/system/Classes/Daemon/Server.php @@ -195,6 +195,13 @@ public function treatCommand($data) $data['data']['date'] ); break; + case 'cancel': + $this->realTimeActionScheduler->cancelFromProcess( + $data['data']['object_class'], + $data['data']['object_id'], + $data['data']['date'] + ); + break; } } diff --git a/system/Classes/Process/ProcessGateway.php b/system/Classes/Process/ProcessGateway.php index e16c5dba0..2167ffbaa 100644 --- a/system/Classes/Process/ProcessGateway.php +++ b/system/Classes/Process/ProcessGateway.php @@ -14,7 +14,6 @@ class ProcessGateway */ public function writeTo(Process $process, $data) { - \Asylamba\Classes\Daemon\Server::debug('Writing to Process : ' . json_encode($data)); fwrite($process->getOutput(), json_encode($data) . "\n"); } @@ -23,7 +22,6 @@ public function writeTo(Process $process, $data) */ public function writeToMaster($data) { - \Asylamba\Classes\Daemon\Server::debug('Writing to master: ' . json_encode($data)); fwrite(STDOUT, json_encode($data) . "\n"); } diff --git a/system/Classes/Scheduler/RealTimeActionScheduler.php b/system/Classes/Scheduler/RealTimeActionScheduler.php index 2253feda8..7d29cf4b8 100644 --- a/system/Classes/Scheduler/RealTimeActionScheduler.php +++ b/system/Classes/Scheduler/RealTimeActionScheduler.php @@ -138,12 +138,36 @@ public function reschedule($object, $date, $oldDate) { */ public function cancel($object, $date) { + if (P_TYPE === 'worker') { + return $this->processGateway->writeToMaster([ + 'command' => 'cancel', + 'data' => [ + 'object_class' => get_class($object), + 'object_id' => $object->id, + 'date' => $date + ] + ]); + } unset($this->queue[$date][get_class($object) . '-' . $object->id]); if (empty($this->queue[$date])) { unset($this->queue[$date]); } } + + /** + * @param string $class + * @param int $id + * @param string $date + */ + public function cancelFromProcess($class, $id, $date) + { + unset($this->queue[$date][$class . '-' . $id]); + + if (empty($this->queue[$date])) { + unset($this->queue[$date]); + } + } /** * @return array diff --git a/system/Classes/Task/TechnicalTask.php b/system/Classes/Task/TechnicalTask.php index 7daa7a368..bfe97c0ba 100644 --- a/system/Classes/Task/TechnicalTask.php +++ b/system/Classes/Task/TechnicalTask.php @@ -4,6 +4,8 @@ class TechnicalTask extends Task { + const DEFAULT_ESTIMATED_TIME = 10.00; + /** * {@inheritdoc} */ diff --git a/system/Classes/Worker/Application.php b/system/Classes/Worker/Application.php index 3a870a78b..199539125 100644 --- a/system/Classes/Worker/Application.php +++ b/system/Classes/Worker/Application.php @@ -90,6 +90,8 @@ public function init() $this->container->get('process_manager')->launchProcesses(); $this->container->get('realtime_action_scheduler')->init(); $this->container->get('cyclic_action_scheduler')->init(); + $this->container->get('gaia.sector_manager')->initOwnershipData(); + $server = $this->container->get('server'); $server->createHttpServer(); $server->listen(); diff --git a/system/Modules/Ares/Manager/CommanderManager.php b/system/Modules/Ares/Manager/CommanderManager.php index ebd2dd944..9bf15b8e1 100644 --- a/system/Modules/Ares/Manager/CommanderManager.php +++ b/system/Modules/Ares/Manager/CommanderManager.php @@ -180,6 +180,29 @@ public function getIncomingAttacks($playerId) return $this->entityManager->getRepository(Commander::class)->getIncomingAttacks($playerId); } + /** + * @param int $playerId + * @return array + */ + public function getVisibleIncomingAttacks($playerId) + { + $attackingCommanders = $this->getIncomingAttacks($playerId) ; + $incomingCommanders = [] ; + + foreach ($attackingCommanders as $commander) { + # va chercher les heures auxquelles il rentre dans les cercles d'espionnage + $startPlace = $this->placeManager->get($commander->getRBase()); + $destinationPlace = $this->placeManager->get($commander->getRPlaceDestination()); + $times = Game::getAntiSpyEntryTime($startPlace, $destinationPlace, $commander->getArrivalDate()); + + if (strtotime(Utils::now()) >= strtotime($times[0])) { + # ajout de l'événement + $incomingCommanders[] = $commander; + } + } + return $incomingCommanders; + } + /** * @param int $playerId * @return array diff --git a/system/Modules/Athena/Manager/OrbitalBaseManager.php b/system/Modules/Athena/Manager/OrbitalBaseManager.php index 2f85354fe..3347b42b4 100644 --- a/system/Modules/Athena/Manager/OrbitalBaseManager.php +++ b/system/Modules/Athena/Manager/OrbitalBaseManager.php @@ -420,7 +420,7 @@ public function changeOwnerById($id, $base, $newOwner, $baseCommanders) { if (in_array($commander->statement, [Commander::INSCHOOL, Commander::ONSALE, Commander::RESERVE])) { $commander->rPlayer = $newOwner; } else if ($commander->statement == Commander::MOVING) { - $commander->statement = Commander::RETIRED; + $this->commanderManager->endTravel($commander, Commander::RETIRED); $this->realtimeActionScheduler->cancel($commander, $commander->getArrivalDate()); } else { $commander->statement = Commander::DEAD; @@ -716,8 +716,7 @@ public function uRecycling($missionId) { # if there is no more resource if ($targetPlace->resources <= 0) { # the place become an empty place - $targetPlace->resources = 0; - $targetPlace->typeOfPlace = Place::EMPTYZONE; + $this->placeManager->turnAsEmptyPlace($targetPlace); # stop the mission $mission->statement = RecyclingMission::ST_DELETED; @@ -860,8 +859,10 @@ public function uRecycling($missionId) { # update u $mission->uRecycling = Utils::addSecondsToDate($mission->uRecycling, $mission->cycleTime); - // Schedule the next mission - $this->realtimeActionScheduler->schedule('athena.orbital_base_manager', 'uRecycling', $mission, $mission->uRecycling); + // Schedule the next mission if there is still resources + if ($mission->getStatement() !== RecyclingMission::ST_DELETED) { + $this->realtimeActionScheduler->schedule('athena.orbital_base_manager', 'uRecycling', $mission, $mission->uRecycling); + } } else { # the place become an empty place $targetPlace->resources = 0; diff --git a/system/Modules/Gaia/EventListener/SectorListener.php b/system/Modules/Gaia/EventListener/SectorListener.php index b0a103479..24d5957af 100644 --- a/system/Modules/Gaia/EventListener/SectorListener.php +++ b/system/Modules/Gaia/EventListener/SectorListener.php @@ -16,42 +16,29 @@ class SectorListener protected $sectorManager; /** @var SystemManager **/ protected $systemManager; - /** @var OrbitalBaseManager **/ - protected $orbitalBaseManager; - /** @var PlayerManager **/ - protected $playerManager; /** @var EntityManager **/ protected $entityManager; - /** @var array **/ - protected $scores; /** @var int **/ protected $minimalScore; /** * @param SectorManager $sectorManager * @param SystemManager $systemManager - * @param OrbitalBaseManager $orbitalBaseManager - * @param PlayerManager $playerManager * @param EntityManager $entityManager + * @param RedisManager $redisManager * @param array $scores * @param int $minimalScore */ public function __construct( SectorManager $sectorManager, SystemManager $systemManager, - OrbitalBaseManager $orbitalBaseManager, - PlayerManager $playerManager, EntityManager $entityManager, - $scores, $minimalScore ) { $this->sectorManager = $sectorManager; $this->systemManager = $systemManager; - $this->orbitalBaseManager = $orbitalBaseManager; - $this->playerManager = $playerManager; $this->entityManager = $entityManager; - $this->scores = $scores; $this->minimalScore = $minimalScore; } @@ -61,31 +48,9 @@ public function __construct( public function onPlaceOwnerChange(PlaceOwnerChangeEvent $event) { $system = $this->systemManager->get($event->getPlace()->rSystem); - $systems = $this->systemManager->getSectorSystems($system->rSector); $sector = $this->sectorManager->get($system->rSector); - $bases = $this->orbitalBaseManager->getSectorBases($system->rSector); - $scores = []; - - foreach ($bases as $base) - { - $player = $this->playerManager->get($base->rPlayer); - - $scores[$player->rColor] = - (!empty($scores[$player->rColor])) - ? $scores[$player->rColor] + $this->scores[$base->typeOfBase] - : $this->scores[$base->typeOfBase] - ; - } - // For each system, the owning faction gains two points - foreach ($systems as $system) { - if ($system->rColor === 0) { - continue; - } - $scores[$system->rColor] = (!empty($scores[$system->rColor])) ? $scores[$system->rColor] + 2 : 2; - } - $scores[0] = 0; - arsort($scores); - reset($scores); + $scores = $this->sectorManager->calculateOwnership($sector); + $newColor = key($scores); $hasEnoughPoints = false; foreach ($scores as $factionId => $score) { diff --git a/system/Modules/Gaia/Manager/PlaceManager.php b/system/Modules/Gaia/Manager/PlaceManager.php index f648d2c50..26906b27b 100644 --- a/system/Modules/Gaia/Manager/PlaceManager.php +++ b/system/Modules/Gaia/Manager/PlaceManager.php @@ -162,6 +162,15 @@ public function updatePlayerPlaces() { $this->entityManager->commit(); $this->entityManager->clear(Place::class); } + + /** + * @param Place $place + * @return bool + */ + public function turnAsEmptyPlace(Place $place) + { + return $this->entityManager->getRepository(Place::class)->turnAsEmptyPlace($place->getId()); + } public function turnAsSpawnPlace($placeId, $playerId) { diff --git a/system/Modules/Gaia/Manager/SectorManager.php b/system/Modules/Gaia/Manager/SectorManager.php index d04dc211c..2d700fbcd 100644 --- a/system/Modules/Gaia/Manager/SectorManager.php +++ b/system/Modules/Gaia/Manager/SectorManager.php @@ -12,20 +12,71 @@ namespace Asylamba\Modules\Gaia\Manager; use Asylamba\Classes\Entity\EntityManager; +use Asylamba\Classes\Redis\RedisManager; +use Asylamba\Classes\Process\LoadBalancer; +use Asylamba\Classes\Task\TaskManager; + +use Asylamba\Modules\Athena\Manager\OrbitalBaseManager; +use Asylamba\Modules\Zeus\Manager\PlayerManager; +use Asylamba\Modules\Gaia\Manager\SystemManager; use Asylamba\Modules\Gaia\Model\Sector; class SectorManager { /** @var EntityManager **/ protected $entityManager; + /** @var RedisManager **/ + protected $redisManager; + /** @var LoadBalancer **/ + protected $loadBalancer; + /** @var TaskManager **/ + protected $taskManager; + /** @var OrbitalBaseManager **/ + protected $orbitalBaseManager; + /** @var PlayerManager **/ + protected $playerManager; + /** @var SystemManager **/ + protected $systemManager; + /** @var array **/ + protected $scores = []; /** * @param EntityManager $entityManager + * @param RedisManager $redisManager + * @param LoadBalancer $loadBalancer + * @param TaskManager $taskManager + * @param OrbitalBaseManager $orbitalBaseManager + * @param PlayerManager $playerManager + * @param SystemManager $systemManager + * @param array $scores */ - public function __construct(EntityManager $entityManager) + public function __construct( + EntityManager $entityManager, + RedisManager $redisManager, + LoadBalancer $loadBalancer, + TaskManager $taskManager, + OrbitalBaseManager $orbitalBaseManager, + PlayerManager $playerManager, + SystemManager $systemManager, + $scores + ) { $this->entityManager = $entityManager; + $this->redisManager = $redisManager; + $this->loadBalancer = $loadBalancer; + $this->taskManager = $taskManager; + $this->orbitalBaseManager = $orbitalBaseManager; + $this->playerManager = $playerManager; + $this->systemManager = $systemManager; + $this->scores = $scores; } + + public function initOwnershipData() + { + $this->loadBalancer->affectTask( + $this->taskManager->createTechnicalTask('gaia.sector_manager', 'calculateAllOwnerships') + ); + } /** * @param int $id @@ -59,4 +110,48 @@ public function changeOwnership(Sector $sector) { $this->entityManager->getRepository(Sector::class)->changeOwnership($sector); } + + public function calculateAllOwnerships() + { + \Asylamba\Classes\Daemon\Server::debug('sectors'); + foreach ($this->getAll() as $sector) { + $this->calculateOwnership($sector); + } + } + + /** + * @param Sector $sector + * @return array + */ + public function calculateOwnership(Sector $sector) + { + $systems = $this->systemManager->getSectorSystems($sector->getId()); + $bases = $this->orbitalBaseManager->getSectorBases($sector->getId()); + $scores = []; + + foreach ($bases as $base) + { + $player = $this->playerManager->get($base->rPlayer); + + $scores[$player->rColor] = + (!empty($scores[$player->rColor])) + ? $scores[$player->rColor] + $this->scores[$base->typeOfBase] + : $this->scores[$base->typeOfBase] + ; + } + // For each system, the owning faction gains two points + foreach ($systems as $system) { + if ($system->rColor === 0) { + continue; + } + $scores[$system->rColor] = (!empty($scores[$system->rColor])) ? $scores[$system->rColor] + 2 : 2; + } + $scores[0] = 0; + arsort($scores); + reset($scores); + + $this->redisManager->getConnection()->set('sector:' . $sector->getId(), serialize($scores)); + + return $scores; + } } \ No newline at end of file diff --git a/system/Modules/Gaia/Repository/PlaceRepository.php b/system/Modules/Gaia/Repository/PlaceRepository.php index 5ec863414..071a8272e 100644 --- a/system/Modules/Gaia/Repository/PlaceRepository.php +++ b/system/Modules/Gaia/Repository/PlaceRepository.php @@ -232,6 +232,21 @@ public function updatePlace($place, $updateDanger = false) ]); } + /** + * @param int $placeId + * @return bool + */ + public function turnAsEmptyPlace($placeId) + { + $statement = $this->connection->prepare( + 'UPDATE place SET typeOfPlace = :place_type WHERE id = :id' + ); + return $statement->execute([ + 'place_type' => Place::EMPTYZONE, + 'id' => $placeId + ]); + } + /** * @param int $placeId * @param int $playerId diff --git a/system/Modules/Gaia/Resource/config/config.yml b/system/Modules/Gaia/Resource/config/config.yml index 9fc04b0e2..3f777fc7f 100644 --- a/system/Modules/Gaia/Resource/config/config.yml +++ b/system/Modules/Gaia/Resource/config/config.yml @@ -39,6 +39,13 @@ services: class: Asylamba\Modules\Gaia\Manager\SectorManager arguments: - "@entity_manager" + - "@redis_manager" + - "@load_balancer" + - "@task_manager" + - "@athena.orbital_base_manager" + - "@zeus.player_manager" + - "@gaia.system_manager" + - "%gaia.scores" gaia.system_manager: class: Asylamba\Modules\Gaia\Manager\SystemManager @@ -53,10 +60,7 @@ services: arguments: - "@gaia.sector_manager" - "@gaia.system_manager" - - "@athena.orbital_base_manager" - - "@zeus.player_manager" - "@entity_manager" - - "%gaia.scores" - "%gaia.sector_minimal_score" tags: - {type: "event_listener", event: "gaia.place_owner_change", method: "onPlaceOwnerChange", order: 2} diff --git a/system/Modules/Zeus/Manager/PlayerManager.php b/system/Modules/Zeus/Manager/PlayerManager.php index 5137bc782..7a0706b0f 100644 --- a/system/Modules/Zeus/Manager/PlayerManager.php +++ b/system/Modules/Zeus/Manager/PlayerManager.php @@ -396,7 +396,7 @@ public function reborn($playerId) { $placeId = NULL; foreach ($sectors as $sector) { # place choice - $qr = $this->entityManager->getConnection()->prepare('SELECT * FROM place AS p + $qr = $this->entityManager->getConnection()->prepare('SELECT p.id FROM place AS p INNER JOIN system AS sy ON p.rSystem = sy.id INNER JOIN sector AS se ON sy.rSector = se.id WHERE p.typeOfPlace = 1 @@ -409,7 +409,7 @@ public function reborn($playerId) { $aw = $qr->fetchAll(); if ($aw !== NULL) { $placeFound = TRUE; - $placeId = $aw[rand(0, (count($aw) - 1))][0]; + $placeId = $aw[rand(0, (count($aw) - 1))]['id']; break; } } diff --git a/system/views/components/faction/data/tactical/sectors.php b/system/views/components/faction/data/tactical/sectors.php index c99bd9848..ff0a04bf4 100755 --- a/system/views/components/faction/data/tactical/sectors.php +++ b/system/views/components/faction/data/tactical/sectors.php @@ -1,41 +1,14 @@ getContainer()->get('demeter.color_manager'); -$database = $this->getContainer()->get('database'); - -$qr = 'SELECT - se.id AS id, - se.rColor AS color, - se.name AS name, - se.points AS points, - (SELECT COUNT(sy.id) FROM system AS sy WHERE sy.rSector = se.id) AS nbc0,'; +$sectorManager = $this->getContainer()->get('gaia.sector_manager'); +$redisManager = $this->getContainer()->get('redis_manager'); +$sectors = $sectorManager->getAll(); $factions = $colorManager->getAll(); -foreach ($factions as $f) { - $qr .= '(SELECT COUNT(sy.id) FROM system AS sy WHERE sy.rColor = ' . $f->id . ' AND sy.rSector = se.id) AS nbc' . $f->id .','; -} - -$qr = substr($qr, 0, -1) . ' FROM sector AS se ORDER BY (nbc' . $faction->id . ' / nbc0) DESC'; - -$qr = $database->prepare($qr); -$qr->execute(); -$aw = $qr->fetchAll(); $qr->closeCursor(); - - -$sectort = array( - 'Secteurs conquis' => array(), - 'Secteurs en balance' => array() -); - -for ($i = 0; $i < count($aw); $i++) { - if ($aw[$i]['color'] == $faction->id) { - $sectort['Secteurs conquis'][] = $aw[$i]; - } else { - $sectort['Secteurs en balance'][] = $aw[$i]; - } -} echo '