3 namespace Org\Mxchange\CoreFramework\User\Points;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Criteria\Storing\StoreableCriteria;
8 use Org\Mxchange\CoreFramework\Factory\Database\Frontend\DatabaseFrontendFactory;
9 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Traits\Manager\Account\ManageableAccountTrait;
13 use Org\Mxchange\CoreFramework\Traits\Result\Search\SearchableResultTrait;
14 use Org\Mxchange\CoreFramework\User\ManageableAccount;
17 * A class for handling user points which can be real or Internet currency
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class UserPoints extends BaseFrameworkSystem implements Registerable, BookablePoints {
40 use ManageableAccountTrait;
41 use SearchableResultTrait;
49 * Protected constructor
53 private function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
59 * Creates an instance of this points class
61 * @param $userInstance An instance of a user class
62 * @return $pointsInstance An instance of this class
64 public static final function createUserPoints (ManageableAccount $userInstance) {
66 $pointsInstance = new UserPoints();
69 $pointsInstance->setUserInstance($userInstance);
71 // Get a critieria instance
72 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
74 // Add search criteria
75 $searchInstance->addCriteria(UserPointsDatabaseFrontend::DB_COLUMN_POINTS_UID, $userInstance->getUserId());
76 $searchInstance->setLimit(1);
78 // Get a frontend instance
79 $frontendInstance = DatabaseFrontendFactory::createFrontendByConfiguredName('user_points_db_frontend_class');
82 $resultInstance = $frontendInstance->doSelectByCriteria($searchInstance);
84 // Advance to first entry by default
85 $resultInstance->next();
87 // Set it in this instance
88 $pointsInstance->setResultInstance($resultInstance);
91 return $pointsInstance;
97 * @param $amount Amount of points to store
100 public final function setAmount (float $amount) {
101 $this->amount = (float) $amount;
107 * @return $amount Amount of points to store
109 public final function getAmount () {
110 return $this->amount;
114 * Checks whether the user has the required amount of points left for the specified action
116 * @param $action The action or configuration entry plus prefix the user wants to perform
117 * @return $hasRequired Whether the user has the required points
118 * @todo Finish loading part of points
120 public function ifUserHasRequiredPoints (string $action) {
121 // Default is that everyone is poor... ;-)
122 $hasRequired = false;
124 // Get the required points entry
125 $requiredPoints = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($action . '_action_points');
128 $this->getResultInstance()->rewind();
130 // Do we have an entry?
131 if ($this->getResultInstance()->valid()) {
133 $currEntry = $this->getResultInstance()->current();
135 // Has he enought points?
136 $hasRequired = ($currEntry['points'] >= $requiredPoints);
144 * "Books" the given points amount on the current user's account
146 * @param $amount Amount of points we shall book
149 public function bookPointsDirectly (float $amount) {
151 $this->getResultInstance()->rewind();
153 // Do we have an entry?
154 if ($this->getResultInstance()->valid()) {
156 $currentEntry = $this->getResultInstance()->current();
159 $amount += $currentEntry[UserPointsDatabaseFrontend::DB_COLUMN_POINTS];
161 // Now get another criteria
162 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
164 // And add our both entries
165 $updateInstance->addCriteria(UserPointsDatabaseFrontend::DB_COLUMN_POINTS, $amount);
167 // Add the search criteria for searching for the right entry
168 $updateInstance->setSearchInstance($searchInstance);
170 // Set frontend class name
171 $updateInstance->setFrontendConfigEntry('user_points_db_frontend_class');
173 // Remember the update in database result
174 $this->getResultInstance()->add2UpdateQueue($updateInstance);
176 // Set the amount in class
177 $this->setAmount($amount);
179 // Create the new entry
180 $frontendInstance->insertUserPoints($this);
185 * Adds registration elements to a given dataset instance
187 * @param $criteriaInstance An instance of a StoreableCriteria class
189 * @todo $requestInstance is currently unused
191 public function addElementsToDataSet (StoreableCriteria $criteriaInstance) {
193 $criteriaInstance->addCriteria(UserPointsDatabaseFrontend::DB_COLUMN_POINTS_UID, $this->getUserInstance()->getUserId());
196 $criteriaInstance->addCriteria(UserPointsDatabaseFrontend::DB_COLUMN_POINTS, $this->getAmount());