View Single Post
  #3  
Old 02-16-2025, 01:22 AM
eyenseq's Avatar
eyenseq
Fire Beetle
 
Join Date: Jan 2014
Location: United States
Posts: 4
Default

  • Supports 3 saved buff sets
  • Buffs can be added / removed from sets with clickable links
  • Add all buffs and block traditional way for the lazy
  • Easy way to add utility buffs and let players build their own ideal buff sets

Code:
use JSON;
use Data::Dumper;
use DBI;

sub EVENT_SAY {
    my $client_level = $client->GetLevel();
    my $lowest_group_level = GetLowestGroupLevel($client);
    my $buff_level = $lowest_group_level ? $lowest_group_level : $client_level;

    if ($text=~/hail/i) {
        plugin::Whisper("Yo, $name! Need a power-up? I致e got buffs hotter than dragon breath and stronger than goblin coffee!");
		plugin::Whisper("Check the menu with " . quest::saylink("list buffs", 1, "[List Buffs]") . " or go all-in with " . quest::saylink("all buffs", 1, "[All Buffs]") . ".");
		plugin::Whisper("Ready to get juiced? Say " . quest::saylink("yes", 1, "[Yes]") . " and I'll make you OP.");
		plugin::Whisper("Wanna see your stash? Peek at " . quest::saylink("saved buffs", 1, "[Saved Buffs]") . ".");
		plugin::Whisper("Messed up? Say 'add spell <id>', 'remove spell <id>', or nuke 'em all with " . quest::saylink("clear buffs", 1, "[Clear Buffs]") . "!");
		plugin::Whisper("Buff sets? I got 'em! Choose your loadout: " . quest::saylink("set 1", 1, "[Set 1]") . ", " . quest::saylink("set 2", 1, "[Set 2]") . ", or " . quest::saylink("set 3", 1, "[Set 3]") . ". Pick your poison!");

    }
    elsif ($text=~/list buffs/i) {
    my $active_set = GetActiveBuffSet($client); # Ensure we get the correct active set
    ListAvailableBuffs($client, $buff_level, $active_set);
}

    elsif ($text=~/yes/i) {
        CastBuffs($client, $buff_level);
    }
    elsif ($text=~/add spell (\d+)/i) {
        AddBuff($client, $1, $buff_level);
    }
    elsif ($text=~/remove spell (\d+)/i) {
        RemoveBuff($client, $1);
    }
    elsif ($text=~/clear buffs/i) {
        ClearBuffList($client);
    }
    elsif ($text=~/saved buffs/i) {
        ListSavedBuffs($client);
    }
    elsif ($text=~/all buffs/i) {
        AddAllBuffs($client);
    }
    elsif ($text=~/set (\d)/i && $1 >= 1 && $1 <= 3) {
        UseBuffSet($client, $1);
    }
}


sub GetSpellType {
    my ($spell_id) = @_;

    # Database connection settings (UPDATE AS NEEDED)
    my $db_host = "127.0.0.1";  	# Change if needed	
    my $db_name = "yourdatabase";  	# Change to your actual database name
    my $db_user = "youuser";  		# Change to your actual user name
    my $db_pass = 'yourpassword';  	# Change to your actual password

    # Connect to MySQL
    my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
                           $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });

    # Run the query
    my $query = "SELECT targettype FROM spells_new WHERE id = ?";
    my $sth = $dbh->prepare($query);
    $sth->execute($spell_id);

    # Fetch result
    my ($target_type) = $sth->fetchrow_array();

    # Close DB connection
    $sth->finish();
    $dbh->disconnect();

    # Debug output
    if (!defined $target_type) {
        return "Unknown";
    }

    return ($target_type == 41 || $target_type == 14) ? "Group" : "Single";
}


sub SetBuffSet {
    my ($client, $set_number, $buff_list) = @_;
    my $bucket_name = "buff_set_" . $set_number;
    $client->SetBucket($bucket_name, encode_json($buff_list));
}

sub SetActiveBuffSet {
    my ($client, $set_number) = @_;
    $client->SetBucket("current_buff_set", $set_number);
}

sub GetActiveBuffSet {
    my ($client) = @_;
    my $set_number = $client->GetBucket("current_buff_set");

    # Ensure we return a valid set number (1, 2, or 3), default to 1 if not set
    return ($set_number && $set_number =~ /^[1-3]$/) ? $set_number : 1;
}


