1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AardsGerds\Game\Infrastructure\Persistence; |
6
|
|
|
|
7
|
|
|
use AardsGerds\Game\Build\Talent\SecretKnowledge\SecretKnowledge; |
8
|
|
|
use AardsGerds\Game\Build\Talent\Talent; |
9
|
|
|
use AardsGerds\Game\Build\Talent\TalentCollection; |
10
|
|
|
use AardsGerds\Game\Build\Talent\WeaponMastery\WeaponMastery; |
11
|
|
|
use AardsGerds\Game\Inventory\Inventory; |
12
|
|
|
use AardsGerds\Game\Inventory\InventoryItem; |
13
|
|
|
use AardsGerds\Game\Inventory\Weapon\RebredirWeapon; |
14
|
|
|
use AardsGerds\Game\Inventory\Weapon\Weapon; |
15
|
|
|
use AardsGerds\Game\Player\Player; |
16
|
|
|
use function Lambdish\Phunctional\map; |
17
|
|
|
|
18
|
|
|
final class NormalizePlayer |
19
|
|
|
{ |
20
|
1 |
|
public function __invoke(Player $player): array |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
1 |
|
'name' => $player->getName(), |
24
|
1 |
|
'health' => $player->getHealth()->get(), |
25
|
1 |
|
'etherum' => $player->getEtherum()->get(), |
26
|
1 |
|
'strength' => $player->getStrength()->get(), |
27
|
1 |
|
'initiative' => $player->getInitiative()->get(), |
28
|
1 |
|
'talents' => self::normalizeTalents($player->getTalents()), |
29
|
1 |
|
'inventory' => self::normalizeInventory($player->getInventory()), |
30
|
1 |
|
'weapon' => $player->getWeapon() !== null ? self::normalizeWeapon($player->getWeapon()) : null, |
31
|
1 |
|
'corruption' => $player->getCorruption()?->get(), |
32
|
|
|
'levelProgress' => [ |
33
|
1 |
|
'level' => $player->getLevelProgress()->getLevel()->get(), |
34
|
1 |
|
'currentExperience' => $player->getLevelProgress()->getCurrentExperience()->get(), |
35
|
|
|
], |
36
|
1 |
|
'maximumHealth' => $player->getMaximumHealth()->get(), |
37
|
1 |
|
'attributePoints' => $player->getAttributePoints()->get(), |
38
|
1 |
|
'talentPoints' => $player->getTalentPoints()->get(), |
39
|
1 |
|
'checkpoint' => ['className' => get_class($player->getCheckpoint())], |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
private static function normalizeTalents(TalentCollection $talentCollection): array |
44
|
|
|
{ |
45
|
1 |
|
return map( |
46
|
1 |
|
static function(Talent $talent): array { |
47
|
1 |
|
$data = ['className' => get_class($talent)]; |
48
|
|
|
|
49
|
|
|
return match (true) { |
50
|
1 |
|
$talent instanceof SecretKnowledge => array_merge($data, [ |
51
|
1 |
|
'ascension' => $talent->getAscension()->get(), |
|
|
|
|
52
|
|
|
]), |
53
|
1 |
|
$talent instanceof WeaponMastery => array_merge($data, [ |
54
|
1 |
|
'type' => (string) $talent->getType(), |
|
|
|
|
55
|
1 |
|
'level' => $talent->getLevel()->get(), |
|
|
|
|
56
|
|
|
]), |
57
|
1 |
|
default => $data, |
58
|
|
|
}; |
59
|
1 |
|
}, |
60
|
1 |
|
$talentCollection, |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
private static function normalizeWeapon(Weapon $weapon): array |
65
|
|
|
{ |
66
|
1 |
|
$data = ['className' => get_class($weapon)]; |
67
|
|
|
|
68
|
|
|
return match (true) { |
69
|
1 |
|
$weapon instanceof RebredirWeapon => array_merge($data, [ |
70
|
1 |
|
'etherumLoad' => $weapon->getEtherumLoad()->get(), |
|
|
|
|
71
|
|
|
]), |
72
|
1 |
|
default => $data, |
73
|
|
|
}; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
private static function normalizeInventory(Inventory $inventory): array |
77
|
|
|
{ |
78
|
1 |
|
return map( |
79
|
1 |
|
static function(InventoryItem $inventoryItem): array { |
80
|
|
|
return match (true) { |
81
|
1 |
|
$inventoryItem instanceof Weapon => self::normalizeWeapon($inventoryItem), |
82
|
1 |
|
default => ['className' => get_class($inventoryItem)], |
83
|
|
|
}; |
84
|
1 |
|
}, |
85
|
1 |
|
$inventory, |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|