...
 
......@@ -16,17 +16,33 @@ use Minds\Core\Events\Dispatcher;
class Rewards extends Cli\Controller implements Interfaces\CliControllerInterface
{
private $start;
private $elasticsearch;
public function help($command = null)
{
$this->out('Syntax usage: cli trending <type>');
switch ($command) {
case 'sync':
$this->out('Get rewards for all users');
$this->out('--timestamp={timestamp} the day to start from. Default is yesterday at midnight');
$this->out('--action={active|subscribe|jury-duty} Type of action');
$this->out('--dry-run={true|false} true prevents saving the data');
break;
case 'single':
$this->out('Get rewards for a single user');
$this->out('--guid={guid} the guid of the user to get rewards for');
$this->out('--timestamp={timestamp} the day to start from. Default is yesterday at midnight');
break;
case 'issue':
$this->out('Issue tokens to a user');
$this->out('--username={string username} username of the user to issue tokens to');
$this->out('--amount={number of tokens} Number of tokens to issue');
default:
$this->out('Syntax usage: cli rewards <type>');
$this->displayCommandHelp();
}
}
public function exec()
{
return $this->help();
}
public function sync()
......@@ -34,9 +50,13 @@ class Rewards extends Cli\Controller implements Interfaces\CliControllerInterfac
error_reporting(E_ALL);
ini_set('display_errors', 1);
$timestamp = $this->getOpt('timestamp') ?: (strtotime('midnight -24 hours') * 1000);
$to = strtotime("+24 hours", $timestamp / 1000) * 1000;
$dryRun = $this->getOpt('dry-run') === 'true';
if ($dryRun) {
$this->out('DRY RUN');
}
$users = new UsersIterator;
$users->setFrom($timestamp)
......@@ -68,7 +88,7 @@ class Rewards extends Cli\Controller implements Interfaces\CliControllerInterfac
$manager->setFrom($timestamp)
->setTo($to);
$manager->setUser($user);
$manager->setDryRun($this->getOpt('dry-run'));
$manager->setDryRun($dryRun);
$reward = $manager->sync();
$total = $total->add($reward->getAmount());
$leaderboard[$user->guid] = ($reward->getAmount() / (10**18));
......
......@@ -6,17 +6,26 @@ namespace Minds\Core\Rewards\Contributions;
use Minds\Core\Analytics;
use Minds\Core\Util\BigNumber;
use Minds\Entities\User;
class Manager
{
/** @var Analytics\Manager */
protected $analytics;
/** @var Repository */
protected $repository;
/** @var User */
protected $user;
/** @var int */
protected $from;
/** @var int */
protected $to;
/** @var bool */
protected $dryRun = false;
protected $site_contribtion_score_cache = [];
/** @var array */
protected $site_contribution_score_cache = [];
/** @var Sums */
protected $sums;
public function __construct($analytics = null, $repository = null, $sums = null)
{
......@@ -27,7 +36,7 @@ class Manager
$this->to = time() * 1000;
}
public function setUser($user)
public function setUser($user): self
{
$this->user = $user;
return $this;
......@@ -39,25 +48,25 @@ class Manager
* @param bool $dryRun
* @return $this
*/
public function setDryRun($dryRun)
public function setDryRun($dryRun): self
{
$this->dryRun = $dryRun;
return $this;
}
public function setFrom($from)
public function setFrom($from): self
{
$this->from = $from;
return $this;
}
public function setTo($to)
public function setTo($to): self
{
$this->to = $to;
return $this;
}
public function sync()
public function sync(): array
{
$this->analytics
->setFrom($this->from)
......@@ -73,10 +82,11 @@ class Manager
foreach($this->analytics->getCounts() as $ts => $data) {
foreach($data as $metric => $count) {
$multiplier = ContributionValues::$multipliers[$metric];
$userStateMultiplier = Analytics\UserStates\RewardFactor::getForUserState($this->user->getUserState());
$contribution = new Contribution();
$contribution->setMetric($metric)
->setTimestamp($ts)
->setScore($count * $multiplier)
->setScore($count * $multiplier * $userStateMultiplier)
->setAmount($count);
if ($this->user) {
......@@ -86,7 +96,6 @@ class Manager
}
}
if ($this->dryRun) {
return $contributions;
}
......@@ -101,12 +110,12 @@ class Manager
* @param Contribution $contribution
* @return bool
*/
public function add(Contribution $contribution) : bool
public function add(Contribution $contribution): bool
{
return (bool) $this->repository->add($contribution);
}
public function issueCheckins($count)
public function issueCheckins($count): void
{
$multiplier = ContributionValues::$multipliers['checkin'];
$contribution = new Contribution();
......@@ -122,12 +131,12 @@ class Manager
/**
* Gather the entire site contribution score
*/
public function getSiteContribtionScore()
public function getSiteContributionScore()
{
if (isset($this->site_contribtion_score_cache[$this->from])) {
return $this->site_contribtion_score_cache[$this->from];
if (isset($this->site_contribution_score_cache[$this->from])) {
return $this->site_contribution_score_cache[$this->from];
}
return $this->site_contribtion_score_cache[$this->from] = $this->sums
return $this->site_contribution_score_cache[$this->from] = $this->sums
->setTimestamp($this->from)
->setUser(null)
->getScore();
......@@ -137,7 +146,7 @@ class Manager
* Gather the contribution score for the user
* @return int
*/
public function getUserContributionScore()
public function getUserContributionScore(): int
{
return $this->sums
->setTimestamp($this->from)
......@@ -149,7 +158,7 @@ class Manager
* Return the number of tokens to be rewarded
* @return string
*/
public function getRewardsAmount()
public function getRewardsAmount(): string
{
//$share = BigNumber::_($this->getUserContributionScore(), 18)->div($this->getSiteContribtionScore());
//$pool = BigNumber::toPlain('100000000', 18)->div(15)->div(365);
......
......@@ -99,7 +99,7 @@ class Overview
$this->manager->setUser($this->user);
$this->manager->setFrom($timestamp);
$this->totalNetworkContribution = $this->manager->getSiteContribtionScore();
$this->totalNetworkContribution = $this->manager->getSiteContributionScore();
$this->yourContribution = $this->manager->getUserContributionScore();
$this->currentReward = $this->manager->getRewardsAmount();
......
......@@ -8,6 +8,7 @@ date_default_timezone_set('UTC');
$minds = new Minds\Core\Minds();
$minds->loadLegacy();
$minds->loadConfigs();
$CONFIG = Minds\Core\Di\Di::_()->get('Config');
$CONFIG->default_access = 2;
......