lib/Game/FaceGenerator.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.3
CC
1
Cov
83%
BEGIN
1.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Game/FaceGenerator/Core.pm ↑ Summary script/face-generator »
1 #!/usr/bin/env perl
2  
3 # Copyright (C) 2015-2018 Alex Schroeder <alex@gnu.org>
4  
5 # This program is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # this program. If not, see <http://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Game::FaceGenerator - a web app to combine random images into faces
22  
23 =head1 DESCRIPTION
24  
25 Face Generator is a web application which uses random images to create faces.
26  
27 On a technical level, Face Generator is a web app based on the Mojolicious
28 framework. This class in particular uses L<Mojolicious::Lite>.
29  
30 See L<Mojolicious::Guides> for more information.
31  
32 =cut
33  
34 package Game::FaceGenerator;
35  
36 our $VERSION = 1.04;
37  
38 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Mojolicious::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 0 use Game::FaceGenerator::Core qw(
42 dir no_flip all_artists random_components all_components
43 1 ▶ all_elements render_components move);
Subroutine BEGIN: called CC=1 SCAR=1.2
44 use Cwd;
45  
46 # Commands for the command line!
47 push @{app->commands->namespaces}, 'Game::FaceGenerator::Command';
48  
49 =head1 CONFIGURATION
50  
51 As a Mojolicious application, it will read a config file called
52 F<face-generator.conf> in the current directory, if it exists. As the default
53 log level is 'debug', one use of the config file is to change the log level
54 using the C<loglevel> key, and if you're not running the server in a terminal,
55 using the C<logfile> key to set a file.
56  
57 The random elements for faces are stored in the F<contrib> directory. You can
58 change this directory using the C<contrib> key. By default, the directory
59 included with the distribution is used. Thus, if you're a developer, you
60 probably want to use something like the following to use the files from the
61 source directory.
62  
63 {
64 loglevel => 'debug',
65 logfile => undef,
66 contrib => 'share',
67 };
68  
69 If you run Face Generator and you have artists contributing face elements, you
70 might be interested in granting them access to a simple image editing interface.
71 There, they shift elements up, down, left and right, and so on. In order to
72 allow this, you can add users to the config file.
73  
74 If you run the application in production, you should change the c<secret>. This
75 is used to protect cookies from tampering. The cookie is where people with a
76 user account store their username and password, so changing the secret is an
77 additional protection.
78  
79 Here's an example of how to set up C<secret> and C<users>:
80  
81 {
82 secret => '*a random string*',
83 users => {
84 'alex' => '*secret*',
85 'berta' => '*secret*',
86 },
87 }
88  
89 When these users edit images online, Face Generator adds a background image to
90 makes it easier for artists to decide where elements need to be placed exactly
91 in relation to everything else. The default background image is F<empty.png>.
92  
93 You can specify the background image to use via the URL parameter C<empty>. It
94 must name an image in the F<contrib> directory.
95  
96 Example:
97  
98 https://campaignwiki.org/face/debug/alex/eyes_dragon?empty=dragon.png
99  
100 You can specify the background image via the config file, too. There, a given
101 type is assigned a background image:
102  
103 {
104 secret => '*a random string*',
105 users => {
106 'alex' => '*secret*',
107 'tuiren' => '*secret*',
108 },
109 empty => {
110 tuiren => {
111 gnome => 'dwarf.png',
112 },
113 alex => {
114 dragon => 'dragon.png',
115 elf => 'elf.png',
116 dwarf => 'dwarf.png',
117 gnome => 'dwarf.png',
118 demon => 'demon.png',
119 },
120 },
121 }
122  
123 As you can see, in a few cases the artists are using a different background
124 image.
125  
126 Usually, Face Generator uses all the image elements provided both as-is and
127 flipped horizontally. Sometimes, that doesn't work. The C<dragon> and C<demon>
128 images, for example, face sideways. You can't just flip elements for these
129 images. Flipping can be prevented using the C<no_flip> key in the config file.
130  
131 {
132 secret => '*a random string*',
133 users => {
134 'alex' => '*secret*',
135 'tuiren' => '*secret*',
136 },
137 empty => {
138 alex => {
139 dragon => 'dragon.png',
140 elf => 'elf.png',
141 dwarf => 'dwarf.png',
142 gnome => 'dwarf.png',
143 demon => 'demon.png',
144 },
145 tuiren => {
146 gnome => 'dwarf.png',
147 },
148 },
149 no_flip => {
150 alex => [
151 'dragon',
152 'demon'
153 ],
154 },
155 }
156  
157 For both the C<empty> and C<no_flip> key, the value is again a hash reference
158 with the keys being the users specified for the C<users> key. In the examples
159 above, C<alex> and C<tuiren> are users, and both use a different background
160 image for some of their image elements, and one of them has image elements that
161 cannot be flipped.
162  
163 =cut
164  
165 plugin 'Config' => {
166 default => {
167 users => {},
168 empty => {},
169 loglevel => 'debug',
170 logfile => undef,
171 contrib => undef,
172 },
173 file => getcwd() . '/face-generator.conf',
174 empty => {
175 tuiren => {
176 gnome => 'dwarf.png' },
177 alex => {
178 dragon => 'dragon.png',
179 elf => 'elf.png',
180 dwarf => 'dwarf.png',
181 gnome => 'dwarf.png',
182 demon => 'demon.png', }},
183 no_flip => { alex => ['dragon', 'demon'] },
184 };
185  
186 # This log is to find bugs...
187 app->log->level(app->config('loglevel'));
188 app->log->path(app->config('logfile'));
189  
190 plugin 'authentication', {
191 autoload_user => 1,
192 load_user => sub {
193 my ($self, $username) = @_;
194 return {
195 'username' => $username,
196 } if app->config('users')->{$username};
197 return undef;
198 },
199 validate_user => sub {
200 my ($self, $username, $password) = @_;
201 if (app->config('users')->{$username}
202 && $password eq app->config('users')->{$username}) {
203 return $username;
204 }
205 return undef;
206 },
207 };
208  
209 dir(app->config('contrib') // dist_dir('Game-FaceGenerator'));
210 no_flip(app->config('no_flip'));
211  
212 get '/' => sub {
213 my $self = shift;
214 $self->render(template => 'index',
215 artists => all_artists());
216 } => 'main';
217  
218 get '/view' => sub {
219 my $self = shift;
220 $self->redirect_to(view => {artist => 'alex', type => 'woman'});
221 };
222  
223 get '/view/:artist/:type' => sub {
224 my $self = shift;
225 my $artist = $self->param('artist');
226 my $type = $self->param('type');
227 $self->render(template => 'view',
228 type => $type,
229 artists => all_artists(),
230 components => [random_components($type, $artist)]);
231 } => 'view';
232  
233 get '/gallery' => sub {
234 my $self = shift;
235 $self->redirect_to(gallery => {artist => 'alex', type => 'man'});
236 };
237  
238 get '/gallery/:type' => sub {
239 my $self = shift;
240 my $type = $self->param('type');
241 $self->redirect_to(gallery => {artist => 'alex', type => $type});
242 };
243  
244 get '/gallery/:artist/:type' => sub {
245 my $self = shift;
246 my $artist = $self->param('artist');
247 my $type = $self->param('type');
248 my $num = $self->param('number') || 20;
249 $self->render(template => 'gallery',
250 artists => all_artists(),
251 type => $type,
252 components => [map {
253 [random_components($type, $artist, $self->param('debug'))];
254 } 1..$num]);
255 } => 'gallery';
256  
257 get '/random' => sub {
258 my $self = shift;
259 $self->redirect_to(random => {artist => 'alex', type => 'woman'});
260 };
261  
262 get '/random/:type' => sub {
263 my $self = shift;
264 my $type = $self->param('type');
265 $self->redirect_to(random => {artist => 'alex', type => $type});
266 };
267  
268 get '/random/:artist/:type' => sub {
269 my $self = shift;
270 my $artist = $self->param('artist');
271 my $type = $self->param('type');
272 $self->render(format => 'png',
273 data => render_components($artist, random_components($type, $artist)));
274 } => 'random';
275  
276 get '/redirect/:artist/:type' => sub {
277 my $self = shift;
278 my $artist = $self->param('artist');
279 my $type = $self->param('type');
280 srand($self->param('seed')) if $self->param('seed');
281 $self->redirect_to(render => {
282 artist => $artist,
283 files => join(',', random_components($type, $artist)), });
284 } => 'redirect';
285  
286 get '/render/#files' => sub {
287 my $self = shift;
288 my $files = $self->param('files');
289 $self->redirect_to(face => {
290 artist => 'alex',
291 files => $files}, );
292 };
293  
294 get '/render/:artist/#files' => sub {
295 my $self = shift;
296 my $artist = $self->param('artist');
297 my $files = $self->param('files');
298 $self->render(format => 'png',
299 data => render_components($artist, split(',', $files)));
300 } => 'render';
301  
302 get '/debug' => sub {
303 my $self = shift;
304 $self->render(template => 'debug',
305 artist => '',
306 elements => [all_elements()],
307 types => []);
308 } => 'debug';
309  
310 get '/debug/:artist' => sub {
311 my $self = shift;
312 my $artist = $self->param('artist');
313 $self->render(template => 'debug',
314 artist => $artist,
315 elements => [all_elements()],
316 types => all_artists()->{$artist}->{types});
317 } => 'debug_artist';
318  
319 get '/debug/:artist/#element' => sub {
320 my $self = shift;
321 my $artist = $self->param('artist');
322 my $element = $self->param('element');
323 my $empty = $self->param('empty');
324 if (not $empty and app->config('empty')->{$artist}) {
325 $empty = app->config('empty')->{$artist}->{$element};
326 }
327 my $days = $self->param('days');
328 $self->render(template => 'debugelement',
329 artist => $artist,
330 element => $element,
331 empty => $empty,
332 components => [all_components($artist,
333 $element,
334 $empty,
335 $days)]);
336 } => 'debug_element';
337  
338 get '/edit/:artist/#component' => sub {
339 my $self = shift;
340 my $component = $self->param('component');
341 my $empty = $self->param('empty');
342 $self->render(template => 'edit',
343 empty => $empty,
344 components => [$empty||'empty.png', 'edit.png', $component]);
345 } => 'edit';
346  
347 get '/move/:artist/#component/:dir' => sub {
348 my $self = shift;
349 if (not $self->is_user_authenticated()) {
350 return $self->redirect_to('login');
351 }
352 my $artist = $self->param('artist');
353 my $component = $self->param('component');
354 my $dir = $self->param('dir');
355 my $step = $self->param('step') || 10;
356 my $empty = $self->param('empty');
357 move($artist, $component, $dir, $step);
358 $self->render(template => 'edit',
359 empty => $empty,
360 components => [$empty||'empty.png', 'edit.png', $component]);
361 } => 'move';
362  
363 any "/login" => sub {
364 my $self = shift;
365 my $username = $self->param('username');
366 my $password = $self->param('password');
367 if ($username) {
368 $self->authenticate($username, $password);
369 if ($self->is_user_authenticated()) {
370 return $self->redirect_to('main');
371 } else {
372 $self->stash(login => 'wrong');
373 }
374 }
375 $self->stash(home => app->config('home'));
376 } => 'login';
377  
378 get "/logout" => sub {
379 my $self = shift;
380 $self->logout();
381 $self->redirect_to('main');
382 } => 'logout';
383  
384 app->secrets([app->config('secret')]) if app->config('secret');
385  
386 app->start;
387  
388 =head2 SEE ALSO
389  
390 L<Game::FaceGenerator::Core> for the subroutines required.
391  
392 =cut
393  
394 __DATA__
« lib/Game/FaceGenerator/Core.pm ↑ Summary script/face-generator »