id = intval( $id ); $this->user_id = intval( $user_id ); $this->adventurebook_id = intval( $adventurebook_id ); $this->name = $name; $this->location_id = $location_id; if ( $stats == '' ) { // stats are depending on the combat system $adventurebook_combat_system = get_combat_system_for_adventurebook( $this->adventurebook_id ); $stats = $adventurebook_combat_system->getInitialCharacterStats(); } $this->stats = $stats; $this->inventory = $inventory === '' ? [] : $inventory; $this->next_action = $next_action; $this->created_at = $created_at; } public static function CreateCharacterByResult( $result ): Character { return new Character( $result['id'], $result['user_id'], $result['adventurebook_id'], $result['name'], $result['location_id'], json_decode( $result['stats'], true ), json_decode( $result['inventory'], true ), $result['next_action'], $result['created_at'] ); } /** * Save the character to the database. * If the character already has an ID, it will be updated. * If the character does not have an ID, it will be created. * * @return int The ID of the character. */ public function save_to_db(): int { global $wpdb; $table_name = $wpdb->prefix . 'adventurebook_characters'; // if this character already has an ID, we should update it instead of creating a new one if ( $this->id > 0 ) { $wpdb->update( $table_name, array( // phpcs:ignore WordPress.DB.DirectDatabaseQuery 'user_id' => $this->user_id, 'name' => $this->name, 'adventurebook_id' => $this->adventurebook_id, 'location_id' => $this->location_id, 'stats' => json_encode( $this->stats ), 'inventory' => json_encode( $this->inventory ), 'next_action' => $this->next_action, ), array( 'id' => $this->id ) ); return $this->id; } // insert the new character into the database $wpdb->insert( $table_name, array(// phpcs:ignore WordPress.DB.DirectDatabaseQuery 'user_id' => $this->user_id, 'name' => $this->name, 'adventurebook_id' => $this->adventurebook_id, 'location_id' => $this->location_id, 'stats' => json_encode( $this->stats ), 'inventory' => json_encode( $this->inventory ), 'next_action' => $this->next_action, 'created_at' => current_time( 'mysql' ), ) ); $new_character_id = $wpdb->insert_id; $this->id = $new_character_id; // iterate through all item_spawns and create an item object for each item // get all item spawns from the database $item_spawns = Item_Spawn::get_all_item_spawns(); foreach ( $item_spawns as $item_spawn ) { // create an item for each item spawn Item::create_item( $new_character_id, $item_spawn->code, $item_spawn->page_id ); } // iterate through all enemy spawn points and create a duplicate of it for this character $enemy_spawn_points = Enemy_Spawn::get_all_enemy_spawn_points(); foreach ( $enemy_spawn_points as $enemy_spawn_point ) { Enemy_Spawn::create_enemy_spawn_point( $new_character_id, $enemy_spawn_point->id ); } return $new_character_id; } /** Delete the character from the database. * This will also delete all items that belong to this character. */ public function delete_from_db(): void { global $wpdb; // delete the character from the database $wpdb->delete( $wpdb->prefix . "adventurebook_characters", array( 'id' => $this->id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery // delete all items that belong to this character Item::delete_items_by_character_id( $this->id ); // delete all enemy spawn points that belong to this character Enemy_Spawn::delete_enemy_spawn_points_by_character_id( $this->id ); // get the adventurebook id of the character $adventurebook_id = $this->adventurebook_id; $user_id = get_current_user_id(); // if the deleted character was the current character, remove the current_character_id meta $current_character_id = get_user_meta( $user_id, 'adventurebook_current_character_id_' . $adventurebook_id, true ); if ( $this->id == $current_character_id ) { delete_user_meta( $user_id, 'adventurebook_current_character_id_' . $adventurebook_id ); } // if the user still has characters, set the first character as the current character $characters = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_characters WHERE user_id = %d", $user_id ) );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $characters ) { update_user_meta( $user_id, 'adventurebook_current_character_id_' . $adventurebook_id, $characters[0]->id ); } } public static function get_characters_by_adventurebook_id( int $adventurebook_id ): array { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_characters WHERE adventurebook_id = %d", $adventurebook_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $characters = array(); foreach ( $results as $result ) { $characters[] = Character::createCharacterByResult( $result ); } return $characters; } public static function get_characters_by_user_id( int $user_id ): array { global $wpdb; $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_characters WHERE user_id = %d", $user_id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $characters = array(); foreach ( $results as $result ) { $characters[] = Character::createCharacterByResult( $result ); } return $characters; } public static function get_character_by_id( int $id ): ?Character { global $wpdb; $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->prefix . "adventurebook_characters WHERE id = %d", $id ), ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( $result === null ) { return null; } return Character::createCharacterByResult( $result ); } /** * render the item list of this character as html * needed for the character overview (management page) * * @return string */ public function get_item_list_html(): string { $content = ''; // get all items this character possesses // show all items of the character that are in his inventory (location_id = 0) $items = adventurebook_sort_items_by_name( $this->get_inventory() ); if ( count( $items ) > 0 ) { $content .= '