Files
thetempusproject/app/plugins/dnd/models/characters.php
2024-08-09 01:05:20 -04:00

298 lines
10 KiB
PHP

<?php
/**
* app/plugins/dnd/models/characters.php
*
* This class is used for the manipulation of the dnd_characters database table.
*
* @package TTE Dungeons & Dragons
* @version 3.0
* @author Joey Kimsey <Joey@tabletopelite.com>
* @link https://TableTopElite.com
*/
namespace TheTempusProject\Models;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Bedrock\Functions\Date;
use TheTempusProject\Canary\Bin\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Houdini\Classes\Filters;
use TheTempusProject\Bedrock\Classes\CustomException;
class Characters extends DatabaseModel {
public $tableName = 'dnd_characters';
public $databaseMatrix = [
// Name
[ 'prefix', 'varchar', '128' ],
[ 'first_name', 'varchar', '128' ],
[ 'middle_name', 'varchar', '128' ],
[ 'last_name', 'varchar', '128' ],
[ 'suffix', 'varchar', '128' ],
// General
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
[ 'privacy', 'varchar', '16' ],
[ 'notes', 'text', '' ], // campaign notes
[ 'version', 'varchar', '5' ],
[ 'image', 'text', '' ],
[ 'avatar', 'text', '' ],
// allignment
[ 'morality', 'varchar', '16' ], // good, neutral, evil
[ 'order', 'varchar', '16' ], // lawful, neutral, chaotic
// Appearance
[ 'age', 'int', '4' ],
[ 'height', 'varchar', '32' ],
[ 'weight', 'varchar', '32' ],
[ 'eyes', 'varchar', '32' ],
[ 'skin', 'varchar', '32' ],
[ 'hair', 'varchar', '32' ],
[ 'gender', 'varchar', '32' ],
// Core
[ 'raceID', 'int', '11' ],
[ 'classID', 'int', '11' ],
[ 'hitpoints', 'int', '5' ],
[ 'current_hitpoints', 'int', '5' ],
[ 'level', 'int', '4' ],
[ 'experience', 'int', '12' ],
// Skills
[ 'strength', 'int', '4' ],
[ 'dexterity', 'int', '4' ],
[ 'constitution', 'int', '4' ],
[ 'charisma', 'int', '4' ],
[ 'intelligence', 'int', '4' ],
[ 'wisdom', 'int', '4' ],
// Expository
[ 'story', 'text', '' ], // backstory
[ 'appearance', 'text', '' ],
[ 'allies', 'text', '' ],
[ 'enemies', 'text', '' ],
[ 'organizations', 'text', '' ],
[ 'flaws', 'text', '' ],
[ 'bonds', 'text', '' ],
[ 'oaths', 'text', '' ],
[ 'ideals', 'text', '' ],
[ 'creed', 'text', '' ],
[ 'motto', 'text', '' ],
[ 'traits', 'text', '' ], // personality traits
// Currency
[ 'copper', 'int', '12' ],
[ 'silver', 'int', '12' ],
[ 'gold', 'int', '12' ],
[ 'platinum', 'int', '12' ],
[ 'electrum', 'int', '12' ],
// [ 'armorclass', 'int', '4' ],
// [ 'initiative', 'int', '4' ],
// [ 'speed', 'int', '4' ],
// [ 'proficiency', 'int', '4' ],
// [ 'inspiration', 'int', '4' ],
// [ 'alignment', 'varchar', '16' ],
// [ 'background', 'varchar', '128' ],
// [ 'languages', 'text', '' ],
// [ 'equipment', 'text', '' ],
// [ 'features', 'text', '' ],
// [ 'spells', 'text', '' ],
// [ 'skills', 'text', '' ],
// [ 'saves', 'text', '' ],
// [ 'bonuses', 'text', '' ],
// [ 'inventory', 'text', '' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
}
public function createFromScreenOne( $first_name, $last_name, $privacy, $version, $prefix = '', $middle_name = '', $suffix = '', $avatar = '' ) {
$fields = [
// required
'first_name' => $first_name,
'last_name' => $last_name,
'privacy' => $privacy,
'version' => $version,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
// optional
'prefix' => $prefix,
'middle_name' => $middle_name,
'suffix' => $suffix,
'avatar' => $avatar,
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'characterCreate' );
Debug::error( "Character: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function updateFromScreenTwo( $id, $raceID ) {
$fields = [
'raceID' => $raceID,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $id not updated: $fields" );
return false;
}
return true;
}
public function updateFromScreenThree( $id, $classID ) {
$fields = [
'classID' => $classID,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $id not updated: $fields" );
return false;
}
return true;
}
// public function updateFromScreenFour( $id, $name ) {
// $fields = [
// 'name' => $name,
// ];
// if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
// new CustomException( 'characterUpdate' );
// Debug::error( "Character: $id not updated: $fields" );
// return false;
// }
// return true;
// }
public function updateFromScreenFive( $id, $strength, $dexterity, $constitution, $charisma, $intelligence, $wisdom ) {
$fields = [
'strength' => $strength,
'dexterity' => $dexterity,
'constitution' => $constitution,
'charisma' => $charisma,
'intelligence' => $intelligence,
'wisdom' => $wisdom,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $id not updated: $fields" );
return false;
}
return true;
}
public function updateFromScreenSix( $id, $order, $morality, $story = '', $allies = '', $enemies = '', $organizations = '', $flaws = '', $bonds = '', $ideals = '', $traits = '', $oaths = '', $motto = '', $creed = '' ) {
$fields = [
'order' => $order,
'morality' => $morality,
'story' => $story,
'allies' => $allies,
'enemies' => $enemies,
'organizations' => $organizations,
'flaws' => $flaws,
'bonds' => $bonds,
'ideals' => $ideals,
'traits' => $traits,
'oaths' => $oaths,
'motto' => $motto,
'creed' => $creed,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $id not updated: $fields" );
return false;
}
return true;
}
public function updateFromScreenSeven( $id, $age = '', $height = '', $weight = '', $eyes = '', $skin = '', $hair = '', $gender = '', $appearance = '' ) {
$fields = [
'age' => $age,
'height' => $height,
'weight' => $weight,
'eyes' => $eyes,
'skin' => $skin,
'hair' => $hair,
'gender' => $gender,
'appearance' => $appearance,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $id not updated: $fields" );
return false;
}
return true;
}
public function updateFromScreenEight( $id, $name ) {
$fields = [
'copper' => $copper,
'silver' => $silver,
'gold' => $gold,
'platinum' => $platinum,
'electrum' => $electrum,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $id not updated: $fields" );
return false;
}
return true;
}
public function create( $name ) {
$fields = [
'name' => $name,
'createdBy' => App::$activeUser->ID,
'createdAt' => time(),
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
new CustomException( 'characterCreate' );
Debug::error( "Character: not created " . var_export($fields,true) );
return false;
}
return self::$db->lastId();
}
public function update( $id, $name ) {
$fields = [
'name' => $name,
];
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'characterUpdate' );
Debug::error( "Character: $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 Characters found.' );
return false;
}
return $this->filter( $objects->results() );
}
// inventory() - items
// spells() - spells
// skills() - skills
// saves() - calculate all the various saving throws
// bonuses() - calculate the various bonuses such as to initiative and AC etc
}