Docendo discimus

$self->explain

  • Calendar

    November 2017
    M T W T F S S
    « Sep    
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  
  • Archives

  • Recent Posts

  • Bling

Posts Tagged ‘moose’

Vedalken: the Magic Librarian

Posted by brunorc on April 5, 2010

Dawn's Reflection

Dawn's Reflection - a simple, one color spell, but it makes your life really colorful!

Finally I decided to get back to my Magic DB and describe the process of creating it here, on my blog. So here we go:

mkdir vedalken
cd vedalken
catalyst.pl Vedalken

Marisi's Twinclaws

Marisi's Twinclaws - Multicolor with a choice; and a double-strike killer

Now I have the skeleton for the Catalyst app, so it can be stored in the git repository. Why? Simply because it’s a good idea to store the code in the repository. I have to get some sleep after being completely killed after the Tangerine Dream concert in Zootermeer, so there’s no time to elaborate on that now. So, while I’m still in ‘vedalken’ – the main directory of my project – I type:

git init

Now the empty Git repository has been created in the current directory. Since it’s empty, I want to add my Catalyst skeleton to it:

git add -A

The -A flag stands for “all”, as we can be told by git help add (every command of git is nicely explained in git help command). All files are now indexed by Git, so it will pay attention to all the changes, introduced to them. Adding a file is also a change; and changes have to be committed – a commit actually stores changes in the Git repository.

Blaze

Blaze - A spell with variable cost

git commit -a -m "Initial commit; created the Catalyst skeleton"

This time is the -a flag which means “all”; the -m flag specifies the commit message. If it was ommitted, the default editor would be launched, since a commit without message is barely useless…

Reaper King

Reaper King - How much for the color?

Now I can use all the power of Git to maintain the project and keep it growing. My plan is to have a list of cards I own, with a list of decks. And since Magic is a collectible card game, a wishlist may be a nice addition…

cd Vedalken
script/vedalken_create.pl controller Card
script/vedalken_create.pl controller Deck

A bunch of views may be useful:

script/vedalken_create.pl view ViewTT TT
script/vedalken_create.pl view ViewJSON JSON

I don’t have a model yet, since I was thinking for quite a long time about the representation of mana cost – without finding any good solution. Mana cost is the amount of mana (magical energy) you have to spend to cast the particular spell. If you take a look at Dawn’s Reflection, you will see that you have to spend one green mana and three mana of any color (you can spend four green mana – it’s also OK) to cast it.

Budoka Gardener

Budoka Gardener - or Dokai, Weaver of Life

That is easy. But then we have something more complicated – Marisi’s Twinclaws.

To summon those kitties you have to pay one green mana, one red or white mana, and two mana of any color. This may be quite important, because the colors of mana define the color of the card. If the spell may “destroy target white creature”, it will smash your Twinclaws, even if you didn’t use white mana to cast it.

Boom or Bust

Boom or Bust - you choose

Another important aspect of mana is the CMC, which means “converted mana cost”. Both Dawn’s Reflection and Marisi’s Twinclaws have CMC equal to 4. But then comes the Blaze.

Surprisingly, the CMC of Blaze is 1, since you *can* actually cast it for one red mana; it just won’t harm anyone, because of dealing 0 damage (there may be a situation, when such move makes sense). But the X cost is important, since it has to be – for instance – graphically rendered on the page.

This way we come to the pinacle of mana representation problems: the Reaper King.

To have Reaper King on your side, you have to spend one mana of each of the five Magic colors. No blue mana? No problem, just pay two mana of any color. That way this card has CMC equal to 10.

I wanted to have a system of storing mana cost in such a way, that would allow me to:

  • represent the “mixed” mana,
  • represent the “one-or-many” mana,
  • filter cards by color,
  • automatically calculate the mana cost.

I’m going to use Moose to achieve it, since Moose classes and attributes allow to hide all the complexity (or gory details) under a nice interface. I hope it will also make it possible to represent the flip cards from Kamigawa block and multi-part cards:

package Vedalken::Mana;
use Moose;

has [ qw/white green red blue black colorless variable/ ] => (
    is => 'rw',
    isa => 'Int',
    default => 0,
);

It may be a little bit overkill (if any visitor has any advice, I’d like to improve this design), but that way Moose will take care of checking all the keys and values on runtime. Also, it makes it easier to specify the list of attributes that share the same attributes. Below I include a commented (using the multiline “=begin/=end” comment) example of another way, based on TypeConstraints.

package Vedalken::Spell;
use Moose;
use Moose::Util::TypeConstraints;

enum 'SpellType' = qw/
    Artifact
    Basic
    Creature
    Enchantment
    Instant
    Land
    Legendary
    Planeswalker
    Plane
    Snow
    Sorcery
    Tribal
    World
/;

enum 'Rarity' = ( qw/
    Rare
    Uncommon
    Common
    Land
    Special
    Promo
/, "Mythic Rare" );