sub AddAllBuffs {
    my ($client) = @_;
    my $client_level = $client->GetLevel();
    my %buffs = GetBuffsByLevel($client_level);
    my $buff_list = GetBuffList($client);

    if (!%buffs) {
        plugin::Whisper("No buffs available for your level.");
        return;
    }

    foreach my $spell_name (keys %buffs) {
        my $spell_id = $buffs{$spell_name};
        $buff_list->{$spell_id} = 1;
    }

    SetBuffList($client, $buff_list);
    plugin::Whisper("All available buffs for level $client_level have been added to your buff list.");
}

sub GetLowestGroupLevel {
    my $client = shift;

    # If the player is not grouped, return their own level
    return $client->GetLevel() unless $client->IsGrouped();

    my $group = $client->GetGroup();
    if ($group) {
        my $lowest_level = 100;  # Assume max level is 100, adjust as needed
        my $member_count = $group->GroupCount();

        for (my $i = 0; $i < $member_count; $i++) {
            my $member = $group->GetMember($i);
            next unless ($member);
            my $level = $member->GetLevel();
            $lowest_level = $level if ($level < $lowest_level);
        }
        return $lowest_level;
    }

    return $client->GetLevel();  # Default to player's level if something goes wrong
}


sub UseBuffSet {
    my ($client, $set_number) = @_;

    # Set the active buff set
    SetActiveBuffSet($client, $set_number);

    my $buff_list = GetBuffSet($client, $set_number);
    if (!%$buff_list) {
        plugin::Whisper("Buff set $set_number is empty or does not exist.");
        return;
    }

    # Apply the selected buff set
    SetBuffList($client, $buff_list);
    plugin::Whisper("Buff set $set_number has been loaded and is now active.");
}



sub GetBuffSet {
    my ($client, $set_number) = @_;
    my $bucket_name = "buff_set_" . $set_number;
    my $buff_set = $client->GetBucket($bucket_name);

    if (!$buff_set || $buff_set eq "null") {  # Ensures valid JSON data
        return {};
    }

    return decode_json($buff_set);
}

sub SaveBuffSet {
    my ($client, $set_number) = @_;
    my $buff_list = GetBuffList($client);

    if (!%$buff_list) {
        plugin::Whisper("Your buff list is empty! Add buffs before saving.");
        return;
    }

    my $bucket_name = "buff_set_" . $set_number;
    $client->SetBucket($bucket_name, encode_json($buff_list));
    plugin::Whisper("Buff set $set_number has been saved.");
}

sub ListSavedBuffs {
    my ($client) = @_;

    my %all_buffs;
    foreach my $level (1..100) {
        my %level_buffs = GetBuffsByLevel($level);
        %all_buffs = (%all_buffs, %level_buffs);
    }

    plugin::Whisper(" ");
    plugin::Whisper("===== 🎒 Your Saved Buff Sets 🎒 =====");
    plugin::Whisper(" ");

    # Define simulated colors and spacing using prefixes
    my %set_prefixes = (
        1 => "★★ Buff Set 1 ★★ ",   # Simulated Yellow
        2 => "◆◆ Buff Set 2 ◆◆ ",   # Simulated Cyan
        3 => "▲▲ Buff Set 3 ▲▲ ",   # Simulated Green
    );

    for my $set_number (1..3) {
        my $buff_list = GetBuffSet($client, $set_number);
        my $prefix = $set_prefixes{$set_number};

        plugin::Whisper(" ");
        plugin::Whisper("末末末末末末末末末末末末末末末末末");
        plugin::Whisper("$prefix");

        if (!%$buff_list) {
            plugin::Whisper("   (empty)");
            next;
        }

        my @sorted_spells = sort { lc($a) cmp lc($b) } keys %$buff_list;

        foreach my $spell_id (@sorted_spells) {
            my $spell_name = GetSpellNameById($spell_id, \%all_buffs);
            my $remove_command = quest::saylink("remove spell $spell_id $set_number", 1, "[Remove from Set $set_number]");
            plugin::Whisper("   - $spell_name (ID: $spell_id) $remove_command");
        }
    }

    plugin::Whisper(" ");
    plugin::Whisper("=====================================");
    plugin::Whisper(" ");
}


