1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property integer $uid |
4
|
|
|
* @property integer $inClub |
5
|
|
|
* @property array $items |
6
|
|
|
* @property string $boardType |
7
|
|
|
* @property string $range |
8
|
|
|
* @property string $title |
9
|
|
|
* @property string $playerRankDescription |
10
|
|
|
* @property string $clubRankDescription |
11
|
|
|
*/ |
12
|
|
|
class Leaderboard extends CModel |
13
|
|
|
{ |
14
|
|
|
const BOARD_RANGE = 5; |
15
|
|
|
const TYPE_PLAYER = 'board_p'; |
16
|
|
|
const TYPE_CLUB = 'board_c'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
const RANGE_ACTUAL = 'actual'; |
|
|
|
|
19
|
|
|
const RANGE_PREVIOUS = 'prev'; |
20
|
|
|
const RANGE_LAST_SIX = 'last'; |
21
|
|
|
|
22
|
|
|
private $uid; |
23
|
|
|
private $inClub; |
24
|
|
|
private $items = []; |
25
|
|
|
private $key = ''; |
|
|
|
|
26
|
|
|
private $boardType; |
27
|
|
|
private $range; |
28
|
|
|
|
29
|
|
|
public function attributeNames() |
30
|
|
|
{ |
31
|
|
|
return []; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getUid() |
35
|
|
|
{ |
36
|
|
|
return $this->uid; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getInClub() |
40
|
|
|
{ |
41
|
|
|
return $this->inClub; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getItems() |
45
|
|
|
{ |
46
|
|
|
return $this->items; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getBoardType() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return $this->boardType; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getRange() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return $this->range; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getTitle() |
60
|
|
|
{ |
61
|
|
|
$titles = [ |
62
|
|
|
self::TYPE_PLAYER => [ |
63
|
|
|
self::RANGE_ACTUAL => 'Aktuális hónap legjobb játékosai', |
64
|
|
|
self::RANGE_PREVIOUS => 'Előző hónap legjobb játékosai', |
65
|
|
|
self::RANGE_LAST_SIX => 'Utolsó 6 hónap legjobb játékosai', |
66
|
|
|
], |
67
|
|
|
self::TYPE_CLUB => [ |
68
|
|
|
self::RANGE_ACTUAL => 'Aktuális hónap legjobb klubjai', |
69
|
|
|
self::RANGE_PREVIOUS => 'Előző hónap legjobb klubjai', |
70
|
|
|
self::RANGE_LAST_SIX => 'Utolsó 6 hónap legjobb klubjai', |
71
|
|
|
] |
72
|
|
|
]; |
73
|
|
|
return $titles[$this->boardType][$this->range]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function getPlayerRankDescription() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$redis = Yii::app()->redis->getClient(); |
79
|
|
|
|
80
|
|
|
$total = $redis->zCard($this->key); |
81
|
|
|
$rank = $redis->zRevRank($this->key, $this->uid) + 1; |
82
|
|
|
|
83
|
|
|
if ($rank == 1) { |
84
|
|
|
return 'Te vagy a király!'; |
85
|
|
|
} else { |
86
|
|
|
$percent = round((1 - ($rank / $total)) * 100, 1); |
87
|
|
|
if (!$percent) { |
88
|
|
|
return 'Több párbajgyőzelemre van szükséged.'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return 'Jobb vagy, mint a játékosok ' . $percent . '%-a!'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function getClubRankDescription() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$redis = Yii::app()->redis->getClient(); |
98
|
|
|
|
99
|
|
|
$total = $redis->zCard($this->key); |
100
|
|
|
$rank = $redis->zRevRank($this->key, $this->inClub) + 1; |
101
|
|
|
|
102
|
|
|
if ($rank == 1) { |
103
|
|
|
return 'A te klubod a király!'; |
104
|
|
|
} else { |
105
|
|
|
$percent = round((1 - ($rank / $total)) * 100, 1); |
106
|
|
|
if (!$percent) { |
107
|
|
|
return 'Több versenygyőzelemre van szükségetek.'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return 'Jobb a klubod, mint a többi klub ' . $percent . '%-a!'; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setBoardType ($type) |
115
|
|
|
{ |
116
|
|
|
$this->boardType = $type; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function setRange ($range) |
120
|
|
|
{ |
121
|
|
|
$d = new DateTime(); |
122
|
|
|
|
123
|
|
|
//create key from date/intersect |
124
|
|
|
switch ($range) { |
125
|
|
|
case self::RANGE_LAST_SIX: |
126
|
|
|
$this->key = $this->boardType . ':6month'; |
127
|
|
|
//create intersect |
128
|
|
|
|
129
|
|
|
$history = []; |
|
|
|
|
130
|
|
|
$history[] = $this->boardType . ':' . $d->format('Ym'); |
131
|
|
|
|
132
|
|
|
for ($i=0; $i<6; $i++) { |
133
|
|
|
$d->modify('first day of previous month'); |
134
|
|
|
$history[] = $this->boardType . ':' . $d->format('Ym'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$redis = Yii::app()->redis->getClient(); |
138
|
|
|
$redis->zUnionStore($this->key, $history); |
139
|
|
|
|
140
|
|
|
break; |
141
|
|
View Code Duplication |
case self::RANGE_PREVIOUS: |
|
|
|
|
142
|
|
|
$d->modify('first day of previous month'); |
143
|
|
|
$this->key = $this->boardType . ':' . $d->format('Ym'); |
144
|
|
|
break; |
145
|
|
View Code Duplication |
default: |
|
|
|
|
146
|
|
|
$range = self::RANGE_ACTUAL; |
|
|
|
|
147
|
|
|
$this->key = $this->boardType . ':' . $d->format('Ym'); |
148
|
|
|
} |
149
|
|
|
$this->range = $range; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function setUid($uid) |
153
|
|
|
{ |
154
|
|
|
$this->uid = (int)$uid; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function setInClub($id) |
158
|
|
|
{ |
159
|
|
|
$this->inClub = (int)$id; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function fetchList() |
163
|
|
|
{ |
164
|
|
|
if ($this->boardType == self::TYPE_PLAYER) { |
165
|
|
|
$class = 'Player'; |
166
|
|
|
} else { |
167
|
|
|
$class = 'Club'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$redis = Yii::app()->redis->getClient(); |
171
|
|
|
|
172
|
|
|
$myRank = $redis->zRevRank($this->key, $this->inClub); |
173
|
|
|
|
174
|
|
|
$range = self::BOARD_RANGE; |
175
|
|
|
$min = $myRank - $range > 0 ? $myRank - $range : 0; |
|
|
|
|
176
|
|
|
$max = $myRank + $range + ($range-$myRank+$min); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$item = new $class; |
179
|
|
|
$i = $min+1; |
|
|
|
|
180
|
|
View Code Duplication |
foreach ($redis->zRevRange($this->key, $min, $max, true) as $id => $score) { |
|
|
|
|
181
|
|
|
$item->subjectId = $id; |
182
|
|
|
|
183
|
|
|
$this->items[$i] = [ |
184
|
|
|
'id'=>$id, |
185
|
|
|
'name'=>$item->getSubjectName(), |
186
|
|
|
'score'=>$score, |
187
|
|
|
]; |
188
|
|
|
$i++; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getRankDescription() |
193
|
|
|
{ |
194
|
|
|
if ($this->boardType == self::TYPE_CLUB) { |
195
|
|
|
$ret = $this->getClubRankDescription(); |
196
|
|
|
} else { |
197
|
|
|
$ret = $this->getPlayerRankDescription(); |
198
|
|
|
} |
199
|
|
|
return $ret; |
200
|
|
|
} |
201
|
|
|
} |
|
|
|
|
202
|
|
|
|
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
will produce issues in the first and second line, while this second example
will produce no issues.