Contest::getCollect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * @property integer $activeId
4
 * @property string $recommendedCollect
5
 * @property integer $recommendedPrize
6
 * @property string $collect
7
 */
8
class Contest extends CModel
9
{
10
    const LIFETIME = 172800; //2 days
11
    const PRIZE = 500;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
12
13
    const ID_ACTIVE = 'contest:active';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 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...
14
    const ID_LIST = 'contest:list:';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 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...
15
    const ID_RECOMMENDED_COLLECT = 'contest:r_collect';
16
    const ID_RECOMMENDED_PRIZE = 'contest:r_prize';
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...
17
18
    const ACT_MISSION = 1;
19
    const ACT_DUEL = 2;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
20
    const ACT_CLUB = 3;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
21
22
    private $activeId;
23
    private $recommendedCollect;
24
    private $recommendedPrize;
25
    private $collect;
26
    private $collectParam;
27
    private $collectTypes = [
28
        'xp','xp_duel','xp_mission',
29
        'dollar','dollar_duel','dollar_mission'
30
        ];
31
32
    public function attributeNames()
33
    {
34
        return [];
35
    }
36
37
    public function getCollect()
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...
38
    {
39
        if (!$this->collect) {
40
            $this->collect = Yii::app()->redis->getClient()->get(self::ID_LIST . $this->activeId . ':collect');
41
        }
42
        return $this->collect;
43
    }
44
45
    public function getActiveId()
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...
46
    {
47
        if (!$this->activeId) {
48
            $this->activeId = Yii::app()->redis->getClient()->get(self::ID_ACTIVE);
49
        }
50
        return $this->activeId;
51
    }
52
53
    public function getRecommendedCollect()
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...
54
    {
55 View Code Duplication
        if (!$this->recommendedCollect) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $this->recommendedCollect = Yii::app()->redis->getClient()->get(self::ID_RECOMMENDED_COLLECT);
57
            Yii::app()->redis->getClient()->del(self::ID_RECOMMENDED_COLLECT);
58
        }
59
60
        if (!in_array($this->recommendedCollect, $this->collectTypes)) {
61
            $randomId = array_rand($this->collectTypes);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 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...
62
            $this->recommendedCollect = $this->collectTypes[$randomId];
63
        }
64
65
        return $this->recommendedCollect;
66
    }
67
68
    public function getRecommendedPrize()
69
    {
70 View Code Duplication
        if (!$this->recommendedPrize) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            $this->recommendedPrize = Yii::app()->redis->getClient()->get(self::ID_RECOMMENDED_PRIZE);
72
            Yii::app()->redis->getClient()->del(self::ID_RECOMMENDED_PRIZE);
73
        }
74
75
        if (!$this->recommendedPrize) {
76
            $this->recommendedPrize = self::PRIZE;
77
        }
78
79
        return (int)$this->recommendedPrize;
80
    }
81
82
    public function create()
83
    {
84
        if ($this->activeId) {
85
            return false;
86
        }
87
88
        $redis = Yii::app()->redis->getClient();
89
90
        $this->activeId = time();
91
92
        $redis->set('contest:active', $this->activeId);
93
        $redis->lPush('contest:log', $this->activeId); //log of contests
94
95
        $redis->set(self::ID_LIST . $this->activeId.':collect', $this->getRecommendedCollect());
96
        $redis->set(self::ID_LIST . $this->activeId.':prize', $this->getRecommendedPrize());
97
        $redis->set(self::ID_LIST . $this->activeId.':created', date('Y.m.d. H:i:s', $this->getActiveId()));
98
        return true;
99
    }
100
101
    /**
102
     * @param integer $energy
103
     */
104
    public function addPoints($uid, $activity, $energy, $xp, $dollar)
105
    {
106
        //todo: log common collecting
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
107
108
        if (!$this->getActiveId()) {
109
            return false; //no active contest
110
        }
111
        if (!$this->validateContest($activity, $xp, $dollar)) {
112
            return false;
113
        }
114
115
        //collect
116
        $points = 0;
117
        if ($this->collectParam == 'xp') {
118
            $points = $xp;
119
        }
120
121
        if ($this->collectParam == 'dollar') {
122
            $points = $dollar;
123
        }
124
125
        Yii::app()->redis->getClient()->zIncrBy(self::ID_LIST . $this->getActiveId() . ':points', $points, $uid);
126
        return true;
127
    }
128
129
    public function complete()
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...
130
    {
131
        $redis = Yii::app()->redis->getClient();
132
133
        $completed = $redis->del(self::ID_ACTIVE);
134
        if ($completed) {
135
            $this->logWinners();
136
            $this->activeId = 0;
137
        }
138
139
        return $completed;
140
    }
141
142
    private function logWinners()
143
    {
144
        $winners = $this->fetchWinners();
145
        if (!$winners) {
146
            return false;
147
        }
148
149
        $redis = Yii::app()->redis->getClient();
150
        $b = Yii::app()->badge->model;
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...
151
        foreach ($winners as $winner) {
152
            //add badges
153
            $b->uid = $winner;
154
            $b->triggerSimple('win_contest');
155
156
            //add winner to the log
157
            $redis->sadd(self::ID_LIST . $this->activeId . ':winners', $winner);
158
        }
159
    }
160
161
    private function fetchWinners()
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...
162
    {
163
        $redis = Yii::app()->redis->getClient();
164
        //get max point
165
        $key = self::ID_LIST . $this->activeId . ':points';
166
        $max = $redis->zRevRange($key, 0, 0, true);
167
168
        if (count($max)) {
169
            $maxScore = array_values($max)[0];
170
            $winners = $redis->zRevRangeByScore($key, $maxScore, $maxScore);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
171
            return $winners;
172
        }
173
        return false;
174
    }
175
176
    public function validateContest($activity, $xp, $dollar)
177
    {
178
        $toCollect = $this->getCollect();
179
180
        //check the lifetime
181
        if (time() > $this->activeId + self::LIFETIME) {
182
            return false;
183
        }
184
185
        $valid = true;
186
        //check activity+toCollect
187
        switch ($toCollect) {
188
            case 'xp':
189
                if (!$xp) {
190
                    $valid = false;
191
                }
192
                $this->collectParam = 'xp';
193
                break;
194
            case 'xp_duel':
195
                if (!$xp || $activity != self::ACT_DUEL) {
196
                    $valid = false;
197
                }
198
                $this->collectParam = 'xp';
199
                break;
200
            case 'xp_mission':
201
                if (!$xp || $activity != self::ACT_MISSION) {
202
                    $valid = false;
203
                }
204
                $this->collectParam = 'xp';
205
                break;
206
            case 'dollar':
207
                if (!$dollar) {
208
                    $valid = false;
209
                }
210
                $this->collectParam = 'dollar';
211
                break;
212
            case 'dollar_duel':
213
                if (!$dollar || $activity != self::ACT_DUEL) {
214
                    $valid = false;
215
                }
216
                $this->collectParam = 'dollar';
217
                break;
218
            case 'dollar_mission':
219
                if (!$dollar || $activity != self::ACT_MISSION) {
220
                    $valid = false;
221
                }
222
                $this->collectParam = 'dollar';
223
                break;
224
        }
225
226
        return $valid;
227
    }
228
}
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...
229