id = intval( $id ); $this->user_id = intval( $user_id ); $this->adventurebook_id = intval( $adventurebook_id ); $this->name = $name; $this->zone = $zone; 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 function get_stat( string $stat_name ) { return $this->stats[ $stat_name ] ?? null; } public function get_stats(): array { ksort( $this->stats ); return $this->stats; } /** * 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, 'zone' => $this->zone, '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, 'zone' => $this->zone, '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->code, $enemy_spawn_point->page_id, $enemy_spawn_point->respawn_time ); } 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 ); } } /** Get all characters from the database * @return array An array of Character objects */ public static function get_all_characters(): array { global $wpdb; $results = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "adventurebook_characters", ARRAY_A );// phpcs:ignore WordPress.DB.DirectDatabaseQuery $characters = array(); foreach ( $results as $result ) { $characters[] = new Character( $result['id'], $result['user_id'], $result['adventurebook_id'], $result['name'], $result['zone'], json_decode( $result['stats'], true ), json_decode( $result['inventory'], true ), $result['next_action'], $result['created_at'] ); } 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[] = new Character( $result['id'], $result['user_id'], $result['adventurebook_id'], $result['name'], $result['zone'], json_decode( $result['stats'], true ), json_decode( $result['inventory'], true ), $result['next_action'], $result['created_at'] ); } 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 new Character( $result['id'], $result['user_id'], $result['adventurebook_id'], $result['name'], $result['zone'], json_decode( $result['stats'], true ), json_decode( $result['inventory'], true ), $result['next_action'], $result['created_at'] ); } /** * 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 { $adventurebook_combat_system = get_combat_system_for_adventurebook( $this->adventurebook_id ); $content = ''; // get all items this character possesses // show all items of the character that are in his inventory (location_id = 0) $items = $this->sortInventoryByTranslatedName( $this->get_inventory() ); if ( count( $items ) > 0 ) { $content .= '
  • ' . esc_html( __( 'Items in Inventory', 'adventurebook' ) ) . ':
  • '; } else { $content .= '
  • ' . esc_html( __( 'No items in inventory', 'adventurebook' ) ) . '
  • '; } return $content; } /** * render the slotted item list of this character as html * needed for the character overview (management page) * * slotted items are items that are in the stat "Slots" and are currently equipped * slot "Slots" has the format "Weapon,Shield,Armor". If an item is equipped, the format is "Weapon:sword_001,Shield:shield_001,Armor:armor_001" * the item after the colon is the item_code of the item that is equipped in this * * @return string */ public function get_slotted_item_list_html(): string { // get the "Slots" stat $slotted_items = $this->get_slotted_items(); $content = ''; $content .= '
  • ' . esc_html( __( 'Equipped Items', 'adventurebook' ) ) . ':
  • '; if ( $slotted_items === [] ) { $content = '
  • ' . esc_html( __( 'No items equipped', 'adventurebook' ) ) . '
  • '; } return $content; } public function get_slotted_items(): array { $slots_stat = $this->get_stat( 'Slots' ); if ( $slots_stat === null || trim( $slots_stat ) === '' ) { return []; } $slots = explode( ',', $slots_stat ); $slotted_items = []; foreach ( $slots as $slot ) { $slot_parts = explode( ':', $slot ); if ( count( $slot_parts ) > 1 ) { $item_code = $slot_parts[1]; $item = Item_Definition::get_item_definition_by_code( $item_code ); if ( $item ) { $slotted_items[ $slot_parts[0] ] = $item; } } } return $slotted_items; } /** * Uses an item with this item_code from the character's inventory * * @param $item_code * * @return void */ public function use_item($item_code) : bool { if($this->has_item($item_code)) { $successfully_used = false; // get the item definition $item_definition = Item_Definition::get_item_definition_by_code($item_code); // check if the item has a "UseEffect" stat if(isset($item_definition->stats['Use'])) { $use_effect = $item_definition->stats['Use']; // apply the use effect to the character // all use effects are in the format "command:param1,param2,...;command:param1,param2,..." $commands = explode(';', $use_effect); foreach($commands as $command) { $parts = explode( ':', $command ); $cmd = strtolower($parts[0]); $params = isset( $parts[1] ) ? explode( ',', $parts[1] ) : []; switch ( $cmd ) { case 'heal': $amount = isset( $params[0] ) ? intval( $params[0] ) : 0; if ( $amount > 0 ) { // heal the character $amount = $this->heal( $amount ); $successfully_used = true; // create a transient message for next page reload add_transient_message(__( 'You used ' , 'adventurebook' ) . $item_definition->name . __( ' and healed for ', 'adventurebook' ) . $amount . __( ' health.', 'adventurebook' )); } } } } // if the item was successfully used, remove it from the inventory if($successfully_used) { $this->remove_item_from_inventory( $item_code, 1 ); $this->save_to_db(); } return true; } return false; } public function has_item( string $item_code ): bool { $inventory = $this->get_inventory(); foreach ( $inventory as $item_info ) { if ( $item_info["item_code"] === $item_code ) { return true; } } return false; } /** Move the character to a different zone * * @param int $zone_id The ID of the zone to move the character to * * upon moving to a different zone, all enemy spawns for this character in the new zone need to be checked * * @return void */ public function move_to_zone( int $zone_id ): void { // we want to know if the character has just entered a new zone so that he can surprise enemies // see CombatSystem::doCombat() set_transient( 'adventurebook_just_entered_zone_' . $this->id, true ); $this->zone = $zone_id; $this->save_to_db(); } /** Respawn the character by setting its current health to max health * and saving the character to the database * * @return void */ public function respawn(): void { // get the combat system for this adventurebook $adventurebook_combat_system = get_combat_system_for_adventurebook( $this->adventurebook_id ); // set the current health to max health $max_health = $adventurebook_combat_system->getInitialCharacterStats()['MaxHealth'] ?? 100; $this->stats['CurHealth'] = $max_health; // save the character to the database $this->save_to_db(); } public function getRespawnZoneTitle(): string { $respawn_zone_id = $this->getStat( 'RespawnZone' ); if ( ! $respawn_zone_id ) { return ''; } $respawn_zone = get_post( $respawn_zone_id ); if ( ! $respawn_zone ) { return ''; } return $respawn_zone->post_title; } static public function delete_character_by_id( int $id ): void { // first, get the character $character = self::get_character_by_id( $id ); if ( $character ) { // remove all items that belong to this character Item::delete_items_by_character_id( $character->id ); // remove all enemy spawn points that belong to this character Enemy_Spawn::delete_enemy_spawn_points_by_character_id( $character->id ); // delete the character from the database $character->delete_from_db(); } } public function get_game_var(string $var_name) { $game_vars = $this->get_stat('VARS'); if ($game_vars === null || trim($game_vars) === '') { return ''; } $game_vars_array = json_decode($game_vars, true); if (isset($game_vars_array[$var_name])) { return $game_vars_array[ $var_name ]; } return ''; } public function set_game_var(string $var_name, $value): void { $game_vars = $this->get_stat('VARS'); if ($game_vars === null || trim($game_vars) === '') { $game_vars_array = array(); } else { $game_vars_array = json_decode($game_vars, true); } $game_vars_array[$var_name] = $value; $this->stats['VARS'] = json_encode($game_vars_array); $this->save_to_db(); } }