|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Gorlum 01.10.2017 15:07 |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Meta\Economic; |
|
7
|
|
|
|
|
8
|
|
|
use Common\GlobalContainer; |
|
9
|
|
|
|
|
10
|
|
|
class EconomicHelper { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var \classConfig $config |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $config; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array[] $resourceExchangeRates |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $resourceExchangeRates = []; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* EconomicHelper constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param GlobalContainer $gc |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(GlobalContainer $gc) { |
|
28
|
|
|
// if ($gc instanceof \classConfig) { |
|
29
|
|
|
// $this->config = $gc; |
|
30
|
|
|
// } elseif ($gc instanceof GlobalContainer) { |
|
31
|
|
|
// $this->config = $gc->config; |
|
32
|
|
|
// } else { |
|
33
|
|
|
// $this->config = \classSupernova::$config; |
|
34
|
|
|
// } |
|
35
|
|
|
|
|
36
|
|
|
$this->config = $gc->config; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Resets internal exchange cache |
|
41
|
|
|
*/ |
|
42
|
|
|
public function resetResourcesExchange() { |
|
43
|
|
|
$this->resourceExchangeRates = []; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets current exchange rates |
|
48
|
|
|
* |
|
49
|
|
|
* @return float[] - [int resourceId] -> [float configuredExchangeRate] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getResourcesExchange() { |
|
52
|
|
|
if (empty($this->resourceExchangeRates[UNIT_ANY])) { |
|
53
|
|
|
$this->resourceExchangeRates[UNIT_ANY] = array( |
|
54
|
|
|
RES_METAL => 'rpg_exchange_metal', |
|
55
|
|
|
RES_CRYSTAL => 'rpg_exchange_crystal', |
|
56
|
|
|
RES_DEUTERIUM => 'rpg_exchange_deuterium', |
|
57
|
|
|
RES_DARK_MATTER => 'rpg_exchange_darkMatter', |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->resourceExchangeRates[UNIT_ANY] as &$rate) { |
|
61
|
|
|
($rate = floatval($this->config->$rate)) <= 0 ? $rate = 1 : false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->resourceExchangeRates[UNIT_ANY]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get cost of all resources normalized to selected one |
|
70
|
|
|
* |
|
71
|
|
|
* Say, get cost of all resources in metal - for main calculations for comparing units between each other |
|
72
|
|
|
* |
|
73
|
|
|
* @param int $resourceId |
|
74
|
|
|
* |
|
75
|
|
|
* @return float[] - [int resourceId] -> [float ratesNormalizedToResourceCost] |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getResourceExchangeIn($resourceId) { |
|
78
|
|
|
if(empty($this->resourceExchangeRates[$resourceId])) { |
|
79
|
|
|
$defaultRates = $this->getResourcesExchange(); |
|
80
|
|
|
|
|
81
|
|
|
$this->resourceExchangeRates[$resourceId] = []; |
|
82
|
|
|
foreach($defaultRates as $defaultResourceId => $defaultRate) { |
|
83
|
|
|
$this->resourceExchangeRates[$resourceId][$defaultResourceId] = $defaultRate / $defaultRates[$resourceId]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->resourceExchangeRates[$resourceId]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.