Acme-CoC-Dice

 view release on metacpan or  search on metacpan

lib/Acme/CoC/Dice.pm  view on Meta::CPAN

package Acme::CoC::Dice;

use strict;
use warnings;
use utf8;

use Acme::CoC::Util;
use Acme::CoC::Types;

use Carp qw/croak/;
use Smart::Args;

our $VERSION = '0.03';

sub role {
    args_pos
        my $self,
        my $command => {isa => 'command'},
    ;

    # MdN in $command can be separated to M/d/N, and M is the times of roling dice, N is the number of sided dice.
    return $self->role_skill(get_target($command)) if is_ccb($command);

    $command =~ /([1-9][0-9]*)d([1-9][0-9]*)/;
    my $role_result = {
        message => 'input invalid command',
    };
    return $role_result unless $command;

    my $times = $1 || 1;
    my $sided_dice = $2 || 100;
    my $results = [];
    my $sum = 0;

    for (1..$times) {
        my $rand_num = int(rand($sided_dice)) + 1;
        push @{ $results }, $rand_num;
        $sum += $rand_num;
    }

    $role_result = {
        dices => $results,
        sum => $sum,
    };
    return $role_result;
}

sub role_skill {
    args_pos
        my $self,
        my $target => {isa => 'Maybe[Int]', optional => 1},
    ;
    my $role = $self->role('1d100');
    if (defined $target) {
        if (is_extream($role->{dices}->[0], $target)) {
            $role->{result} = 'extream success';
        } elsif (is_hard($role->{dices}->[0], $target)) {
            $role->{result} = 'hard success';
        } elsif (is_failed($role->{dices}->[0], $target)) {
            $role->{result} = 'failed';
        } else {
            $role->{result} = 'normal success';
        }
    }
    return $role;



( run in 2.040 seconds using v1.01-cache-2.11-cpan-14f38c9f855 )