Added sorting options for list: best/worst/newest/oldest (via /list).

This commit is contained in:
Max Klaversma 2014-04-29 15:20:20 +02:00 committed by Steffen Schröder
parent c23f7dd5b9
commit 84b44c29b9
2 changed files with 100 additions and 10 deletions

View File

@ -316,7 +316,79 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb
* @param Player $player
*/
public function command_List(array $chatCallback, Player $player) {
$this->maniaControl->mapManager->mapList->showMapList($player);
$chatCommands = explode(' ', $chatCallback[1][2]);
$this->maniaControl->mapManager->mapList->playerCloseWidget($player);
if(isset($chatCommands[1])) {
if($chatCommands[1] == ' ' || $chatCommands[1] == 'all') {
$this->maniaControl->mapManager->mapList->showMapList($player);
} elseif($chatCommands[1] == 'best') {
$this->showMapListKarma(true, $player);
} elseif($chatCommands[1] == 'worst') {
$this->showMapListKarma(false, $player);
} elseif($chatCommands[1] == 'newest') {
$this->showMapListDate(true, $player);
} elseif($chatCommands[1] == 'oldest') {
$this->showMapListDate(false, $player);
}
} else {
$this->maniaControl->mapManager->mapList->showMapList($player);
}
}
private function showMapListKarma($best, $player) {
/** @var \MCTeam\KarmaPlugin $karmaPlugin */
$karmaPlugin = $this->maniaControl->pluginManager->getPlugin(MapList::DEFAULT_KARMA_PLUGIN);
if($karmaPlugin) {
$maps = $this->maniaControl->mapManager->getMaps();
$mapList = array();
foreach($maps as $map) {
if($map instanceof Map) {
$votes = $karmaPlugin->getMapVotes($map);
$min = 0;
$plus = 0;
foreach($votes as $vote) {
if(isset($vote->vote)) {
if($vote->vote != 0.5) {
if($vote->vote < 0.5) {
$min = $min+$vote->count;
} else {
$plus = $plus+$vote->count;
}
}
}
}
$map->karma = $plus-$min;
$mapList[] = $map;
}
}
usort($mapList, array($this, 'sortByKarma'));
if($best) {
$mapList = array_reverse($mapList);
}
$this->maniaControl->mapManager->mapList->showMapList($player, $mapList);
} else {
$this->maniaControl->chat->sendError('KarmaPlugin is not enabled!', $player->login);
}
}
private function sortByKarma($a, $b) {
return $a->karma - $b->karma;
}
private function showMapListDate($newest, $player) {
$maps = $this->maniaControl->mapManager->getMaps();
usort($maps, function($a, $b) {
return $a->index - $b->index;
});
if($newest) {
$maps = array_reverse($maps);
}
$this->maniaControl->mapManager->mapList->showMapList($player, $maps);
}
/**

View File

@ -123,19 +123,20 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
// Get Maps
$mapList = array();
if (is_array($maps)) {
$mapList = $maps;
$pageCount = ceil(count($mapList) / self::MAX_MAPS_PER_PAGE);
$mapList = array_slice($maps, $chunk, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
$this->mapsInListShown[$player->login] = $maps;
$pageCount = ceil(count($maps) / self::MAX_MAPS_PER_PAGE);
}
else if (array_key_exists($player->login, $this->mapsInListShown)) {
$completeList = $this->mapsInListShown[$player->login];
$this->mapsInListShown[$player->login] = $completeList;
$mapList = array_slice($completeList, $chunk * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
$pageCount = ceil(count($completeList) / self::MAX_MAPS_PER_PAGE);
}
else if ($maps !== 'redirect') {
$mapList = $this->maniaControl->mapManager->getMaps($chunk * self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE, self::MAX_PAGES_PER_CHUNK * self::MAX_MAPS_PER_PAGE);
$pageCount = ceil($this->maniaControl->mapManager->getMapsCount() / self::MAX_MAPS_PER_PAGE);
}
else if (array_key_exists($player->login, $this->mapsInListShown)) {
$mapList = $this->mapsInListShown[$player->login];
$pageCount = ceil(count($mapList) / self::MAX_MAPS_PER_PAGE);
}
$this->mapsInListShown[$player->login] = $mapList;
// Create ManiaLink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
@ -422,6 +423,21 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$karma = $karmaPlugin->getMapKarma($map);
$votes = $karmaPlugin->getMapVotes($map);
if (is_numeric($karma)) {
$min = 0;
$plus = 0;
foreach($votes as $vote) {
if(isset($vote->vote)) {
if($vote->vote != 0.5) {
if($vote->vote < 0.5) {
$min = $min+$vote->count;
} else {
$plus = $plus+$vote->count;
}
}
}
}
$endKarma = $plus-$min;
$karmaGauge = new Gauge();
$mapFrame->add($karmaGauge);
$karmaGauge->setZ(2);
@ -441,7 +457,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
$karmaLabel->setTextSize(0.9);
$karmaLabel->setTextColor('000');
$karmaLabel->setAlign(Control::CENTER, Control::CENTER);
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $votes['count'] . ')');
$karmaLabel->setText(' ' . $endKarma . ' (' . $votes['count'] . 'x / ' . round($karma * 100.) . '%)');
}
}
@ -533,6 +549,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
*/
public function closeWidget(Player $player) {
unset($this->mapListShown[$player->login]);
unset($this->mapsInListShown[$player->login]);
}
/**
@ -542,6 +559,7 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener {
*/
public function playerCloseWidget(Player $player) {
unset($this->mapListShown[$player->login]);
unset($this->mapsInListShown[$player->login]);
$this->maniaControl->manialinkManager->closeWidget($player);
}