id = intval( $id ); $this->code = $code; $this->page_id = intval( $page_id ); $this->respawn_time = intval( $respawn_time ); $this->last_defeated = $last_defeated; $this->character_id = intval( $character_id ); $this->parent_id = intval( $parent_id ); } public function get_enemy_definition(): ?Enemy_Definition { return Enemy_Definition::get_enemy_definition_by_id( $this->id ); } public static function get_enemy_spawn_by_id( $id ): ?Enemy_Spawn { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix."adventurebook_enemy_spawns WHERE id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return new Enemy_Spawn( $result['id'], $result['code'], $result['page_id'], null, $result['respawn_time'], $result['character_id'], $result['parent_id'] ); } public static function get_enemy_spawns_by_page_id( $page_id, $character_id = 0 ): array { global $wpdb; if ( $character_id > 0 ) { $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix . "adventurebook_enemy_spawns WHERE page_id = %d AND character_id = %d", $page_id, $character_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } else { $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix . "adventurebook_enemy_spawns WHERE page_id = %d AND character_id = 0", $page_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } $enemy_spawns = array(); foreach ( $results as $result ) { $enemy_spawns[] = new Enemy_Spawn( $result['id'], $result['code'], $result['page_id'], $result['last_defeated'], $result['respawn_time'], $result['character_id'], $result['parent_id'] ); } return $enemy_spawns; } public static function create_enemy_spawn($code, $page_id, $respawn_time = 0): void { self::create_enemy_spawn_point_root( $code, $page_id, $respawn_time ); } public static function create_enemy_spawn_point_root($code, $page_id, $respawn_time = 0): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemy_spawns'; $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $code, 'page_id' => $page_id, 'respawn_time' => $respawn_time, 'last_defeated' => null, 'character_id' => 0, 'parent_id' => 0, ) ); // now create an instance of this spawn point for each existing character in the same adventurebook, with parent_id set to the id of the root spawn point // first, get the id of the newly created spawn point $spawn_id = $wpdb->insert_id; // get the adventurebook_id of the page (it's the parent id of the page) $adventurebook_id = wp_get_post_parent_id( $page_id ); // get all characters in this adventurebook $characters = Character::get_characters_by_adventurebook_id( $adventurebook_id ); foreach ( $characters as $character ) { self::create_enemy_spawn_point( $character->id, $spawn_id ); } } public static function create_enemy_spawn_point( $character_id, $parent_spawn_point_id): void { global $wpdb; // get the parent spawn point $parent_spawn_point = self::get_enemy_spawn_by_id( $parent_spawn_point_id ); if ( ! $parent_spawn_point ) { return; } $table_name = $wpdb->prefix . 'adventurebook_enemy_spawns'; // set the last_defeated to a date in the past so the enemy will spawn immediately $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'code' => $parent_spawn_point->code, 'page_id' => $parent_spawn_point->page_id, 'respawn_time' => $parent_spawn_point->respawn_time, 'last_defeated' => gmdate( 'Y-m-d H:i:s', time() - 360000 ), 'character_id' => $character_id, 'parent_id' => $parent_spawn_point_id, ) ); } public static function delete_enemy_spawn_point( $id ): void { global $wpdb; // first, get all child spawn points (instances for characters) and delete their enemies $child_spawns = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix . "adventurebook_enemy_spawns WHERE parent_id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery // remove all enemy instances associated with these spawn points foreach ( $child_spawns as $child_spawn ) { Enemy::delete_enemies_for_spawn_id( $child_spawn['id'] ); } // then remove all enemies associated with this spawn point Enemy::delete_enemies_for_spawn_id( $id ); $table_name = $wpdb->prefix . 'adventurebook_enemy_spawns'; // delete all adventurebook_enemy_spawns with parent_id = $id (child spawn points) $wpdb->delete( $table_name, array( 'parent_id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery // then delete the spawn point itself $wpdb->delete( $table_name, array( 'id' => $id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } public static function delete_enemy_spawn_points_by_character_id( $character_id ): void { global $wpdb; // remove all enemy instances associated with these spawn points $enemy_spawns = Enemy_Spawn::get_all_enemy_spawn_points( $character_id ); foreach ( $enemy_spawns as $enemy_spawn ) { Enemy::delete_enemies_for_spawn_id( $enemy_spawn->id ); } $table_name = $wpdb->prefix . 'adventurebook_enemy_spawns'; $wpdb->delete( $table_name, array( 'character_id' => $character_id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } public static function get_all_enemy_spawn_points( $character_id = 0 ): array { global $wpdb; if ( $character_id > 0 ) { $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix . "adventurebook_enemy_spawns WHERE character_id = %d", $character_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } else { $results = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix . "adventurebook_enemy_spawns WHERE character_id = 0", ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } $enemy_spawns = array(); foreach ( $results as $result ) { $enemy_spawns[] = new Enemy_Spawn( $result['id'], $result['code'], $result['page_id'], $result['last_defeated'], $result['respawn_time'], $result['character_id'], $result['parent_id'] ); } return $enemy_spawns; } /** * Check if any enemies need to be respawned for the given character. * This function should be called whenever a page is loaded or an action is taken. * * @param int $character_id The ID of the character to check for respawns. */ public static function check_for_respawns( int $character_id ): void { global $wpdb; // get all enemy spawns for this character and his current location reset them if respawn_time has passed $character = Character::get_character_by_id( $character_id ); if ( ! $character ) { return; } $enemy_spawns = Enemy_Spawn::get_enemy_spawns_by_page_id($character->location_id, $character_id); foreach ( $enemy_spawns as $enemy_spawn ) { // check if enemy instance already exists for this spawn point // if it does, skip to the next spawn point if ( $enemy_spawn->is_enemy_spawned() ) { continue; } $last_defeated = !empty($enemy_spawn->last_defeated) ? strtotime($enemy_spawn->last_defeated) : time(); $respawn_time = intval( $enemy_spawn->respawn_time ); if ( ($last_defeated + $respawn_time <= time()) && $respawn_time > 0) { // respawn the enemy now, set last_defeated NULL $wpdb->update( $wpdb->prefix . "adventurebook_enemy_spawns", array( 'last_defeated' => null ), array( 'id' => $enemy_spawn->id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery // spawn a new enemy instance for this character in this zone $enemy_definition = Enemy_Definition::get_enemy_definition_by_code( $enemy_spawn->code ); Enemy::create_enemy( $enemy_definition->code, $enemy_definition->stats, $enemy_definition->is_hostile, $enemy_spawn->id , $enemy_spawn->page_id ); } } } public function is_enemy_spawned(): bool { $existing_enemies = Enemy::get_enemies_for_spawn_id( $this->id ); // if there is at least one enemy for this spawn point, return true if ( count( $existing_enemies ) > 0 ) { return true; } // otherwise return false return false; } public function update_last_defeated(): void { global $wpdb; $now = gmdate( 'Y-m-d H:i:s' ); $wpdb->update( $wpdb->prefix . "adventurebook_enemy_spawns", array( 'last_defeated' => $now ), array( 'id' => $this->id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } }