improved query code
This commit is contained in:
		| @@ -144,15 +144,15 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 	private function initTables() { | 	private function initTables() { | ||||||
| 		$mysqli = $this->maniaControl->database->mysqli; | 		$mysqli = $this->maniaControl->database->mysqli; | ||||||
| 		$query  = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_RANK . "` ( | 		$query  = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_RANK . "` ( | ||||||
| 		           `PlayerIndex` mediumint(9) NOT NULL default 0, | 				`PlayerIndex` int(11) NOT NULL, | ||||||
| 		           `Rank` mediumint(9) NOT NULL default 0, | 				`Rank` int(11) NOT NULL, | ||||||
| 		           `Avg` float NOT NULL default 0, | 				`Avg` float NOT NULL, | ||||||
| 				KEY `PlayerIndex` (`PlayerIndex`), | 				KEY `PlayerIndex` (`PlayerIndex`), | ||||||
| 				UNIQUE `Rank` (`Rank`) | 				UNIQUE `Rank` (`Rank`) | ||||||
| 				) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Mania Control ServerRanking';"; | 				) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ServerRanking';"; | ||||||
| 		$mysqli->query($query); | 		$mysqli->query($query); | ||||||
| 		if ($mysqli->error) { | 		if ($mysqli->error) { | ||||||
| 			trigger_error($mysqli->error, E_USER_ERROR); | 			throw new \Exception($mysqli->error); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -182,7 +182,12 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 		$mysqli = $this->maniaControl->database->mysqli; | 		$mysqli = $this->maniaControl->database->mysqli; | ||||||
|  |  | ||||||
| 		// Erase old Average Data | 		// Erase old Average Data | ||||||
| 		$mysqli->query('TRUNCATE TABLE ' . self::TABLE_RANK); | 		$query = "TRUNCATE TABLE `" . self::TABLE_RANK . "`;"; | ||||||
|  | 		$mysqli->query($query); | ||||||
|  | 		if ($mysqli->error) { | ||||||
|  | 			trigger_error($mysqli->error); | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		$type = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MIN_RANKING_TYPE); | 		$type = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MIN_RANKING_TYPE); | ||||||
|  |  | ||||||
| 		switch ($type) { | 		switch ($type) { | ||||||
| @@ -199,18 +204,14 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 						continue; | 						continue; | ||||||
| 					} | 					} | ||||||
| 					$ranks[$player] = $killDeathRatios[$player] * $accuracies[$player] * 1000; | 					$ranks[$player] = $killDeathRatios[$player] * $accuracies[$player] * 1000; | ||||||
|  |  | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				arsort($ranks); | 				arsort($ranks); | ||||||
|  |  | ||||||
|  |  | ||||||
| 				break; | 				break; | ||||||
| 			case self::RANKING_TYPE_POINTS: | 			case self::RANKING_TYPE_POINTS: | ||||||
| 				$minHits = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MIN_HITS_POINTS_RANKING); | 				$minHits = $this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MIN_HITS_POINTS_RANKING); | ||||||
|  |  | ||||||
| 				$ranks   = $this->maniaControl->statisticManager->getStatsRanking(StatisticCollector::STAT_ON_HIT, -1, $minHits); | 				$ranks   = $this->maniaControl->statisticManager->getStatsRanking(StatisticCollector::STAT_ON_HIT, -1, $minHits); | ||||||
|  |  | ||||||
| 				break; | 				break; | ||||||
| 			case self::RANKING_TYPE_RECORDS: //TODO verify workable status | 			case self::RANKING_TYPE_RECORDS: //TODO verify workable status | ||||||
| 				/** @var LocalRecordsPlugin $localRecordsPlugin */ | 				/** @var LocalRecordsPlugin $localRecordsPlugin */ | ||||||
| @@ -238,13 +239,13 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 				foreach ($maps as $map) { | 				foreach ($maps as $map) { | ||||||
| 					$records = $localRecordsPlugin->getLocalRecords($map, $maxRecords); | 					$records = $localRecordsPlugin->getLocalRecords($map, $maxRecords); | ||||||
|  |  | ||||||
| 					$i = 1; | 					$index = 1; | ||||||
| 					foreach ($records as $record) { | 					foreach ($records as $record) { | ||||||
| 						if (isset($players[$record->playerIndex])) { | 						if (isset($players[$record->playerIndex])) { | ||||||
| 							$players[$record->playerIndex][0] += $i; | 							$players[$record->playerIndex][0] += $index; | ||||||
| 							$players[$record->playerIndex][1]++; | 							$players[$record->playerIndex][1]++; | ||||||
| 						} | 						} | ||||||
| 						$i++; | 						$index++; | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| @@ -270,16 +271,19 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 		$this->recordCount = count($ranks); | 		$this->recordCount = count($ranks); | ||||||
|  |  | ||||||
| 		//Compute each player's new average score | 		//Compute each player's new average score | ||||||
| 		$query = "INSERT INTO " . self::TABLE_RANK . " VALUES "; | 		$query = "INSERT INTO `" . self::TABLE_RANK . "` VALUES "; | ||||||
| 		$i     = 1; | 		$index = 1; | ||||||
|  |  | ||||||
| 		foreach ($ranks as $player => $rankValue) { | 		foreach ($ranks as $player => $rankValue) { | ||||||
| 			$query .= '(' . $player . ',' . $i . ',' . $rankValue . '),'; | 			$query .= '(' . $player . ',' . $index . ',' . $rankValue . '),'; | ||||||
| 			$i++; | 			$index++; | ||||||
| 		} | 		} | ||||||
| 		$query = substr($query, 0, strlen($query) - 1); // strip trailing ',' | 		$query = substr($query, 0, strlen($query) - 1); // strip trailing ',' | ||||||
|  |  | ||||||
| 		$mysqli->query($query); | 		$mysqli->query($query); | ||||||
|  | 		if ($mysqli->error) { | ||||||
|  | 			trigger_error($mysqli->error); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| @@ -350,7 +354,9 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 		//TODO setting global from db or local | 		//TODO setting global from db or local | ||||||
| 		$mysqli = $this->maniaControl->database->mysqli; | 		$mysqli = $this->maniaControl->database->mysqli; | ||||||
|  |  | ||||||
| 		$result = $mysqli->query('SELECT * FROM ' . self::TABLE_RANK . ' WHERE PlayerIndex=' . $player->index); | 		$query  = "SELECT * FROM `" . self::TABLE_RANK . "` | ||||||
|  | 				WHERE `PlayerIndex` = {$player->index};"; | ||||||
|  | 		$result = $mysqli->query($query); | ||||||
| 		if ($mysqli->error) { | 		if ($mysqli->error) { | ||||||
| 			trigger_error($mysqli->error); | 			trigger_error($mysqli->error); | ||||||
| 			return null; | 			return null; | ||||||
| @@ -400,15 +406,21 @@ class ServerRankingPlugin implements Plugin, CallbackListener, CommandListener { | |||||||
| 		$rankObject = $this->getRank($player); | 		$rankObject = $this->getRank($player); | ||||||
| 		$nextRank   = $rankObject->rank - 1; | 		$nextRank   = $rankObject->rank - 1; | ||||||
|  |  | ||||||
| 		$result = $mysqli->query('SELECT * FROM ' . self::TABLE_RANK . ' WHERE Rank=' . $nextRank); | 		$query  = "SELECT * FROM `" . self::TABLE_RANK . "` | ||||||
| 		if ($result->num_rows > 0) { | 				WHERE `Rank` = {$nextRank}"; | ||||||
| 			$row = $result->fetch_array(); | 		$result = $mysqli->query($query); | ||||||
| 			$result->free_result(); | 		if ($mysqli->error) { | ||||||
| 			return Rank::fromArray($row); | 			trigger_error($mysqli->error); | ||||||
| 		} else { |  | ||||||
| 			$result->free_result(); |  | ||||||
| 			return null; | 			return null; | ||||||
| 		} | 		} | ||||||
|  | 		if ($result->num_rows <= 0) { | ||||||
|  | 			$result->free(); | ||||||
|  | 			return null; | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		$row = $result->fetch_array(); | ||||||
|  | 		$result->free(); | ||||||
|  | 		return Rank::fromArray($row); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user