Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
xxFLORII committed Jan 19, 2025
0 parents commit 6492b3f
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/bStats-PMMP.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/git_toolbox_blame.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/xxFLORII/bStats/Chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace xxFLORII\bStats;

abstract class Chart {

/** @var string */
private $id;

public function __construct(string $id) {
$this->id = $id;
}

/**
* @return string
*/
public function getId(): string {
return $this->id;
}

/**
* @return mixed
*/
abstract public function getValue(): mixed;
}
105 changes: 105 additions & 0 deletions src/xxFLORII/bStats/Metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace xxFLORII\bStats;

use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\Task;
use pocketmine\utils\Internet;
use pocketmine\utils\SingletonTrait;

class Metrics {

/** @var PluginBase */
private $plugin;

/** @var string */
private $uuid;

/** @var string */
private $pluginName;

/** @var array */
private $charts = [];

/** @var int */
private $pluginId;

public function __construct(PluginBase $plugin, int $pluginId) {
$this->plugin = $plugin;
$this->pluginName = $plugin->getName();
$this->uuid = $this->generateUUID();
$this->pluginId = $pluginId;
}

/**
* @param Chart $chart
*/
public function addCustomChart(Chart $chart) {
$this->charts[] = $chart;
}

private function generateUUID(): string {
return md5($this->pluginName . time());
}

public function sendData() {
$customCharts = [];

/** @var Chart $chart */
foreach ($this->charts as $chart) {
$customCharts[] = [
"chartId" => $chart->getId(),
"data" => $chart->getValue()
];
}

$data = json_encode([
"serverUUID" => $this->uuid,
"metricsVersion" => "3.1.1-SNAPSHOT",
"service" => [
"id" => $this->pluginId,
"customCharts" => $customCharts
]
], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

if (json_last_error() !== JSON_ERROR_NONE) {
$this->plugin->getLogger()->error("JSON Error: " . json_last_error_msg());
}

$url = 'https://bstats.org/api/v2/data/bukkit';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Content-Length: " . strlen($data),
]);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

$response = curl_exec($ch);

if (curl_errno($ch)) {
$this->plugin->getLogger()->error("Error whilst sending data to bStats: " . curl_error($ch));
}

curl_close($ch);
}


public function scheduleMetricsDataSend() {
$this->plugin->getScheduler()->scheduleRepeatingTask(new class($this) extends Task {
private $metrics;

public function __construct(Metrics $metrics) {
$this->metrics = $metrics;
}

public function onRun(): void {
$this->metrics->sendData();
}
}, 20 * 60 * 10);
}
}

0 comments on commit 6492b3f

Please sign in to comment.