is_main_query() && $query->is_search() ) { // Deine Custom Post Types ausschließen $excluded = [ 'adventurebook', 'adventurebook_page' ]; // Falls es bereits post_type gesetzt ist, nur ergänzen if ( ! empty( $query->get( 'post_type' ) ) ) { $post_types = (array) $query->get( 'post_type' ); $post_types = array_diff( $post_types, $excluded ); $query->set( 'post_type', $post_types ); } else { // Standardmäßig auf 'post' und 'page' beschränken $query->set( 'post_type', [ 'post', 'page' ] ); } } } ); /** * Safely send HTML content in a wp_send_json_error or wp_send_json_success call. * * @param string $message The message string, can contain HTML tags. * @param array $allowed_html Optional. Allowed HTML tags. Defaults to common post tags. * @param bool $success Optional. True = wp_send_json_success, False = wp_send_json_error. */ function adventurebook_send_json_message( $message, $allowed_html = null, $success = false ) { // Default allowed tags if none provided if ( $allowed_html === null ) { $allowed_html = array( 'p' => array( 'style' => array(), 'class' => array(), ), 'a' => array( 'href' => array(), 'title' => array(), 'target' => array(), ), 'strong' => array(), 'em' => array(), 'br' => array(), ); } // Escape URLs in tags inside the message $message = wp_kses( $message, $allowed_html ); // Send the JSON response if ( $success ) { wp_send_json_success( $message ); } else { wp_send_json_error( $message ); } } /** * checks if a character can travel from one zone to another * on the from_zone page, a decision has to exist which leads to the to_zone page * if the decision has a restriction, the character must fulfill the restriction to travel * * @param $character_id * @param $from_zone_id * @param $to_zone_id * * @return bool true if the character can travel, false otherwise */ function adventurebook_can_travel_from_zone_to_zone( $character_id, $from_zone_id, $to_zone_id ): bool { global $adventurebook_id; // if the character_id is 0, the character is not logged in or this is a story without game elements and restrictions if ( $character_id == 0 ) { $decisions = get_post_meta( $from_zone_id, '_adventurebook_decision_options', true ); $decisions = is_array( $decisions ) ? $decisions : array(); foreach ( $decisions as $decision ) { if ( $decision['target'] == $to_zone_id ) { return true; } } // if the destination page is the starting page, we return true because jumping to the start is always possible $start_page_id = get_post_meta( $adventurebook_id, '_adventurebook_start_page', true ); if ( $to_zone_id == $start_page_id ) { return true; } return false; } // get the current character record $adventurebook_current_character = Character::get_character_by_id( $character_id ); $character_zone = $adventurebook_current_character->location_id; $decisions = get_post_meta( $from_zone_id, '_adventurebook_decision_options', true ); $decisions = is_array( $decisions ) ? $decisions : array(); foreach ( $decisions as $decision ) { if ( $decision['target'] == $to_zone_id ) { // check if there is a restriction for this decision if ( isset( $decision['restriction_type'] ) && isset( $decision['restriction_value'] ) && $decision['restriction_type'] != '' && $decision['restriction_value'] != '' ) { $restriction_type = $decision['restriction_type']; $restriction_value = $decision['restriction_value']; // check if the character has the restriction // restriction type: item if ( $restriction_type == 'item' ) { if( $adventurebook_current_character->has_item_with_code( $restriction_value ) ) { return true; } return false; } // restriction type: game_var if ( $restriction_type == 'game_var' ) { // the restriction value is in the form game_var_name=expected_value or game_var_nameexpected_value if ( preg_match( '/^([a-zA-Z0-9_]+)([=<>]{1})(.+)$/', $restriction_value, $matches ) ) { $var_name = $matches[1]; $operator = $matches[2]; $expected_value = $matches[3]; $actual_value = $adventurebook_current_character->get_game_var( $var_name ); if ( $operator == '=' && strval( $actual_value ) == $expected_value ) { return true; } if ( $operator == '<' && floatval( $actual_value ) < floatval( $expected_value ) ) { return true; } if ( $operator == '>' && floatval( $actual_value ) > floatval( $expected_value ) ) { return true; } return false; } else { // invalid restriction value format return false; } } // restriction type: stat if ( $restriction_type == 'stat' ) { // the restriction value is in the form stat_name=expected_value or stat_nameexpected_value if ( preg_match( '/^([a-zA-Z0-9_ ]+)([=<>]{1})(.+)$/', $restriction_value, $matches ) ) { $stat_name = $matches[1]; $operator = $matches[2]; $expected_value = $matches[3]; $stats = $adventurebook_current_character->get_stats(); $actual_value = $stats[ $stat_name ] ?? 0; if ( $operator == '=' && strval( $actual_value ) == $expected_value ) { return true; } if ( $operator == '<' && floatval( $actual_value ) < floatval( $expected_value ) ) { return true; } if ( $operator == '>' && floatval( $actual_value ) > floatval( $expected_value ) ) { return true; } return false; } else { // invalid restriction value format return false; } } // restriction type: skill if ( $restriction_type == 'skill' ) { // the restriction value is in the form skill_name=expected_value or skill_nameexpected_value if ( preg_match( '/^([a-zA-Z0-9_ ]+)([=<>]{1})(.+)$/', $restriction_value, $matches ) ) { $skill_name = $matches[1]; $operator = $matches[2]; $expected_value = $matches[3]; $actual_value = $adventurebook_current_character->get_skill( $skill_name ); if ( $operator == '=' && strval( $actual_value ) == $expected_value ) { return true; } if ( $operator == '<' && floatval( $actual_value ) < floatval( $expected_value ) ) { return true; } if ( $operator == '>' && floatval( $actual_value ) > floatval( $expected_value ) ) { return true; } return false; } else { // invalid restriction value format return false; } } } } } return true; } /** * checks if the given adventurebook_id is a game * * @param $adventurebook_id * * @return bool */ function adventurebook_is_game( $adventurebook_id ): bool { // the adventure book id is the parent of the page $adventurebook_type = get_post_meta( $adventurebook_id, '_adventurebook_type', true ); if ( $adventurebook_type === 'game' ) { return true; } else { return false; } } function adventurebook_check_ownership( $character_id ) { // check if the character is owned by the actual user if ( $character_id == 0 ) { $response = array( 'status' => 'no character id provided', ); wp_send_json( $response ); } $user_id = get_current_user_id(); $character = Character::get_character_by_id( $character_id ); if ( $character->user_id != $user_id ) { $response = array( 'status' => 'character not owned by user', ); wp_send_json( $response ); } } function adventurebook_drop_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } // get the current character id if ( isset( $_POST['character_id'] ) ) { $character_id = intval( $_POST['character_id'] ); } else { $character_id = 0; } adventurebook_check_ownership( $character_id ); // get the character $character = Character::get_character_by_id( $character_id ); // check if the required parameters are set if ( isset( $_POST['item_id'] ) ) { $item_id = sanitize_text_field( wp_unslash( $_POST['item_id'] ) ); // remove the item from the character's inventory $character->remove_item_from_inventory( $item_id); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'missing item_id', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_drop_item', 'adventurebook_drop_item_ajax' ); function adventurebook_equip_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } // get the current character id if ( isset( $_POST['character_id'] ) ) { $character_id = intval( $_POST['character_id'] ); } else { $character_id = 0; } adventurebook_check_ownership( $character_id ); // get the character $character = Character::get_character_by_id( $character_id ); // check if the required parameters are set if ( isset( $_POST['item_id'] ) ) { $item_id = intval(sanitize_text_field( wp_unslash( $_POST['item_id'] ) )); // equip the item to the character's inventory $result = $character->equip_item( $item_id ); if ( $result ) { $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'failed', ); } } else { $response = array( 'status' => 'missing item_code', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); //wp_send_json( $response ); } add_action( 'wp_ajax_adventurebook_equip_item', 'adventurebook_equip_item_ajax' ); function adventurebook_unequip_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren // get the current character id if ( isset( $_POST['character_id'] ) ) { $character_id = intval( $_POST['character_id'] ); } else { $character_id = 0; } adventurebook_check_ownership( $character_id ); // get the character $character = Character::get_character_by_id( $character_id ); // check if the required parameters are set if ( isset( $_POST['slotname'] ) ) { $slotname = sanitize_text_field( wp_unslash( $_POST['slotname'] ) ); // just unequip this slot $character->unequip_slot( $slotname ); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'missing item_code', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_unequip_item', 'adventurebook_unequip_item_ajax' ); function adventurebook_use_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren // get the current character id if ( isset( $_POST['character_id'] ) ) { $character_id = intval( $_POST['character_id'] ); } else { $character_id = 0; } // get the character $character = Character::get_character_by_id( $character_id ); adventurebook_check_ownership( $character_id ); // check if the required parameters are set if ( isset( $_POST['item_id'] ) ) { $item_id = sanitize_text_field( wp_unslash( $_POST['item_id'] ) ); // use the item from the character's inventory $output = $character->use_item( $item_id ); foreach( $output as $line ) { add_transient_message($line); } } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_use_item', 'adventurebook_use_item_ajax' ); /** * AJAX handler to take an item from the page * this will remove the item from the game, but add it's code to the character's inventory with a new unique inventory id * and an amount ("count") of 1. */ function adventurebook_take_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } if ( isset( $_POST['character_id'] ) ) { $character_id = intval( $_POST['character_id'] ); } else { $character_id = 0; } adventurebook_check_ownership( $character_id ); // check if the required parameters are set if ( isset( $_POST['item_id'] ) ) { $item_id = intval( $_POST['item_id'] ); $item = Item::get_item_by_id( $item_id ); $character = Character::get_character_by_id( $character_id ); // check if this item belongs to the character's current zone if ( $item->character_id == $character_id && $item->location_id == $character->location_id ) { // add the item to the character's inventory -> set the item_location to 0 $item->set_location(0); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'character not found', ); } } else { $response = array( 'status' => 'missing item_id', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_take_item', 'adventurebook_take_item_ajax' ); function adventurebook_buy_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } // check if the required parameters are set if ( isset( $_POST['item_id'] ) && isset( $_POST['page_id'] ) && isset( $_POST['character_id'] ) ) { $item_id = intval( $_POST['item_id'] ); $page_id = intval( $_POST['page_id'] ); $character_id = intval( $_POST['character_id'] ); adventurebook_check_ownership( $character_id ); // check if the item is for sale on this page $shop_items_codes = get_post_meta( $page_id, '_adventurebook_shop_items', true ); $shop_items_codes = is_array( $shop_items_codes ) ? $shop_items_codes : explode( ',', $shop_items_codes ); $shop_items_codes = array_map( 'trim', $shop_items_codes ); $shop_items_codes = array_filter( $shop_items_codes ); $item_definition = Item_Definition::get_item_definition_by_id( $item_id ); if ( ! $item_definition || ! in_array( $item_definition->code, $shop_items_codes ) ) { wp_send_json_error( esc_html( __( 'This item is not for sale on this page.', 'adventurebook' ) ) ); } // get the wealth stat for the character $character = Character::get_character_by_id( $character_id ); if ( ! $character ) { wp_send_json_error( esc_html( __( 'Character not found.', 'adventurebook' ) ) ); } $adventurebook_combat_system = get_combat_system_for_adventurebook( $adventurebook_id ); if ( ! $adventurebook_combat_system ) { wp_send_json_error( esc_html( __( 'Combat system not found.', 'adventurebook' ) ) ); } $character_stats = $character->get_stats(); $character_wealth = isset( $character_stats['Wealth'] ) ? intval( $character_stats['Wealth'] ) : 0; // get the item price $stats = $item_definition->get_stats(); $item_price_stat_name = $adventurebook_combat_system->getItemStatNameForPrice(); $item_price = $item_definition->get_base_price(); $shop_price_factor = get_post_meta( $page_id, '_adventurebook_shop_price_factor', true ); $shop_price_factor = floatval( $shop_price_factor ); if ( $shop_price_factor <= 0 ) { $shop_price_factor = 1.0; } $item_price = intval( $item_price * $shop_price_factor ); // check if the character has enough wealth to buy the item if ( $character_wealth < $item_price ) { status_header( 403 ); $unit = $adventurebook_combat_system->getWealthUnit(); /* translators: %s is the unit of wealth, e.g. "gold" */ wp_send_json_error( sprintf( esc_html( __( 'You do not have enough %s to buy this item.', 'adventurebook' ) ), esc_html( $unit ) ) ); } // deduct the item price from the character's wealth $character->removeWealth( $item_price ); // create a new item instance in the character's inventory Item::create_item($character_id, $item_definition->code, 0); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'missing parameters', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_buy_item', 'adventurebook_buy_item_ajax' ); function adventurebook_sell_item_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } $adventurebook_combat_system = get_combat_system_for_adventurebook( $adventurebook_id ); if ( ! $adventurebook_combat_system ) { wp_send_json_error( esc_html( __( 'Combat system not found.', 'adventurebook' ) ) ); } // check if the required parameters are set if ( isset( $_POST['item_id'] ) && isset( $_POST['page_id'] ) && isset( $_POST['character_id'] ) ) { $item_id = sanitize_text_field( wp_unslash( $_POST['item_id'] ) ); $page_id = intval( $_POST['page_id'] ); $character_id = intval( $_POST['character_id'] ); adventurebook_check_ownership( $character_id ); // check if this page is of type "Shop" // Gespeicherter Typ $page_type = get_post_meta( $page_id, '_adventurebook_page_type', true ); // Nur anzeigen, wenn Typ bereits "Shop" ist if ( $page_type !== 'shop' ) { wp_send_json_error( esc_html( __( 'Here is no shop.', 'adventurebook' ) ) ); } $character = Character::get_character_by_id( $character_id ); if ( ! $character ) { wp_send_json_error( esc_html( __( 'Character not found.', 'adventurebook' ) ) ); } // check if the item is in the character's inventory $item_count = $character->has_item_with_id($item_id); if ( ! $item_count ) { wp_send_json_error( esc_html( __( 'This item is not in your inventory.', 'adventurebook' ) ) ); } $item = Item::get_item_by_id( $item_id ); // get the item price $stats = $item->get_complete_stats(); $item_price_stat_name = $adventurebook_combat_system->getItemStatNameForPrice(); $item_price = $item->get_base_price(); // add the item price to the character's wealth $character->addWealth( $item_price ); // delete the item, thus removing it from the character's inventory and from the game Item::delete_item( $item_id ); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'missing parameters', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_sell_item', 'adventurebook_sell_item_ajax' ); /** * This function is called via AJAX when the player clicks on a link generated by the "showkeylink" script command. * It sets the current key of the script to the given key, so that the script can continue execution from that point. * The function expects the following parameters to be passed via POST: * - script_code: the code of the script to be executed * - key: the key to be set * - character_id: the id of the character * * @return void */ function adventurebook_set_key_for_script() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } // check if the required parameters are set if ( isset( $_POST['script_code'] ) && isset( $_POST['key'] ) && isset( $_POST['character_id'] ) ) { $script_code = sanitize_text_field( wp_unslash( $_POST['script_code'] ) ); $key = sanitize_text_field( wp_unslash( $_POST['key'] ) ); $character_id = intval( $_POST['character_id'] ); adventurebook_check_ownership( $character_id ); $character = Character::get_character_by_id( $character_id ); // set the current key of the script to the given key // this is done by setting a game var $character->set_game_var( "script_" . $script_code . "_key", $key ); // save the character $character->save_to_db(); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'missing parameters', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_set_key_for_script', 'adventurebook_set_key_for_script' ); function adventurebook_attack_enemy_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } // check if the required parameters are set if ( isset( $_POST['enemy_id'] ) && isset( $_POST['character_id'] ) ) { $enemy_id = intval( $_POST['enemy_id'] ); $character_id = intval( $_POST['character_id'] ); adventurebook_check_ownership( $character_id ); $character = Character::get_character_by_id( $character_id ); // this will set the next action of the character to "attack_".$enemy_id $character->setNextAction( "attack_" . $enemy_id ); // save the character $character->save_to_db(); // afterwards, we return success - the next round will be started by the frontend via calling the refresh $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'missing parameters', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_attack_enemy', 'adventurebook_attack_enemy_ajax' ); function adventurebook_respawn_ajax() { check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } else { // json error $response = array( 'status' => 'missing adventurebook_id', ); wp_send_json( $response ); return; } // get the current character id if ( isset( $_POST['character_id'] ) ) { $character_id = intval( $_POST['character_id'] ); } else { // json error $response = array( 'status' => 'missing character_id', ); wp_send_json( $response ); return; } adventurebook_check_ownership( $character_id ); // get the character $character = Character::get_character_by_id( $character_id ); if ( $character ) { // respawn the character at the start page of this adventurebook // get the start page $start_page_id = get_post_meta( $adventurebook_id, '_adventurebook_start_page', true ); $character->location_id = intval($character->get_stat( 'RespawnZone' )) ? intval($character->get_stat( 'RespawnZone' )) : $start_page_id; // clear the next action $character->setNextAction( "" ); // respawn the character $character->respawn(); $response = array( 'status' => 'success', ); } else { $response = array( 'status' => 'character not found', ); } // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } add_action( 'wp_ajax_adventurebook_respawn', 'adventurebook_respawn_ajax' ); /** * AJAX handler to get the content of an adventurebook page * This function checks if the user is logged in (if the adventurebook is a game) * and if the user has a current character selected. * It then fetches the current page of the adventurebook and returns the HTML content. * * IMPORTANT * * Each call of this function will calculate a new round for the character if the adventurebook is a game. */ function adventurebook_get_page_content_ajax() { global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_current_character, $adventurebook_id, $adventurebook_current_character_id, $decisions, $adventurebook_character_management_url; check_ajax_referer( 'adventurebook_ajax_nonce' ); $error_msg = ''; // the adventurebook_id was passed to the ajax function if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } $adventurebook_is_game = adventurebook_is_game( $adventurebook_id ); // for adventurebook type "game" only: check if the character management page is configured if ( $adventurebook_is_game ) { $character_page_id = get_option( 'adventurebook_character_page_id', 0 ); if ( ! $character_page_id ) { wp_send_json_error( '

' . esc_html( __( 'Character management page not configured.', 'adventurebook' ) ) . '

' ); } $adventurebook_character_management_url = esc_url( get_permalink( $character_page_id ) ); // Is the user logged in? You need to be logged in to use the Adventurebook // else we can't assign a character to the user if ( ! is_user_logged_in() ) { $login_url = wp_login_url(); /* translators: %s is the login url */ wp_send_json_error( '

' . sprintf( __( 'You need to log in to access this page.', 'adventurebook' ), esc_url( $login_url ) ) . '

' ); } global $wpdb; $user_id = get_current_user_id(); // Get the current character of the user $adventurebook_current_character_id = get_user_meta( $user_id, 'adventurebook_current_character_id_' . $adventurebook_id, true ); if ( ! $adventurebook_current_character_id ) { // No current character selected /* translators: %s is the character management url */ adventurebook_send_json_message( sprintf( __( 'You have not selected a current character. Go to character management.', 'adventurebook' ), esc_url( $adventurebook_character_management_url ) ) ); } // Get the current character from the database $adventurebook_current_character = Character::get_character_by_id( $adventurebook_current_character_id ); if ( ! $adventurebook_current_character ) { // If the current character is not found, reset the meta delete_user_meta( $user_id, 'adventurebook_current_character_id_' . $adventurebook_id ); /* translators: %s is the character management url */ wp_send_json_error( '

' . sprintf( __( 'The current character no longer exists. Go to character management.', 'adventurebook' ), $adventurebook_character_management_url ) . '

' ); } adventurebook_check_ownership( $adventurebook_current_character_id ); } // if there is no adventurebook_id, return an error message if ( ! $adventurebook_id ) { wp_send_json_error( '

' . esc_html( __( 'No Adventurebook ID provided.', 'adventurebook' ) ) . '

' ); } // fetch the Adventurebook $adventurebook = get_post( $adventurebook_id ); // fetch the combat system $adventurebook_combat_system = get_combat_system_for_adventurebook( $adventurebook_id ); // Check if the Adventurebook exists and is of the type `adventurebook` if ( ! $adventurebook || $adventurebook->post_type !== 'adventurebook' ) { wp_send_json_error( '

' . esc_html( __( 'Adventurebook not found.', 'adventurebook' ) ) . '

' ); } $page_id = get_actual_page_id( $adventurebook_id ); $adventurebook_page = get_post( $page_id ); // Check if the Adventurebook page exists and is of the type `adventurebook_page` if ( ! $adventurebook_page || $adventurebook_page->post_type !== 'adventurebook_page' ) { wp_send_json_error( '

' . esc_html( __( 'Adventurebook page not found.', 'adventurebook' ) ) . '

' ); } // Get the decision options for the current page $decisions = get_post_meta( $page_id, '_adventurebook_decision_options', true ); $decisions = is_array( $decisions ) ? $decisions : array(); // set the current page as the current zone for the character, if this is a game // else set the page id in the session if ( $adventurebook_is_game ) { $wpdb->update( $wpdb->prefix . "adventurebook_characters", array( 'location_id' => $page_id ), array( 'id' => $adventurebook_current_character_id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery } else { // set the adventurebook_id AND the page_id in the session adventurebook_save_to_session( 'adventurebook_page_id_' . $adventurebook_id, $page_id ); } if ( $adventurebook_is_game ) { // add 1 to the total rounds played $rounds_played = intval( $adventurebook_current_character->get_game_var( 'rounds_played' ) ); $rounds_played ++; $adventurebook_current_character->set_game_var( 'rounds_played', $rounds_played ); // finally, check for respawned enemies on this page Enemy_Spawn::check_for_respawns( $adventurebook_current_character_id ); // check for an attached script on this page and run it $script_code = get_post_meta( $page_id, '_adventurebook_page_script', true ); $script = Script::get_script_by_code( $script_code ); if ( $script ) { $script_output = $script->run_script( $adventurebook_current_character ); } else { $script_output = []; } // each page refresh means a new round - so we call the central doCombat method of the combat system // for this we need all enemies on this page for this character $enemies = Enemy::get_enemies_for_page( $page_id, $adventurebook_current_character_id ); $output = $adventurebook_combat_system->doCombat( $adventurebook_current_character, $enemies ); // TODO: check if the combat modifications (e.g. health) are saved to the character and enemies and are shown correctly below } // --------------------------- // // Start output // // --------------------------- $content = ''; $audio_id = get_post_meta( $page_id, '_adventurebook_audio_id', true ); if ( $audio_id ) { $audio_url = wp_get_attachment_url( $audio_id ); $content .= ''; } $content .= '
'; if ( $adventurebook_is_game ) { $content .= adventurebook_get_character_topbar_output(); } $content .= '

' . $adventurebook_page->post_title . '

'; $content .= '
' . esc_html( $error_msg ) . '
'; // show any transient messages (e.g. from buying/selling items) $messages = get_transient( 'adventurebook_messages' ); if ( $messages && is_array( $messages ) ) { foreach ( $messages as $message ) { $content .= '
' . wp_kses_post( $message ) . '
'; } } delete_transient( 'adventurebook_messages' ); // add character output if ( $adventurebook_is_game ) { $content .= adventurebook_get_character_output(); } $content .= '
'; $content .= '
'; // add the custom image and the page content $image_id = get_post_meta( $page_id, '_additional_image', true ); if ( $image_id ) { // fetch the image id $image_id = get_post_meta( $page_id, '_additional_image', true ); // fetch the image URL $thumb_url = wp_get_attachment_image_url( $image_id, 'medium' ); // Large version $large_url = wp_get_attachment_image_url( $image_id, 'large' ); // output the image if ( $thumb_url ) { $content .= ''; } } $content .= '
'; $content .= apply_filters( 'the_content', $adventurebook_page->post_content ); // add the script output here, so that it is shown directly below the page content $formatted_script_output = []; // add any script output // the script output is an array, so wrap each line in a div if ( is_array( $script_output ) ) { foreach ( $script_output as $line ) { $line = trim( $line ); if ( ! empty( $line ) ) { // allow a, button, div, onclick, strong, em, br, span with class // to allow links, bold, italic, line breaks and spans with classes for JS // e.g. for showkeylink // escape everything else $formatted_script_output[] = '
' . wp_kses( $line, array( 'a' => array( 'href' => array(), 'onclick' => array(), 'target' => array(), 'rel' => array(), ), 'button' => array( 'onclick' => array(), 'class' => array(), 'type' => array(), ), 'font' => array( 'color' => array() ), 'div' => array( 'class' => array() ), 'span' => array( 'class' => array() ), 'strong' => array(), 'em' => array(), 'br' => array(), ) ) . '
'; } } } if ( ! empty( $formatted_script_output ) ) { $content .= '
'; $content .= implode( '', $formatted_script_output ); $content .= '
'; } $content .= '
'; if ( $fixed_height ) { $content .= '
--- ↓ scroll ---
'; } if ( $adventurebook_is_game ) { // check if the character is dead if ( ! $adventurebook_current_character->isAlive() ) { // show the combat output so that the player can see what killed him $content .= adventurebook_show_combat_log($output); // show a message with the option to respawn at the start page $content .= '
'; $content .= '

' . esc_html( __( 'You are dead!', 'adventurebook' ) ) . '

'; // get the page title of the RespawnZone stat $respawn_zone = intval($adventurebook_current_character->get_stat( 'RespawnZone' )) ? intval($adventurebook_current_character->get_stat( 'RespawnZone' )) : get_post_meta( $adventurebook_id, '_adventurebook_start_page', true ); $respawn_page = get_post( $respawn_zone ); if ( $respawn_page ) { /* translators: %s is the respawn page title */ $content .= '

' . sprintf( esc_html( __( 'Your character has died. You can respawn at %s.', 'adventurebook' ) ), '' . esc_html( $respawn_page->post_title ) . '' ) . '

'; } // respawn button $content .= ''; $content .= '

'; // stop further output $content .= '
'; // .adventurebook_inner_content $content .= '
'; // #adventurebook_content $content .= ''; wp_send_json_success( $content ); } // add the shop output if this is a shop page $content .= adventurebook_get_shop_page_output(); // add enemies $content .= adventurebook_get_enemies_on_page_output(); // if there is a combat output (array of strings), show it under "Combat Log" if ( isset( $output ) && ! empty( $output ) ) { $content .= adventurebook_show_combat_log($output); } // add the items on the page output $content .= adventurebook_get_items_on_page_output(); } // add the decisions output $content .= adventurebook_get_decisions_on_page_output( $adventurebook_is_game ); $content .= ''; // .adventurebook_inner_content $content .= ''; // #adventurebook_content $content .= ''; wp_send_json_success( $content ); } add_action( 'wp_ajax_adventurebook_get_page_content', 'adventurebook_get_page_content_ajax' ); // add action for non-logged in users add_action( 'wp_ajax_nopriv_adventurebook_get_page_content', 'adventurebook_get_page_content_ajax' ); function adventurebook_show_combat_log($output) : string { $content = '
'; $content .= '

' . esc_html( __( 'Combat Log', 'adventurebook' ) ) . '

'; $content .= '
'; foreach ( $output as $line ) { // if the line ends with !, make it red if ( substr( $line, - 1 ) === '!' ) { $line = '' . esc_html( $line ) . ''; } else { $line = esc_html( $line ); } $content .= '
  • ' . $line . '
  • '; } $content .= '
    '; // #adventurebook_combat_log_content $content .= '
    '; // #adventurebook_combat_log return $content; } /** * Returns the HTML output for the character top bar * The Top Bar shows the character name, health, XP, Money and a link to the character management page in one line * When you click on the line, it expands to show the character stats, equipped items and inventory * * @return string */ function adventurebook_get_character_topbar_output(): string { global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_current_character, $adventurebook_id, $adventurebook_current_character_id, $decisions, $adventurebook_character_management_url; adventurebook_check_ownership( $adventurebook_current_character_id ); $content = ''; /** @var Character $adventurebook_current_character */ $content .= '
    '; // add a toggle indicator div $content .= '
    '; // show the current character name and a link to the character management page $content .= '
    '; $content .= ''; $content .= '' . esc_html( $adventurebook_current_character->getName() ) . ''; // show CurHealth/MaxHealth $stats = $adventurebook_current_character->get_stats(); $current_health = isset( $stats['CurHealth'] ) ? intval( $stats['CurHealth'] ) : 0; $max_health = isset( $stats['MaxHealth'] ) ? intval( $stats['MaxHealth'] ) : 0; // show health only if health is available if ( $max_health > 0 ) { $content .= ' • ' . esc_html( __( 'Health', 'adventurebook' ) ) . ': ' . esc_html( $current_health ) . '/' . esc_html( $max_health ) . ''; } // show XP and Money (if the combat system is set) if ( $adventurebook_combat_system ) { $wealth = intval( $adventurebook_current_character->get_stat( 'Wealth' ) ); $wealth_unit = $adventurebook_combat_system->getWealthUnit(); // usually "G" or "$" or "Gold" $content .= ' • ' . esc_html( $wealth_unit ) . ': ' . esc_html( $wealth ) . ''; // if the character has a stat "XP", show it if ( isset( $stats['XP'] ) ) { $xp = intval( $stats['XP'] ); $content .= ' • ' . esc_html( __( 'XP', 'adventurebook' ) ) . ': ' . esc_html( $xp ) . ''; } $content .= ''; } $content .= '
    '; // adventurebook_character_title $content .= ''; // adventurebook_character_details $content .= '
    '; // #adventurebook_character_topbar return $content; } /** * Returns the HTML output for the current character and their inventory * * @return string HTML output */ function adventurebook_get_character_output(): string { global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_current_character, $adventurebook_id, $adventurebook_current_character_id, $decisions, $adventurebook_character_management_url; $content = ''; /** @var Character $adventurebook_current_character */ $content .= '
    '; // show the current character name and a link to the character management page $content .= '
    '; $content .= '' . esc_html( $adventurebook_current_character->getName() ) . ' '; $content .= ''; // show CurHealth/MaxHealth $stats = $adventurebook_current_character->get_stats(); $current_health = isset( $stats['CurHealth'] ) ? intval( $stats['CurHealth'] ) : 0; $max_health = isset( $stats['MaxHealth'] ) ? intval( $stats['MaxHealth'] ) : 0; // show health only if health is available if ( $max_health > 0 ) { $content .= '
    ' . esc_html( __( 'Health', 'adventurebook' ) ) . ': ' . esc_html( $current_health ) . '/' . esc_html( $max_health ) . ''; } // show XP and Money (if the combat system is set) if ( $adventurebook_combat_system ) { $wealth = intval( $adventurebook_current_character->get_stat( 'Wealth' ) ); $wealth_unit = $adventurebook_combat_system->getWealthUnit(); // usually "G" or "$" or "Gold" $content .= ''; $content .= esc_html( $wealth_unit ) . ': ' . esc_html( $wealth ) . ''; // if the character has a stat "XP", show it if ( isset( $stats['XP'] ) ) { $xp = intval( $stats['XP'] ); $content .= '  ' . esc_html( __( 'XP', 'adventurebook' ) ) . ': ' . esc_html( $xp ) . ''; } $combat_stats = $adventurebook_combat_system->getCombatStatsOutput( $adventurebook_current_character ); $content .= ' ' . implode( ' ', $combat_stats ) . ''; $content .= ''; } $content .= '
    '; // adventurebook_character_title // check if there are any equipped items $equipped_items = $adventurebook_current_character->get_equipped_items(); if ( ! empty( $equipped_items ) ) { $content .= '
    '; $content .= '
    '; $content .= '' . esc_html( __( 'Equipped Items', 'adventurebook' ) ) . ''; $content .= '
    '; $content .= ''; // adventurebook_items $content .= '
    '; // adventurebook_items_list } $wealth_unit = $adventurebook_combat_system->getWealthUnit(); // show the current character inventory // get all items that are owned by the character // get_inventory already returns an array key/value pairs ("item_code" => y, "count" => z) $inventory = $adventurebook_current_character->get_inventory(); $inventory = adventurebook_sort_items_by_name( $inventory ); $content .= '
    '; $content .= '
    '; $content .= '' . esc_html( __( 'Inventory', 'adventurebook' ) ) . ''; $content .= '
    '; // if the inventory is empty, we return here if ( empty( $inventory ) ) { $content .= '

    ' . esc_html( __( 'Your inventory is empty.', 'adventurebook' ) ) . '

    '; $content .= '
    '; $content .= '
    '; // #adventurebook_character return $content; } $content .= ''; $content .= ''; // #adventurebook_character return $content; } /** * Returns the HTML output for the enemies on the current page * * @return string HTML output */ function adventurebook_get_enemies_on_page_output(): string { global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_current_character, $adventurebook_id, $adventurebook_current_character_id; $content = ''; if ( $adventurebook_is_game ) { // get all enemies that are on the page $enemies = Enemy::get_enemies_for_page( $page_id, $adventurebook_current_character_id ); if ( ! empty( $enemies ) ) { $content .= '
    '; $content .= '

    ' . esc_html( __( 'Enemies', 'adventurebook' ) ) . '

    '; $content .= ''; $content .= '
    '; } } return $content; } /** * Returns the HTML output for the decisions on the current page * NOTE: if a restriction is set for a decision, the decision will not be shown if the restriction is not met * * @return string HTML output */ function adventurebook_get_decisions_on_page_output( $adventurebook_is_game ): string { global $page_id, $adventurebook_id, $decisions, $current_character, $adventurebook_current_character_id; $content = ''; // remove decisions that have a restriction that is not met // this is done by checking if the character adventurebook_can_travel_from_zone_to_zone for the decision target // if the decision has no restriction, it is always shown // the function adventurebook_can_travel_from_zone_to_zone checks the restriction // source = current page id, target = decision ['target'] $decisions = array_filter( $decisions, function ( $decision ) use ( $adventurebook_current_character_id, $page_id) { return adventurebook_can_travel_from_zone_to_zone( $adventurebook_current_character_id, $page_id, $decision['target']); } ); if ( ! empty( $decisions ) ) { $content .= '
    '; $content .= '

    ' . esc_html( __( 'Decisions', 'adventurebook' ) ) . '

    '; $content .= '
    '; } return $content; } /** * Returns the HTML output for the items on the current page * * @return string HTML output */ function adventurebook_get_items_on_page_output(): string { global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_id, $adventurebook_current_character_id; $content = ''; if ( $adventurebook_is_game ) { // get all items that are on the page $items = get_items_for_page( $page_id, $adventurebook_current_character_id ); // sort by name $items = adventurebook_sort_items_by_name( $items ); if ( ! empty( $items ) ) { $content .= '
    '; $content .= '

    ' . __( 'Items', 'adventurebook' ) . '

    '; $content .= ''; $content .= '
    '; } } return $content; } /** * Returns the HTML output for the shop on the current page * * @return string HTML output */ function adventurebook_get_shop_page_output(): string { global $adventurebook_is_game, $page_id, $adventurebook_combat_system, $adventurebook_current_character, $adventurebook_id, $adventurebook_current_character_id; $content = ''; /** @var Character $adventurebook_current_character */ // if this is a game, and the page type is "shop", show additional shop information // - a list of items that are for sale with their prices (stats['price']) * the shop price factor $page_type = get_post_meta( $page_id, '_adventurebook_page_type', true ); if ( $adventurebook_is_game && $page_type === 'shop' ) { $shop_price_factor = get_post_meta( $page_id, '_adventurebook_shop_price_factor', true ); $shop_price_factor = floatval( $shop_price_factor ); if ( $shop_price_factor <= 0 ) { $shop_price_factor = 1.0; } // get all items that are for sale on this page (_adventurebook_shop_items meta field contains a comma-separated list of item codes) $shop_items_codes = get_post_meta( $page_id, '_adventurebook_shop_items', true ); $shop_items_codes = is_array( $shop_items_codes ) ? $shop_items_codes : explode( ',', $shop_items_codes ); $shop_items_codes = array_map( 'trim', $shop_items_codes ); $shop_items_codes = array_filter( $shop_items_codes ); if ( ! empty( $shop_items_codes ) ) { // get the items from the database global $wpdb; $shop_items = []; foreach ( $shop_items_codes as $code ) { $shop_items[] = $wpdb->get_row(// phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}adventurebook_item_definitions WHERE code = %s", $code ) ); } if ( ! empty( $shop_items ) ) { $content .= '
    '; $content .= '

    ' . esc_html( __( 'Shop', 'adventurebook' ) ) . '

    '; // show the character's wealth $character_stats = $adventurebook_current_character->get_stats(); $character_wealth = isset( $character_stats['Wealth'] ) ? intval( $character_stats['Wealth'] ) : 0; $content .= '
    '; $content .= '' . // translators: %d is the character's wealth esc_html( sprintf( __( 'Your Wealth: %d', 'adventurebook' ), $character_wealth ) ) . ' ' . $adventurebook_combat_system->getWealthUnit() . ''; $content .= '
    '; // get the price wealth_unit from the combat system $wealth_unit = $adventurebook_combat_system->getWealthUnit(); $content .= ''; $content .= '
    '; } } } return $content; } function adventurebook_try_decision_ajax() { // check for nonce check_ajax_referer( 'adventurebook_ajax_nonce' ); // Nonce validieren global $adventurebook_id; // needed here for adventurebook_can_travel_from_zone_to_zone function if ( isset( $_POST['source_page_id'] ) ) { $source_page_id = intval( $_POST['source_page_id'] ); } if ( isset( $_POST['destination_page_id'] ) ) { $destination_page_id = intval( $_POST['destination_page_id'] ); } if ( isset( $_POST['adventurebook_id'] ) ) { $adventurebook_id = intval( $_POST['adventurebook_id'] ); } // check if this is a game $adventurebook_is_game = adventurebook_is_game( $adventurebook_id ); if ( $adventurebook_is_game ) { // check if the user is logged in if ( ! is_user_logged_in() ) { status_header( 400 ); wp_send_json_error( __( 'You need to log in to play the game.', 'adventurebook' ) ); } global $wpdb; $user_id = get_current_user_id(); // Get the current character_id of the user as integer $current_character_id = (int) get_user_meta( $user_id, 'adventurebook_current_character_id_' . $adventurebook_id, true ); adventurebook_check_ownership( $current_character_id ); if ( adventurebook_can_travel_from_zone_to_zone( $current_character_id, $source_page_id, $destination_page_id ) ) { // set the destination_page as the actual zone for the character $character = Character::get_character_by_id( $current_character_id ); $character->move_to_zone( $destination_page_id ); $response = array( 'status' => 'success', 'destination_page_id' => $destination_page_id, ); // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } else { status_header( 403 ); wp_send_json_error( esc_html( __( 'You can not go this way.', 'adventurebook' ) ) ); } } else { if ( adventurebook_can_travel_from_zone_to_zone( 0, $source_page_id, $destination_page_id ) ) { // set the adventurebook_id AND the page_id in the session adventurebook_save_to_session( 'adventurebook_page_id_' . $adventurebook_id, $destination_page_id ); $response = array( 'status' => 'success', 'destination_page_id' => $destination_page_id, ); // now we need to call the adventurebook_get_page_content_ajax function to get the updated page content adventurebook_get_page_content_ajax(); } else { status_header( 403 ); wp_send_json_error( esc_html( __( 'You can not go this way.', 'adventurebook' ) ) ); } } } add_action( 'wp_ajax_adventurebook_try_decision', 'adventurebook_try_decision_ajax' ); // add action for non-logged in users add_action( 'wp_ajax_nopriv_adventurebook_try_decision', 'adventurebook_try_decision_ajax' ); /** * Liefert die erste Seite oder Post, die den AdventureBook-Shortcode mit einer bestimmten ID enthält. * * @param int $id Die ID des AdventureBook-Shortcodes * * @return WP_Post|null Die gefundene Seite/Post oder null, wenn keine gefunden wurde */ function adventurebook_get_first_page_using_shortcode( int $id ): ?WP_Post { global $wpdb; // Bereite die Suche vor: wir suchen nach [adventurebook_start_page="ID"] $like = '%[adventurebook%id="' . $wpdb->esc_like( $id ) . '"%]%'; // SQL-Abfrage, die nur die erste passende Seite/Post zurückliefert $post_id = $wpdb->get_var(// phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ('post','page') AND post_content LIKE %s LIMIT 1", $like ) ); if ( $post_id ) { return get_post( $post_id ); } return null; } /** * Fügt eine Transient-Nachricht hinzu, die auf der nächsten Seite angezeigt wird. * Es wird geschaut ob es bereits eine Nachricht (ein Array) in adventurebook_messages gibt, wenn ja wird die neue Nachricht angehängt. * * @param string $message Die Nachricht, die angezeigt werden soll */ function add_transient_message( string $message ) { $messages = get_transient( 'adventurebook_messages' ); if ( ! is_array( $messages ) ) { $messages = array(); } $messages[] = $message; set_transient( 'adventurebook_messages', $messages, 30 ); // 30 Sekunden gültig } function adventurebook_save_to_session( $key, $value ) { // Session starten, falls sie noch nicht gestartet wurde if ( ! session_id() ) { session_start(); } // Wert in der Session speichern $_SESSION[ $key ] = $value; } function adventurebook_sort_items_by_name( $items ) { // sort the items by their name usort( $items, function ( Item $a, Item $b ) { $name_a = $a->get_name() ?? ''; $name_b = $b->get_name() ?? ''; return strcmp( $name_a, $name_b ); } ); return $items; } function do_log( $message ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { if ( is_array( $message ) || is_object( $message ) ) { error_log( print_r( $message, true ) ); } else { error_log( $message ); } } }