DuelShield::getPrices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * @property integer $caller
4
 * @property integer $opponent
5
 * @property boolean $isChallenge
6
 * @property boolean $played
7
 * @property array $competitors
8
 */
9
class DuelShield extends CModel
10
{
11
    private $uid;
12
    private $last = 0;
13
    
14
    public function attributeNames()
15
    {
16
        return [];
17
    }
18
19
    public function setUid($uid)
20
    {
21
        $this->uid = (int)$uid;
22
        $this->fetch();
23
    }
24
25
    /**
26
     * Get the time, until the shield is active
27
     */
28
    public function getLifetime()
29
    {
30
        return $this->last - time();
31
    }
32
33
    protected function fetch()
34
    {
35
        $last = Yii::app()->db->createCommand()
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
36
            ->select('until')
37
            ->from('duel_shield')
38
            ->where('uid = :uid', [':uid'=>$this->uid])
39
            ->order('until DESC')
40
            ->limit(1)
41
            ->queryScalar();
42
        $this->last = strtotime($last);
43
    }
44
45
    public function getPrices()
46
    {
47
        // 10min/gold
48
        return [
49
            15 => ['price'=>2, 'label'=>'15 perc'], //interval => price
50
            30 => ['price'=>3, 'label'=>'30 perc'],
51
            60 => ['price'=>6, 'label'=>'1 óra'],
52
            120 => ['price'=>12, 'label'=>'2 óra'],
53
            180 => ['price'=>18, 'label'=>'3 óra'],
54
        ];
55
    }
56
57
    public function activate($time)
58
    {
59
        $player = Yii::app()->player->model;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
60
        $logger = new Logger;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
61
        $logger->key = 'shield:'.date('Y-m-d').':'.$this->uid;
0 ignored issues
show
Bug introduced by
The property key cannot be accessed from this context as it is declared private in class Logger.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
62
        $logger->addToSet('----start: '.date('H:i:s').'----');
63
        $logger->addToSet('gold:'.$player->gold.', time: ' . $time);
64
65
        $prices = $this->getPrices();
66
        if (!array_key_exists($time, $prices)) {
67
            throw new CFlashException('A pajzsot csak a listában megadott időkre aktiválhatod.');
68
        }
69
        $price = $prices[$time]['price'];
70
71
        $logger->addToSet('price found: ' . $price);
72
        
73
        if ($player->gold < $price) {
74
            throw new CFlashException('Nincs elég aranyad a kiválasztott pajzs aktiválásához.');
75
        }
76
        $logger->addToSet('gold > price');
77
78
        if ($this->getLifetime() > 0) {
79
            throw new CFlashException('A korábban aktivált pajzsod még érvényes.');
80
        }
81
        $logger->addToSet('lifeTime = 0, can activate new');
82
83
        $player->updateAttributes([], ['gold'=>$price]);
84
        $logger->addToSet('gold decreased');
85
        
86
        Yii::app()->db->createCommand()->insert('duel_shield', [
87
            'uid'   =>  $this->uid,
88
            'until' =>  date('Y-m-d H:i:s', time()+($time*60)),
89
        ]);
90
        $logger->addToSet('shield activated, time:' . $time . ', price:' . $price);
91
        return true;
92
    }
93
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
94