=begin Alternative

my %colors = map { $_ => 1 } qw/
    White
    Red
    Green
    Blue
    Black
    Colorless
    Variable
/;

subtype 'Mana'
    => as 'Hash',
    => where {
        not grep { not exists $colors{$_} } keys %$_
        and
        not grep { $_ !~ /^[0-9]+$/ } values %$_
    };

=end Alternative

has name => (
    is => 'rw',
    isa => 'Str',
);

has mana_cost => (
    is => 'rw',
    isa => 'Array[Vedalken::Mana]', # or 'Array[Mana]'
);

has cmc => (
    is => 'rw',
    isa => 'Int',
);

has types => (
    is => 'rw',
    isa => 'Array[SpellType]',
);

has subtypes => (
    is => 'rw',
    isa => 'Array[SpellSubtype]',
);

has [ qw/text flavor/ ] => (
    is => 'rw',
    isa => 'Str',
);

has [ qw/power toughness/ ] => (
    is => 'rw',
    isa => 'Int',
);

has rarity => (
    is => 'rw',
    isa => 'Rarity',
);

package Vedalken::Card;
use Moose;

has name => (
    is => 'rw',
    isa => 'Str',
);

has spells => (
    is => 'rw',
    isa => 'HashRef[Vedalken::Spell]',
);

has quantity => (
    is => 'rw',
    isa => 'Int',
);

That way one card can have many spells (we shouldn’t need more than two, but it’s Magic, so you never know for sure). Later we can add a method which would tell us if the card includes two completely independent spells, or it’s a flip card (Kamigawa specific).

package Vedalken::Collection;
use Moose;

has items => (
    is => 'rw',
    isa => 'HashRef[CollectionItem]',
);

has quantity => (
    is => 'rw',
    isa => 'Int',
);

The generic Collection can serve as a basis for the Deck object, as well as for the wishlist and the whole collection.

The advantage of using Moose is – again – that you declare in your package all the information about types (which get checked on runtime) and coertion from other types (so you can expect an Object of type Vedalken::Whatever, but still be able to get an ordinary hash and use it, as long as it meets all conditions). Yeah, in Java or C++ you take it for granted. But on the other hand, it’s still Perl, so you can use all its magic and power plus the joys of strong typing. You declare more, but you write less code. Code, which is usually just the reinvented wheel for checking, if the user input is an integer.

Advertisements

Posted in Moose | Tagged: , , , , | Leave a Comment »

YAPC::EU 2009 recap

Posted by brunorc on August 9, 2009

Four days packed with Perl, Portuguese food, warm Chartreuse, cold beer and even more Perl! It was really nice to meet Legendary Perl Creatures, see some friends and got to know more Perl people.

But what was even more important, was the content. Everyone was using words like Moose, Catalyst, DBIC or five-dot-ten (well, some well known hackers were also using other words, starting with 0xF), and presentations were full of new ideas. It was not about some magic artefacts, that can explode in your hand if you tamper with them in the wrong way. It was not about CGI. It was not about Perl obfuscation and the (ab)use of some fancy and “meaningful” operators.

Instead I saw Damian Conway talking about Regexp::Grammars. You’ve probably heard about Ruby and maybe even get fed up with the whole DSL stuff. Now you can build the parser and get the abstract tree – all using meaningful regular expressions. Yes, meaningful regular expressions. Thanks to the named captures you don’t have to rely on $1, $2, ... variables. But in Regexp::Grammars you can use named subrules as well, so your parser really doesn’t have to be hairy. Oh, and you can even make it easy to debug, as it is able to notify you about errors. Then you get a nice, minimalistic syntax tree.

Yes, I agree, most of us don’t spend their days writing parsers. But still, there’s a lot of nice things to be discovered. Paul Fenwick went with a talk about awesome things in Perl. Of course he mentioned Moose, but I was really surprised with the autobox and autodie things. Such things show how Perl evolves – in contrary to its critics, apparently, who still find it relevant to point out that you have to manually unroll the @_ list.

Of course you have to, unless you use Moose with MooseX::Declare! Ah, you have to use it… is it a problem? Well, you can write your own solution by hand if you don’t trust this guy. Or you can start to complaint, because it’s not part of the core of Perl. And it should be, since it is so important for oneliners you launch from your command line, isn’t it?

OK, we don’t have to unroll the @_ anymore. Also, we can use threads. And exceptions. And they are not part of the core again. But you know what? The topic of this YAPC was Corporate Perl. And there were people from Cisco, Nagra, Opera, Booking, Sapo – and they went to tell us how they use Perl and want to use it even more, so they would like to hire more Perl developers. And apparently they weren’t worried about exceptions being used from a module. Looks like their main concern is how to get more and more talented, open-minded Perl programmers, because – yes, you guessed it – they are not the part of the core and you still have to unroll them manually.

