One Moose, Many Mooses, or is that Mise?

Now that I am getting into the game play part of AD&D I am finding there are more and more times where I want to keep a record of some attribute not just a running total.

For example after a successful encounter experience for monsters killed or defeated (making them run away is defeating them) and the value of all treasure taken in gold peices is summed and then split evenly across all party members, including henchmen and friendly NPC, who actively engaged in the encounter as Expreriance Points or EP for short.

Well a running tally for each player is needed, as well player always like to look back and see how bad ass they are so a history of encounters is needed, as well players like to know how many monsters they whacked in an encounter and of course the list goes on.

In the end I was creating all sorts of attributes to record the present or running value of some attribute but i was creating a history attribute as well to store the last values.

If only there was some way to just get the history without rewritiend all the accesoors over and over again. Well I was just about to start wtiting one wehn I checked out CPAN and typed in 'cpan attribute remember history Moose' and sure engouth such a module exists MooseX::RememberHistory

Ok so I had a look at it and it was about what I needed so here is the result.


use MooseX::RememberHistory;

has 'EP'=>(
is =>'rw',
isa =>'Int',
traits=>['RememberHostory'],

);


now I have a neat little 'trait' (a trait and a role are the same thing but in this case a 'trait' is a 'role' that is applied only to the attribute not the while class) that I can apply to any of the attributes that I need to keep a history of.

Here it is in action


$sir_guy->encounter();
$sir_guy->encounter();
$sir_guy->encounter();
$sir_guy->encounter();

foreach my $battle (@{$sir_guy->EP_history()}){
print "Total EP after encounter ".$battle."\n";
}
...

Total EP after encounter 0
Total EP after encounter 260
Total EP after encounter 359
Total EP after encounter 614
Total EP after encounter 1001

Though I am not there yet I could see something like this very useful in my 'Party' class


has 'encounters' =>(
        is	=>'rw',
	isa =>'Encounter',
	traits=>['RememberHostory'],
)
has 'members' =>(
        is	=>'rw',
	isa =>'Character',
	traits=>['RememberHostory'],
)

So no more extra typing for me just sugar the 'RememberHostory' trait whenever I need an attribute to keep its history. Sweet.

Funny these simple little things are morphing my 'attributes' from simple accessors to mini-classes within my class. I wonder how many more of these little majik gems I will add in as time goes by.


1o4c1350.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations