Mine::refresh()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright (c) 2016 Francois-Xavier Soubirou.
5
 *
6
 * This file is part of eco4.
7
 *
8
 * eco4 is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * eco4 is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with eco4. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace AppBundle\Entity;
24
25
use DateTime;
26
use Doctrine\ORM\Mapping as ORM;
27
28
/**
29
 * Mine entity class.
30
 *
31
 * @category  Eco4 App
32
 *
33
 * @author    Francois-Xavier Soubirou <[email protected]>
34
 * @copyright 2016 Francois-Xavier Soubirou
35
 * @license   http://www.gnu.org/licenses/   GPLv3
36
 *
37
 * @link      https://www.eco4.io
38
 *
39
 * @ORM\Table(name="eco4_mine")
40
 * @ORM\Entity(repositoryClass="AppBundle\Repository\MineRepository")
41
 */
42
class Mine extends AbstractBuilding
43
{
44
    /**
45
     * @var float
46
     *
47
     * @ORM\Column(name="r1", type="float")
48
     */
49
    private $r1;
50
51
    /**
52
     * @var float
53
     *
54
     * @ORM\Column(name="r2", type="float")
55
     */
56
    private $r2;
57
58
    /**
59
     * @var float
60
     *
61
     * @ORM\Column(name="r3", type="float")
62
     */
63
    private $r3;
64
65
    /**
66
     * @var string
67
     *
68
     * @ORM\Column(name="factor", type="string")
69
     */
70
    private $factor;
71
72
    /**
73
     * @var MineFactor
74
     */
75
    private $mineFactor = null;
76
77
    /**
78
     * Set r1.
79
     *
80
     * @param float $r1
81
     *
82
     * @return Mine
83
     */
84
    public function setR1(float $r1): Mine
85
    {
86
        $this->r1 = $r1;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Get r1.
93
     *
94
     * @return float
95
     */
96
    public function getR1(): float
97
    {
98
        return $this->r1;
99
    }
100
101
    /**
102
     * Set r2.
103
     *
104
     * @param float $r2
105
     *
106
     * @return Mine
107
     */
108
    public function setR2(float $r2): Mine
109
    {
110
        $this->r2 = $r2;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Get r2.
117
     *
118
     * @return float
119
     */
120
    public function getR2(): float
121
    {
122
        return $this->r2;
123
    }
124
125
    /**
126
     * Set r3.
127
     *
128
     * @param float $r3
129
     *
130
     * @return Mine
131
     */
132
    public function setR3(float $r3): Mine
133
    {
134
        $this->r3 = $r3;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Get r3.
141
     *
142
     * @return float
143
     */
144
    public function getR3(): float
145
    {
146
        return $this->r3;
147
    }
148
149
    /**
150
     * Set factor.
151
     *
152
     * @param string $factor
153
     *
154
     * @return Mine
155
     */
156
    public function setFactor(string $factor): Mine
157
    {
158
        $this->factor = $factor;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Get factor.
165
     *
166
     * @return string
167
     */
168
    public function getfactor(): string
169
    {
170
        return $this->factor;
171
    }
172
173
    /**
174
     * Get mine factor.
175
     *
176
     * @return MineFactor
177
     */
178
    public function getMineFactor(): MineFactor
179
    {
180
        if (is_null($this->mineFactor)) {
181
            $this->mineFactor = new MineFactor($this->factor);
182
        }
183
184
        return $this->mineFactor;
185
    }
186
187
    /**
188
     * Get r1Factor.
189
     *
190
     * @return int
191
     */
192
    public function getR1Factor(): int
193
    {
194
        return $this->getMineFactor()->getR1Factor();
195
    }
196
197
    /**
198
     * Get r2Factor.
199
     *
200
     * @return int
201
     */
202
    public function getR2Factor(): int
203
    {
204
        return $this->getMineFactor()->getR2Factor();
205
    }
206
207
    /**
208
     * Get r3Factor.
209
     *
210
     * @return int
211
     */
212
    public function getR3Factor(): int
213
    {
214
        return $this->getMineFactor()->getR3Factor();
215
    }
216
217
    /**
218
     * Get production of r1 per hour.
219
     *
220
     * @return float
221
     */
222
    public function getProdR1PerHour(): float
223
    {
224
        return $this->getProdRxPerHour($this->getR1Factor());
225
    }
226
227
    /**
228
     * Get production of r2 per hour.
229
     *
230
     * @return float
231
     */
232
    public function getProdR2PerHour(): float
233
    {
234
        return $this->getProdRxPerHour($this->getR2Factor());
235
    }
236
237
    /**
238
     * Get production of r3 per hour.
239
     *
240
     * @return float
241
     */
242
    public function getProdR3PerHour(): float
243
    {
244
        return $this->getProdRxPerHour($this->getR3Factor());
245
    }
246
247
    /**
248
     * Get lastUpdate.
249
     *
250
     * @return DateTime
251
     */
252
    public function getType(): int
253
    {
254
        return ObjectType::MINE;
255
    }
256
257
    /**
258
     * Get the production index for a level.
259
     *
260
     * @return float
261
     */
262
    public function getProdLevelIndex(): float
263
    {
264
        return 5 * $this->getLevel() * 1.1 ^ $this->getLevel();
265
    }
266
267
    /**
268
     * Get the production of resource on a period.
269
     *
270
     * @param DateTime $from     Beginning of period
271
     * @param DateTime $to       End of period
272
     * @param int      $rxFactor Factor of production of resource
273
     *
274
     * @return float
275
     */
276
    public function getProdRxOnPeriod(DateTime $from, DateTime $to, int $rxFactor): float
277
    {
278
        $period = ($to->format('U') - $from->format('U')) / 3600;
279
280
        return $period * $this->getProdRxPerHour($rxFactor);
281
    }
282
283
    /**
284
     * Get the production of resource on a period.
285
     *
286
     * @param int $rxFactor Factor of production of resource
287
     *
288
     * @return float
289
     */
290
    public function getProdRxPerHour(int $rxFactor): float
291
    {
292
        return $this->getProdLevelIndex() * $rxFactor / 100;
293
    }
294
295
    /**
296
     * Update resources.
297
     *
298
     * @param DateTime $dateTime Date time
299
     */
300
    public function refresh(DateTime $dateTime)
301
    {
302
        $this->setR1($this->getR1() + $this->getProdRxOnPeriod($this->getLastUpdate(), $dateTime, $this->getR1Factor()));
303
        $this->setR2($this->getR2() + $this->getProdRxOnPeriod($this->getLastUpdate(), $dateTime, $this->getR2Factor()));
304
        $this->setR3($this->getR3() + $this->getProdRxOnPeriod($this->getLastUpdate(), $dateTime, $this->getR3Factor()));
305
306
        $this->setLastUpdate($dateTime);
307
    }
308
}
309