Oh really? There was a bunch of famous Perl hackers, but there also was an awful lot of people from mailing lists, IRC channels, Perl Mongers communities. And they may be part of the core, as there is a lot of projects that don’t require a rocket-science level, so everyone can participate. Also, they went to YAPC to learn something, to get some books, to discover new things.

I especially liked the Thursday, with the workshop led by two gentlemen from Shadowcat Systems. There were some bits about Catalyst, Moose and DBIx::Class and a lot of Royal Navy spirit, provided by inimitable, faster than a shutter Matt S. Trout, who also signed (as a co-author) a copy of the new book about Catalyst that was sold for some horrible amount of the European currency, but you can get one from Amazon for the regular retail price; no fancy stuff from Matt though, but you can still carry it to the next YAPC in Pisa (get familiar with the city, attend the Italian Perl Workshop in October, it’s really worthy) and ask him to sign it, making him feel like a Tom Jones.

And last but not least – Jose Castro gave a brilliant keynote, ending it with a phrase we somehow felt in the air: Perl is Alive and Kicking, and Stronger than ever!

Posted in Catalyst, Moose, Perl | Tagged: , , , , , , , , , | 1 Comment »

Generating HTML with Moose, part 2

Posted by brunorc on November 18, 2008

Previous part wasn’t an example of Good Desing (TM). According to my promises, this will be sorted out a little bit.

Instead of using the inheritance in all cases, I’ll switch to Roles. Role is somehow similar to what one can know as interface in Java. In the case of HTML generation it would be nice to use role “Renderizable”:

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

{
    package Renderizable;

    use Moose::Role;

    requires 'get_content';

    sub render {
        my $self = shift;
        return '<div>' . $self->get_content . '</div>';
    }
}

This role puts the requirement for the method get_content – it means every class which wants to use this role must implement (or inherit) this method.

Since there’s no need to use inheritance chain, I put classes on the same level:

{
    package Container::Section;

    use Moose;

    with 'Renderizable';

    has 'content' => (
        is => 'rw',
        isa => 'Str',
        default => 'I love Moose!',
    );

    sub get_content {
        return shift->content;
    }
}

{
    package Container::Page;

    use Moose;

    with 'Renderizable';

    has 'title' => (
        is => 'rw',
        isa => 'Str',
        default => 'Moose rulez!'
    );

    has 'body' => (
        is => 'rw',
        isa => 'ArrayRef[Container::Section]',
        auto_deref => '1'
    );

    sub get_content {
        my $self = shift;
        print join '', map { $_->render . "\n" } $self->body;
    }

    before 'render' => sub {
        my $self = shift;
        say '<html><head><title>' . $self->title
            . '</title></head><body>';
    };

    after 'render' => sub {
        say '</body></html>';
    };
}

Now we can see it in action:

my $block = Container::Section->new(
    { content => 'This is an example of the Moose power!' }
);
my $defblock = Container::Section->new;

my $page = Container::Page->new;
$page->body( [ $block, $defblock ] );

$page->render;

This is nice, but sometimes one may want to put some dynamically generated content. It can be some piece of Perl code (we leave the security apart for a moment…).

{
    package Container::Section::Evaluable;

    use Moose;

    extends 'Container::Section';

    has '+content' => (
        default => 'localtime()'
    );

    sub get_content {
        my $self = shift;

        return eval $self->content;
    }
}

Here the attribute content is specialized (with the ‘+’ sign) to have some default value. Specialization can only restrict things (e.g. one cannot specialize isa => ‘Int’ to ‘Num’).

But let’s say there is some value that doesn’t need to be recalculated on every request. Even more, we are happy if it’s calculated only once. We can benefit from lazy attribute, which in turn depends on default value or builder method.

{
    package Container::Section::Evaluable::Once;

    use Moose;

    extends 'Container::Section::Evaluable';

    has 'lazy_content' => (
        isa => 'Str',
        is => 'rw',
        lazy => 1,
        builder => '_builder',
    );

    sub _builder {
        return eval shift->content;
    }

    sub get_content {
        return shift->lazy_content;
    }
}

The lazy_attribute will be initialized with the first call to get_content. And since it has value, there’s no need to burden the server with heavy calls to localtime.

Now we can check if it works properly…

my $block = Container::Section->new(
    { content => 'This is an example of the Moose power!' }
);
my $defblock = Container::Section->new;
my $eval = Container::Section::Evaluable->new;
my $evalonce = Container::Section::Evaluable::Once->new(
    { content => 'localtime()' }
);

my $page = Container::Page->new;
$page->body( [ $block, $defblock, $eval, $evalonce ] );

$page->render;

say "\n" . ( '-' x 24 );
sleep 5;

$page->render;

I would be glad to read all your comments, rants and corrections!

Posted in Moose | Tagged: , , , , , | Leave a Comment »