File Coverage

blib/lib/Acme/CoC/Dice.pm
Criterion Covered Total %
statement 43 46 93.4
branch 8 12 66.6
condition 2 4 50.0
subroutine 9 9 100.0
pod 1 1 100.0
total 63 72 87.5


line stmt bran cond sub pod time code
1             package Acme::CoC::Dice;
2              
3 2     2   104003 use strict;
  2         13  
  2         58  
4 2     2   11 use warnings;
  2         3  
  2         46  
5 2     2   629 use utf8;
  2         17  
  2         54  
6              
7 2     2   902 use Acme::CoC::Util;
  2         5  
  2         1549  
8 2     2   1192 use Acme::CoC::Types;
  2         6  
  2         122  
9              
10 2     2   15 use Carp qw/croak/;
  2         3  
  2         108  
11 2     2   13 use Smart::Args;
  2         58  
  2         831  
12              
13             our $VERSION = '0.03';
14              
15             sub role {
16 8     8 1 17398 args_pos
17             my $self,
18             my $command => {isa => 'command'},
19             ;
20              
21             # 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.
22 8 100       106 return $self->role_skill(get_target($command)) if is_ccb($command);
23              
24 5         22 $command =~ /([1-9][0-9]*)d([1-9][0-9]*)/;
25 5         14 my $role_result = {
26             message => 'input invalid command',
27             };
28 5 50       12 return $role_result unless $command;
29              
30 5   50     21 my $times = $1 || 1;
31 5   50     13 my $sided_dice = $2 || 100;
32 5         9 my $results = [];
33 5         8 my $sum = 0;
34              
35 5         30 for (1..$times) {
36 14         32 my $rand_num = int(rand($sided_dice)) + 1;
37 14         18 push @{ $results }, $rand_num;
  14         21  
38 14         23 $sum += $rand_num;
39             }
40              
41             $role_result = {
42 5         16 dices => $results,
43             sum => $sum,
44             };
45 5         13 return $role_result;
46             }
47              
48             sub role_skill {
49 3     3   57 args_pos
50             my $self,
51             my $target => {isa => 'Maybe[Int]', optional => 1},
52             ;
53 3         500 my $role = $self->role('1d100');
54 3 100       9 if (defined $target) {
55 2 50       7 if (is_extream($role->{dices}->[0], $target)) {
    50          
    50          
56 0         0 $role->{result} = 'extream success';
57             } elsif (is_hard($role->{dices}->[0], $target)) {
58 0         0 $role->{result} = 'hard success';
59             } elsif (is_failed($role->{dices}->[0], $target)) {
60 0         0 $role->{result} = 'failed';
61             } else {
62 2         5 $role->{result} = 'normal success';
63             }
64             }
65 3         12 return $role;
66             }
67              
68             1;
69             __END__
70              
71             =encoding utf-8
72              
73             =head1 NAME
74              
75             Acme::CoC::Dice - Dice role module for CoC TRPG.
76              
77             =head1 SYNOPSIS
78              
79             use Acme::CoC::Dice;
80              
81             my $dice_role = Acme::CoC::Dice->role('1d100');
82             print $dice_role->{dices}; # this property can have some result with giving parameter as '2d6'.
83             print $dice_role->{sum};
84              
85             =head1 DESCRIPTION
86              
87             Acme::CoC::Dice is getting random number like 1d100.
88              
89             =head1 METHODS
90              
91             =head2 C<< role >>
92              
93             Gets random number like dice roling.
94             Format is "ndm" ("n" and "m" is Natural number). For example, it's like "1d6".
95              
96             my $result = Acme::CoC::Dice->role('1d6');
97              
98             =head2 C<< role_skill >>
99              
100             Runs "role" with giving "1d100". Usually we can play dice as "1d100" for using skill on CoC-TRPG.
101             This method is for it.
102              
103             my $result = Acme::CoC::Dice->role_skill;
104             my $result = Acme::CoC::Dice->role_skill(50); ## 50 is given for success threshold.
105              
106             =head1 AUTHOR
107              
108             bedoshi
109              
110             =head1 COPYRIGHT
111              
112             Copyright 2021- bedoshi
113              
114             =head1 LICENSE
115              
116             This library is free software; you can redistribute it and/or modify
117             it under the same terms as Perl itself.
118              
119             =cut