Stats::getTopPilots()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/* zKillboard
3
 * Copyright (C) 2012-2015 EVE-KILL Team and EVSCO.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
class Stats
20
{
21
22
	public static function getTopPilots($parameters = array(), $allTime = false)
23
	{
24
		$parameters["cacheTime"] = 3600;
25
		return self::getTop("characterID", $parameters, $allTime);
26
	}
27
28
	public static function getTopPointsPilot($parameters = array(), $allTime = false)
29
	{
30
		$parameters["cacheTime"] = 3600;
31
		return self::getTopPoints("characterID", $parameters, $allTime);
32
	}
33
34
	public static function getTopCorps($parameters = array(), $allTime = false)
35
	{
36
		$parameters["cacheTime"] = 3600;
37
		return self::getTop("corporationID", $parameters, $allTime);
38
	}
39
40
	public static function getTopPointsCorp($parameters = array(), $allTime = false)
41
	{
42
		$parameters["cacheTime"] = 3600;
43
		return self::getTopPoints("corporationID", $parameters, $allTime);
44
	}
45
46
	public static function getTopAllis($parameters = array(), $allTime = false)
47
	{
48
		$parameters["cacheTime"] = 3600;
49
		return self::getTop("allianceID", $parameters, $allTime);
50
	}
51
52
	public static function getTopFactions($parameters = array(), $allTime = false)
53
	{
54
		$parameters["cacheTime"] = 3600;
55
		return self::getTop("factionID", $parameters, $allTime);
56
	}
57
58
	public static function getTopPointsAlli($parameters = array(), $allTime = false)
59
	{
60
		$parameters["cacheTime"] = 3600;
61
		return self::getTopPoints("allianceID", $parameters, $allTime);
62
	}
63
64
	public static function getTopShips($parameters = array(), $allTime = false)
65
	{
66
		$parameters["cacheTime"] = 3600;
67
		return self::getTop("shipTypeID", $parameters, $allTime);
68
	}
69
70
	public static function getTopGroups($parameters = array(), $allTime = false)
71
	{
72
		$parameters["cacheTime"] = 3600;
73
		return self::getTop("groupID", $parameters, $allTime);
74
	}
75
76
	public static function getTopWeapons($parameters = array(), $allTime = false)
77
	{
78
		$parameters["cacheTime"] = 3600;
79
		return self::getTop("weaponTypeID", $parameters, $allTime);
80
	}
81
82
	public static function getTopSystems($parameters = array(), $allTime = false)
83
	{
84
		$parameters["cacheTime"] = 3600;
85
		return self::getTop("solarSystemID", $parameters, $allTime);
86
	}
87
88
	public static function getTopRegions($parameters = array(), $allTime = false)
89
	{
90
		$parameters["cacheTime"] = 3600;
91
		return self::getTop("regionID", $parameters, $allTime);
92
	}
93
94
	/**
95
	 * @param string $groupByColumn
96
	 */
97
	public static function getTopPoints($groupByColumn, $parameters = array(), $allTime = false)
98
	{
99
		$whereClauses = array();
100
		$tables = array();
101
		Filters::buildFilters($tables, $whereClauses, $whereClauses, $parameters, $allTime);
102
		$whereClauses[] = "characterID != 0";
103
104
		// Remove 0 values
105
		$whereClauses[] = "$groupByColumn != 0";
106
		if ($groupByColumn == "corporationID") $whereClauses[] = "$groupByColumn > 6000000";
107
108
		$limit = array_key_exists("limit", $parameters) ? (int)$parameters["limit"] : 10;
109
110
		$query = "select $groupByColumn, sum(kills) kills from (select killID gg, $groupByColumn, points kills from ";
111
		if (sizeof(array_unique($tables)) > 1) die("Multiple table joins not ready in Stats just yet");
0 ignored issues
show
Coding Style Compatibility introduced by
The method getTopPoints() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
112
		$query .= implode(",", array_unique($tables));
113
		if (sizeof($whereClauses) > 0) $query .= " where " . implode(" and ", $whereClauses);
114
115
		$npcFilter = $groupByColumn == "corporationID" ? "where $groupByColumn > 6000000" : "";
116
		$query .= " group by killID, $groupByColumn) as f $npcFilter group by $groupByColumn order by 2 desc limit $limit";
117
118
		$result = Db::query($query, array(), 3600);
119
		$data = array();
120
		foreach ($result as $row) $data[] = Info::addInfo($row);
121
		unset($result);
122
		return $data;
123
	}
