prefix . 'adventurebook_scripts'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, commands JSON ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function create_enemy_definitions_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemy_definitions'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, is_hostile TINYINT(1) NOT NULL DEFAULT 1, can_be_attacked TINYINT(1) NOT NULL DEFAULT 1, xp_value INT(11) NOT NULL DEFAULT 0, description TEXT, stats JSON, inventory JSON, image_url VARCHAR(255) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function create_enemy_spawns_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemy_spawns'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, page_id INT(11) NOT NULL DEFAULT 0, respawn_time INT(11) NOT NULL DEFAULT 0, last_defeated TIMESTAMP NULL DEFAULT NULL, character_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, parent_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function create_enemies_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_enemies'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, location_id INT(11) NOT NULL DEFAULT 0, stats JSON, is_hostile TINYINT(1) NOT NULL DEFAULT 1, next_action VARCHAR(255) DEFAULT NULL, spawn_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } /** * in the item definitions table, we store all items that are available in the game * stats are stored in a json column * * @return void */ function create_item_definitions_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_definitions'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, description TEXT, stats JSON, image_url VARCHAR(255) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function create_item_spawns_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_item_spawns'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, page_id INT(11) NOT NULL DEFAULT 0 ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } /** * Create the items table * This function creates the table for the items * * The table has the following columns: * - id: The ID of the item * - parent_item_id: The ID of the parent item * - character_id: The ID of the character who owns the item * - location_id: The ID of the location where the item is at the moment * * @return void */ function create_items_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_items'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, code VARCHAR(255) NOT NULL, character_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, location_id INT(11) NOT NULL DEFAULT 0 ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } /** * Create the character table * This function creates the table for the characters * * The table has the following columns: * - id: The ID of the character * - user_id: The ID of the user who owns the character * - name: The name of the character * - zone: The ID of the zone the character is in * - created_at: The timestamp when the character was created * * The table also has a foreign key constraint on the user_id column * to ensure that the user exists in the users table. * * Character stats are stored in a json column * * @return void */ function create_characters_table(): void { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_characters'; // Tabelle prefix + name $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id BIGINT(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY, user_id BIGINT(20) UNSIGNED NOT NULL, adventurebook_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, name VARCHAR(255) NOT NULL, location_id BIGINT(20) NOT NULL DEFAULT 0, stats JSON, inventory JSON, next_action VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES {$wpdb->prefix}users(ID) ON DELETE CASCADE ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function adventurebook_deactivate(): void { drop_table_characters(); drop_table_items(); drop_table_item_spawns(); drop_table_item_definitions(); drop_table_enemies(); drop_table_enemy_spawns(); drop_table_enemy_definitions(); drop_table_scripts(); } function drop_table_scripts(): void { global $wpdb; $wpdb->query( "DROP TABLE IF EXISTS " . $wpdb->prefix . "adventurebook_scripts" );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_enemy_definitions(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_enemy_definitions");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_enemy_spawns(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_enemy_spawns");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_enemies(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_enemies");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_characters(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_characters");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_item_definitions(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_item_definitions");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_item_spawns(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_item_spawns");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } function drop_table_items(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."adventurebook_items");// phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** * Get all items 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 items). * * @return Item[] An array of Item entries */ function get_items_for_page(int $page_id, int $character_id): array { global $wpdb; $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."adventurebook_items WHERE character_id = %d AND location_id=%d", $character_id, $page_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; } function adventurebook_update_database_schema( $old_version ) { global $wpdb; // rename the column 'zone' to 'location_id' in the adventurebook_characters table $table_name = $wpdb->prefix . 'adventurebook_characters'; $column_exists = $wpdb->get_results( $wpdb->prepare( "SHOW COLUMNS FROM $table_name LIKE %s", 'location_id' ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( empty( $column_exists ) ) { $wpdb->query( "ALTER TABLE $table_name CHANGE zone location_id BIGINT(20) NOT NULL DEFAULT 0" );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } $table_name = $wpdb->prefix . 'adventurebook_enemy_spawns'; // we need to add the column 'parent_id' to the adventurebook_enemy_spawns table // check if the column already exists $column_exists = $wpdb->get_results( $wpdb->prepare( "SHOW COLUMNS FROM $table_name LIKE %s", 'parent_id' ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( empty( $column_exists ) ) { $wpdb->query( "ALTER TABLE $table_name ADD parent_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0" );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } // we need to add the column 'Stats' to the adventurebook_items table $table_name = $wpdb->prefix . 'adventurebook_items'; $column_exists = $wpdb->get_results( $wpdb->prepare( "SHOW COLUMNS FROM $table_name LIKE %s", 'stats' ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( empty( $column_exists ) ) { $wpdb->query( "ALTER TABLE $table_name ADD stats JSON" );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } // look in the characters table and see if the inventory JSON consists of "code:count" pairs $table_name = $wpdb->prefix . 'adventurebook_characters'; $results = $wpdb->get_results( "SELECT id, inventory FROM $table_name WHERE inventory IS NOT NULL AND inventory != ''" , ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $results as $result ) { $inventory = json_decode( $result['inventory'], true ); if ( is_array( $inventory ) ) { // check if the first element is a string with a colon if ( isset( $inventory[0] ) && is_string( $inventory[0] ) && strpos( $inventory[0], ':' ) !== false ) { // we need to convert the inventory $new_inventory = array(); foreach ( $inventory as $item ) { $parts = explode( ':', $item ); if ( count( $parts ) == 2 ) { $code = $parts[0]; $count = intval( $parts[1] ); for ( $i = 0; $i < $count; $i ++ ) { // create a new item for this code $item = Item::create_item( $result['id'], $code, 0 ); // add this item to the new inventory if ( $item ) { $new_inventory[] = $item->id; } } } } // update the character's inventory $wpdb->update( $table_name, array( 'inventory' => wp_json_encode( $new_inventory ) ), array( 'id' => $result['id'] ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } } } }