name = $enemy_definition->name; $this->id = intval( $id ); $this->code = $code; $this->stats = $stats; $this->is_hostile = $is_hostile; $this->spawn_id = intval( $spawn_id ); $this->location_id = intval( $location_id ); } public function get_enemy_definition(): ?Enemy_Definition { return Enemy_Definition::get_enemy_definition_by_code( $this->code ); } public function makeHostile(): void { $this->is_hostile = true; $this->save_to_db(); } public function get_name(): string { $enemy_definition = Enemy_Definition::get_enemy_definition_by_code( $this->code ); return $enemy_definition->name; } public function get_description(): string { $enemy_definition = Enemy_Definition::get_enemy_definition_by_code( $this->code ); return $enemy_definition->description; } public static function create_enemy( $code, $stats, $is_hostile, $spawn_id, $location_id): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemies'; $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'stats' => json_encode( $stats ), 'spawn_id' => $spawn_id, 'is_hostile' => $is_hostile, 'location_id' => $location_id, ) ); } public static function get_enemy_by_id( $id ): ?Enemy { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemies'; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %s WHERE id = %d", $table_name, $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Enemy( $result['id'], $result['code'], $result['stats'], $result['is_hostile'], $result['spawn_id'], $result['location_id'] ); } /** * Get all enemies for a specific page and character. * * @param int $page_id The ID of the page. * @param int $character_id The ID of the character (for character-specific spawns). * * @return Enemy[] An array of Enemy objects. */ public static function get_enemies_for_page( $page_id, $character_id ): array { // get all enemies for this page that have a spawn point on this page and for this character $enemy_spawns = Enemy_Spawn::get_enemy_spawns_by_page_id( $page_id, $character_id ); $enemies = array(); foreach ( $enemy_spawns as $enemy_spawn ) { $enemies_for_spawn = Enemy::get_enemies_for_spawn_id( $enemy_spawn->id ); $enemies = array_merge( $enemies, $enemies_for_spawn ); } return $enemies; } public static function delete_enemy_by_id( $id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemies'; $wpdb->delete( $table_name, array( 'id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } public static function delete_enemies_for_spawn_id( $spawn_id ): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemies'; $wpdb->delete( $table_name, array( 'spawn_id' => $spawn_id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** * Get all enemies associated with a specific spawn point. * * @param int $spawn_id The ID of the spawn point. * * @return Enemy[] An array of Enemy objects. */ public static function get_enemies_for_spawn_id( $spawn_id ): array { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_enemies WHERE spawn_id = %d", $spawn_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $enemies = array(); foreach ( $results as $result ) { $enemies[] = new Enemy( $result['id'], $result['code'], json_decode($result['stats'], true), $result['is_hostile'], $result['spawn_id'], $result['location_id'] ); } return $enemies; } public function get_html(): string { $enemy_definition = $this->get_enemy_definition(); if ( $enemy_definition === null ) { return '
Unknown Enemy