sub ListAvailableBuffs {
    my ($client, $level, $active_set) = @_;

    # Ensure we are using the correct active buff set
    $active_set ||= GetActiveBuffSet($client);  

    # Get all available buffs for the player's level
    my %available_buffs = GetBuffsByLevel($level);

    # Get the buffs saved in the active set
    my $saved_buffs = GetBuffSet($client, $active_set);

    plugin::Whisper("Available buffs for level $level (Active Buff Set: $active_set):");

    if (!%available_buffs) {
        plugin::Whisper("No buffs available at this level.");
        return;
    }

    my @sorted_spells = sort { lc($a) cmp lc($b) } keys %available_buffs;

    foreach my $spell_name (@sorted_spells) {
        my $spell_id = $available_buffs{$spell_name};

        if (exists $saved_buffs->{$spell_id}) {
            # If the spell is already in the saved set, show [Remove] button
            my $remove_command = quest::saylink("remove spell $spell_id $active_set", 1, "[Remove from Set $active_set]");
            plugin::Whisper("$spell_name (ID: $spell_id) $remove_command");
        } else {
            # If the spell is not in the saved set, show [Add] button
            my $add_command = quest::saylink("add spell $spell_id $active_set", 1, "[Add to Set $active_set]");
            plugin::Whisper("$spell_name (ID: $spell_id) $add_command");
        }
    }
}


sub AddBuff {
    my ($client, $spell_id, $set_number) = @_;

    # Ensure the correct buff set number
    if (!$set_number || $set_number !~ /^[1-3]$/) {
        $set_number = GetActiveBuffSet($client);
    }

    # Get the current active buff list
    my $buff_list = GetBuffSet($client, $set_number);

    # Allow any spell to be added, even if it's not in GetBuffsByLevel
    $buff_list->{$spell_id} = 1;
    SetBuffSet($client, $set_number, $buff_list);

    plugin::Whisper("Spell ID $spell_id added to Buff Set $set_number.");
}

sub RemoveBuff {
    my ($client, $spell_id, $set_number) = @_;

    # Ensure the correct buff set number is used
    if (!$set_number || $set_number !~ /^[1-3]$/) {
        $set_number = GetActiveBuffSet($client);
    }

    my $buff_list = GetBuffSet($client, $set_number);  # Load the correct buff set

    if (exists $buff_list->{$spell_id}) {
        delete $buff_list->{$spell_id};  # Remove the spell from the set
        SetBuffSet($client, $set_number, $buff_list);  # Save the updated set
        plugin::Whisper("Spell ID $spell_id removed from Buff Set $set_number.");
    } else {
        plugin::Whisper("That spell is not in Buff Set $set_number.");
    }
}


sub CastBuffs {
    my ($client, $level) = @_;

    my $active_set = GetActiveBuffSet($client);
    my $buff_list = GetBuffSet($client, $active_set);

    if (!%$buff_list) {
        plugin::Whisper("Uh-oh, Buff Set $active_set is empty! Add some spells first.");
        return;
    }

    plugin::Whisper("Buffing you up with Buff Set $active_set!");

    my $group = $client->GetGroup();
    my %unique_targets;

    # Always add the initiator (player who requested buffs)
    $unique_targets{$client->GetID()} = $client;

    # Check if the player is in a group
    if ($group) {
        plugin::Whisper("You're in a group! Buffing all group members, including bots!");

        for (my $i = 0; $i < $group->GroupCount(); $i++) {
            my $member = $group->GetMember($i);
            
            # Ensure member is valid
            next unless defined $member;

            # Include both players (IsClient) and bots (IsBot)
            if ($member->IsClient() || $member->IsBot()) {
                my $member_id = $member->GetID();
                $unique_targets{$member_id} = $member;
            }
        }
    }

    # Convert the hash back into an array of unique targets
    my @targets = values %unique_targets;

    # Prevent duplicate group buffs
    my %cast_group_buffs;

    foreach my $spell_id (sort { $a <=> $b } keys %$buff_list) {
        my $spell_name = GetSpellNameById($spell_id);
        my $spell_type = GetSpellType($spell_id);

        if ($spell_type eq "Group") {
            if (!$cast_group_buffs{$spell_id}) {
                
                
                # Now casting on all members including bots
                foreach my $member (@targets) {
                    $client->SpellFinished($spell_id, $member);
                }

                $cast_group_buffs{$spell_id} = 1;
            }
        } else {
            foreach my $member (@targets) {
                
                $client->SpellFinished($spell_id, $member);
            }
        }
    }

    plugin::Whisper("Buffs applied successfully!");
}


sub GetBuffList {
    my ($client) = @_;
    my $buff_list = $client->GetBucket("buff_list");
    return {} unless $buff_list;
    return decode_json($buff_list);
}

sub SetBuffList {
    my ($client, $buff_list) = @_;
    my $active_set = GetActiveBuffSet($client);
    SetBuffSet($client, $active_set, $buff_list);
}


sub ClearBuffList {
    my ($client) = @_;
    SetBuffList($client, {});
    plugin::Whisper("All buffs successfully removed.");
}


