|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: benedikt |
|
6
|
|
|
* Date: 1/7/18 |
|
7
|
|
|
* Time: 9:22 PM |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Service\RankingSystem; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use Tfboe\FmLib\Entity\Helpers\TournamentHierarchyInterface; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class EntityComparerByTimeStartTimeAndLocalIdentifier |
|
18
|
|
|
* @package Tfboe\FmLib\Service\RankingSystemListService |
|
19
|
|
|
*/ |
|
20
|
|
|
class EntityComparerByTimeStartTimeAndLocalIdentifier implements EntityComparerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
//<editor-fold desc="Fields"> |
|
23
|
|
|
/** @var TimeServiceInterface */ |
|
24
|
|
|
private $timeService; |
|
25
|
|
|
//</editor-fold desc="Fields"> |
|
26
|
|
|
|
|
27
|
|
|
//<editor-fold desc="Constructor"> |
|
28
|
|
|
/** |
|
29
|
|
|
* EntityComparerByTimeStartTimeAndLocalIdentifier constructor. |
|
30
|
|
|
* @param TimeServiceInterface $timeService |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(TimeServiceInterface $timeService) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->timeService = $timeService; |
|
35
|
|
|
} |
|
36
|
|
|
//</editor-fold desc="Constructor"> |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
//<editor-fold desc="Public Methods"> |
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function compareEntities(TournamentHierarchyInterface $entity1, TournamentHierarchyInterface $entity2): int |
|
44
|
|
|
{ |
|
45
|
|
|
$tmpE1 = $entity1; |
|
46
|
|
|
$tmpE2 = $entity2; |
|
47
|
|
|
while ($tmpE1 !== null && $tmpE2 !== null && $tmpE1->getId() !== $tmpE2->getId()) { |
|
48
|
|
|
$comparison = $this->compareEntityTimes($tmpE1, $tmpE2); |
|
49
|
|
|
if ($comparison !== 0) { |
|
50
|
|
|
return $comparison; |
|
51
|
|
|
} |
|
52
|
|
|
$tmpE1 = $tmpE1->getParent(); |
|
53
|
|
|
$tmpE2 = $tmpE2->getParent(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->compareLocalIdentifiersWithinTournament($entity1, $entity2); |
|
57
|
|
|
} |
|
58
|
|
|
//</editor-fold desc="Public Methods"> |
|
59
|
|
|
|
|
60
|
|
|
//<editor-fold desc="Protected Methods"> |
|
61
|
|
|
/** |
|
62
|
|
|
* Compares the times of the two entities using the time array and using their start times |
|
63
|
|
|
* @param TournamentHierarchyInterface $entity1 the first entity to compare |
|
64
|
|
|
* @param TournamentHierarchyInterface $entity2 the second entity to compare |
|
65
|
|
|
* @return int returns -1 if the relevant times of entity1 are before the relevant times of entity2, 1 if the times of |
|
66
|
|
|
* entity2 are before the times of entity1 and 0 if they have the same times |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function compareEntityTimes(TournamentHierarchyInterface $entity1, |
|
69
|
|
|
TournamentHierarchyInterface $entity2) |
|
70
|
|
|
{ |
|
71
|
|
|
$time1 = $this->timeService->getTime($entity1); |
|
72
|
|
|
$time2 = $this->timeService->getTime($entity2); |
|
73
|
|
|
if ($time1 < $time2) { |
|
74
|
|
|
return -1; |
|
75
|
|
|
} else if ($time1 > $time2) { |
|
76
|
|
|
return 1; |
|
77
|
|
|
} |
|
78
|
|
|
if ($entity1->getStartTime() !== null && $entity2->getStartTime() !== null) { |
|
79
|
|
|
if ($entity1->getStartTime() < $entity2->getStartTime()) { |
|
80
|
|
|
return -1; |
|
81
|
|
|
} else if ($entity1->getStartTime() > $entity2->getStartTime()) { |
|
82
|
|
|
return 1; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return 0; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Compares the local identifiers of the predecessors of the given entities, beginning from the tournament level |
|
90
|
|
|
* @param TournamentHierarchyInterface $entity1 the first entity to compare |
|
91
|
|
|
* @param TournamentHierarchyInterface $entity2 the second entity to compare |
|
92
|
|
|
* @return int returns -1 if the first predecessor with a lower local identifier is of entity1, 1 if it is of entity2 |
|
93
|
|
|
* and 0 if the two entities are equal (<=> all predecessors have same local identifiers) |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function compareLocalIdentifiersWithinTournament(TournamentHierarchyInterface $entity1, |
|
96
|
|
|
TournamentHierarchyInterface $entity2) |
|
97
|
|
|
{ |
|
98
|
|
|
//compare unique identifiers within tournament |
|
99
|
|
|
$predecessors1 = $this->getPredecessors($entity1); |
|
100
|
|
|
$predecessors2 = $this->getPredecessors($entity2); |
|
101
|
|
|
|
|
102
|
|
|
for ($i = count($predecessors1) - 1; $i >= 0; $i--) { |
|
103
|
|
|
if ($predecessors1[$i]->getLocalIdentifier() !== $predecessors2[$i]->getLocalIdentifier()) { |
|
104
|
|
|
return $predecessors1[$i]->getLocalIdentifier() <=> $predecessors2[$i]->getLocalIdentifier(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
//the two entities are equal |
|
109
|
|
|
return 0; |
|
110
|
|
|
} |
|
111
|
|
|
//</editor-fold desc="Protected Methods"> |
|
112
|
|
|
|
|
113
|
|
|
//<editor-fold desc="Private Methods"> |
|
114
|
|
|
/** |
|
115
|
|
|
* Gets a list of all predecessors of the given entity $entity (inclusive $entity itself). |
|
116
|
|
|
* @param TournamentHierarchyInterface $entity the entity for which to get the predecessors |
|
117
|
|
|
* @return TournamentHierarchyInterface[] the predecessors of $entity inclusive $entity |
|
118
|
|
|
*/ |
|
119
|
|
|
private function getPredecessors(TournamentHierarchyInterface $entity): array |
|
120
|
|
|
{ |
|
121
|
|
|
$res = []; |
|
122
|
|
|
while ($entity !== null) { |
|
123
|
|
|
$res[] = $entity; |
|
124
|
|
|
$entity = $entity->getParent(); |
|
125
|
|
|
} |
|
126
|
|
|
return $res; |
|
127
|
|
|
} |
|
128
|
|
|
//</editor-fold desc="Private Methods"> |
|
129
|
|
|
} |