Skip to content

Commit

Permalink
Merge pull request #1 from VictorWesterlund/feature/backend
Browse files Browse the repository at this point in the history
Merge "feature/backend"
  • Loading branch information
VictorWesterlund committed Nov 18, 2020
2 parents ed7cd20 + c2fa933 commit 8798340
Show file tree
Hide file tree
Showing 34 changed files with 184 additions and 7 deletions.
Binary file removed backend/background.png
Binary file not shown.
36 changes: 36 additions & 0 deletions backend/classes/Crafatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

// Get Crafatar from API
class Crafatar {

public function __construct($username,$scale) {
$this->uuid = $this->getPlayerUUID($username);
$this->url = "https://crafatar.com/renders/body/{$this->uuid}?scale={$scale}";

$this->data = $this->curl();
$this->image = imagecreatefromstring($this->data);
$this->size = [imagesx($this->image),imagesy($this->image)];
}

// Get player UUID from username
private function getPlayerUUID($username) {
$url = "https://playerdb.co/api/player/minecraft/";

$api = json_decode(file_get_contents($url.$username));
return $api->data->player->id;
}

private function curl() {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.

$data = curl_exec($curl);
curl_close($curl);

return $data;
}

}
27 changes: 27 additions & 0 deletions backend/classes/Minotar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

// Get Minotar from API
class Minotar {

public function __construct($username,$size) {
$this->url = "https://minotar.net/armor/bust/{$username}/{$size}.png";

$this->data = $this->curl();
$this->image = imagecreatefromstring($this->data);
$this->size = [imagesx($this->image),imagesy($this->image)];
}

private function curl() {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.

$data = curl_exec($curl);
curl_close($curl);

return $data;
}

}
44 changes: 44 additions & 0 deletions backend/classes/TextLayer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

// Add horizontally centered text at x,y
class TextLayer {

public function __construct($text,$font,$size) {
$this->text = $text;
$this->font = $font;

$this->size = $this->scaleFont($text,$size);
$this->setFontPath();
}

// Prevent default GD font lookup path from being used
private function setFontPath() {
putenv("GDFONTPATH=".realpath("./".$this->font));
}

// Calculate scaled front size from max-width
private function scaleFont($text,$baseline) {
$len = strlen($this->text);

$log = $len / 0.6;
$size = $baseline - $log;

return $size;
}

// Center text
private function offsetX($x) {
$len = strlen($this->text);

$offset = $x - ($len * ($this->size / 2.2));

return $offset;
}

// Image resource, x, y
public function textcopy($image,$x,$y,$color) {
imagettftext($image,$this->size,0,$this->offsetX($x),$y,$color,"font",$this->text);
return true;
}

}
Empty file removed backend/render.php
Empty file.
Empty file removed backend/servers.json
Empty file.
Binary file added backend/servers/us.mineplex.com/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/font.ttf
Binary file not shown.
Binary file added backend/servers/us.mineplex.com/tags/admin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/eternal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/hero.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/immortal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/leader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/legend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/mod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/owner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/srmod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/titan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/trainee.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/servers/us.mineplex.com/tags/ultra.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed backend/tags/us.mineplex.com/rank_admin.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_eternal.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_hero.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_immortal.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_leader.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_legend.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_mod.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_owner.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_srmod.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_titan.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_trainee.png
Binary file not shown.
Binary file removed backend/tags/us.mineplex.com/rank_ultra.png
Diff not rendered.
69 changes: 69 additions & 0 deletions backend/texture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

require "classes/Minotar.php"; // Player avatar
require "classes/TextLayer.php";

// Main class
class Texture {

public function __construct() {
$this->input = [
"server" => $_GET["server"],
"username" => $_GET["username"],
"tag" => $_GET["tag"].".png"
];

$this->path = "servers/{$this->input["server"]}/";

$this->validate();
}

// Make sure all required urlparams are set
private function validate() {
foreach($this->input as $key => $value) {
if(!$value) {
$error[] = "Missing urlparam: '{$key}'.";
}
}

if($error) {
http_response_code("400 Bad Request");
echo implode(" ",$error);
die();
}

return true;
}

// Create a new texture
function create($x,$y) {
// Layers
$bg = imagecreatefrompng($this->path."bg.png");
$player = new Minotar($this->input["username"],100);
$name = new TextLayer($this->input["username"],$this->path,30);
$tag = imagecreatefrompng($this->path."tags/".$this->input["tag"]);

// Create texture
$image = imagecreatetruecolor($x,$y);
$alpha = imagecolorallocatealpha($image,0,0,0,127);
$color = imagecolorallocate($image,255,255,255);

// Preserve transparency
imagefill($image,0,0,$alpha);
imagesavealpha($image,true);

// Blend layers
imagecopy($image,$bg,0,0,0,0,$x,$y); // Background
imagecopy($image,$player->image,45,60,0,0,$player->size[0],$player->size[1]); // Player avatar
$name->textcopy($image,110,210,$color); // Player name
imagecopy($image,$tag,0,0,0,0,$x,$y); // Player rank

// Output image
header("Content-Type: image/png");
echo imagepng($image);;
}

}

$texture = new Texture();
$texture->create(355,275);
15 changes: 8 additions & 7 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Chattycape(Thread):

pollRate = 1 # Logfile pollrate
updateRate = 1 # Update rate of Labylib cosmetic
updateRate = 15 # Update rate of Labylib cosmetic

def __init__(self,event,phpsessid,endpoint,logfile,me = "None"):
Thread.__init__(self)
Expand All @@ -23,7 +23,7 @@ def __init__(self,event,phpsessid,endpoint,logfile,me = "None"):
self.server = None # Connected to server (hostname)
self.config = None # Chat config for current server

self.line = "[21:42:25] [main/INFO]: [CHAT] 725 IMMORTAL VicW test"
self.line = ""
self.i = 0

# Read last line from logfile
Expand Down Expand Up @@ -52,7 +52,7 @@ def getUser(self):
continue

if substr in tagList:
tag = substr
tag = substr.lower()
continue

# Not ignored, and not a tag, so assume we've reached the username
Expand All @@ -65,7 +65,7 @@ def getUser(self):
def update(self):
args = self.getUser() # Get minecraft username and tag

urlparams = f"?server={self.server}&ign={args[0]}&rank={args[1]}"
urlparams = f"?server={self.server}&username={args[0]}&tag={args[1]}"

# Save cape texture from endpoint to disk
with open(".cache.png","wb") as handle:
Expand All @@ -80,7 +80,8 @@ def update(self):

handle.write(block)

# TODO: Labylib here
cape = Cape.Texture(self.params[0],".cache.png")
cape.update()

# ----------------------------

Expand Down Expand Up @@ -118,7 +119,7 @@ def __init__(self):
print("-- Labylib Chattycape --")
self.logfile = self.locate()

self.endpoint = self.prompt("Cape render endpoint","http://192.168.86.21/victor-westerlund/labylib-chattycape/back-end/render.php")
self.endpoint = self.prompt("Cape render endpoint","https://api.victorwesterlund.com/chattycape")
self.me = self.prompt("My Minecraft in-game name (Case Sensitive)","Don't exclude me")
self.phpsessid = self.prompt("PHPSESSID cookie")

Expand Down Expand Up @@ -168,7 +169,7 @@ def locate(self):

# Failed to locate Minecraft-installation automatically

path = self.prompt("Path to your '.minecraft'-folder","/mnt/c/Users/victo/AppData/Roaming/.minecraft")
path = self.prompt("Path to your '.minecraft'-folder")

if(Path(path).exists()):
return path + mclog
Expand Down

0 comments on commit 8798340

Please sign in to comment.