sub GetSpellNameById {
    my ($spell_id) = @_;

    # Database connection settings (UPDATE AS NEEDED)
    my $db_host = "127.0.0.1";  	# Change if needed	
    my $db_name = "yourdatabase";  	# Change to your actual database name
    my $db_user = "youuser";  		# Change to your actual user name
    my $db_pass = 'yourpassword';  	# Change to your actual password

    # Connect to MySQL
    my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host",
                           $db_user, $db_pass, { RaiseError => 1, PrintError => 1, AutoCommit => 1 });

    if (!$dbh) {
        return "Unknown Spell";
    }

    # Run the query
    my $query = "SELECT name FROM spells_new WHERE id = ?";
    my $sth = $dbh->prepare($query);
    $sth->execute($spell_id);

    # Fetch result
    my ($spell_name) = $sth->fetchrow_array();

    # Close DB connection
    $sth->finish();
    $dbh->disconnect();

    # Debug output
    if (!defined $spell_name) {
        return "Unknown Spell";
    }

    return $spell_name;
}


sub GetBuffsByLevel {
    my ($level) = @_;

    if ($level >= 1 && $level <= 44) {
        return (
            "Blessing of Temperance" => 4053,
            "Infusion of Spirit" => 3454,
            "Stamina" => 158,
            "Spirit of Bih'Li" => 2524,
            "Swift Like the Wind" => 172,
            "Boon of the Clear Mind" => 1694,
            "Brell's Steadfast Aegis" => 3578,
        );
    }
    elsif ($level >= 45 && $level <= 54) {
        return (
            "Ancient: Gift of Aegolism" => 2122,
            "Spirit of Bih'Li" => 2524,
            "Riotous Health" => 1595,
            "Deliriously Nimble" => 1594,
            "Talisman of Kragg" => 1585,
            "Gift of Insight" => 1409,
            "Aanya's Quickening" => 1708,
            "Clarity II" => 1693,
            "Brell's Steadfast Aegis" => 3578,
            "Strength of Nature" => 1397,
        );
    }
    elsif ($level >= 55 && $level <= 64) {
        return (
            "Hand of Virtue" => 3479,
            "Aura of Reverence" => 4108,
            "Kazad's Mark" => 3047,
            "Ward of Gallantry" => 3470,
            "Focus of the Seventh" => 3397,
            "Talisman of the Boar" => 3389,
            "Talisman of the Wrulan" => 3383,
            "Spirit of Bih'Li" => 2524,
            "Voice of Quellious" => 3360,
            "Vallon's Quickening" => 3178,
            "Brell's Stalwart Shield" => 3432,
            "Spirit of the Predator" => 3417,
            "Strength of Tunare" => 3487,
            "Ferocity" => 3463,
            "Spiritual Dominion" => 3460,
            "Spiritual Vigor" => 3456,
            "Blessing of the Nine" => 3451,
            "Blessing of Replenishment" => 3441,
        );
    }

    elsif ($level >= 65 && $level <= 74) {
        return (
            "Hand of Tenacity Rk. III" => 9811,
            "Elushar's Mark Rk. III" => 9808,
            "Aura of Purpose Rk. III" => 9781,
            "Ward of the Dauntless Rk. III" => 9714,
            "Specter of Renewal Rk. III" => 11781,
            "Talisman of the Dire Rk. III" => 10058,
            "Talisman of the Stoic One Rk. III" => 10031,
            "Talisman of Foresight Rk. III" => 10013,
            "Voice of Intuition Rk. III" => 10664,
            "Hastening of Ellowind Rk. III" => 10661,
            "Brell's Stony Guard Rk. III" => 10211,
            "Snarl of the Predator Rk. III" => 10115,
            "Strength of the Forest Stalker Rk. III" => 10100,
        );
    }
    elsif ($level >= 75) {
        return (
            "Unified Hand of Gezat Rk. III" => 34228,
            "Unified Hand of Certitude Rk. III" => 34246,
			"Talisman of the Courageous Rk. III" => 35459,
			"Talisman of the Steadfast Rk. III" => 34849,
			"Spirit of Dauntlessness Rk. III" => 35365,
			"Voice of Foresight Rk. III" => 36267,
			"Hastening of Sviir Rk. III" => 36223,
			"Brell's Steadfast Bulwark Rk. III" => 34415,
			"Granitebark Blessing Rk. III" => 34988,
			"Shout of the Predator Rk. III" => 34568,
			"Strength of the Bosquestalker Rk. III" => 34541,
			"Shared Merciless Ferocity Rk. III" => 36424,
			"Spiritual Unity Rk. III" => 40524,

        );
    }
    return ();
}
__________________
Eyenseq
Nowhere EQ
Reply With Quote