Skip to content

Instantly share code, notes, and snippets.

@fwolf
Last active September 2, 2021 08:59
Show Gist options
  • Save fwolf/6d184ce85dfbd9152af1787fcbdc26ab to your computer and use it in GitHub Desktop.
Save fwolf/6d184ce85dfbd9152af1787fcbdc26ab to your computer and use it in GitHub Desktop.
Query IOTA field score
<?php
$basename = basename(__FILE__);
if (2 > $argc) {
echo <<<TAG
Usage: php $basename Name/Id [Option]
Options:
simple Default, only output total score
full Show score detail
json Show raw json string
When use options, do not add '-', eg:
php $basename id full
Notice: 依据 api 中的次数和网站上的 Multiplier 计算总分,可能与官方结果不一致。
Tip for loop check score:
while sleep 90; do php score-query.php name; done
TAG;
exit;
}
//// Constant
$allowedOptions = ['simple', 'full', 'json'];
/** @noinspection SpellCheckingInspection */
$multipliers = [
'attachToTangle' => 50,
'broadcastTransactions' => 5,
'checkConsistency' => 5,
'findTransactions' => 5,
'getBalances' => 3,
'getInclusionStates' => 3,
'getNodeInfo' => 1,
'getTips' => 2,
'getTransactionsToApprove' => 3,
'getTrytes' => 3,
'storeTransactions' => 20,
'wereAddressesSpentFrom' => 5,
];
//$apiUrl = 'https://field.carriota.com/api/v1/graph';
$apiUrl = 'http://field.deviota.com/api/v1/graph';
// @see https://en.wikipedia.org/wiki/ANSI_escape_code for escape color
//// Argument
$queryName = $argv[1];
if (2 < $argc) {
$option = $argv[2];
} else {
$option = 'simple';
}
if (!in_array($option, $allowedOptions)) {
echo "Invalid option $option" . PHP_EOL;
exit;
}
//// Retrieve json
/** @noinspection PhpUsageOfSilenceOperatorInspection */
$apiResult = @file_get_contents($apiUrl);
if (false === $apiResult) {
echo "Query fail" . PHP_EOL;
exit;
}
$apiResultAr = json_decode($apiResult, true);
$found = false;
$name = '';
$publicId = '';
$isSynced = false;
$row = [];
$matchAr = [];
$ourScore = 0;
$totalScore = 0;
foreach ($apiResultAr as $row) {
$isSynced = $row['iri']['isSynchronized'];
$publicId = $row['field']['publicId'];
$name = $row['field']['name'];
if (empty($name)) {
$name = $publicId;
}
//// Compute score
$score = 0;
foreach ($row['workDone'] as $code => $point) {
if (!array_key_exists($code, $multipliers)) {
echo "Work $code not recognized" . PHP_EOL;
exit;
}
$score += $point * $multipliers[$code];
}
$totalScore += $score;
if ($queryName == $publicId || 1 == preg_match("/$queryName/i", $name)) {
$found = true;
$matchAr[$name] = [
'isSynced' => $isSynced,
'score' => $score,
];
$ourScore += $score;
}
}
if (!$found) {
echo "Name or id not found" . PHP_EOL;
exit;
}
//// Sort result and print
uasort($matchAr, function ($a, $b) {
return ($a['score'] > $b['score']) ? -1 : 1;
});
foreach ($matchAr as $name => $row) {
$isSynced = $row['isSynced'];
$score = $row['score'];
$syncStatus = ($isSynced ? ' S' : 'xS') . "\e[0m";
$time = str_replace('T', ' ', date(DateTimeInterface::W3C)); // Eye handy
$time = substr($time, 0, 19); // Remove timezone
$output = sprintf(
"\033[1;92;40m%6s\033[0m %s \e[0;92m%s\e[0m",
$score,
$syncStatus,
$name
);
echo "[$time] $output" . PHP_EOL;
}
$percent = round(($ourScore / $totalScore) * 100, 1);
$ourScore = number_format($ourScore);
$totalScore = number_format($totalScore);
echo "全网总分为 {$totalScore},我们总分为 {$ourScore},占 $percent%。" .
PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment