1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stu\Module\Tick\Colony\Component; |
4
|
|
|
|
5
|
|
|
use Stu\Module\Research\ResearchStateFactoryInterface; |
6
|
|
|
use Stu\Orm\Entity\ColonyInterface; |
7
|
|
|
use Stu\Orm\Entity\ResearchedInterface; |
8
|
|
|
use Stu\Orm\Repository\ResearchedRepositoryInterface; |
9
|
|
|
|
10
|
|
|
final class AdvanceResearch implements ColonyTickComponentInterface |
11
|
|
|
{ |
12
|
|
|
private ResearchedRepositoryInterface $researchedRepository; |
13
|
|
|
|
14
|
|
|
private ResearchStateFactoryInterface $researchStateFactory; |
15
|
|
|
|
16
|
|
|
/** @var array<int, int> */ |
17
|
|
|
private array $userToResearchCommodity = []; |
18
|
|
|
|
19
|
10 |
|
public function __construct( |
20
|
|
|
ResearchedRepositoryInterface $researchedRepository, |
21
|
|
|
ResearchStateFactoryInterface $researchStateFactory |
22
|
|
|
) { |
23
|
10 |
|
$this->researchedRepository = $researchedRepository; |
24
|
10 |
|
$this->researchStateFactory = $researchStateFactory; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
10 |
|
public function work(ColonyInterface $colony, array &$production): void |
29
|
|
|
{ |
30
|
10 |
|
$researches = $this->researchedRepository->getCurrentResearch($colony->getUser()); |
31
|
10 |
|
$currentResearch = empty($researches) ? null : current($researches); |
32
|
10 |
|
$waitingResearch = count($researches) > 1 ? $researches[1] : null; |
33
|
|
|
|
34
|
10 |
|
if ($currentResearch === null) { |
35
|
1 |
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
9 |
|
$commodityId = $currentResearch->getResearch()->getCommodityId(); |
39
|
9 |
|
if (!array_key_exists($commodityId, $production)) { |
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
if (!$this->isCommodityAllowed($commodityId, $colony->getUser()->getId())) { |
44
|
1 |
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
8 |
|
$remaining = $this->advanceResearchState($currentResearch, $production[$commodityId]->getProduction()); |
48
|
|
|
|
49
|
8 |
|
if ($remaining < 1) { |
50
|
5 |
|
return; |
51
|
|
|
} |
52
|
4 |
|
if ($waitingResearch === null) { |
53
|
1 |
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
$commodityId = $waitingResearch->getResearch()->getCommodityId(); |
57
|
3 |
|
if (!$this->isCommodityAllowed($commodityId, $colony->getUser()->getId())) { |
58
|
1 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$this->advanceResearchState($waitingResearch, $remaining); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
private function isCommodityAllowed(int $commodityId, int $userId): bool |
65
|
|
|
{ |
66
|
8 |
|
if (!array_key_exists($userId, $this->userToResearchCommodity)) { |
67
|
8 |
|
$this->userToResearchCommodity[$userId] = $commodityId; |
68
|
|
|
|
69
|
8 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
return $commodityId === $this->userToResearchCommodity[$userId]; |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
private function advanceResearchState(ResearchedInterface $researched, int $amount): int |
76
|
|
|
{ |
77
|
8 |
|
return $this->researchStateFactory->createResearchState()->advance( |
78
|
8 |
|
$researched, |
79
|
8 |
|
$amount |
80
|
8 |
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|