124
125
	public static function getTopIsk($parameters = array(), $allTime = false)
0 ignored issues
show
Unused Code introduced by
The parameter $allTime is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
126
	{
127
		$parameters["orderBy"] = "p.total_price";
128
		if (!isset($parameters["limit"])) $parameters["limit"] = 5;
129
		return Kills::getKills($parameters);
130
	}
131
132
	private static $extendedGroupColumns = array("characterID"); //, "corporationID"); //, "allianceID");
0 ignored issues
show
Unused Code introduced by
The property $extendedGroupColumns is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
133
134
	/**
135
	 * @param string $groupByColumn
136
	 */
137
	private static function getTop($groupByColumn, $parameters = array(), $allTime = false)
138
	{
139
		$whereClauses = array();
140
		$tables = array();
141
		$tables[] = "zz_participants p";
142
		Filters::buildFilters($tables, $whereClauses, $whereClauses, $parameters, $allTime);
143
		$whereClauses[] = "characterID != 0";
144
145
		// Remove 0 values
146
		$whereClauses[] = "$groupByColumn != 0";
147
		if ($groupByColumn == "corporationID") $whereClauses[] = "$groupByColumn > 6000000";
148
149
		$limit = array_key_exists("limit", $parameters) ? (int)$parameters["limit"] : 10;
150
151
		$tablePrefixes = array();
152
		if (sizeof($tables) > 1) {
153
			foreach($tables as $table) $tablePrefixes[] = substr($table, strlen($table) - 1, 1) . ".killID";
154
		}
155
156
		$query = "select $groupByColumn, count(distinct p.killID) kills from ";
157
		$query .= implode(" left join ", array_unique($tables));
158
		if (sizeof($tables) > 1) $query .= " on (" . implode(" = ", $tablePrefixes) . ") ";
159
		if (sizeof($whereClauses) > 0) $query .= " where " . implode(" and ", $whereClauses);
160
161
		$query .= " group by 1 order by 2 desc limit $limit";
162
163
		$cacheTime = isset($parameters["cacheTime"]) ? (int)$parameters["cacheTime"] : 3600;
164
		if ($cacheTime < 30) $cacheTime = 30;
165
		$result = Db::query($query, array(), $cacheTime);
166
		$data = array();
167
		foreach ($result as $row) $data[] = Info::addInfo($row);
168
		unset($result);
169
		//if (sizeof($data) <= 1) return self::getExtendedTop($groupByColumn, $parameters, $allTime);
170
		return $data;
171
	}
172
173
	private static function getExtendedTop($groupByColumn, $parameters = array(), $allTime = false)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
174
	{
175
		$whereClauses = array();
176
		$tables = array();
177
		$tables[] = "zz_participants p";
178
		Filters::buildFilters($tables, $whereClauses, $whereClauses, $parameters, $allTime);
179
180
		// Remove 0 values
181
		$whereClauses[] = "p.$groupByColumn != 0";
182
		$whereClauses[] = "x.$groupByColumn != 0";
183
184
		$limit = array_key_exists("limit", $parameters) ? (int)$parameters["limit"] : 10;
185
186
		$query = "select x.$groupByColumn, count(distinct x.killID) kills from zz_participants x left join zz_participants p on (x.killID = p.killID)";
187
		$whereClauses[] = "x.killID = p.killID";
188
		$whereClauses[] = "x.isVictim = 'F'";
189
		$query .= " where " . implode(" and ", $whereClauses);
190
191
		$query .= " group by 1 order by 2 desc limit $limit";
192
193
		$result = Db::query($query, array(), 3600);
194
		$data = array();
195
		foreach ($result as $row) $data[] = Info::addInfo($row);
196
		unset($result);
197
		return $data;
198
	}
199
200
	public static function calcStats($killID, $adding = true)
