|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Adventure\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use App\Adventure\Location; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
class Connector |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Connects two locations in a specified direction. |
|
12
|
|
|
* |
|
13
|
|
|
* @param Location $firstLocation The first location. |
|
14
|
|
|
* @param Location $secondLocation The location to connect. |
|
15
|
|
|
* @param string $direction The direction in which to connect the locations. |
|
16
|
|
|
* |
|
17
|
|
|
* @return Location The current location after the connection |
|
18
|
|
|
* @throws Exception If the direction is already connected to another location. |
|
19
|
|
|
*/ |
|
20
|
3 |
|
public static function connectLocations(Location $firstLocation, Location $secondLocation, string $direction): Location |
|
21
|
|
|
{ |
|
22
|
|
|
// Check if the direction is already used |
|
23
|
3 |
|
if ($firstLocation->hasConnection($direction)) { |
|
24
|
1 |
|
throw new Exception("The direction '{$direction}' is already connected to another location."); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// Connect the locations |
|
28
|
2 |
|
$firstLocation->connectTo($secondLocation, $direction); |
|
29
|
2 |
|
$secondLocation->connectTo($firstLocation, self::getOppositeDirection($direction)); |
|
30
|
|
|
|
|
31
|
2 |
|
return $firstLocation; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Swaps one location for another and connects the new location to all locations previously connected to the old location. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Location $oldLocation The location to be replaced. |
|
38
|
|
|
* @param Location $newLocation The new location to replace the old location. |
|
39
|
|
|
* |
|
40
|
|
|
* @return Location The new location. |
|
41
|
|
|
* @throws Exception If there is an error in swapping the locations. |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public static function swapLocations(Location $oldLocation, Location $newLocation): Location |
|
44
|
|
|
{ |
|
45
|
|
|
// Get the connections of the old location |
|
46
|
2 |
|
$connections = $oldLocation->getConnectedLocations(); |
|
47
|
|
|
|
|
48
|
|
|
// Remove the old location from its connections |
|
49
|
2 |
|
foreach ($connections as $location) { |
|
50
|
1 |
|
$location->disconnectFrom($oldLocation); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Add the new location to the connections |
|
54
|
2 |
|
foreach ($connections as $direction => $location) { |
|
55
|
1 |
|
self::connectLocations($newLocation, $location, $direction); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
return $newLocation; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the opposite direction for the given direction. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $direction The direction for which to find the opposite direction. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string The opposite direction. |
|
67
|
|
|
* @throws Exception If no opposite direction is found for the given direction. |
|
68
|
|
|
*/ |
|
69
|
2 |
|
private static function getOppositeDirection(string $direction): string |
|
70
|
|
|
{ |
|
71
|
2 |
|
$oppositeDirections = [ |
|
72
|
2 |
|
'north' => 'south', |
|
73
|
2 |
|
'south' => 'north', |
|
74
|
2 |
|
'east' => 'west', |
|
75
|
2 |
|
'west' => 'east', |
|
76
|
2 |
|
]; |
|
77
|
|
|
|
|
78
|
|
|
// Check if the opposite direction exists |
|
79
|
2 |
|
if (isset($oppositeDirections[$direction])) { |
|
80
|
2 |
|
return $oppositeDirections[$direction]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
throw new Exception("No opposite direction found for '{$direction}'."); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|