removed 'application' folder to have everything in the root directory

This commit is contained in:
Steffen Schröder
2014-09-29 18:20:09 +02:00
parent 1569fd5488
commit 9642433363
284 changed files with 4 additions and 50 deletions

View File

@ -0,0 +1,117 @@
<?php
namespace ManiaControl\ManiaExchange;
use ManiaControl\Utils\Formatter;
/**
* Mania Exchange Map Info Object
*
* @author Xymph
* @updated kremsy <kremsy@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class MXMapInfo {
/*
* Public properties
*/
public $prefix, $id, $uid, $name, $userid, $author, $uploaded, $updated, $type, $maptype;
public $titlepack, $style, $envir, $mood, $dispcost, $lightmap, $modname, $exever;
public $exebld, $routes, $length, $unlimiter, $laps, $difficulty, $lbrating, $trkvalue;
public $replaytyp, $replayid, $replaycnt, $authorComment, $commentCount, $awards;
public $pageurl, $replayurl, $imageurl, $thumburl, $downloadurl, $dir;
public $ratingVoteCount, $ratingVoteAverage, $vehicleName;
/**
* Returns map object with all available data from MX map data
*
* @param String $prefix MX URL prefix
* @param $mx
* @return MXMapInfo
*/
public function __construct($prefix, $mx) {
$this->prefix = $prefix;
if (!$mx) {
return;
}
if ($this->prefix === 'tm') {
$this->dir = 'tracks';
$this->id = $mx->TrackID;
$this->uid = isset($mx->TrackUID) ? $mx->TrackUID : '';
} else {
$this->dir = 'maps';
$this->id = $mx->MapID;
$this->uid = isset($mx->MapUID) ? $mx->MapUID : '';
}
if (!isset($mx->GbxMapName) || $mx->GbxMapName === '?') {
$this->name = $mx->Name;
} else {
$this->name = Formatter::stripDirtyCodes($mx->GbxMapName);
}
$this->userid = $mx->UserID;
$this->author = $mx->Username;
$this->uploaded = $mx->UploadedAt;
$this->updated = $mx->UpdatedAt;
$this->type = $mx->TypeName;
$this->maptype = isset($mx->MapType) ? $mx->MapType : '';
$this->titlepack = isset($mx->TitlePack) ? $mx->TitlePack : '';
$this->style = isset($mx->StyleName) ? $mx->StyleName : '';
$this->envir = $mx->EnvironmentName;
$this->mood = $mx->Mood;
$this->dispcost = $mx->DisplayCost;
$this->lightmap = $mx->Lightmap;
$this->modname = isset($mx->ModName) ? $mx->ModName : '';
$this->exever = $mx->ExeVersion;
$this->exebld = $mx->ExeBuild;
$this->routes = isset($mx->RouteName) ? $mx->RouteName : '';
$this->length = isset($mx->LengthName) ? $mx->LengthName : '';
$this->unlimiter = isset($mx->UnlimiterRequired) ? $mx->UnlimiterRequired : false;
$this->laps = isset($mx->Laps) ? $mx->Laps : 0;
$this->difficulty = $mx->DifficultyName;
$this->lbrating = isset($mx->LBRating) ? $mx->LBRating : 0;
$this->trkvalue = isset($mx->TrackValue) ? $mx->TrackValue : 0;
$this->replaytyp = isset($mx->ReplayTypeName) ? $mx->ReplayTypeName : '';
$this->replayid = isset($mx->ReplayWRID) ? $mx->ReplayWRID : 0;
$this->replaycnt = isset($mx->ReplayCount) ? $mx->ReplayCount : 0;
$this->awards = isset($mx->AwardCount) ? $mx->AwardCount : 0;
$this->vehicleName = isset($mx->VehicleName) ? $mx->VehicleName : '';
$this->authorComment = $mx->Comments;
$this->commentCount = $mx->CommentCount;
$this->ratingVoteCount = isset($mx->RatingVoteCount) ? $mx->RatingVoteCount : 0;
$this->ratingVoteAverage = isset($mx->RatingVoteAverage) ? $mx->RatingVoteAverage : 0;
if (!$this->trkvalue && $this->lbrating > 0) {
$this->trkvalue = $this->lbrating;
} elseif (!$this->lbrating && $this->trkvalue > 0) {
$this->lbrating = $this->trkvalue;
}
$this->pageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $this->dir . '/view/' . $this->id;
$this->downloadurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $this->dir . '/download/' . $this->id;
if ($mx->HasScreenshot) {
$this->imageurl = 'http://' . $this->prefix . '.mania-exchange.com/' . $this->dir . '/screenshot/normal/' . $this->id;
} else {
$this->imageurl = '';
}
if ($mx->HasThumbnail) {
$this->thumburl = 'http://' . $this->prefix . '.mania-exchange.com/' . $this->dir . '/screenshot/small/' . $this->id;
} else {
$this->thumburl = '';
}
if ($this->prefix === 'tm' && $this->replayid > 0) {
$this->replayurl = 'http://' . $this->prefix . '.mania-exchange.com/replays/download/' . $this->replayid;
} else {
$this->replayurl = '';
}
}
}

