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 ) $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, zone 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 entries in the form: 'item' => Item, 'count' => int */ function get_items_for_page($page_id, $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']); } // generate an array with item and count $items_counted = array(); foreach ($items as $item) { if (isset($items_counted[$item->code])) { $items_counted[$item->code]['count']++; } else { $items_counted[$item->code] = array('item' => $item, 'count' => 1); } } // sort by item name usort($items_counted, function ($a, $b) { $item_a = Item_Definition::get_item_definition_by_code($a['item']->code); $item_b = Item_Definition::get_item_definition_by_code($b['item']->code); if ($item_a && $item_b) { return strcmp($item_a->name, $item_b->name); } return 0; }); return array_values($items_counted); }