This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThroneGen.pm
123 lines (99 loc) · 2.28 KB
/
ThroneGen.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package ThroneGen;
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;
use Carp;
use JSON qw/encode_json/;
use List::Util qw/any/;
use POSIX qw/ceil/;
use feature 'say';
use ThroneGen::Throne;
use ThroneGen::DM;
has 'num_thrones' => (
is => 'ro',
isa => 'Int',
default => 5,
);
has 'thrones' => (
is => 'ro',
isa => 'ArrayRef[ThroneGen::Throne]',
builder => '_generate_thrones',
init_arg => undef,
lazy => 1,
);
sub _generate_thrones {
my $self = shift;
my $pts_per_throne = 4;
local $_;
my @thrones;
my $num_darkvis_thrones = 0;
my $max_darkvis_thrones = ceil( $self->num_thrones / 8 );
for ( 1..$self->num_thrones ) {
my $throne_candidate = ThroneGen::Throne->new(
pts => $pts_per_throne,
disallowed_names => [ map { $_->name } @thrones ],
);
# ensure there's not too many darkvision thrones
# this should probably be implemented as a TG::Power member
if ( any { defined $_->dm_claimed && $_->dm_claimed =~ m/\#blessdarkvis / } @{$throne_candidate->powers} ) {
if ( $num_darkvis_thrones + 1 > $max_darkvis_thrones ) {
carp "too many darkvis thrones (already had $num_darkvis_thrones/$max_darkvis_thrones): regenerating last one";
redo;
} else {
$num_darkvis_thrones++;
}
}
push @thrones, $throne_candidate;
}
return \@thrones;
}
#
# Output
#
sub print_thrones {
my $self = shift;
for my $throne ( @{$self->thrones} ) {
say $throne->name;
say " " . $_->title for ( $throne->outputfriendly_powers );
say "";
}
}
sub as_json {
# print as JSON, ready to use by the svelte web interface
my $self = shift;
# thrones
my @json_thrones;
for my $throne ( @{$self->thrones} ) {
my $json_throne = {
name => $throne->name,
pts => $throne->pts,
powers => [
map { {
pts => $_->pts,
title => $_->title,
} } $throne->outputfriendly_powers
]
};
push @json_thrones, $json_throne;
}
# dm JSON
my $dm_content;
open( my $fh, '>', \$dm_content) or die $!;
$self->write_dm( $fh );
close $fh or die $!;
# return
return encode_json( {
thrones => \@json_thrones,
dm => $dm_content,
} );
}
sub write_dm {
my ( $self, $fh ) = @_;
my $dm = ThroneGen::DM->new(
thrones => $self->thrones,
fh => $fh,
);
$dm->write();
}
__PACKAGE__->meta->make_immutable;
1;