PDA

View Full Version : stupid perl tricks, vol. 1


c0ncrete
11-27-2012, 08:29 PM
i have a migraine, so i decided to share.

plugins\client_first.pl
use Scalar::Util qw(blessed);

# insert missing $client object as first parameter passed to subroutine
# usage: @_ = plugin::ClientFirst(@_);
sub ClientFirst
{
unshift(@_, plugin::val('$client'))
if ((blessed($_[0]) // '') ne 'Client');
@_;
}example usage (in plugins\check_hasitem.pl):
#checks to see if player has item
#useage plugin::check_hasitem($client, itemid);
sub check_hasitem
{
@_ = plugin::ClientFirst(@_);
my $client = shift;
my $itmchk = shift;
# yada yada
...;
}
with this addition, either of these would be valid:

my $found = plugin::check_hasitem($client, $itemid);
my $found = plugin::check_hasitem($itemid);

c0ncrete
12-13-2012, 03:13 PM
this plugin will reduce the length of quest conditions where multiple qglobals are checked in a single statement.

# checks that quest globals are defined and set to values passed in parameters
# usage: my $flagged = plugin::IsFlagged(key=>val, ...);
sub IsFlagged
{
my %flag = @_;
return 0 unless %qglobals;
while (my ($key, $val) = each %flag) {
return 0 unless defined $qglobals{$key} and $qglobals{$key} == $val;
}
1;
}this, for example:
if (defined $qglobals{bic} && $qglobals{bic} == 10
&& defined $qglobals{bic_fer} && $qglobals{bic_fer} == 11
&& defined $qglobals{bic_riw} && $qglobals{bic_riw} == 10
&& defined $qglobals{bic_bar} && $qglobals{bic_bar} == 6
&& defined $qglobals{bic_qin} && $qglobals{bic_qin} == 4) {
# do whatever here
}becomes this:
my $flagged = plugin::IsFlagged(bic=>10, bic_fer=>11, bic_riw=>10, bic_bar=>6, bic_qin=>4);
if ($flagged) {
# do whatever here
}or this:
if (plugin::IsFlagged(bic=>10, bic_fer=>11, bic_riw=>10, bic_bar=>6, bic_qin=>4)) {
# do whatever here
}

sorvani
12-13-2012, 03:52 PM
the example is bic_qin == 4 and your plugin only actually looks like it checks == 4 that is great, but what about needing to check >= 4 ?

c0ncrete
12-13-2012, 04:37 PM
valid point.

sub IsFlagged
{
my %flag = @_;
return 0 unless %qglobals;
while (my ($key, $val) = each %flag) {
if (ref $val eq 'HASH') {
my $op = $key;
unless ($op ~~ ['==', '>=', '>', '<=', '<', '!=']) {
warn "invalid operator ($key) found in plugin::IsFlagged()";
warn "skipped ".keys(%$val)." check(s)";
next;
}
while (my ($key, $val) = each %{$val}) {
return 0 unless defined $qglobals{$key}
and eval "$qglobals{$key} $op $val";
}
}
else {
return 0 unless defined $qglobals{$key}
and $qglobals{$key} == $val;
}
}
1;
}now you can pass an operator as a key with an anonymous hash reference as a value (containing all the key/value pairs you want to check using that operator). if no operator is found (well, it's actually checking for a hash reference as a value), it defaults to ==.

example:
my $flagged = plugin::IsFlagged('>=' => {bic_quin => 4}, bic_fer => 11, bic_riw => 10);would make sure $qglobals{bic_quin} was greater than or equal to 4, and that $qglobals{bic_fer} was equal to 11 and $qglobals{bic_riw} was equal to 10.

EDIT:
changed it to where it will only accept numeric comparison operators. that way an accidental assignment won't happen in eval.

c0ncrete
12-16-2012, 10:02 PM
the majority of the additional lines come from my breaking up the quest::say() and quest::emote() text to a maximum width of 80 columns. the horizontal scrolling you're likely experiencing is what happens when you put long strings in a single line. other than that, i didn't bother correcting any of the text.

most of the logic is handled in a single foreach loop, which has very readable syntax because of how the $items hashref is constructed.

before (bloodfields/Barowsar.pl):
sub EVENT_ITEM
{
%rhands = ("Monk" => 70786, "Beastlord" => 70842, "Druid" => 70779);
%rwrists = ("Monk" => 70785, "Beastlord" => 70841, "Druid" => 70778);
%rarms = ("Monk" => 70784, "Beastlord" => 70840, "Druid" => 70777);
%rhead = ("Monk" => 70783, "Beastlord" => 70839, "Druid" => 70776);
%rchest = ("Monk" => 70788, "Beastlord" => 70844, "Druid" => 70781);
%rlegs = ("Monk" => 70789, "Beastlord" => 70845, "Druid" => 70782);
%rfeet = ("Monk" => 70787, "Beastlord" => 70843, "Druid" => 70780);
my $ihands = $rhands{$class};
my $iwrists = $rwrists{$class};
my $iarms = $rarms{$class};
my $ihead = $rhead{$class};
my $ichest = $rchest{$class};
my $ilegs = $rlegs{$class};
my $ifeet = $rfeet{$class};
if($faction <= 2) #must be warmly to do this quest
{
if (plugin::check_handin(\%itemcount, 51446 => 1, 51467 => 2))
{
quest::say("Fantastic work $race . The loss of the shard should prevent the opportunity we were waiting for. Please take these and wear them with pride."); #made up
quest::exp(100000);
quest::summonitem($ihands);
}
if (plugin::check_handin(\%itemcount, 51445 => 1, 51466 => 2))
{
quest::say("The Blood Standard of Dranik! I did not believe it still existed! This will bring hope to the few dragorn who still resist Mata Muram. I only wish I was ready to join them. Thank you. As promised, here is a leather wristguard as a reward.");
quest::exp(100000);
quest::summonitem($iwrists);
}
if (plugin::check_handin(\%itemcount, 51444 => 1, 51465 => 2))
{
quest::emote("looks at the map with yearning, 'Thankyou young $race, just seeing this brings some hope to my heart that one day what was may be again. Take these sleeves and use them to bring down the muramite."); #made up
quest::exp(100000);
quest::summonitem($iarms);
}
if (plugin::check_handin(\%itemcount, 51440 => 1, 51461 => 2))
{
quest::emote("takes the book and scrolls from you gratefully, 'Excellent, with these writings I should be able to begin to understand discord, hopefully giving me information we can use to our advantage. Here $name, take this as promised."); #made up
quest::exp(100000);
quest::summonitem($ihead);
}
if (plugin::check_handin(\%itemcount, 51441 => 1, 51462 => 3))
{
quest::say("He lives yet! You see it glows still, if only faintly.' Barowsar pauses for a moment in quite contemplation before continuing. 'Here $name, this was my finest work, and you deserve no loss for the favor you have done me this day."); #made up
quest::exp(100000);
quest::summonitem($ichest);
}
if (plugin::check_handin(\%itemcount, 51442 => 1, 51463 => 3))
{
quest::emote("quickly conceals the ember within the bowls. 'Good work. I will lay this ember to rest, so that the spirits of this forsaken place may rest at last. As promises here is your reward."); #made up
quest::exp(100000);
quest::summonitem($ilegs);
}
if (plugin::check_handin(\%itemcount, 51443 => 1, 51464 => 2))
{
quest::say("Well done $name. I understand the Muramites guard these well. I'll bury this stone so the dragorn of Kuua can move on from the horrors of the past and instead look to the future. As promised, here is a pair of finely crafted leather boots as your reward.");
quest::exp(100000);
quest::summonitem($ifeet);
}
else
{
plugin::return_items(\%itemcount);
quest::say("I am sorry $name, but these are not the items i was looking for."); #made up text
}
}
else
{
plugin::return_items(\%itemcount);
quest::emote("returns the items to you without responding."); #made up text
}
}after:

sub earned
{
unshift @_, plugin::var('itemcount');
plugin::check_handin(@_);
}

sub give
{
quest::exp(100000);
quest::summonitem(shift);
}

sub EVENT_ITEM
{
if ($faction <= 2 and $class =~ /^dru|mon|bea/) {
my $items = {
head => {
earned => sub { earned(51440 => 1, 51461 => 2) },
Druid => 70776, Monk => 70783, Beastlord => 70839,
spam => sub { quest::emote(
"takes the book and scrolls from you gratefully, " .
"'Excellent, with these writings I should be able to " .
"begin to understand discord, hopefully giving me " .
"information we can use to our advantage. Here $name, " .
"take this as promised."
) }
},
chest => {
earned => sub { earned(51441 => 1, 51462 => 3) },
Druid => 70781, Monk => 70788, Beastlord => 70844,
spam => sub { quest::say(
"He lives yet! You see it glows still, if only faintly.' " .
"Barowsar pauses for a moment in quite contemplation " .
"before continuing. 'Here $name, this was my finest work, ".
"and you deserve no loss for the favor you have done me " .
"this day."
) }
},
legs => {
earned => sub { earned(51442 => 1, 51463 => 3) },
Druid => 70782, Monk => 70789, Beastlord => 70845,
spam => sub { quest::emote(
"quickly conceals the ember within the bowls. 'Good work. ".
"I will lay this ember to rest, so that the spirits of " .
"this forsaken place may rest at last. As promises here " .
"is your reward."
) }
},
feet => {
earned => sub { earned(51443 => 1, 51464 => 2) },
Druid => 70780, Monk => 70787, Beastlord => 70843,
spam => sub { quest::say(
"Well done $name. I understand the Muramites guard these " .
"well. I'll bury this stone so the dragorn of Kuua can " .
"move on from the horrors of the past and instead look to ".
"the future. As promised, here is a pair of finely " .
"crafted leather boots as your reward."
) }
},
arms => {
earned => sub { earned(51444 => 1, 51465 => 2) },
Druid => 70777, Monk => 70784, Beastlord => 70840,
spam => sub { quest::emote(
"looks at the map with yearning, 'Thankyou young $race, ".
"just seeing this brings some hope to my heart that one ".
"day what was may be again. Take these sleeves and use " .
"them to bring down the muramite."
) }
},
wrist => {
earned => sub { earned(51445 => 1, 51466 => 2) },
Druid => 70778, Monk => 70785, Beastlord => 70841,
spam => sub { quest::say(
"The Blood Standard of Dranik! I did not believe it still ".
"existed! This will bring hope to the few dragorn who " .
"still resist Mata Muram. I only wish I was ready to join ".
"them. Thank you. As promised, here is a leather wrist-" .
"guard as a reward."
) }
},
hands => {
earned => sub { earned(51446 => 1, 51467 => 2) },
Druid => 70779, Monk => 70786, Beastlord => 70842,
spam => sub { quest::say(
"Fantastic work $race. The loss of the shard should " .
"prevent the opportunity we were waiting for. Please take ".
"these and wear them with pride." # /boggle
) }
}
};
foreach my $slot (keys %$items) {
my $reward = $items->{$slot};
$reward->{spam}() and
give $reward->{$class} and
last if $reward->{earned}();
}
}
plugin::return_items(\%itemcount);
}