0, "Body" => 0, ...] * If the slot is used by an item, the value is the item_id, else it is 0 * * The inventory ID of an item is generated each time the item is added to the character's inventory. * It is not the same as the item definition ID. */ class Item_Definition { public $id; public $code; public $name; public $description; public $stats; public $image_url; public function __construct( $id, $code, $name, $description, $stats, $image_url ) { $this->id = $id; $this->code = $code; $this->name = $name; $this->description = $description; $this->stats = json_decode( $stats, true ); $this->image_url = $image_url; } public static function get_item_definition_by_id( $id ): ?Item_Definition { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_item_definitions WHERE id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Item_Definition( $result['id'], $result['code'], $result['name'], $result['description'], $result['stats'], $result['image_url'] ); } public static function get_item_definition_by_code( $code ): ?Item_Definition { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_item_definitions WHERE code = %s", $code ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Item_Definition( $result['id'], $result['code'], $result['name'], $result['description'], $result['stats'], $result['image_url'] ); } public static function get_all_item_definitions(): array { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "adventurebook_item_definitions", ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $item_definitions = array(); foreach ( $results as $result ) { $item_definitions[] = new Item_Definition( $result['id'], $result['code'], $result['name'], $result['description'], $result['stats'], $result['image_url'] ); } // sort by name usort( $item_definitions, function ( $a, $b ) { return strcmp( $a->name, $b->name ); } ); return $item_definitions; } public static function create_item_definition( $code, $name, $description, $stats, $image_url ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'name' => $name, 'description' => $description, 'stats' => json_encode( $stats ), 'image_url' => $image_url ) ); } public static function update_item_definition( $id, $code, $name, $description, $stats, $image_url ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; $wpdb->update( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'name' => $name, 'description' => $description, 'stats' => json_encode( $stats ), 'image_url' => $image_url ), array( 'id' => $id ) ); } public static function delete_item_definition( $id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; $wpdb->delete( $table_name, array( 'id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } // get stats for this item public function get_stats(): array { return $this->stats; } public function get_item_slot(): ?string { if ( isset( $this->stats['Slot'] ) && ! empty( $this->stats['Slot'] ) ) { return $this->stats['Slot']; } return null; } /** * Generates the HTML representation of the item, including its image, name, and description. * * @param Combat_System $adventurebook_combat_system The combat system to determine which stats to show. * * @return string The HTML content for the item. */ public function get_item_html( Combat_System $adventurebook_combat_system, $count = 0 ): string { $item = $this; $item_id = 'adventurebook_item_' . $item->id; // eindeutige ID für JS-Selektoren $content = '
'; // Bild $content .= '
'; $content .= $item->image_url ? '' : ''; $content .= '
'; // Stats ermitteln if ( $adventurebook_combat_system ) { $stats = $adventurebook_combat_system->getItemStatsToShow( $item->stats ); } else { $stats = $item->get_stats(); } // Name + Toggle $content .= '
name); if($count > 1) { $content .= ' ('.$count.'x)'; } $stats_to_show = 0; if (!empty($stats)) { // Stats-Container (versteckt) $stats_content = '
'; foreach ($stats as $stat_name => $stat_value) { $stats_content .= '
' . esc_html($stat_name) . ': ' . esc_html($stat_value); // if the stat is "Value", add the Wealth Unit from the Combat System if ($stat_name === 'Value' && $adventurebook_combat_system) { $stats_content .= ' ' . esc_html($adventurebook_combat_system->getWealthUnit()); } $stats_content .= '
'; $stats_to_show++; } // only show the stats expander (+) if there are stats to show if( $stats_to_show > 0 ) { $content .= ' [+]'; } } $content .= '
'; // adventurebook_item_name // now show the stats container if there are stats to show if ($stats_to_show > 0 ) { $content .= $stats_content; $content .= '
'; // adventurebook_item_stats } // Beschreibung $content .= '
' . $item->description . "
"; $content .= '
'; // adventurebook_item return $content; } public function get_stat( $stat_name ): ?string { return $this->stats[$stat_name] ?? null; } public function is_usable(): bool { return isset($this->stats['Use']) && $this->stats['Use'] !== ''; } public function get_base_price(): int { // the Value stat is multiplied by Charges or 1 if Charges is not set $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); $charges = $this->get_stat( 'Charges' ) ?? 1; if( !is_numeric($charges) || $charges < 1 ) { $charges = 1; } $base_value = $item_definition->get_stat('Value'); return intval($base_value) * intval($charges); } }