* @link https://TheTempusProject.com * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Models; use TheTempusProject\Bedrock\Classes\Config; use TheTempusProject\Bedrock\Functions\Check; use TheTempusProject\Canary\Canary as Debug; use TheTempusProject\Classes\DatabaseModel; use TheTempusProject\Plugins\Tablefinder as Plugin; class Players extends DatabaseModel { public $tableName = 'players'; public $databaseMatrix = [ [ 'userID', 'int', '10' ], [ 'tableID', 'int', '10' ], [ 'characterID', 'int', '10' ], [ 'applicationDate', 'int', '10' ], [ 'rejectionDate', 'int', '10' ], [ 'joinDate', 'int', '10' ], [ 'leaveDate', 'int', '10' ], ]; public $plugin; /** * The model constructor. */ public function __construct() { parent::__construct(); $this->plugin = new Plugin; } /** * Adds a player to the db. * * @return bool */ public function create( $userID, $tableID, $type = 'apply', $characterID = 0 ) { if ( !$this->plugin->checkEnabled() ) { Debug::info( 'TableFinder is disabled in the config.' ); return false; } $fields = [ 'userID' => $userID, 'tableID' => $tableID, ]; if ( ! empty( $characterID ) ) { $fields['characterID'] = $characterID; } if ( 'apply' == $type ) { $fields['applicationDate'] = time(); } else { $fields['joinDate'] = time(); } if ( !self::$db->insert( $this->tableName, $fields ) ) { Debug::info( 'TableFinder::create - failed to insert to db' ); return false; } return self::$db->lastId(); } public function listPlayersByUser( $userID, $type = 'applied' ) { if ( !$this->plugin->checkEnabled() ) { Debug::info( 'TableFinder is disabled in the config.' ); return false; } switch ($type) { case 'applied': $type = 'applicationDate'; break; case 'denied': $type = 'rejectionDate'; break; case 'joined': $type = 'joinDate'; break; case 'left': $type = 'leaveDate'; break; default: // simple way to not limit the results $type = 'id'; break; } $terms = [ 'userID', '=', $userID, 'AND', $type, '>', 0 ]; $results = self::$db->get( $this->tableName, $terms ); if ( $results->count() == 0 ) { Debug::info( 'Failed to mark message as read.' ); return false; } return $results; } public function listPlayersByTable( $tableID, $type = 'applied' ) { if ( !$this->plugin->checkEnabled() ) { Debug::info( 'TableFinder is disabled in the config.' ); return false; } switch ($type) { case 'applied': $type = 'applicationDate'; break; case 'denied': $type = 'rejectionDate'; break; case 'joined': $type = 'joinDate'; break; case 'left': $type = 'leaveDate'; break; default: // simple way to not limit the results $type = 'id'; break; } $terms = [ 'tableID', '=', $tableID, 'AND', $type, '>', 0 ]; $results = self::$db->get( $this->tableName, $terms ); if ( $results->count() == 0 ) { Debug::info( 'Failed to mark message as read.' ); return false; } return $results; } }