Store::refillEnergy()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 0
1
<?php
2
/**
3
 * @property integer $uid
4
 * @property array $blackBait
5
 * @property array $packagesSms
6
 */
7
class Store extends CModel
8
{
9
    private $uid;
10
    private $missingSetItemPrice = 100;
11
12
    public function attributeNames()
13
    {
14
        return [];
15
    }
16
17
    public function getUid()
18
    {
19
        return $this->uid;
20
    }
21
22
    public function getPackagesSms()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
23
    {
24
        return Yii::app()->params['packagesSms'];
25
    }
26
27
    public function getMissingSetItemPrice()
28
    {
29
        return $this->missingSetItemPrice;
30
    }
31
    public function setUid($uid)
32
    {
33
        $this->uid = (int)$uid;
34
    }
35
36
    public function refillEnergy()
37
    {
38
        $player = Yii::app()->player->model;
39
40
        $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...
41
        $logger->key = 'refillEnergy:'.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...
42
        $logger->addToSet('----start: '.date('H:i:s').'----');
43
        $logger->addToSet('gold:'.$player->gold.', energy: ' . $player->energy.'/'.$player->energy_max);
44
45
        if ($player->gold < 20) {
46
            throw new CFlashException('Nincs elég aranyad az energiaital kifizetésére.');
47
        }
48
49
        $logger->addToSet('gold > 20');
50
        if ($player->energy_missing < 3) {
51
            throw new CFlashException('Kevesebb, mint 3 energiára van szükséged. Emiatt nem érdemes energiaitalt innod.');
52
        }
53
54
        $logger->addToSet('energy_missing > 3');
55
56
        $player->updateAttributes(['energy'=>$player->energy_missing], ['gold'=>20]);
57
        $logger->addToSet('energy increased, gold decreased');
58
59
        $logger->addToSet('---- end: '.date('H:i:s').'----');
60
61
        Yii::app()->badge->model->triggerSimple('energy_drink');
62
        return true;
63
    }
64
65
    public function listMissingSetItems()
66
    {
67
        $shop = new Shop;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
68
        $shop->item_type = Shop::TYPE_PART;
0 ignored issues
show
Bug introduced by
The property item_type cannot be accessed from this context as it is declared private in class Shop.

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...
69
        $shop->fetchSets();
70
        $sets = $shop->items;
0 ignored issues
show
Bug introduced by
The property items cannot be accessed from this context as it is declared private in class Shop.

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...
71
72
        $missing = [];
73
        foreach ($sets as $set) {
74
            foreach ($set['items'] as $item) {
75
                if (!$item->owned) {
76
                    $missing[] = $item;
77
                }
78
            }
79
        }
80
81
        return $missing;
82
    }
83
    public function buySetItem($id)
84
    {
85
        $player = Yii::app()->player->model;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
86
        $userName = $player->user;
87
88
        //check is we have the selected item in the missing list
89
        $missingList = $this->listMissingSetItems();
90
        $buyItem = null;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
91
        foreach ($missingList as $item) {
92
            if ($item['id'] == $id) {
93
                $buyItem = $item;
94
                break;
95
            }
96
        }
97
        //check it it is an existing item
98
        if ($buyItem == null) {
99
            throw new CFlashException('A keresett elem nem létezik.');
100
        }
101
        //check the amount of gold
102
        if ($player->gold < $this->missingSetItemPrice) {
103
            throw new CFlashException('Nincs elég aranyad a szett elem kifizetésére.');
104
        }
105
106
        //check is we bought one this week of this type
107
        $setItemType = strtok($buyItem['title'], ' ');
108
        $logKey = 'debug:buySetItem:' . date('Y:W') . ':' . strtolower($setItemType);
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...
109
110
        $hash = new ARedisHash($logKey);
111
        if ($hash->offsetExists($userName)) {
112
            throw new CFlashException($setItemType .' típusú szett elemet már vettél a héten. Jövő héten vásárolhatsz belőle újból.');
113
        }
114
115
        //buy the item
116
        $i = new Item;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
117
        $i->id = $buyItem['id'];
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class Item.

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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
118
        $i->item_type = Item::TYPE_PART;
0 ignored issues
show
Bug introduced by
The property item_type cannot be accessed from this context as it is declared private in class Item.

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...
119
        $i->fetch();
120
        $i->buy(1);
121
122
        //pay the price
123
        $player->updateAttributes([], ['gold'=>$this->missingSetItemPrice]);
124
125
        //log the purchase
126
        $hash->add($userName, date('H:i:s') . ', ' . $buyItem['title']);
127
128
        return true;
129
    }
130
}
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...
131