Initial commit

This commit is contained in:
Joey Kimsey
2024-08-04 21:15:59 -04:00
parent c9d1fb983f
commit 0d469501ee
695 changed files with 70184 additions and 71 deletions

View File

@ -0,0 +1,153 @@
<?php
/**
* app/plugins/dnd/models/monsters.php
*
* This class is used for the manipulation of the dnd_monsters database table.
*
* @package TTE Dungeons & Dragons
* @version 3.0
* @author Joey Kimsey <Joey@tabletopelite.com>
* @link https://TableTopElite.com
*/
namespace TheTempusProject\Models;
use TheTempusProject\Canary\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Bedrock\Classes\CustomException;
use TheTempusProject\Models\Sourcebooks as SourcebooksModel;
class Monsters extends DatabaseModel {
protected static $sourcebooks;
public $tableName = 'dnd_monsters';
public $databaseMatrix = [
// Name
[ 'name', 'varchar', '32' ],
// General
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
[ 'privacy', 'varchar', '16' ],
[ 'version', 'varchar', '5' ],
[ 'sourcebookID', 'int', '11' ],
[ 'shortDescription', 'text', '' ],
[ 'description', 'text', '' ],
[ 'image', 'text', '' ],
[ 'avatar', 'text', '' ],
// Core
[ 'experience', 'int', '12' ],
[ 'perception', 'varchar', '4' ],
[ 'armor_class', 'varchar', '4' ],
[ 'armor_type', 'varchar', '16' ],
[ 'alignment', 'varchar', '16' ],
[ 'hit_die', 'varchar', '4' ],
[ 'hit_die_count', 'int', '4' ],
[ 'hit_die_modifier', 'varchar', '4' ],
[ 'race', 'varchar', '32' ],
[ 'size', 'varchar', '32' ],
[ 'type', 'varchar', '32' ],
[ 'sub_type', 'varchar', '32' ],
[ 'challenge_rating', 'varchar', '32' ],
[ 'average_hp', 'int', '5' ],
// Skills
[ 'strength', 'int', '4' ],
[ 'dexterity', 'int', '4' ],
[ 'constitution', 'int', '4' ],
[ 'charisma', 'int', '4' ],
[ 'intelligence', 'int', '4' ],
[ 'wisdom', 'int', '4' ],
// Expository
[ 'characteristics', 'text', '' ],
[ 'special_traits', 'text', '' ],
[ 'actions', 'text', '' ],
[ 'reactions', 'text', '' ],
[ 'bonus_actions', 'text', '' ],
[ 'mythic_actions', 'text', '' ],
[ 'legendary_actions', 'text', '' ],
[ 'lair_actions', 'text', '' ],
[ 'languages', 'text', '' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
self::$sourcebooks = new SourcebooksModel;
}
public function create( $name, $privacy, $version, $sourcebookID, $description = '', $shortDescription = '' ) {
$fields = [
'name' => $name,
'privacy' => $privacy,
'version' => $version,
'sourcebookID' => $sourcebookID,
'description' => $description,
'shortDescription' => $shortDescription,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'Create' );
Debug::error( "Monster: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function update( $id, $name, $privacy, $version, $sourcebookID, $description = '', $shortDescription = '' ) {
$fields = [
'name' => $name,
'privacy' => $privacy,
'version' => $version,
'sourcebookID' => $sourcebookID,
'description' => $description,
'shortDescription' => $shortDescription,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'Update' );
Debug::error( "Monster: $id not updated: $fields" );
return false;
}
return true;
}
public function byUser( $limit = null ) {
$whereClause = ['createdBy', '=', App::$activeUser->ID];
if ( empty( $limit ) ) {
$objects = self::$db->get( $this->tableName, $whereClause );
} else {
$objects = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( !$objects->count() ) {
Debug::info( 'No Monsters found.' );
return false;
}
return $this->filter( $objects->results() );
}
public function filter( $data, $params = [] ) {
$out = [];
foreach ( $data as $instance ) {
if ( !is_object( $instance ) ) {
$instance = $data;
$end = true;
}
$sourcebook = self::$sourcebooks->findById( $instance->sourcebookID );
if ( !empty( $sourcebook ) ) {
$instance->sourcebookName = $sourcebook->name;
} else {
$instance->sourcebookName = 'Unknown';
}
$out[] = $instance;
if ( !empty( $end ) ) {
$out = $out[0];
break;
}
}
return $out;
}
}