id = $id; $this->character_id = $character_id; $this->code = $code; $this->location_id = $location_id; } 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 { $item_definition = Item_Definition::get_item_definition_by_code($this->code); // return the stats sorted by key ksort($item_definition->stats); return $item_definition->stats; } // get a single stats by it's name (key) public function get_stat(string $stat_name) { $stats = $this->get_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_stats()); } else { $stats = $item->get_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 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']); } public static function create_item($character_id, $code, $location_id): void { // check if the item definition exists $item_definition = Item_Definition::get_item_definition_by_code($code); if ($item_definition === null) { return; } 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, )); } 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']); } 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 getSlot(): string { $stats = $this->get_stats(); return $stats['Slot'] ?? ''; } }