Исправлен формат массива с текстурами, освежён код проекта

This commit is contained in:
ErickSkrauch 2015-10-15 14:01:23 +03:00
parent 800523aef2
commit 10be2a66ca
6 changed files with 74 additions and 77 deletions

52
app.php
View File

@ -3,8 +3,8 @@
define('ENCODING', 'UTF-8'); define('ENCODING', 'UTF-8');
$app->get('/skins/{nickname}', function ($nickname) use ($app) { $app->get('/skins/{nickname}', function ($nickname) use ($app) {
$systemVersion = $app->request->get('version', 'int'); // $systemVersion = $app->request->get('version', 'int');
$minecraftVersion = $app->request->get('minecraft_version', 'string'); // $minecraftVersion = $app->request->get('minecraft_version', 'string');
// На всякий случай проверка на наличие .png для файла // На всякий случай проверка на наличие .png для файла
if (strrpos($nickname, '.png') != -1) { if (strrpos($nickname, '.png') != -1) {
@ -13,10 +13,7 @@ $app->get('/skins/{nickname}', function ($nickname) use ($app) {
// TODO: восстановить функцию деградации скинов // TODO: восстановить функцию деградации скинов
$skin = Skins::findFirst(array(array( $skin = Skins::findByNickname($nickname);
'nickname' => mb_convert_case($nickname, MB_CASE_LOWER, ENCODING)
)));
if (!$skin || $skin->skinId == 0) { if (!$skin || $skin->skinId == 0) {
return $app->response->redirect('http://skins.minecraft.net/MinecraftSkins/' . $nickname . '.png', true); return $app->response->redirect('http://skins.minecraft.net/MinecraftSkins/' . $nickname . '.png', true);
} }
@ -34,10 +31,7 @@ $app->get('/cloaks/{nickname}', function ($nickname) use ($app) {
}); });
$app->get('/textures/{nickname}', function($nickname) use ($app) { $app->get('/textures/{nickname}', function($nickname) use ($app) {
$skin = Skins::findFirst(array(array( $skin = Skins::findByNickname($nickname);
'nickname' => mb_convert_case($nickname, MB_CASE_LOWER, ENCODING)
)));
if ($skin && $skin->skinId != 0) { if ($skin && $skin->skinId != 0) {
$url = $skin->url; $url = $skin->url;
$hash = $skin->hash; $hash = $skin->hash;
@ -46,21 +40,21 @@ $app->get('/textures/{nickname}', function($nickname) use ($app) {
$hash = md5('non-ely-' . mktime(date('H'), 0, 0) . '-' . $nickname); $hash = md5('non-ely-' . mktime(date('H'), 0, 0) . '-' . $nickname);
} }
$textures = array( // TODO: в authserver.ely.by есть готовый класс для работы с форматом текстур. Так что если мы его вынесем в
'SKIN' => array( // common library, то нужно будет заменить его здесь
$textures = [
'SKIN' => [
'url' => $url, 'url' => $url,
'hash' => $hash, 'hash' => $hash,
'metadata' => array( ],
'model' => ($skin && $skin->isSlim) ? 'slim' : 'default' ];
)
),
'CAPE' => array(
'url' => '',
'hash' => ''
)
);
return $app->response->setJsonContent($textures); if ($skin && $skin->isSlim) {
$textures['SKIN']['metadata']['model'] = 'slim';
}
return $app->response->setContentType('application/json')->setJsonContent($textures);
}); });
$app->post('/system/setSkin', function() use ($app) { $app->post('/system/setSkin', function() use ($app) {
@ -72,10 +66,7 @@ $app->post('/system/setSkin', function() use ($app) {
$request = $app->request; $request = $app->request;
$nickname = mb_convert_case($request->getPost('nickname', 'string'), MB_CASE_LOWER, ENCODING); $nickname = mb_convert_case($request->getPost('nickname', 'string'), MB_CASE_LOWER, ENCODING);
$skin = Skins::findFirst(array(array( $skin = Skins::findByNickname($nickname);
'nickname' => $nickname
)));
if (!$skin) { if (!$skin) {
$skin = new Skins(); $skin = new Skins();
$skin->nickname = $nickname; $skin->nickname = $nickname;
@ -88,16 +79,9 @@ $app->post('/system/setSkin', function() use ($app) {
$skin->isSlim = (bool) $request->getPost('isSlim', 'int'); $skin->isSlim = (bool) $request->getPost('isSlim', 'int');
$skin->url = $request->getPost('url', 'string'); $skin->url = $request->getPost('url', 'string');
if ($skin->save()) { return $app->view->setContent($skin->save() ? 'OK' : 'ERROR');
echo 'OK';
} else {
echo 'ERROR';
}
}); });
/**
* Not found handler
*/
$app->notFound(function () use ($app) { $app->notFound(function () use ($app) {
$app->response $app->response
->setStatusCode(404, 'Not Found') ->setStatusCode(404, 'Not Found')

View File

@ -1,15 +1,15 @@
<?php <?php
return new \Phalcon\Config(array( return new \Phalcon\Config([
'mongo' => array( 'mongo' => [
'host' => 'localhost', 'host' => 'localhost',
'port' => 27017, 'port' => 27017,
'username' => '', 'username' => '',
'password' => '', 'password' => '',
'dbname' => 'ely_skins', 'dbname' => 'ely_skins',
), ],
'application' => array( 'application' => [
'modelsDir' => __DIR__ . '/../models/', 'modelsDir' => __DIR__ . '/../models/',
'baseUri' => '/', 'baseUri' => '/',
) ]
)); ]);

View File

@ -1,4 +1,7 @@
<?php <?php
/**
* @var \Phalcon\Config $config
*/
$loader = new \Phalcon\Loader(); $loader = new \Phalcon\Loader();

View File

@ -1,13 +1,17 @@
<?php <?php
/**
* @var \Phalcon\Config $config
*/
use Phalcon\Mvc\Collection\Manager;
use Phalcon\Mvc\View; use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver; use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\DI\FactoryDefault; use Phalcon\DI\FactoryDefault;
$di = new FactoryDefault(); $di = new FactoryDefault();
$di->set("view", function () { $di->set('view', function () {
$view = new \Phalcon\Mvc\View(); $view = new View();
$view->disable(); $view->disable();
return $view; return $view;
@ -16,35 +20,27 @@ $di->set("view", function () {
/** /**
* The URL component is used to generate all kind of urls in the application * The URL component is used to generate all kind of urls in the application
*/ */
$di->set("url", function () use ($config) { $di->set('url', function () use ($config) {
$url = new UrlResolver(); $url = new UrlResolver();
$url->setBaseUri($config->application->baseUri); $url->setBaseUri($config->application->baseUri);
return $url; return $url;
}); });
$di->set("mongo", function() use ($config) { $di->set('mongo', function() use ($config) {
if (!$config->mongo->username || !$config->mongo->password) { /** @var StdClass $mongoConfig */
$mongo = new MongoClient( $mongoConfig = $config->mongo;
"mongodb://". $connectionString = 'mongodb://';
$config->mongo->host.":". if ($mongoConfig->username && $mongoConfig->password) {
$config->mongo->port $connectionString .= "{$mongoConfig->username}:{$mongoConfig->password}@";
);
} else {
$mongo = new MongoClient(
"mongodb://".
$config->mongo->username.":".
$config->mongo->password."@".
$config->mongo->host.":".
$config->mongo->port
);
} }
return $mongo->selectDb($config->mongo->dbname); $connectionString .= $mongoConfig->host . ':' . $mongoConfig->port;
$mongo = new MongoClient($connectionString);
return $mongo->selectDb($mongoConfig->dbname);
}); });
//Registering the collectionManager service
$di->setShared('collectionManager', function() { $di->setShared('collectionManager', function() {
$modelsManager = new Phalcon\Mvc\Collection\Manager(); return new Manager();
return $modelsManager;
}); });

View File

@ -3,11 +3,10 @@
use Phalcon\Mvc\Collection; use Phalcon\Mvc\Collection;
/** /**
* @method static Skins findFirst()
*
* @property string $id * @property string $id
*/ */
class Skins extends Collection { class Skins extends Collection {
public $_id; public $_id;
public $userId; public $userId;
public $nickname; public $nickname;
@ -22,6 +21,19 @@ class Skins extends Collection {
} }
public function getSource() { public function getSource() {
return "skins"; return 'skins';
} }
/**
* @param string $nickname
* @return bool|Skins
*/
public static function findByNickname($nickname) {
return static::findFirst([
[
'nickname' => mb_convert_case($nickname, MB_CASE_LOWER, ENCODING),
],
]);
}
} }

View File

@ -1,16 +1,18 @@
<?php <?php
use Phalcon\Mvc\Micro;
error_reporting(E_ALL); error_reporting(E_ALL);
try { try {
/** @var \Phalcon\Config $config */ /** @var \Phalcon\Config $config */
$config = include __DIR__ . "/../config/config.php"; $config = include __DIR__ . '/../config/config.php';
/** @var \Phalcon\Loader $loader */ /** @var \Phalcon\Loader $loader */
include __DIR__ . '/../config/loader.php'; include __DIR__ . '/../config/loader.php';
/** @var Phalcon\DI\FactoryDefault $di */ /** @var Phalcon\DI\FactoryDefault $di */
include __DIR__ . '/../config/services.php'; include __DIR__ . '/../config/services.php';
$app = new \Phalcon\Mvc\Micro($di); $app = new Micro($di);
include __DIR__ . '/../app.php'; include __DIR__ . '/../app.php';
$app->handle(); $app->handle();