1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @property array $opponents |
4
|
|
|
* @property CPagination $pagination |
5
|
|
|
* @property integer $count |
6
|
|
|
* @property string $clubName |
7
|
|
|
* @property integer $page |
8
|
|
|
*/ |
9
|
|
|
class DuelList extends CModel |
10
|
|
|
{ |
11
|
|
|
private $opponents = []; |
12
|
|
|
private $pagination; |
13
|
|
|
private $count; |
14
|
|
|
private $page = 0; |
15
|
|
|
|
16
|
|
|
public function attributeNames() |
17
|
|
|
{ |
18
|
|
|
return []; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getOpponents() |
22
|
|
|
{ |
23
|
|
|
return $this->opponents; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getPagination() |
27
|
|
|
{ |
28
|
|
|
return $this->pagination; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getCount() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return $this->count; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Club $club |
38
|
|
|
*/ |
39
|
|
|
public function getClubName($club, $id) |
40
|
|
|
{ |
41
|
|
|
if ($id) { |
42
|
|
|
$club->id = $id; |
|
|
|
|
43
|
|
|
$club->fetchName(); |
44
|
|
|
return '<span> | ' . $club->name . '</span>'; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
return ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setPage($page) |
50
|
|
|
{ |
51
|
|
|
$this->page = $page; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function fetchOpponents() |
55
|
|
|
{ |
56
|
|
|
$player = Yii::app()->player->model; |
|
|
|
|
57
|
|
|
$limit = Yii::app()->params['listPerPage']; |
|
|
|
|
58
|
|
|
$minLevel = $player->level - Yii::app()->params['duelWeakerOpponentLevelDiff']; |
59
|
|
|
if ($minLevel < Yii::app()->params['duelLevelRequirement']) { |
60
|
|
|
$minLevel = Yii::app()->params['duelLevelRequirement']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->count = Yii::app()->db->createCommand() |
64
|
|
|
->select('COUNT(*) AS count') |
65
|
|
|
->from('main') |
66
|
|
|
->where('uid <> :uid AND level >= :minLevel', [':uid'=>$player->uid, ':minLevel'=>$minLevel]) |
67
|
|
|
->queryScalar(); |
68
|
|
|
|
69
|
|
|
$res = Yii::app()->db->createCommand() |
70
|
|
|
->select('uid, user, level, energy_max, energy, dollar, in_club') |
71
|
|
|
->from('main') |
72
|
|
|
->where('uid <> :uid AND level >= :minLevel', [':uid'=>$player->uid, ':minLevel'=>$minLevel]) |
73
|
|
|
->order('level ASC') |
74
|
|
|
->limit($limit, ($this->page * $limit) - $limit) // the trick is here! |
75
|
|
|
->queryAll(); |
76
|
|
|
|
77
|
|
|
$this->pagination = new CPagination($this->count); |
78
|
|
|
$this->pagination->setPageSize(Yii::app()->params['listPerPage']); |
79
|
|
|
|
80
|
|
|
$club = new Club(); |
|
|
|
|
81
|
|
|
$duelShiel = new DuelShield(); |
82
|
|
|
|
83
|
|
|
foreach ($res as $item) { |
84
|
|
|
$duelShiel->uid = $item['uid']; |
|
|
|
|
85
|
|
|
if ($duelShiel->lifeTime > 0) { |
86
|
|
|
$item['energy'] = 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$item['disabled'] = 0; |
90
|
|
|
if ($item['energy'] <= $item['energy_max'] / 10) { |
91
|
|
|
$item['disabled'] = 1; //low energy |
92
|
|
|
} |
93
|
|
|
$item['prize'] = round($item['dollar'] / 10); |
|
|
|
|
94
|
|
|
$item['clubName'] = $this->getClubName($club, $item['in_club']); |
95
|
|
|
|
96
|
|
|
$this->opponents[$item['uid']] = $item; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function fetchCommonRivals() |
101
|
|
|
{ |
102
|
|
|
$player = Yii::app()->player->model; |
103
|
|
|
$limit = Yii::app()->params['listPerPage']; |
|
|
|
|
104
|
|
|
|
105
|
|
|
$res = Yii::app()->db->createCommand() |
|
|
|
|
106
|
|
|
->select('uid, COUNT(*) AS cnt') |
107
|
|
|
->from('duel_player') |
108
|
|
|
->where('uid <> :uid AND duel_id IN (SELECT duel_id FROM duel_player WHERE uid=:uid)', [':uid'=>$player->uid]) |
109
|
|
|
->group('uid') |
110
|
|
|
->order('cnt DESC') |
111
|
|
|
->limit($limit) |
112
|
|
|
->queryAll(); |
113
|
|
|
$club = new Club(); |
|
|
|
|
114
|
|
|
$duelShiel = new DuelShield(); |
115
|
|
|
|
116
|
|
|
foreach ($res as $item) { |
117
|
|
|
$p = Yii::app()->db->createCommand() |
118
|
|
|
->select('user, energy, energy_max, dollar, level, in_club') |
119
|
|
|
->from('main') |
120
|
|
|
->where('uid=:uid', [':uid'=>(int)$item['uid']]) |
121
|
|
|
->queryRow(); |
122
|
|
|
if (!is_array($p)) { |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($p as $k => $v) { |
127
|
|
|
$item[$k] = $v; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$duelShiel->uid = $item['uid']; |
|
|
|
|
131
|
|
|
if ($duelShiel->lifeTime > 0) { |
132
|
|
|
$item['energy'] = 0; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$item['disabled'] = 0; |
136
|
|
|
if ($item['energy'] <= $item['energy_max'] / 10) { |
137
|
|
|
$item['disabled'] = 1; //low energy |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($player->level - $item['level'] > Yii::app()->params['duelWeakerOpponentLevelDiff']) { |
141
|
|
|
$item['disabled'] = 2; //too weak |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$item['prize'] = round($item['dollar'] / 10); |
|
|
|
|
145
|
|
|
$item['clubName'] = $this->getClubName($club, $item['in_club']); |
|
|
|
|
146
|
|
|
$this->opponents[$item['uid']] = $item; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function fetchLastRivals() |
151
|
|
|
{ |
152
|
|
|
$player = Yii::app()->player->model; |
153
|
|
|
$limit = Yii::app()->params['listPerPage']; |
|
|
|
|
154
|
|
|
|
155
|
|
|
$res = Yii::app()->db->createCommand() |
156
|
|
|
->select('id, caller, created') |
157
|
|
|
->from('duel') |
158
|
|
|
->where('opponent=:uid', [':uid'=>$player->uid]) |
159
|
|
|
->order('id DESC') |
160
|
|
|
->limit($limit) |
161
|
|
|
->queryAll(); |
162
|
|
|
|
163
|
|
|
$club = new Club(); |
|
|
|
|
164
|
|
|
$duelShiel = new DuelShield(); |
165
|
|
|
$cache = []; |
|
|
|
|
166
|
|
|
foreach ($res as $item) { |
167
|
|
|
$item['uid'] = $item['caller']; |
168
|
|
|
|
169
|
|
|
if (!isset($cache[$item['uid']])) { |
170
|
|
|
$p = Yii::app()->db->createCommand() |
171
|
|
|
->select('user, energy, energy_max, dollar, level, in_club') |
172
|
|
|
->from('main') |
173
|
|
|
->where('uid=:uid', [':uid'=>(int)$item['uid']]) |
174
|
|
|
->queryRow(); |
175
|
|
|
if (!is_array($p)) { |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$cache[$item['uid']] = $p; |
180
|
|
|
} else { |
181
|
|
|
$p = $cache[$item['uid']]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
foreach ($p as $k => $v) { |
185
|
|
|
$item[$k] = $v; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$duelShiel->uid = $item['uid']; |
|
|
|
|
189
|
|
|
if ($duelShiel->lifeTime > 0) { |
190
|
|
|
$item['energy'] = 0; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$item['disabled'] = 0; |
194
|
|
|
if ($item['energy'] <= $item['energy_max'] / 10) { |
195
|
|
|
$item['disabled'] = 1; //low energy |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$item['prize'] = round($item['dollar'] / 10); |
|
|
|
|
199
|
|
|
$item['clubName'] = $this->getClubName($club, $item['in_club']); |
|
|
|
|
200
|
|
|
$this->opponents[] = $item; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
|
|
|
|
204
|
|
|
|
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.