View File

@ -0,0 +1,356 @@
<?php
namespace ManiaControl\ManiaExchange;
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Gauge;
use FML\Controls\Label;
use FML\Controls\Labels\Label_Button;
use FML\Controls\Labels\Label_Text;
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_BgsPlayerCard;
use FML\Controls\Quads\Quad_Icons64x64_1;
use FML\ManiaLink;
use FML\Script\Features\Paging;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\ManiaControl;
use ManiaControl\Manialinks\IconManager;
use ManiaControl\Manialinks\ManialinkManager;
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
use ManiaControl\Maps\MapCommands;
use ManiaControl\Maps\MapManager;
use ManiaControl\Players\Player;
use ManiaControl\Utils\ColorUtil;
use ManiaControl\Utils\Formatter;
/**
* ManiaExchange List Widget Class
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener {
/*
* Constants
*/
const ACTION_ADD_MAP = 'ManiaExchangeList.AddMap';
const ACTION_SEARCH_MAPNAME = 'ManiaExchangeList.SearchMapName';
const ACTION_SEARCH_AUTHOR = 'ManiaExchangeList.SearchAuthor';
const ACTION_GET_MAPS_FROM_AUTHOR = 'ManiaExchangeList.GetMapsFromAuthor';
const MAX_MX_MAPS_PER_PAGE = 14;
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $mapListShown = array();
/**
* Construct a new MX List instance
*
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
// Callbacks
$this->maniaControl->getCallbackManager()->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_CLOSED, $this, 'closeWidget');
$this->maniaControl->getCallbackManager()->registerCallbackListener(ManialinkManager::CB_MAIN_WINDOW_OPENED, $this, 'handleWidgetOpened');
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SEARCH_MAPNAME, $this, 'showList');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_SEARCH_AUTHOR, $this, 'showList');
}
/**
* Handle ManialinkPageAnswer Callback
*
* @param array $callback
*/
public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2];
$actionArray = explode('.', $actionId);
if (count($actionArray) <= 2) {
return;
}
$action = $actionArray[0] . '.' . $actionArray[1];
$login = $callback[1][1];
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
$mapId = (int)$actionArray[2];
switch ($action) {
case self::ACTION_GET_MAPS_FROM_AUTHOR:
$callback[1][2] = 'auth:' . $actionArray[2];
$this->showList($callback, $player);
break;
case self::ACTION_ADD_MAP:
$this->maniaControl->getMapManager()->addMapFromMx($mapId, $player->login);
break;
}
}
/**
* Shows the List
*
* @param array $chatCallback
* @param Player $player
*/
public function showList(array $chatCallback, Player $player) {
$this->mapListShown[$player->login] = true;
$params = explode(' ', $chatCallback[1][2]);
$searchString = '';
$author = '';
$environment = '';
if (count($params) >= 1) {
foreach ($params as $param) {
if ($param === '/xlist' || $param === MapCommands::ACTION_OPEN_XLIST) {
continue;
}
if ($param === self::ACTION_SEARCH_MAPNAME) {
$searchString = $chatCallback[1][3][0]['Value'];
} else if ($param === self::ACTION_SEARCH_AUTHOR) {
$author = $chatCallback[1][3][0]['Value'];
} else if (strtolower(substr($param, 0, 5)) === 'auth:') {
$author = substr($param, 5);
} else if (strtolower(substr($param, 0, 4)) === 'env:') {
$environment = substr($param, 4);
} else {
if (!$searchString) {
$searchString = $param;
} else {
// concatenate words in name
$searchString .= '%20' . $param;
}
}
}
}
// search for matching maps
$this->maniaControl->getMapManager()->getMXManager()->fetchMapsAsync(function (array $maps) use (&$player) {
if (!$maps) {
$this->maniaControl->getChat()->sendError('No maps found, or MX is down!', $player->login);
return;
}
$this->showManiaExchangeList($maps, $player);
}, $searchString, $author, $environment);
}
/**
* Display the Mania Exchange List
*
* @param MXMapInfo[] $maps
* @param Player $player
* @internal param array $chatCallback
*/
private function showManiaExchangeList(array $maps, Player $player) {
// Start offsets
$width = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsWidth();
$height = $this->maniaControl->getManialinkManager()->getStyleManager()->getListWidgetsHeight();
$posX = -$width / 2;
$posY = $height / 2;
//Create ManiaLink
$maniaLink = new ManiaLink(ManialinkManager::MAIN_MLID);
$script = $maniaLink->getScript();
$paging = new Paging();
$script->addFeature($paging);
// Main frame
$frame = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultListFrame($script, $paging);
$maniaLink->add($frame);
//Predefine description Label
$descriptionLabel = $this->maniaControl->getManialinkManager()->getStyleManager()->getDefaultDescriptionLabel();
$frame->add($descriptionLabel);
// Headline
$headFrame = new Frame();
$frame->add($headFrame);
$headFrame->setY($posY - 12);
$array = array('$oId' => $posX + 3.5, '$oName' => $posX + 12.5, '$oAuthor' => $posX + 59, '$oKarma' => $posX + 85, '$oType' => $posX + 103, '$oMood' => $posX + 118, '$oLast Update' => $posX + 130);
$this->maniaControl->getManialinkManager()->labelLine($headFrame, $array);
$index = 0;
$posY = $height / 2 - 16;
$pageFrame = null;
foreach ($maps as $map) {
//TODO order possibilities
if ($index % self::MAX_MX_MAPS_PER_PAGE === 0) {
$pageFrame = new Frame();
$frame->add($pageFrame);
$posY = $height / 2 - 16;
$paging->addPage($pageFrame);
}
// Map Frame
$mapFrame = new Frame();
$pageFrame->add($mapFrame);
if ($index % 2 === 0) {
$lineQuad = new Quad_BgsPlayerCard();
$mapFrame->add($lineQuad);
$lineQuad->setSize($width, 4);
$lineQuad->setSubStyle($lineQuad::SUBSTYLE_BgPlayerCardBig);
$lineQuad->setZ(0.001);
}
$time = Formatter::time_elapsed_string(strtotime($map->updated));
$array = array('$s' . $map->id => $posX + 3.5, '$s' . $map->name => $posX + 12.5, '$s' . $map->author => $posX + 59, '$s' . str_replace('Arena', '', $map->maptype) => $posX + 103, '$s' . $map->mood => $posX + 118, '$s' . $time => $posX + 130);
$labels = $this->maniaControl->getManialinkManager()->labelLine($mapFrame, $array);
$authorLabel = $labels[2];
$authorLabel->setAction(self::ACTION_GET_MAPS_FROM_AUTHOR . '.' . $map->author);
$mapFrame->setY($posY);
$mxQuad = new Quad();
$mapFrame->add($mxQuad);
$mxQuad->setSize(3, 3);
$mxQuad->setImage($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON));
$mxQuad->setImageFocus($this->maniaControl->getManialinkManager()->getIconManager()->getIcon(IconManager::MX_ICON_MOVER));
$mxQuad->setX($posX + 56);
$mxQuad->setUrl($map->pageurl);
$mxQuad->setZ(0.01);
$description = 'View $<' . $map->name . '$> on Mania-Exchange';
$mxQuad->addTooltipLabelFeature($descriptionLabel, $description);
if ($this->maniaControl->getAuthenticationManager()->checkPermission($player, MapManager::SETTING_PERMISSION_ADD_MAP)
) {
$addQuad = new Quad_Icons64x64_1();
$mapFrame->add($addQuad);
$addQuad->setX($posX + 53);
$addQuad->setZ(-0.1);
$addQuad->setSubStyle($addQuad::SUBSTYLE_Add);
$addQuad->setSize(4, 4);
$addQuad->setAction(self::ACTION_ADD_MAP . '.' . $map->id);
$addQuad->setZ(0.01);
$description = 'Add-Map: $<' . $map->name . '$>';
$addQuad->addTooltipLabelFeature($descriptionLabel, $description);
}
//Award Quad
if ($map->awards > 0) {
$awardQuad = new Quad_Icons64x64_1();
$mapFrame->add($awardQuad);
$awardQuad->setSize(3, 3);
$awardQuad->setSubStyle($awardQuad::SUBSTYLE_OfficialRace);
$awardQuad->setX($posX + 97);
$awardQuad->setZ(0.01);
$awardLabel = new Label_Text();
$mapFrame->add($awardLabel);
$awardLabel->setX($posX + 98.5);
$awardLabel->setHAlign($awardLabel::LEFT);
$awardLabel->setText($map->awards);
$awardLabel->setTextSize(1.3);
}
//Map Karma
$karma = $map->ratingVoteAverage / 100;
$voteCount = $map->ratingVoteCount;
if (is_numeric($karma) && $voteCount > 0) {
$karmaGauge = new Gauge();
$mapFrame->add($karmaGauge);
$karmaGauge->setZ(2);
$karmaGauge->setX($posX + 89);
$karmaGauge->setSize(16.5, 9);
$karmaGauge->setDrawBg(false);
$karma = floatval($karma);
$karmaGauge->setRatio($karma + 0.15 - $karma * 0.15);
$karmaColor = ColorUtil::floatToStatusColor($karma);
$karmaGauge->setColor($karmaColor . '9');
$karmaLabel = new Label();
$mapFrame->add($karmaLabel);
$karmaLabel->setZ(2);
$karmaLabel->setX($posX + 89);
$karmaLabel->setSize(16.5 * 0.9, 5);
$karmaLabel->setTextSize(0.9);
$karmaLabel->setTextColor('000');
$karmaLabel->setText(' ' . round($karma * 100.) . '% (' . $voteCount . ')');
}
$posY -= 4;
$index++;
}
$label = new Label_Text();
$frame->add($label);
$label->setPosition(-$width / 2 + 5, $height / 2 - 5);
$label->setHAlign($label::LEFT);
$label->setTextSize(1.3);
$label->setText('Search: ');
$entry = new Entry();
$frame->add($entry);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
$entry->setHAlign($entry::LEFT);
$entry->setPosition(-$width / 2 + 15, $height / 2 - 5);
$entry->setTextSize(1);
$entry->setSize($width * 0.25, 4);
$entry->setName('SearchString');
//Search for Map-Name
$label = new Label_Button();
$frame->add($label);
$label->setPosition(-$width / 2 + 63, $height / 2 - 5);
$label->setText('MapName');
$label->setTextSize(1.3);
$quad = new Quad_BgsPlayerCard();
$frame->add($quad);
$quad->setPosition(-$width / 2 + 63, $height / 2 - 5, 0.01);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setSize(18, 5);
$quad->setAction(self::ACTION_SEARCH_MAPNAME);
//Search for Author
$label = new Label_Button();
$frame->add($label);
$label->setPosition(-$width / 2 + 82, $height / 2 - 5);
$label->setText('Author');
$label->setTextSize(1.3);
$quad = new Quad_BgsPlayerCard();
$frame->add($quad);
$quad->setPosition(-$width / 2 + 82, $height / 2 - 5, 0.01);
$quad->setSubStyle($quad::SUBSTYLE_BgPlayerCardBig);
$quad->setSize(18, 5);
$quad->setAction(self::ACTION_SEARCH_AUTHOR);
// render and display xml
$this->maniaControl->getManialinkManager()->displayWidget($maniaLink, $player, 'ManiaExchangeList');
}
/**
* Unset the player if he opened another Main Widget
*
* @param Player $player
* @param $openedWidget
*/
public function handleWidgetOpened(Player $player, $openedWidget) {
//unset when another main widget got opened
if ($openedWidget !== 'ManiaExchangeList') {
unset($this->mapListShown[$player->login]);
}
}
/**
* Closes the widget
*
* @param \ManiaControl\Players\Player $player
* @internal param array $callback
*/
public function closeWidget(Player $player) {
unset($this->mapListShown[$player->login]);
}
}

