Skip to content
This repository was archived by the owner on Dec 6, 2019. It is now read-only.

Commit a190646

Browse files
committed
Add more API routes
1 parent 83085f9 commit a190646

File tree

9 files changed

+90
-36
lines changed

9 files changed

+90
-36
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This project uses the Laravel framework. They have great documentation here: htt
5555
* Sponge: https://github.com/SpongePowered/Ore/blob/1420c404ded5ce8b392e993fe1679beaa6f15be5/conf/routes#L7
5656
* Server GEO location
5757
* Player database
58+
* Add servers at once
5859
* Automatically detect teamspeak and website by doing a SRV lookup
5960
* API for all features, including UUID resolver, Banner generator, ...
6061

app/Console/Commands/Ping.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use xPaw\MinecraftPing;
77
use xPaw\MinecraftPingException;
88
use \App\Server;
9+
use \File;
10+
use \Exception;
911

1012
class Ping extends Command {
1113

@@ -57,7 +59,7 @@ function pingServer(Server $server) {
5759

5860
$this->parsePingData($server, $result);
5961
$server->save();
60-
} catch (\Exception $exception) {
62+
} catch (Exception $exception) {
6163
$this->error($server->address . " " . $exception->getMessage());
6264

6365
//Reset the these data online if the server was online before
@@ -169,7 +171,7 @@ function saveIcon($hostname, $favicon) {
169171

170172
$path = public_path() . "/img/favicons/$hostname.png";
171173
//check if it's still the same folder
172-
if (\File::dirname($path) == public_path() . "/img/favicons"
174+
if (File::dirname($path) == public_path() . "/img/favicons"
173175
&& (!file_exists($path) || md5_file($path) !== md5($data))) {
174176
//strip invalid path characters
175177
file_put_contents($path, $data);

app/Console/Commands/Query.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use \xPaw\MinecraftQueryException;
88
use \App\Server;
99
use \App\PluginUsage;
10+
use \Exception;
1011

1112
class Query extends Command {
1213

@@ -62,7 +63,7 @@ function queryServer(Server $server, $port = 25565) {
6263
if ($port === 25565) {
6364
$this->queryServer($server, Server::DEFAULT_BUNGEE_QUERY_PORT);
6465
}
65-
} catch (\Exception $exception) {
66+
} catch (Exception $exception) {
6667
$this->error($exception);
6768
$this->error($server->address . " " . $exception->getMessage());
6869
}

app/Http/Controllers/ApiController.php

+46-2
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,65 @@
55
use App\Server;
66
use App\Skin;
77
use App\Player;
8+
use \App\PluginUsage;
89

910
class ApiController extends Controller {
1011

1112
public function index() {
12-
return App\Server::paginate();
13+
return Server::paginate();
1314
}
1415

1516
public function getServer($address) {
16-
return App\Server::whereAddress($address)->firstOrFail();
17+
return Server::whereAddress($address)->firstOrFail();
1718
}
1819

1920
public function getIcon($address) {
2021
return redirect('/img/favicons/' . $address . ".png");
2122
}
2223

24+
public function getPlayerByUUID($uuid) {
25+
return Player::whereUuid($uuid)->firstOrFail();
26+
}
27+
28+
public function getPlayerByName($name) {
29+
$players = Player::whereName($name)->get();
30+
if (count($players) == 0) {
31+
return response('', 404);
32+
}
33+
34+
return $players;
35+
}
36+
37+
public function getSkinByUUID($uuid) {
38+
$skin = Skin::whereProfileId($uuid)->firstOrFail();
39+
return [
40+
'timestamp' => $skin->timestamp,
41+
'profileId' => $skin->profile_id,
42+
'profileName' => $skin->profile_name,
43+
'skinUrl' => $skin->skin_url,
44+
'slimMode' => $skin->slim_model,
45+
'capeUrl' => $skin->cape_url,
46+
'signature' => base64_encode($skin->signature)
47+
];
48+
}
49+
50+
public function getSkinByName($name) {
51+
$skin = Skin::whereProfileName($name)->firstOrFail();
52+
return [
53+
'timestamp' => $skin->timestamp,
54+
'profileId' => $skin->profile_id,
55+
'profileName' => $skin->profile_name,
56+
'skinUrl' => $skin->skin_url,
57+
'slimMode' => $skin->slim_model,
58+
'capeUrl' => $skin->cape_url,
59+
'signature' => base64_encode($skin->signature)
60+
];
61+
}
62+
63+
public function getPluginUsage($pluginName) {
64+
return PluginUsage::wherePlugin($pluginName)->count();
65+
}
66+
2367
public function stats() {
2468
$totalServers = Server::count();
2569
$onlineServers = Server::whereOnline(true)->count();

app/Http/Controllers/ServerController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
class ServerController extends Controller {
1010

11-
//http://regexr.com/3d8n1
12-
const SERVER_REGEX = "/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/";
11+
//http://regexr.com/3ddc0
12+
const SERVER_REGEX = "/^(([\w-]+\.)?([\w-]+\.)?[\w-]+\.\w+|((2[0-5]{2}|1[0-9]{2}|[0-9]{1,2})\.){3}(2[0-5]{2}|1[0-9]{2}|[0-9]{1,2}))?$/";
1313

1414
public function index() {
1515
$servers = Server::where('online', true)->whereNotNull('motd')->orderBy("players", "desc")->paginate(5);

app/Http/routes.php

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
Route::get('/server/{address}', 'ApiController@getServer');
3636
Route::get('/server/{address}/favicon', 'ApiController@getIcon');
3737

38+
Route::get('/plugin/{pluginName}/usage', 'ApiController@getPluginUsage');
39+
40+
Route::get('/player/{uuid}', 'ApiController@getPlayerByUUID')
41+
->where("uuid", "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
42+
Route::get('/player/{username}', 'ApiController@getPlayerByName')->where("username", "\w{2,16}");
43+
44+
//todo: rendered skin routes
45+
Route::get('/skin/{uuid}', 'ApiController@getSkinByUUID')
46+
->where("uuid", "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
47+
Route::get('/skin/{name}', 'ApiController@getSkinByName')->where("username", "\w{2,16}");
48+
3849
Route::get('/stats', 'ApiController@stats');
3950
});
4051

app/Skin.php

+13-18
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,47 @@
88
* App\Skin
99
*
1010
* @property integer $id
11-
* @property integer $timestamp
12-
* @property string $profileId
13-
* @property string $profileName
14-
* @property string $skinUrl
15-
* @property string $capeUrl
16-
* @property boolean $slimModel
11+
* @property string $profile_id
12+
* @property string $profile_name
13+
* @property string $skin_url
14+
* @property string $cape_url
15+
* @property boolean $slim_model
1716
* @property mixed $signature
17+
* @property integer $timestamp
1818
* @property \Carbon\Carbon $created_at
1919
* @property \Carbon\Carbon $updated_at
2020
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereId($value)
21-
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereTimestamp($value)
2221
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereProfileId($value)
2322
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereProfileName($value)
2423
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereSkinUrl($value)
2524
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereCapeUrl($value)
2625
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereSlimModel($value)
2726
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereSignature($value)
27+
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereTimestamp($value)
2828
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereCreatedAt($value)
2929
* @method static \Illuminate\Database\Query\Builder|\App\Skin whereUpdatedAt($value)
3030
* @mixin \Eloquent
3131
*/
3232
class Skin extends Model {
3333

34-
/**
35-
* The database table used by the model.
36-
*
37-
* @var string
38-
*/
3934
protected $table = 'skins';
4035

4136
public function getEncodedData() {
4237
$data = array();
4338
$data['timestamp'] = $this->timestamp;
44-
$data['profileId'] = str_replace("-", "", $this->profileId);
45-
$data['profileName'] = $this->profileName;
39+
$data['profileId'] = str_replace("-", "", $this->profile_id);
40+
$data['profileName'] = $this->profile_name;
4641
$data['signatureRequired'] = true;
4742

4843
$textures = array();
49-
$textures['SKIN'] = ["url" => $this->skinUrl];
44+
$textures['SKIN'] = ["url" => $this->skin_url];
5045

51-
if ($this->slimModel) {
46+
if ($this->slim_model) {
5247
$textures['SKIN']["metadata"] = ["model" => "slim"];
5348
}
5449

55-
if ($this->capeUrl) {
56-
$textures['CAPE'] = ["url" => $this->capeUrl];
50+
if ($this->cape_url) {
51+
$textures['CAPE'] = ["url" => $this->cape_url];
5752
}
5853

5954
$data['textures'] = $textures;

database/migrations/2016_04_19_175109_create_skins_table.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public function up() {
1414
Schema::create('skins', function (Blueprint $table) {
1515
$table->increments('id')->unsigned();
1616

17-
$table->uuid("profileId");
17+
$table->uuid("profile_id");
1818

1919
//due name changes the profile name could be different while we want to keep this database entry
20-
$table->string("profileName", 16);
20+
$table->string("profile_name", 16);
2121

22-
$table->string("skinUrl");
23-
$table->string("capeUrl")->nullable();
22+
$table->string("skin_url");
23+
$table->string("cape_url")->nullable();
2424

25-
$table->boolean("slimModel")->default(0);
25+
$table->boolean("slim_model")->default(0);
2626

2727
$table->binary("signature");
2828

@@ -31,8 +31,8 @@ public function up() {
3131

3232
$table->timestamps();
3333

34-
$table->index("profileId");
35-
$table->index("profileName");
34+
$table->index("profile_id");
35+
$table->index("profile_name");
3636
});
3737
}
3838

database/seeds/SkinTableSeeder.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ class SkinTableSeeder extends Seeder {
1212
public function run() {
1313
App\Skin::create([
1414
'timestamp' => 1461163718120,
15-
'profileId' => "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6",
16-
'profileName' => "Dinnerbone",
17-
'skinUrl' => "http://textures.minecraft.net/texture/"
15+
'profile_id' => "61699b2e-d327-4a01-9f1e-0ea8c3f06bc6",
16+
'profile_name' => "Dinnerbone",
17+
'skin_url' => "http://textures.minecraft.net/texture/"
1818
. "cd6be915b261643fd13621ee4e99c9e541a551d80272687a3b56183b981fb9a",
19-
'capeUrl' => "http://textures.minecraft.net/texture/"
19+
'cape_url' => "http://textures.minecraft.net/texture/"
2020
. "eec3cabfaeed5dafe61c6546297e853a547c39ec238d7c44bf4eb4a49dc1f2c0",
2121
'signature' => base64_decode("FjF5sk3ZC6mUEV9Z3QMNIFsmudgdhf2sSSy8bR/5fmAeg8df/o8nth28uk4gFADUoydhcCzB8fW2b"
2222
. "Fcr32pb9ujw0i6D/nX82mBhENjU29YZ5b7mCzdmNW2zH04tIDoF3mRLw2Arn+NovwoUJmSo4g/QVsT0GbQGIMaRpKzW4YTn8"

0 commit comments

Comments
 (0)