id = $id; $this->character_id = $character_id; $this->code = $code; $this->location_id = $location_id; $this->stats = json_decode( $stats, true ); } public function get_item_definition(): ?Item_Definition { return Item_Definition::get_item_definition_by_code( $this->code ); } public function get_name(): string { $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); return $item_definition->name; } public function get_description(): string { $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); return $item_definition->description; } // get stats as associative array public function get_stats(): array { return $this->stats; } 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); } /** * Gets the complete stats of the item, including the stats from the item_definition * and any additional stats that might be stored in the item instance itself. * Stats with the same name are overwritten by the item instance stats. * * @return array */ public function get_complete_stats(): array { $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); $definition_stats = $item_definition->stats; // merge the two arrays, instance stats overwrite definition stats $complete_stats = array_merge( $definition_stats, $this->get_stats() ); ksort( $complete_stats ); return $complete_stats; } // get a single stats by it's name (key) public function get_stat( string $stat_name ) { $stats = $this->get_complete_stats(); return $stats[ $stat_name ] ?? null; } public function get_image_url(): string { $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); return $item_definition->image_url; } /** * Generates the HTML representation of the item, including its image, name, and description. * * @param mixed $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( $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->get_image_url() ? '' : ''; $content .= '
'; // Stats ermitteln if ( $adventurebook_combat_system ) { $stats = $adventurebook_combat_system->getItemStatsToShow( $item->get_complete_stats() ); } else { $stats = $item->get_complete_stats(); } if ( $item->id == 0 ) { // this is an item that exists only in a shop // Name + Toggle $content .= '
'; $content .= esc_html( $item->get_name() ); $content .= '
'; // adventurebook_item_name if ( ! empty( $stats ) ) { // Stats-Container (always visible) $content .= '
'; foreach ( $stats as $stat_name => $stat_value ) { // don't show the value, it's price is shown on the BUY button later if ( $stat_name === $adventurebook_combat_system->getItemStatNameForPrice() ) { continue; } $content .= '' . esc_html( $stat_name ) . ': ' . esc_html( $stat_value ) . ' '; } $content .= '
'; // adventurebook_item_stats } } else { // Name + Toggle $content .= '
get_name() ); if ( $count > 1 ) { $content .= ' (' . $count . 'x)'; } if ( ! empty( $stats ) ) { $content .= ' [+]'; } $content .= '
'; // adventurebook_item_name if ( ! empty( $stats ) ) { // Stats-Container (versteckt) $content .= '
'; foreach ( $stats as $stat_name => $stat_value ) { $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 ) { $content .= ' ' . esc_html( $adventurebook_combat_system->getWealthUnit() ); } $content .= '
'; } $content .= '
'; // adventurebook_item_stats } } // Beschreibung $content .= '
' . $item->get_description() . "
"; $content .= '
'; // adventurebook_item return $content; } public function get_hidden_item_info(Combat_System $combat_system, string $prefix) : string { $content = '
'; $content .= $this->get_description() ? esc_html( $this->get_description() ) . '
' : ''; $stats = $combat_system->getItemStatsToShow( $this->get_complete_stats() ); if ( ! empty( $stats ) ) { $content .= ''; } $content .= '
'; // adventurebook_item_info return $content; } public static function get_item_by_id( $id ): ?Item { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_items WHERE id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Item( $result['id'], $result['character_id'], $result['code'], $result['location_id'], $result['stats'] ); } public static function create_item( $character_id, $code, $location_id, array $stats = null ): ?Item { // check if the item definition exists $item_definition = Item_Definition::get_item_definition_by_code( $code ); if ( $item_definition === null ) { return null; } // get the stats which are copied to the item instance from the combat system $combat_system = get_combat_system_for_character_id( $character_id ); $stats_to_be_copied = $combat_system->getItemStatsToBeInstantiated(); // if no stats are given, copy the default stats from the item definition // but only those stats which are defined to be copied by the combat system // this way we avoid copying unnecessary stats like "Value" or "Description" // to the item instance in the database if( $stats === null ) { $stats = array(); foreach ( $stats_to_be_copied as $stat_name ) { if ( isset( $item_definition->stats[ $stat_name ] ) ) { $stats[ $stat_name ] = $item_definition->stats[ $stat_name ]; } } } global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_items'; $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'character_id' => $character_id, 'code' => $code, 'location_id' => $location_id, 'stats' => json_encode( $stats ), ) ); $id = intval( $wpdb->insert_id ); return new Item( $id, $character_id, $code, $location_id, json_encode( $stats ) ); } public static function delete_item( $id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_items'; $wpdb->delete( $table_name, array( 'id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } public static function get_items_by_character_id( $character_id ): array { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_items WHERE character_id = %d", $character_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $items = array(); foreach ( $results as $result ) { $items[] = new Item( $result['id'], $result['character_id'], $result['code'], $result['location_id'], $result['stats'] ); } return $items; } public static function delete_items_by_character_id( $character_id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_items'; $wpdb->delete( $table_name, array( 'character_id' => $character_id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } public function get_slot(): string { $stats = $this->get_complete_stats(); return $stats['Slot'] ?? ''; } public function set_location( int $location_id ): void { $this->location_id = $location_id; $this->save_to_db(); } public function save_to_db(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_items'; $wpdb->update( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'character_id' => $this->character_id, 'code' => $this->code, 'location_id' => $this->location_id, 'stats' => json_encode( $this->stats ), ), array( 'id' => $this->id ) ); } public function is_usable() : bool { $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); return $item_definition->is_usable(); } public function get_charges() : int { $charges = $this->get_stat('Charges'); if( !is_numeric($charges) || $charges < 0 ) { return 1; } return intval($charges); } public function get_item_slot() : string { $item_definition = Item_Definition::get_item_definition_by_code( $this->code ); return $item_definition->get_item_slot(); } /** * Compares this item with another item to determine if they are identical. * Two items are considered identical if they have the same item definition code * and the same stats (both keys and values). * * this is used for a grouped inventory display * * @param ?Item $other The other item to compare with. * @return bool True if the items are identical, false otherwise. */ public function is_identical_to_item(?Item $other) : bool { if($other == null || $this->code !== $other->code) { return false; } // compare stats $stats1 = $this->get_complete_stats(); $stats2 = $other->get_complete_stats(); if(count($stats1) !== count($stats2)) { return false; } foreach($stats1 as $key => $value) { if(!array_key_exists($key, $stats2) || $stats2[$key] !== $value) { return false; } } return true; } }