Complex classes like PlayerManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PlayerManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class PlayerManager { |
||
| 48 | /** @var EntityManager **/ |
||
| 49 | protected $entityManager; |
||
| 50 | /** @var GalaxyColorManager **/ |
||
| 51 | protected $galaxyColorManager; |
||
| 52 | /** @var SectorManager */ |
||
| 53 | protected $sectorManager; |
||
| 54 | /** @var NotificationManager **/ |
||
| 55 | protected $notificationManager; |
||
| 56 | /** @var OrbitalBaseManager **/ |
||
| 57 | protected $orbitalBaseManager; |
||
| 58 | /** @var PlaceManager **/ |
||
| 59 | protected $placeManager; |
||
| 60 | /** @var CommanderManager **/ |
||
| 61 | protected $commanderManager; |
||
| 62 | /** @var ColorManager **/ |
||
| 63 | protected $colorManager; |
||
| 64 | /** @var ResearchManager **/ |
||
| 65 | protected $researchManager; |
||
| 66 | /** @var TransactionManager **/ |
||
| 67 | protected $transactionManager; |
||
| 68 | /** @var CommercialRouteManager **/ |
||
| 69 | protected $commercialRouteManager; |
||
| 70 | /** @var TechnologyManager **/ |
||
| 71 | protected $technologyManager; |
||
| 72 | /** @var PlayerBonusManager **/ |
||
| 73 | protected $playerBonusManager; |
||
| 74 | /** @var SessionWrapper **/ |
||
| 75 | protected $sessionWrapper; |
||
| 76 | /** @var API **/ |
||
| 77 | protected $api; |
||
| 78 | /** @var int **/ |
||
| 79 | protected $playerBaseLevel; |
||
| 80 | /** @var int **/ |
||
| 81 | protected $playerTaxCoeff; |
||
| 82 | /** @var string **/ |
||
| 83 | protected $serverId; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param EntityManager $entityManager |
||
| 87 | * @param GalaxyColorManager $galaxyColorManager |
||
| 88 | * @param SectorManager $sectorManager |
||
| 89 | * @param NotificationManager $notificationManager |
||
| 90 | * @param OrbitalBaseManager $orbitalBaseManager |
||
| 91 | * @param PlaceManager $placeManager |
||
| 92 | * @param CommanderManager $commanderManager |
||
| 93 | * @param ColorManager $colorManager |
||
| 94 | * @param ResearchManager $researchManager |
||
| 95 | * @param TransactionManager $transactionManager |
||
| 96 | * @param CommercialRouteManager $commercialRouteManager |
||
| 97 | * @param TechnologyManager $technologyManager |
||
| 98 | * @param PlayerBonusManager $playerBonusManager |
||
| 99 | * @param SessionWrapper $session |
||
| 100 | * @param API $api |
||
| 101 | * @param int $playerBaseLevel |
||
| 102 | * @param int $playerTaxCoeff |
||
| 103 | * @param string $serverId |
||
| 104 | */ |
||
| 105 | public function __construct( |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param int $playerId |
||
| 148 | * @return Player |
||
| 149 | */ |
||
| 150 | public function get($playerId) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $name |
||
| 163 | * @return Player |
||
| 164 | */ |
||
| 165 | public function getByName($name) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $bindKey |
||
| 172 | * @return Player |
||
| 173 | */ |
||
| 174 | public function getByBindKey($bindKey) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param int $id |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function getGodSons($id) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param array $ids |
||
| 196 | * @param array $statements |
||
| 197 | */ |
||
| 198 | public function getByIdsAndStatements($ids, $statements) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param array $statements |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getByStatements($statements) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | public function countActivePlayers() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | public function countAllPlayers() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param int $factionId |
||
| 230 | * @param array $statements |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function countByFactionAndStatements($factionId, $statements) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param int $factionId |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function getFactionPlayers($factionId) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param int $factionId |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | public function getFactionPlayersByRanking($factionId) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param int $factionId |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getFactionPlayersByName($factionId) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param int $factionId |
||
| 267 | * @return Player |
||
| 268 | */ |
||
| 269 | public function getFactionAccount($factionId) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param int $factionId |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | public function getLastFactionPlayers($factionId) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param int $factionId |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function getParliamentMembers($factionId) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param int $factionId |
||
| 294 | * @param int $status |
||
| 295 | * @return Player |
||
| 296 | */ |
||
| 297 | public function getGovernmentMember($factionId, $status) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param int $factionId |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function getGovernmentMembers($factionId) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param int $factionId |
||
| 313 | * @return Player |
||
| 314 | */ |
||
| 315 | public function getFactionLeader($factionId) |
||
| 322 | |||
| 323 | public function getActivePlayers() |
||
| 327 | |||
| 328 | public function search($search) { |
||
| 331 | |||
| 332 | protected function fill(Player $player) { |
||
| 337 | |||
| 338 | public function isSynchronized(Player $player) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param Player $player |
||
| 345 | */ |
||
| 346 | public function saveSessionData(Player $player) |
||
| 358 | |||
| 359 | public function add(Player $player) { |
||
| 363 | |||
| 364 | public function kill($playerId) { |
||
| 388 | |||
| 389 | public function reborn($playerId) { |
||
| 481 | |||
| 482 | public function updatePlayersCredits() |
||
| 541 | |||
| 542 | public function uCredit(Player $player, $playerBases, $playerBonus, $commanders, $rsmSession, &$factions, $transactions) { |
||
| 543 | |||
| 544 | $popTax = 0; $nationTax = 0; |
||
| 545 | $credits = $player->credit; |
||
| 546 | $schoolInvests = 0; $antiSpyInvests = 0; |
||
| 547 | |||
| 548 | $totalGain = 0; |
||
| 549 | |||
| 550 | # university investments |
||
| 551 | $uniInvests = $player->iUniversity; |
||
| 552 | $naturalTech = ($player->iUniversity * $player->partNaturalSciences / 100); |
||
| 553 | $lifeTech = ($player->iUniversity * $player->partLifeSciences / 100); |
||
| 554 | $socialTech = ($player->iUniversity * $player->partSocialPoliticalSciences / 100); |
||
| 555 | $informaticTech = ($player->iUniversity * $player->partInformaticEngineering / 100); |
||
| 556 | |||
| 557 | foreach ($playerBases as $base) { |
||
| 558 | $popTax = Game::getTaxFromPopulation($base->getPlanetPopulation(), $base->typeOfBase, $this->playerTaxCoeff); |
||
| 559 | $popTax += $popTax * $playerBonus->bonus->get(PlayerBonus::POPULATION_TAX) / 100; |
||
| 560 | $nationTax = $base->tax * $popTax / 100; |
||
| 561 | |||
| 562 | # revenu des routes commerciales |
||
| 563 | $routesIncome = $this->commercialRouteManager->getBaseIncome($base); |
||
| 564 | $routesIncome += $routesIncome * $playerBonus->bonus->get(PlayerBonus::COMMERCIAL_INCOME) / 100; |
||
| 565 | |||
| 566 | $credits += ($popTax - $nationTax + $routesIncome); |
||
| 567 | $totalGain += $popTax - $nationTax + $routesIncome; |
||
| 568 | |||
| 569 | # investments |
||
| 570 | $schoolInvests += $base->getISchool(); |
||
| 571 | $antiSpyInvests += $base->getIAntiSpy(); |
||
| 572 | |||
| 573 | # paiement à l'alliance |
||
| 574 | if ($player->rColor != 0) { |
||
| 575 | foreach ($factions as $faction) { |
||
| 576 | if ($faction->id == $base->sectorColor) { |
||
| 577 | $faction->increaseCredit($nationTax); |
||
| 578 | break; |
||
| 579 | } |
||
| 580 | } |
||
| 581 | } |
||
| 582 | } |
||
| 583 | # si la balance de crédit est positive |
||
| 584 | $totalInvests = $uniInvests + $schoolInvests + $antiSpyInvests; |
||
| 585 | if ($credits >= $totalInvests) { |
||
| 586 | $credits -= $totalInvests; |
||
| 587 | $newCredit = $credits; |
||
| 588 | } else { # si elle est négative |
||
| 589 | $n = new Notification(); |
||
| 590 | $n->setRPlayer($player->id); |
||
| 591 | $n->setTitle('Caisses vides'); |
||
| 592 | $n->addBeg()->addTxt('Domaine')->addSep(); |
||
| 593 | $n->addTxt('Vous ne disposez pas d\'assez de crédits.')->addBrk()->addTxt('Les impôts que vous percevez ne suffisent plus à payer vos investissements.'); |
||
| 594 | |||
| 595 | if ($totalInvests - $uniInvests <= $totalGain) { |
||
| 596 | # we can decrease only the uni investments |
||
| 597 | $newIUniversity = $totalGain - $schoolInvests - $antiSpyInvests; |
||
| 598 | |||
| 599 | $player->iUniversity = $newIUniversity; |
||
| 600 | $credits -= ($newIUniversity + $schoolInvests + $antiSpyInvests); |
||
| 601 | |||
| 602 | # recompute the real amount for each research |
||
| 603 | $naturalTech = ($player->iUniversity * $player->partNaturalSciences / 100); |
||
| 604 | $lifeTech = ($player->iUniversity * $player->partLifeSciences / 100); |
||
| 605 | $socialTech = ($player->iUniversity * $player->partSocialPoliticalSciences / 100); |
||
| 606 | $informaticTech = ($player->iUniversity * $player->partInformaticEngineering / 100); |
||
| 607 | |||
| 608 | $n->addBrk()->addTxt(' Vos investissements dans l\'université ont été modifiés afin qu\'aux prochaines relèves vous puissiez payer. Attention, cette situation ne vous apporte pas de crédits.'); |
||
| 609 | } else { |
||
| 610 | # we have to decrease the other investments too |
||
| 611 | # investments in university to 0 |
||
| 612 | $player->iUniversity = 0; |
||
| 613 | # then we decrease the other investments with a ratio |
||
| 614 | $ratioDifference = floor($totalGain / ($schoolInvests + $antiSpyInvests) * 100); |
||
| 615 | |||
| 616 | $naturalTech = 0; $lifeTech = 0; $socialTech = 0; $informaticTech = 0; |
||
| 617 | |||
| 618 | foreach ($playerBases as $orbitalBase) { |
||
| 619 | $newISchool = ceil($orbitalBase->getISchool() * $ratioDifference / 100); |
||
| 620 | $newIAntiSpy = ceil($orbitalBase->getIAntiSpy() * $ratioDifference / 100); |
||
| 621 | |||
| 622 | $orbitalBase->setISchool($newISchool); |
||
| 623 | $orbitalBase->setIAntiSpy($newIAntiSpy); |
||
| 624 | |||
| 625 | $credits -= ($newISchool + $newIAntiSpy); |
||
| 626 | |||
| 627 | $naturalTech += ($newISchool * $player->partNaturalSciences / 100); |
||
| 628 | $lifeTech += ($newISchool * $player->partLifeSciences / 100); |
||
| 629 | $socialTech += ($newISchool * $player->partSocialPoliticalSciences / 100); |
||
| 630 | $informaticTech += ($newISchool * $player->partInformaticEngineering / 100); |
||
| 631 | } |
||
| 632 | $n->addTxt(' Seuls ')->addStg($ratioDifference . '%')->addTxt(' des crédits d\'investissements peuvent être honorés.')->addBrk(); |
||
| 633 | $n->addTxt(' Vos investissements dans l\'université ont été mis à zéro et les autres diminués de façon pondérée afin qu\'aux prochaines relèves vous puissiez payer. Attention, cette situation ne vous apporte pas de crédits.'); |
||
| 634 | } |
||
| 635 | |||
| 636 | $n->addSep()->addLnk('financial', 'vers les finances →'); |
||
| 637 | $n->addEnd(); |
||
| 638 | |||
| 639 | $this->notificationManager->add($n); |
||
| 640 | |||
| 641 | $newCredit = $credits; |
||
| 642 | } |
||
| 643 | |||
| 644 | # payer les commandants |
||
| 645 | $nbOfComNotPaid = 0; |
||
| 646 | $comList = new ArrayList(); |
||
| 647 | foreach ($commanders as $commander) { |
||
| 648 | if (!in_array($commander->getStatement(), [Commander::AFFECTED, Commander::MOVING])) { |
||
| 649 | continue; |
||
| 650 | } |
||
| 651 | if ($newCredit >= (Commander::LVLINCOMECOMMANDER * $commander->getLevel())) { |
||
| 652 | $newCredit -= (Commander::LVLINCOMECOMMANDER * $commander->getLevel()); |
||
| 653 | continue; |
||
| 654 | } |
||
| 655 | # on remet les vaisseaux dans les hangars |
||
| 656 | $this->commanderManager->emptySquadrons($commander); |
||
| 657 | |||
| 658 | # on vend le commandant |
||
| 659 | $commander->setStatement(Commander::ONSALE); |
||
| 660 | $commander->setRPlayer(ID_GAIA); |
||
| 661 | |||
| 662 | # TODO : vendre le commandant au marché |
||
| 663 | # (ou alors le mettre en statement COM_DESERT et supprimer ses escadrilles) |
||
| 664 | |||
| 665 | $comList->add($nbOfComNotPaid, $commander->getName()); |
||
| 666 | $nbOfComNotPaid++; |
||
| 667 | $this->entityManager->flush($commander); |
||
| 668 | } |
||
| 669 | # si au moins un commandant n'a pas pu être payé --> envoyer une notif |
||
| 670 | if ($nbOfComNotPaid) { |
||
| 671 | $n = new Notification(); |
||
| 672 | $n->setRPlayer($player->id); |
||
| 673 | $n->setTitle('Commandant impayé'); |
||
| 674 | |||
| 675 | $n->addBeg()->addTxt('Domaine')->addSep(); |
||
| 676 | if ($nbOfComNotPaid == 1) { |
||
| 677 | $n->addTxt('Vous n\'avez pas assez de crédits pour payer votre commandant ' . $comList->get(0) . '. Celui-ci a donc déserté ! '); |
||
| 678 | $n->addBrk()->addTxt('Il est allé proposer ses services sur le marché. Si vous voulez le récupérer, vous pouvez vous y rendre et le racheter.'); |
||
| 679 | } else { |
||
| 680 | $n->addTxt('Vous n\'avez pas assez de crédits pour payer certains de vos commandants. Ils ont donc déserté ! ')->addBrk(); |
||
| 681 | $n->addTxt('Voici la liste de ces commandants : '); |
||
| 682 | for ($i = 0; $i < $comList->size() - 2; $i++) { |
||
| 683 | $n->addTxt($comList->get($i) . ', '); |
||
| 684 | } |
||
| 685 | $n->addTxt($comList->get($comList->size() - 2) . ' et ' . $comList->get($comList->size() - 1) . '.'); |
||
| 686 | $n->addBrk()->addTxt('Ils sont tous allés proposer leurs services sur le marché. Si vous voulez les récupérer, vous pouvez vous y rendre et les racheter.'); |
||
| 687 | } |
||
| 688 | $n->addEnd(); |
||
| 689 | $this->notificationManager->add($n); |
||
| 690 | } |
||
| 691 | |||
| 692 | # payer l'entretien des vaisseaux |
||
| 693 | # vaisseaux en vente |
||
| 694 | $transactionTotalCost = 0; |
||
| 695 | $nbTransactions = count($transactions); |
||
| 696 | for ($i = ($nbTransactions - 1); $i >= 0; $i--) { |
||
| 697 | $transaction = $transactions[$i]; |
||
| 698 | $transactionTotalCost += ShipResource::getInfo($transaction->identifier, 'cost') * ShipResource::COST_REDUCTION * $transaction->quantity; |
||
| 699 | } |
||
| 700 | if ($newCredit >= $transactionTotalCost) { |
||
| 701 | $newCredit -= $transactionTotalCost; |
||
| 702 | } else { |
||
| 703 | $newCredit = 0; |
||
| 704 | } |
||
| 705 | # vaisseaux affectés |
||
| 706 | foreach ($commanders as $commander) { |
||
| 707 | $ships = $commander->getNbrShipByType(); |
||
| 708 | $cost = Game::getFleetCost($ships, TRUE); |
||
| 709 | |||
| 710 | if ($newCredit >= $cost) { |
||
| 711 | $newCredit -= $cost; |
||
| 712 | continue; |
||
| 713 | } |
||
| 714 | # on vend le commandant car on n'arrive pas à payer la flotte (trash hein) |
||
| 715 | $commander->setStatement(Commander::ONSALE); |
||
| 716 | $commander->setRPlayer(ID_GAIA); |
||
| 717 | |||
| 718 | $n = new Notification(); |
||
| 719 | $n->setRPlayer($player->id); |
||
| 720 | $n->setTitle('Flotte impayée'); |
||
| 721 | $n->addBeg()->addTxt('Domaine')->addSep(); |
||
| 722 | $n->addTxt('Vous n\'avez pas assez de crédits pour payer l\'entretien de la flotte de votre officier ' . $commander->name . '. Celui-ci a donc déserté ! ... avec la flotte, désolé.'); |
||
| 723 | $n->addEnd(); |
||
| 724 | $this->notificationManager->add($n); |
||
| 725 | $this->entityManager->flush($commander); |
||
| 726 | } |
||
| 727 | # vaisseaux sur la planète |
||
| 728 | foreach ($playerBases as $base) { |
||
| 729 | $cost = Game::getFleetCost($base->shipStorage, FALSE); |
||
| 730 | |||
| 731 | if ($newCredit >= $cost) { |
||
| 732 | $newCredit -= $cost; |
||
| 733 | } else { |
||
| 734 | # n'arrive pas à tous les payer ! |
||
| 735 | for ($j = ShipResource::SHIP_QUANTITY-1; $j >= 0; $j--) { |
||
| 736 | if ($base->shipStorage[$j] > 0) { |
||
| 737 | $unitCost = ShipResource::getInfo($j, 'cost'); |
||
| 738 | |||
| 739 | $possibleMaintenable = floor($newCredit / $unitCost); |
||
| 740 | if ($possibleMaintenable > $base->shipStorage[$j]) { |
||
| 741 | $possibleMaintenable = $base->shipStorage[$j]; |
||
| 742 | } |
||
| 743 | $newCredit -= $possibleMaintenable * $unitCost; |
||
| 744 | |||
| 745 | $toKill = $base->shipStorage[$j] - $possibleMaintenable; |
||
| 746 | if ($toKill > 0) { |
||
| 747 | $this->orbitalBaseManager->removeShipFromDock($base, $j, $toKill); |
||
| 748 | |||
| 749 | $n = new Notification(); |
||
| 750 | $n->setRPlayer($player->id); |
||
| 751 | $n->setTitle('Entretien vaisseau impayé'); |
||
| 752 | |||
| 753 | $n->addBeg()->addTxt('Domaine')->addSep(); |
||
| 754 | if ($toKill == 1) { |
||
| 755 | $n->addTxt('Vous n\'avez pas assez de crédits pour payer l\'entretien d\'un(e) ' . ShipResource::getInfo($j, 'codeName') . ' sur ' . $base->name . '. Ce vaisseau part donc à la casse ! '); |
||
| 756 | } else { |
||
| 757 | $n->addTxt('Vous n\'avez pas assez de crédits pour payer l\'entretien de ' . $toKill . ' ' . ShipResource::getInfo($j, 'codeName') . 's sur ' . $base->name . '. Ces vaisseaux partent donc à la casse ! '); |
||
| 758 | } |
||
| 759 | $n->addEnd(); |
||
| 760 | $this->notificationManager->add($n); |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | } |
||
| 765 | } |
||
| 766 | |||
| 767 | # faire les recherches |
||
| 768 | $S_RSM1 = $this->researchManager->getCurrentSession(); |
||
| 769 | $this->researchManager->changeSession($rsmSession); |
||
| 770 | if ($this->researchManager->size() == 1) { |
||
| 771 | # add the bonus |
||
| 772 | $naturalTech += $naturalTech * $playerBonus->bonus->get(PlayerBonus::UNI_INVEST) / 100; |
||
| 773 | $lifeTech += $lifeTech * $playerBonus->bonus->get(PlayerBonus::UNI_INVEST) / 100; |
||
| 774 | $socialTech += $socialTech * $playerBonus->bonus->get(PlayerBonus::UNI_INVEST) / 100; |
||
| 775 | $informaticTech += $informaticTech * $playerBonus->bonus->get(PlayerBonus::UNI_INVEST) / 100; |
||
| 776 | |||
| 777 | $tech = $this->researchManager->get(); |
||
| 778 | $this->researchManager->update($tech, $player->id, $naturalTech, $lifeTech, $socialTech, $informaticTech); |
||
| 779 | } else { |
||
| 780 | throw new ErrorException('une erreur est survenue lors de la mise à jour des investissements de recherche pour le joueur ' . $player->id . '.'); |
||
| 781 | } |
||
| 782 | $this->researchManager->changeSession($S_RSM1); |
||
| 783 | |||
| 784 | $player->credit = $newCredit; |
||
| 785 | } |
||
| 786 | |||
| 787 | // OBJECT METHOD |
||
| 788 | public function increaseCredit(Player $player, $credit) { |
||
| 793 | |||
| 794 | public function decreaseCredit(Player $player, $credit) { |
||
| 803 | |||
| 804 | public function increaseExperience(Player $player, $exp) { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @param int $playerId |
||
| 858 | * @param int $investment |
||
| 859 | */ |
||
| 860 | public function updateUniversityInvestment($playerId, $investment) |
||
| 864 | } |