Skip to content

Commit

Permalink
api: fixed and enhanced top money sorting, closes #412
Browse files Browse the repository at this point in the history
* Tested with over 800,000 items and had no TPS degradation
  • Loading branch information
onebone committed Feb 1, 2020
1 parent b487e6f commit 5e278b9
Show file tree
Hide file tree
Showing 21 changed files with 213 additions and 152 deletions.
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_ch.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"player-not-connected": "玩家 %1 当前不在服务器上",
"player-never-connected": "服务器中不存在玩家 %1",
"topmoney-tag": "- 查看服务器金钱排行 (%1 of %2) -",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "付款失败",
"pay-success": "付款 %$1 给 %2",
"money-paid": "%1 付款给你 %$2",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"request-cancelled": "Zadost byla zrusena",
"player-not-connected": "Hrac %1 neni na serveru",
"player-never-connected": "Hrac %1 nikdy na tomto serveru nehral",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "Platba nebyla uspesna.",
"pay-success": "Zaplaceno %$1 hraci %2.",
"money-paid": "%1 ti daroval %$2.",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_de.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"player-not-connected": "%1 ist Offline.",
"player-never-connected": "%1 war noch nie auf diesem Server. Bitte überprüfe deine Rechtschreibung.",
"topmoney-tag": "> Topliste (%1/%2) <",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "Oops bei der Überweisung ist ein Fehler aufgetreten",
"pay-success": "Du hast %$1 an %2 überwiesen.",
"money-paid": "%1 hat dir %$2 überwiesen.",
Expand Down
3 changes: 2 additions & 1 deletion EconomyAPI/resources/lang_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"currency-unavailable": "Currency %1 is not available.",
"account-created": "Account created for player %1 with currency %2(%3)",
"topmoney-tag": "- Showing top money list (%1 of %2) -",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"topmoney-failed": "Fetching leaderboard has failed.",
"pay-ask-title": "Payment",
"pay-ask-content": "Payee: &l&b%1&r&f\nAmount: &l&6%$2&r&f\n\nAre you really sure you want to pay?",
"pay-cancel": "Cancel",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"request-cancelled": "Votre requète a été annulee",
"player-not-connected": "%1 n'est pas connecté au serveur",
"player-never-connected": "Le joueur %1 ne s'est jamais connecté sur ce serveur",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "Erreur lors du payement.",
"pay-success": "Vous avez donné %$1 à %2.",
"money-paid": "%1 vous a donné %$2.",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_id.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"request-cancelled": "Permintaan Anda dibatalkan",
"player-not-connected": "Pemain %1 tidak dalam server",
"player-never-connected": "Pemain%1 tidak pernah terlihat di server ini",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "Gagal bayar.",
"pay-success": "Telah membayar %$1 ke %2.",
"takemoney-must-be-number": "Jumlah harus berupa angka",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_it.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"request-cancelled": "La tua richiesta e stata cancellata",
"player-not-connected": "IL player %1 non e online nel server",
"player-never-connected": "Il player %1 non esiste",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"takemoney-must-be-number": "Devi specificare una cifra valida",
"takemoney-invalid-number": "Numero non valido",
"takemoney-player-lack-of-money": "%1 non ha %$2. Soldi di %1 : %$3",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"player-not-connected": "%1はオフラインです。",
"player-never-connected": "%1は存在しません。",
"topmoney-tag": "- トップランキングを表示 (%2の%1ページ) -",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "支払いに失敗しました。",
"pay-success": "%2に%$1支払いました。",
"money-paid": "%1があなたに%$2支払いました。",
Expand Down
3 changes: 2 additions & 1 deletion EconomyAPI/resources/lang_ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"player-never-connected": "%1님은 서버에 접속한 적이 없습니다.",
"currency-unavailable": "화폐 %1은(는) 사용이 불가능합니다.",
"topmoney-tag": "- 돈 순위를 표시합니다 (%1 / %2 페이지) -",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"topmoney-failed": "돈 순위를 얻어오는 데 실패했습니다.",
"pay-ask-title": "송금",
"pay-ask-content": "수취인: &l&b%1&r&f\n금액: &l&6%$2&r&f\n\n정말로 송금하시겠습니까?",
"pay-confirm": "송금",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"request-cancelled": "jou aanvraag is geannuleerd",
"player-not-connected": "speler %1 zit niet op de server",
"player-never-connected": "speler %1 is nog nooit op de server geweest",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "kan niet betalen.",
"pay-success": "%$1 betaald aan %2.",
"money-paid": "%1 geeft jou %$2.",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"request-cancelled": "Запрос отменен",
"player-not-connected": "Игрок %1 оффлайн",
"player-never-connected": "Игрок %1 никогда не был на сервере",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "Не удалось передать деньги",
"pay-success": "Игроку %2 выплачено %$1",
"takemoney-must-be-number": "Сумма должна быть числом",
Expand Down
2 changes: 1 addition & 1 deletion EconomyAPI/resources/lang_zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"player-not-connected": "玩家 %1 目前不在伺服器上",
"player-never-connected": "伺服器中不存在玩家 %1",
"topmoney-tag": "- 查看伺服器金錢排行 (%1 of %2) -",
"topmoney-format": "[%1] %2: %3",
"topmoney-format": "&b#1&f %2: &6%$3",
"pay-failed": "付款失敗",
"pay-success": "付款 %$1 給 %2",
"money-paid": "%1 付款給你 %$2",
Expand Down
8 changes: 8 additions & 0 deletions EconomyAPI/src/onebone/economyapi/EconomyAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use onebone\economyapi\event\money\MoneyChangedEvent;
use onebone\economyapi\event\money\ReduceMoneyEvent;
use onebone\economyapi\event\money\SetMoneyEvent;
use onebone\economyapi\util\Promise;
use onebone\economyapi\util\Replacer;
use onebone\economyapi\provider\DummyProvider;
use onebone\economyapi\provider\user\DummyUserProvider;
Expand Down Expand Up @@ -728,6 +729,13 @@ private function validateTransaction(Transaction $transaction, ?Issuer $issuer)
return true;
}