201
	{
202
		$modifier = $adding ? 1 : -1;
203
204
		$victim = Db::queryRow("select * from zz_participants where isVictim != 0 and killID = :killID", array(":killID" => $killID));
205
		$chars = Db::query("select characterID, shipTypeID, groupID from zz_participants where isVictim = 0 and killID = :killID", array(":killID" => $killID));
206
		$corps = Db::query("select distinct corporationID from zz_participants where isVictim = 0 and killID = :killID", array(":killID" => $killID));
207
		$allis = Db::query("select distinct allianceID from zz_participants where isVictim = 0 and killID = :killID", array(":killID" => $killID));
208
		$factions = Db::query("select distinct factionID from zz_participants where isVictim = 0 and killID = :killID", array(":killID" => $killID));
209
210
		$groupID = isset($victim["groupID"]) ? $victim["groupID"] : 0;
211
		$points = isset($victim["points"]) ? $modifier * $victim["points"] : 0;
212
		$isk = isset($victim["total_price"]) ? $modifier * $victim["total_price"] : 0;
213
214
		if ($victim) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $victim of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
215
			self::statLost("pilot", $victim["characterID"], $groupID, $modifier, $points, $isk);
216
			self::statLost("corp", $victim["corporationID"], $groupID, $modifier, $points, $isk);
217
			self::statLost("alli", $victim["allianceID"], $groupID, $modifier, $points, $isk);
218
			self::statLost("faction", $victim["factionID"], $groupID, $modifier, $points, $isk);
219
			self::statLost("ship", $victim["shipTypeID"], $groupID, $modifier, $points, $isk);
220
			self::statLost("group", $victim["groupID"], $groupID, $modifier, $points, $isk);
221
			self::statLost("system", $victim["solarSystemID"], $groupID, $modifier, $points, $isk);
222
			self::statLost("region", $victim["regionID"], $groupID, $modifier, $points, $isk);
223
		}
224
225
		$shipTypes = array();
226
		$groups = array();
227
		foreach($chars as $char) {
228
			self::statDestroyed("pilot", $char["characterID"], $groupID, $modifier, $points, $isk);
229
			if (!in_array($char["shipTypeID"], $shipTypes)) {
230
				self::statDestroyed("ship", $char["shipTypeID"], $groupID, $modifier, $points, $isk);
231
				$shipTypes[] = $char["shipTypeID"];
232
			}
233
			if (!in_array($char["groupID"], $groups)) {
234
				self::statDestroyed("group", $char["groupID"], $groupID, $modifier, $points, $isk);
235
				$groups[] = $char["groupID"];
236
			}
237
		}
238
		foreach($corps as $corp) self::statDestroyed("corp", $corp["corporationID"], $groupID, $modifier, $points, $isk);
239
		foreach($allis as $alli) self::statDestroyed("alli", $alli["allianceID"], $groupID, $modifier, $points, $isk);
240
		foreach($factions as $faction) self::statDestroyed("faction", $faction["factionID"], $groupID, $modifier, $points, $isk);
241
242
		if ($modifier == -1) {
243
			Db::execute("delete from zz_participants where killID = :killID", array(":killID" => $killID));
244
		}
245
	}
246
247
	/**
248
	 * @param string $type
249
	 */
250
	private static function statLost($type, $typeID, $groupID, $modifier, $points, $isk)
251
	{
252
		if ($typeID == 0) return;
253
		Db::execute("insert into zz_stats (type, typeID, groupID, lost, pointsLost, iskLost) values (:type, :typeID, :groupID, :modifier, :points, :isk) on duplicate key update lost = lost + :modifier, pointsLost = pointsLost + :points, iskLost = iskLost + :isk", array(":type" => $type, ":typeID" => $typeID, ":groupID" => $groupID, ":modifier" => $modifier, ":points" => $points, ":isk" => $isk));
254
	}
255
256
	/**
257
	 * @param string $type
258
	 */
259
	private static function statDestroyed($type, $typeID, $groupID, $modifier, $points, $isk)
260
	{
261
		if ($typeID == 0) return;
262
		Db::execute("insert into zz_stats (type, typeID, groupID, destroyed, pointsDestroyed, iskDestroyed) values (:type, :typeID, :groupID, :modifier, :points, :isk) on duplicate key update destroyed = destroyed + :modifier, pointsDestroyed = pointsDestroyed + :points, iskDestroyed = iskDestroyed + :isk", 	array(":type" => $type, ":typeID" => $typeID, ":groupID" => $groupID, ":modifier" => $modifier, ":points" => $points, ":isk" => $isk));
263
	}
264
}
265