|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Database\Lib; |
|
6
|
|
|
|
|
7
|
|
|
use Stu\Module\Award\Lib\CreateUserAwardInterface; |
|
8
|
|
|
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface; |
|
9
|
|
|
use Stu\Orm\Entity\DatabaseCategory; |
|
10
|
|
|
use Stu\Orm\Entity\DatabaseEntry; |
|
11
|
|
|
use Stu\Orm\Entity\User; |
|
12
|
|
|
use Stu\Orm\Repository\DatabaseCategoryAwardRepositoryInterface; |
|
13
|
|
|
use Stu\Orm\Repository\DatabaseEntryRepositoryInterface; |
|
14
|
|
|
use Stu\Orm\Repository\DatabaseUserRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class CreateDatabaseEntry implements CreateDatabaseEntryInterface |
|
17
|
|
|
{ |
|
18
|
2 |
|
public function __construct( |
|
19
|
|
|
private DatabaseEntryRepositoryInterface $databaseEntryRepository, |
|
20
|
|
|
private DatabaseUserRepositoryInterface $databaseUserRepository, |
|
21
|
|
|
private CreatePrestigeLogInterface $createPrestigeLog, |
|
22
|
|
|
private CreateUserAwardInterface $createUserAward, |
|
23
|
|
|
private DatabaseCategoryAwardRepositoryInterface $databaseCategoryAwardRepository |
|
24
|
2 |
|
) {} |
|
25
|
|
|
|
|
26
|
|
|
#[\Override] |
|
27
|
|
|
public function createDatabaseEntryForUser(User $user, int $databaseEntryId): ?DatabaseEntry |
|
28
|
|
|
{ |
|
29
|
|
|
if ($databaseEntryId === 0) { |
|
30
|
|
|
return null; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (!$user->hasColony()) { |
|
34
|
|
|
return null; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$databaseEntry = $this->databaseEntryRepository->find($databaseEntryId); |
|
38
|
|
|
|
|
39
|
|
|
if ($databaseEntry === null) { |
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
//create new user entry |
|
44
|
|
|
$userEntry = $this->databaseUserRepository |
|
45
|
|
|
->prototype() |
|
46
|
|
|
->setUser($user) |
|
47
|
|
|
->setDatabaseEntry($databaseEntry) |
|
48
|
|
|
->setDate(time()); |
|
49
|
|
|
|
|
50
|
|
|
$this->databaseUserRepository->save($userEntry); |
|
51
|
|
|
|
|
52
|
|
|
if (!$user->isNpc()) { |
|
53
|
|
|
//create prestige log |
|
54
|
|
|
$this->createPrestigeLog->createLogForDatabaseEntry($databaseEntry, $user, $userEntry->getDate()); |
|
55
|
|
|
|
|
56
|
|
|
$this->checkForCategoryCompletion( |
|
57
|
|
|
$user, |
|
58
|
|
|
$databaseEntry->getCategory(), |
|
59
|
|
|
$databaseEntryId, |
|
60
|
|
|
$databaseEntry->getLayerId() |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $databaseEntry; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
#[\Override] |
|
68
|
|
|
public function checkForCategoryCompletion( |
|
69
|
|
|
User $user, |
|
70
|
|
|
DatabaseCategory $category, |
|
71
|
|
|
?int $ignoredDatabaseEntryId = null, |
|
72
|
|
|
?int $layerId = null |
|
73
|
|
|
): void { |
|
74
|
1 |
|
if ($ignoredDatabaseEntryId === null && $layerId === null) { |
|
75
|
1 |
|
$layerIds = $this->databaseEntryRepository->getDistinctLayerIdsByCategory($category->getId()); |
|
76
|
1 |
|
foreach ($layerIds as $currentLayerId) { |
|
77
|
1 |
|
if ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer( |
|
78
|
1 |
|
$user->getId(), |
|
79
|
1 |
|
$category->getId(), |
|
80
|
1 |
|
null, |
|
81
|
1 |
|
$currentLayerId |
|
82
|
1 |
|
)) { |
|
83
|
1 |
|
$categoryAward = $this->databaseCategoryAwardRepository->findByCategoryIdAndLayerId( |
|
84
|
1 |
|
$category->getId(), |
|
85
|
1 |
|
$currentLayerId |
|
86
|
1 |
|
); |
|
87
|
|
|
|
|
88
|
1 |
|
if ($categoryAward !== null && $categoryAward->getAward() !== null) { |
|
89
|
|
|
$this->createUserAward->createAwardForUser($user, $categoryAward->getAward()); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} elseif ($this->databaseUserRepository->hasUserCompletedCategoryAndLayer( |
|
94
|
|
|
$user->getId(), |
|
95
|
|
|
$category->getId(), |
|
96
|
|
|
$ignoredDatabaseEntryId, |
|
97
|
|
|
$layerId |
|
98
|
|
|
)) { |
|
99
|
|
|
$categoryAward = $this->databaseCategoryAwardRepository->findByCategoryIdAndLayerId( |
|
100
|
|
|
$category->getId(), |
|
101
|
|
|
$layerId |
|
102
|
|
|
); |
|
103
|
|
|
if ($categoryAward !== null && $categoryAward->getAward() !== null) { |
|
104
|
|
|
$this->createUserAward->createAwardForUser($user, $categoryAward->getAward()); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|