View Single Post
  #2  
Old 03-07-2018, 06:26 AM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default

... and once you have access to data, you can build objects to consume it. You can make complex things simple and have code you can reuse anywhere. You can use it in a script that fires from an EVENT subroutine. You can use the same framework for a web interface for your website. Whatever is clever.

I guess this is as much a concept as a tutorial. It's the sort of thing that would make life easier for folks to understand what they can do with Perl in addition to other methods. They write books about this shit for a reason!

<PerlDir>\site\lib\EQEmu\Mob.pm
Code:
package EQEmu::Mob;

use strict;
use warnings::register;
use Carp qw(confess);

# construction
sub new
{
    my $c = shift;
    my $p = length @_ == 1 && ref $_[0] ? shift : {@_};
    # required parameters
    foreach ('name', 'class') {
        exists $p->{$_}
            or confess("$_ is a required attribute");
    }
    # validate and initialize these attributes
    my $_name   = delete $p->{name};
    my $_class  = delete $p->{class};
    my $_health = delete $p->{health} || 100;
    $p->{_name}   = $c->_validateName($_name);
    $p->{_class}  = $c->_validateClass($_class);
    $p->{_health} = $c->_validateHealth($_health);
    # set flags
    $p->{_isClient} = ($c =~ /^Client/) || 0;
    $p->{_isBot}    = ($c =~ /^Bot/)    || 0;
    $p->{_isNPC}    = ($c =~ /^NPC/)    || 0;
    $c->_initDone($p) if $c =~ /^Mob/;
    return bless $p, $c;
}
sub _initDone
{
    my ($s, $p) = @_;
    my $c = ref $s || $s;
    foreach my $k (keys %{$p}) {
        next if $k =~ /^_/;
        warnings::warn("unhandled attribute [$k => ".$p->{$k}."] in $c");
    }
}

sub _validateName
{
    my ($self, $name) = @_;
    local $Carp::CarpLevel = $Carp::CarpLevel + 1;
    $name =~ /^#?[a-z_]*$/i
        or confess("invalid name [$name]");
    return $name;
}
sub _validateClass
{
    my ($self, $class) = @_;
    local $Carp::CarpLevel = $Carp::CarpLevel + 1;
    $class ~~ [1..16]  ||
    $class ~~ [20..35] ||
    $class ~~ [40..41] ||
    $class ~~ [59..64] ||
    $class ~~ [67..71]
        or confess("invalid class [$class]");
    return $class;
}
sub _validateHealth
{
    my ($self, $health) = @_;
    local $Carp::CarpLevel = $Carp::CarpLevel + 1;
    $health ~~ [0..100]
        or confess("invalid health % [$health]");
    return $health;
}

sub GetClass
{
    shift->{_class};
}
sub GetCleanName
{
    shift->{_name};
}
sub IsBot
{
    shift->{_isBot};
}
sub IsClient
{
    shift->{_isClient};
}
sub IsNPC
{
    shift->{_isNPC};
}
sub GetHPRatio
{
    shift->{_health};
}
sub GetGroup
{
    return 0;
}

1;
__________________
I muck about @ The Forge.
say(rand 99>49?'try '.('0x'.join '',map{unpack 'H*',chr rand 256}1..2):'incoherent nonsense')while our $Noport=1;
Reply With Quote