You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.9 KiB
PHP
62 lines
1.9 KiB
PHP
<?php
|
|
namespace Isekai\UserPoints\Api;
|
|
|
|
use ApiBase;
|
|
use Isekai\UserPoints\Service\IsekaiUserDailySignFactory;
|
|
use Isekai\UserPoints\Utils;
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
class ApiUserDailySign extends ApiBase {
|
|
public function __construct( $main, $method ) {
|
|
parent::__construct( $main->getMain(), $method );
|
|
}
|
|
|
|
public function execute() {
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$user = $this->getUser();
|
|
|
|
if (!$user->isRegistered()) {
|
|
$this->dieWithError('apierror-mustbeloggedin-generic', 'login-required');
|
|
}
|
|
|
|
/** @var IsekaiUserDailySignFactory */
|
|
$dailySignFactory = $services->getService('IsekaiUserDailySign');
|
|
$dailySignService = $dailySignFactory->newFromUser($user);
|
|
|
|
$result = $this->getResult();
|
|
if (date('Y-m-d') === date('Y-m-d', strtotime($dailySignService->lastSignDate))) {
|
|
$result->addValue(['userdailysign'], 'success', 0);
|
|
$result->addValue(['userdailysign'], 'point_delta', []);
|
|
$this->addWarning('isekai-userpoints-already-signed-today', 'already-signed-today');
|
|
return;
|
|
}
|
|
|
|
$pointDelta = $dailySignService->doSign();
|
|
|
|
if (!empty($pointDelta)) {
|
|
$result->addValue(['userdailysign'], 'success', 1);
|
|
} else {
|
|
$result->addValue(['userdailysign'], 'success', 0);
|
|
}
|
|
|
|
$resultPointDelta = [];
|
|
foreach ($pointDelta as $pointType => $delta) {
|
|
$pointName = Utils::getPointName($pointType);
|
|
$pointIcon = Utils::getPointIcon($pointType);
|
|
|
|
$resultPointDelta[] = [
|
|
'point_type' => $pointType,
|
|
'name' => $pointName,
|
|
'icon' => $pointIcon,
|
|
'points' => $delta,
|
|
];
|
|
}
|
|
|
|
$result->addValue(['userdailysign'], 'point_delta', $resultPointDelta);
|
|
}
|
|
|
|
public function needsToken() {
|
|
return 'csrf';
|
|
}
|
|
} |