1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Smr\Routes; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use InfiniteIterator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Cyclically iterate over actions on a trade route |
10
|
|
|
*/ |
11
|
|
|
class RouteIterator { |
12
|
|
|
|
13
|
|
|
private InfiniteIterator $routeIterator; |
14
|
|
|
private string $transaction = TRADER_BUYS; |
15
|
|
|
|
16
|
|
|
public function __construct( |
17
|
|
|
private MultiplePortRoute $route |
18
|
|
|
) { |
19
|
|
|
$oneWayRoutes = $route->getOneWayRoutes(); |
20
|
|
|
$this->routeIterator = new InfiniteIterator(new ArrayIterator($oneWayRoutes)); |
21
|
|
|
|
22
|
|
|
// PHP bug prevents IteratorIterator cache from initializing properly. |
23
|
|
|
// Just rewind to force it to populate its cache. |
24
|
|
|
$this->routeIterator->rewind(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getEntireRoute(): MultiplePortRoute { |
28
|
|
|
return $this->route; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getCurrentRoute(): OneWayRoute { |
32
|
|
|
return $this->routeIterator->current(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getCurrentTransaction(): string { |
36
|
|
|
return $this->transaction; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getCurrentSectorID(): int { |
40
|
|
|
return match ($this->transaction) { |
41
|
|
|
TRADER_BUYS => $this->getCurrentRoute()->getBuySectorId(), |
42
|
|
|
TRADER_SELLS => $this->getCurrentRoute()->getSellSectorId(), |
43
|
|
|
}; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Advance to the next action on the route |
48
|
|
|
*/ |
49
|
|
|
public function next(): void { |
50
|
|
|
if ($this->transaction == TRADER_SELLS) { |
51
|
|
|
$this->routeIterator->next(); |
52
|
|
|
} |
53
|
|
|
$this->transaction = match ($this->transaction) { |
54
|
|
|
TRADER_SELLS => TRADER_BUYS, |
55
|
|
|
TRADER_BUYS => TRADER_SELLS, |
56
|
|
|
}; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|