public function getSortByRange(Currency $currency, int $from, ?int $len = null): ?Promise {
$holder = $this->getCurrencyHolder($currency);
if($holder === null) return null;

return $holder->getProvider()->sortByRange($from, $len);
}

public function hasLanguage(string $lang): bool {
return isset($this->langList[$lang]);
}
Expand Down
32 changes: 15 additions & 17 deletions EconomyAPI/src/onebone/economyapi/command/TopMoneyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace onebone\economyapi\command;

use onebone\economyapi\EconomyAPI;
use onebone\economyapi\task\SortTask;
use onebone\economyapi\internal\CurrencyReplacer;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginCommand;

Expand All @@ -38,28 +38,26 @@ public function __construct(EconomyAPI $plugin) {
public function execute(CommandSender $sender, string $label, array $params): bool {
if(!$this->testPermission($sender)) return false;

$page = (int) array_shift($params);
$page = max(1, (int) array_shift($params));

/** @var EconomyAPI $plugin */
$plugin = $this->getPlugin();
$server = $plugin->getServer();

$banned = [];
foreach($server->getNameBans()->getEntries() as $entry) {
if($plugin->hasAccount($entry->getName())) {
$banned[] = $entry->getName();
}
}
$ops = [];
foreach($server->getOps()->getAll() as $op) {
if($plugin->hasAccount($op)) {
$ops[] = $op;
}
}
$currency = $plugin->getPlayerPreferredCurrency($sender, false);

$task = new SortTask($sender->getName(), $plugin->getAllMoney(), $plugin->getPluginConfig()->getAddOpAtRank(), $page, $ops, $banned);
$server->getAsyncPool()->submitTask($task);
$plugin->getSortByRange($currency, ($page - 1) * 5, 5)->then(function($value) use ($plugin, $currency, $sender, $page) {
$sender->sendMessage($plugin->getMessage('topmoney-tag', $sender, [$page, '&enull&f'])); // TODO: show max page

$i = 0;
foreach($value as $player => $money) {
$i++;
$sender->sendMessage($plugin->getMessage('topmoney-format', $sender, [
($page - 1) * 5 + $i, $player, new CurrencyReplacer($currency, $money)
]));
}
})->catch(function() use ($sender) {
$sender->sendMessage('Failed to fetch money leaderboard :(');
});
return true;
}
}
9 changes: 9 additions & 0 deletions EconomyAPI/src/onebone/economyapi/provider/DummyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace onebone\economyapi\provider;

use onebone\economyapi\util\Promise;

class DummyProvider implements Provider {
public function hasAccount($player): bool {
return false;
Expand Down Expand Up @@ -53,6 +55,13 @@ public function getAll(): array {
return [];
}

public function sortByRange(int $from, ?int $len): Promise {
$promise = new Promise();
$promise->reject(null);

return $promise;
}

public function getName(): string {
return "Dummy";
}
Expand Down
9 changes: 9 additions & 0 deletions EconomyAPI/src/onebone/economyapi/provider/MySQLProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use mysqli;
use onebone\economyapi\EconomyAPI;
use onebone\economyapi\task\MySQLPingTask;
use onebone\economyapi\util\Promise;
use pocketmine\Player;

class MySQLProvider implements Provider {
Expand Down Expand Up @@ -181,6 +182,14 @@ public function getAll(): array {
return $ret;
}

public function sortByRange(int $from, ?int $len): Promise {
// TODO implement here
$promise = new Promise();
$promise->reject(null);

return $promise;
}

/**
* @return string
*/
Expand Down
9 changes: 3 additions & 6 deletions EconomyAPI/src/onebone/economyapi/provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use onebone\economyapi\util\Promise;
use pocketmine\Player;

// Provider is currency-related data provider
Expand Down Expand Up @@ -88,14 +89,10 @@ public function addMoney($player, float $amount): bool;
*/
public function reduceMoney($player, float $amount): bool;

/**
* @return array
*/
public function getAll(): array;

/**
* @return string
*/
public function sortByRange(int $from, ?int $len): Promise;

public function getName(): string;

public function save();
Expand Down
11 changes: 11 additions & 0 deletions EconomyAPI/src/onebone/economyapi/provider/YamlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
namespace onebone\economyapi\provider;

use onebone\economyapi\EconomyAPI;
use onebone\economyapi\task\YamlSortTask;
use onebone\economyapi\util\Promise;
use pocketmine\Player;
use pocketmine\utils\Config;

Expand Down Expand Up @@ -135,6 +137,15 @@ public function getAll(): array {
return isset($this->money["money"]) ? $this->money["money"] : [];
}

public function sortByRange(int $from, ?int $len): Promise {
$promise = new Promise();
$task = new YamlSortTask($promise, $this->money['money'], $from, $len);

$this->plugin->getServer()->getAsyncPool()->submitTask($task);

return $promise;
}

public function getName(): string {
return "Yaml";
}
Expand Down
117 changes: 0 additions & 117 deletions EconomyAPI/src/onebone/economyapi/task/SortTask.php

This file was deleted.

Loading

0 comments on commit 5e278b9

Please sign in to comment.