View File

@ -0,0 +1,375 @@
<?php
namespace ManiaControl\ManiaExchange;
use ManiaControl\Files\AsynchronousFileReader;
use ManiaControl\ManiaControl;
use ManiaControl\Maps\Map;
use ManiaControl\Maps\MapManager;
use Maniaplanet\DedicatedServer\Xmlrpc\GameModeException;
/**
* Mania Exchange Info Searcher Class
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class ManiaExchangeManager {
/*
* Constants
*/
//Search others
const SEARCH_ORDER_NONE = -1;
const SEARCH_ORDER_TRACK_NAME = 0;
const SEARCH_ORDER_AUTHOR = 1;
const SEARCH_ORDER_UPLOADED_NEWEST = 2;
const SEARCH_ORDER_UPLOADED_OLDEST = 3;
const SEARCH_ORDER_UPDATED_NEWEST = 4;
const SEARCH_ORDER_UPDATED_OLDEST = 5;
const SEARCH_ORDER_ACTIVITY_LATEST = 6;
const SEARCH_ORDER_ACTIVITY_OLDEST = 7;
const SEARCH_ORDER_AWARDS_MOST = 8;
const SEARCH_ORDER_AWARDS_LEAST = 9;
const SEARCH_ORDER_COMMENTS_MOST = 10;
const SEARCH_ORDER_COMMENTS_LEAST = 11;
const SEARCH_ORDER_DIFFICULTY_EASIEST = 12;
const SEARCH_ORDER_DIFFICULTY_HARDEST = 13;
const SEARCH_ORDER_LENGTH_SHORTEST = 14;
const SEARCH_ORDER_LENGTH_LONGEST = 15;
//Maximum Maps per request
const MAPS_PER_MX_FETCH = 50;
const MIN_EXE_BUILD = "2014-04-01_00_00";
/*
* Private properties
*/
/** @var ManiaControl $maniaControl */
private $maniaControl = null;
private $mxIdUidVector = array();
/**
* Construct map manager
*
* @param \ManiaControl\ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Unset Map by Mx Id
*
* @param int $mxId
*/
public function unsetMap($mxId) {
if (isset($this->mxIdUidVector[$mxId])) {
unset($this->mxIdUidVector[$mxId]);
}
}
/**
* Fetch Map Information from Mania Exchange
*
* @param mixed $maps
*/
public function fetchManiaExchangeMapInformation($maps = null) {
if ($maps) {
// Fetch Information for a single map
$maps = array($maps);
} else {
// Fetch Information for whole MapList
$maps = $this->maniaControl->getMapManager()->getMaps();
}
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
$mapIdString = '';
// Fetch mx ids
$fetchMapQuery = "SELECT `mxid`, `changed` FROM `" . MapManager::TABLE_MAPS . "`
WHERE `index` = ?;";
$fetchMapStatement = $mysqli->prepare($fetchMapQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$index = 0;
foreach ($maps as $map) {
if (!$map) {
// TODO: remove after resolving of error report about "non-object"
$this->maniaControl->getErrorHandler()->triggerDebugNotice('Non-Object-Map', $map, $maps);
continue;
}
/** @var Map $map */
$fetchMapStatement->bind_param('i', $map->index);
$fetchMapStatement->execute();
if ($fetchMapStatement->error) {
trigger_error($fetchMapStatement->error);
continue;
}
$fetchMapStatement->store_result();
$fetchMapStatement->bind_result($mxId, $changed);
$fetchMapStatement->fetch();
$fetchMapStatement->free_result();
//Set changed time into the map object
$map->lastUpdate = strtotime($changed);
if ($mxId) {
$appendString = $mxId . ',';
//Set the mx id to the mxidmapvektor
$this->mxIdUidVector[$mxId] = $map->uid;
} else {
$appendString = $map->uid . ',';
}
$index++;
//If Max Maplimit is reached, or string gets too long send the request
if ($index % self::MAPS_PER_MX_FETCH === 0) {
$mapIdString = substr($mapIdString, 0, -1);
$this->fetchMaplistByMixedUidIdString($mapIdString);
$mapIdString = '';
}
$mapIdString .= $appendString;
}
if ($mapIdString) {
$mapIdString = substr($mapIdString, 0, -1);
$this->fetchMaplistByMixedUidIdString($mapIdString);
}
$fetchMapStatement->close();
}
/**
* Fetch the whole Map List from MX via mixed Uid and Id Strings
*
* @param string $string
*/
public function fetchMaplistByMixedUidIdString($string) {
// Get Title Prefix
$titlePrefix = $this->maniaControl->getMapManager()->getCurrentMap()->getGame();
// compile search URL
$url = "http://api.mania-exchange.com/{$titlePrefix}/maps/?ids={$string}";
$this->maniaControl->getFileReader()->loadFile($url, function ($mapInfo, $error) use ($titlePrefix, $url) {
if ($error) {
trigger_error("Error: '{$error}' for Url '{$url}'");
return;
}
if (!$mapInfo) {
return;
}
$mxMapList = json_decode($mapInfo);
if ($mxMapList === null) {
trigger_error("Can't decode searched JSON Data from Url '{$url}'");
return;
}
$maps = array();
foreach ($mxMapList as $map) {
if ($map) {
$mxMapObject = new MXMapInfo($titlePrefix, $map);
if ($mxMapObject) {
array_push($maps, $mxMapObject);
}
}
}
$this->updateMapObjectsWithManiaExchangeIds($maps);
}, AsynchronousFileReader::CONTENT_TYPE_JSON);
}
/**
* Store MX Map Info in the Database and the MX Info in the Map Object
*
* @param array $mxMapInfos
*/
public function updateMapObjectsWithManiaExchangeIds(array $mxMapInfos) {
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
// Save map data
$saveMapQuery = "UPDATE `" . MapManager::TABLE_MAPS . "`
SET `mxid` = ?
WHERE `uid` = ?;";
$saveMapStatement = $mysqli->prepare($saveMapQuery);
if ($mysqli->error) {
trigger_error($mysqli->error);
return;
}
$saveMapStatement->bind_param('is', $mapMxId, $mapUId);
foreach ($mxMapInfos as $mxMapInfo) {
/** @var MXMapInfo $mxMapInfo */
$mapMxId = $mxMapInfo->id;
$mapUId = $mxMapInfo->uid;
$saveMapStatement->execute();
if ($saveMapStatement->error) {
trigger_error($saveMapStatement->error);
}
//Take the uid out of the vector
if (isset($this->mxIdUidVector[$mxMapInfo->id])) {
$uid = $this->mxIdUidVector[$mxMapInfo->id];
} else {
$uid = $mxMapInfo->uid;
}
$map = $this->maniaControl->getMapManager()->getMapByUid($uid);
if ($map) {
// TODO: how does it come that $map can be empty here? we got an error report for that
/** @var Map $map */
$map->mx = $mxMapInfo;
}
}
$saveMapStatement->close();
}
/**
* @deprecated
* @see \ManiaControl\ManiaExchange\ManiaExchangeManager::fetchMaplistByMixedUidIdString()
*/
public function getMaplistByMixedUidIdString($string) {
$this->fetchMaplistByMixedUidIdString($string);
return true;
}
/**
* Fetch Map Info asynchronously
*
* @param int $mapId
* @param callable $function
*/
public function fetchMapInfo($mapId, callable $function) {
// Get Title Prefix
$titlePrefix = $this->maniaControl->getMapManager()->getCurrentMap()->getGame();
// compile search URL
$url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/?ids=' . $mapId;
$this->maniaControl->getFileReader()->loadFile($url, function ($mapInfo, $error) use (&$function, $titlePrefix, $url) {
$mxMapInfo = null;
if ($error) {
trigger_error($error);
} else {
$mxMapList = json_decode($mapInfo);
if (!is_array($mxMapList)) {
trigger_error('Cannot decode searched JSON data from ' . $url);
} else if (!empty($mxMapList)) {
$mxMapInfo = new MXMapInfo($titlePrefix, $mxMapList[0]);
}
}
call_user_func($function, $mxMapInfo);
}, AsynchronousFileReader::CONTENT_TYPE_JSON);
}
/**
* @deprecated
* @see \ManiaControl\ManiaExchange\ManiaExchangeManager::fetchMapsAsync()
*/
public function getMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
$this->fetchMapsAsync($function, $name, $author, $env, $maxMapsReturned, $searchOrder);
return true;
}
/**
* Fetch a MapList Asynchronously
*
* @param callable $function
* @param string $name
* @param string $author
* @param string $env
* @param int $maxMapsReturned
* @param int $searchOrder
*/
public function fetchMapsAsync(callable $function, $name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
// TODO: remove $env because it's not really used?
// Get Title Id
$titleId = $this->maniaControl->getServer()->titleId;
$titlePrefix = $this->maniaControl->getMapManager()->getCurrentMap()->getGame();
// compile search URL
$url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch2/search?api=on';
$game = explode('@', $titleId);
$envNumber = $this->getEnvironment($game[0]);
if ($env || $envNumber > -1) {
$url .= '&environments=' . $envNumber;
}
if ($name) {
$url .= '&trackname=' . str_replace(" ", "%20", $name);
}
if ($author) {
$url .= '&author=' . $author;
}
$url .= '&priord=' . $searchOrder;
$url .= '&limit=' . $maxMapsReturned;
if ($titlePrefix !== "tm") {
$url .= '&minexebuild=' . self::MIN_EXE_BUILD;
}
// Get MapTypes
try {
$scriptInfos = $this->maniaControl->getClient()->getModeScriptInfo();
$mapTypes = $scriptInfos->compatibleMapTypes;
$url .= '&mtype=' . $mapTypes;
} catch (GameModeException $e) {
}
$this->maniaControl->getFileReader()->loadFile($url, function ($mapInfo, $error) use (&$function, $titlePrefix) {
if ($error) {
trigger_error($error);
return;
}
$mxMapList = json_decode($mapInfo);
if (!isset($mxMapList->results)) {
trigger_error('Cannot decode searched JSON data');
return;
}
$mxMapList = $mxMapList->results;
if ($mxMapList === null) {
trigger_error('Cannot decode searched JSON data');
return;
}
$maps = array();
foreach ($mxMapList as $map) {
if (!empty($map)) {
array_push($maps, new MXMapInfo($titlePrefix, $map));
}
}
call_user_func($function, $maps);
}, AsynchronousFileReader::CONTENT_TYPE_JSON);
}
/**
* Get the Current Environment by String
*
* @param string $env
* @return int
*/
private function getEnvironment($env) {
switch ($env) {
case 'TMCanyon':
return 1;
case 'TMStadium':
return 2;
case 'TMValley':
return 3;
default:
return -1;
}
}
}