File Coverage

script/traveller
Criterion Covered Total %
statement 28 33 84.8
branch 0 4 0.0
condition n/a
subroutine 10 11 90.9
pod n/a
total 38 48 79.1


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # Copyright (C) 2009-2021 Alex Schroeder
3             # Copyright (C) 2020 Christian Carey
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 .
16              
17             package Traveller;
18 2     2   2038 use Traveller::Subsector;
  2         7  
  2         15  
19 2     2   1180 use Traveller::Mapper::Classic::MPTS;
  2         7  
  2         41  
20 2     2   84 use Traveller::Mapper::Classic;
  2         4  
  2         11  
21 2     2   52 use Traveller::Mapper;
  2         5  
  2         9  
22 2     2   57 use Traveller::Util qw(flush);
  2         5  
  2         88  
23 2     2   12 use Modern::Perl;
  2         4  
  2         20  
24 2     2   1628 use Mojolicious::Lite;
  2         433948  
  2         15  
25 2     2   53307 use POSIX qw(INT_MAX);
  2         6  
  2         20  
26 2     2   165 use utf8;
  2         4  
  2         9  
27              
28             get '/' => sub {
29             my $c = shift;
30             $c->redirect_to('main');
31             };
32              
33             get '/random' => sub {
34             my $c = shift;
35             my $id = int(rand(INT_MAX));
36             $c->redirect_to($c->url_for('uwp', size => 'subsector', rules => 'mgp', id => $id));
37             };
38              
39             get '/random/:size' => [size => ['subsector', 'sector']] => sub {
40             my $c = shift;
41             my $size = $c->param('size');
42             my $id = int(rand(INT_MAX));
43             $c->redirect_to($c->url_for('uwp', size => $size, rules => 'mgp', id => $id));
44             };
45              
46             get '/random/:size/:rules' => [size => ['subsector', 'sector']] => sub {
47             my $c = shift;
48             my $size = $c->param('size');
49             my $rules = $c->param('rules');
50             my $density = $c->param('density');
51             my $id = int(rand(INT_MAX));
52             $c->redirect_to($c->url_for('uwp', size => $size, rules => $rules, id => $id)->query(density => $density));
53             } => 'random';
54              
55             get '/:id' => [id => qr/\d+/] => sub {
56             my $c = shift;
57             my $id = $c->param('id');
58             $c->redirect_to($c->url_for('uwp', size => 'subsector', rules => 'mgp', id => $id));
59             };
60              
61             get '/uwp/:id' => [id => qr/\d+/] => sub {
62             my $c = shift;
63             my $id = $c->param('id');
64             $c->redirect_to($c->url_for('uwp', size => 'subsector', rules => 'mgp', id => $id));
65             };
66              
67             get '/uwp/:size/:id' => [size => ['subsector', 'sector']] => [id => qr/\d+/] => sub {
68             my $c = shift;
69             my $size = $c->param('size');
70             my $id = $c->param('id');
71             $c->redirect_to($c->url_for('uwp', size => $size, rules => 'mgp', id => $id));
72             };
73              
74             get '/uwp/:size/:rules/:id' => [size => ['subsector', 'sector']] => [id => qr/\d+/] => sub {
75             my $c = shift;
76             my $size = $c->param('size');
77             my $rules = $c->param('rules');
78             my $id = $c->param('id');
79             my $density = $c->param('density') || 50;
80             srand($id);
81             if ($size eq 'sector') {
82             my $uwp = subsector()->init(32, 40, $rules, $density/100)->str;
83             $c->render(template => 'uwp-sector', id => $id, rules => $rules, uwp => $uwp, density => $density);
84             } else {
85             my $uwp = subsector()->init(8, 10, $rules, $density/100)->str;
86             $c->render(template => 'uwp', id => $id, rules => $rules, uwp => $uwp, density => $density);
87             }
88             flush();
89             } => 'uwp';
90              
91             any '/edit' => sub {
92             my $c = shift;
93             my $uwp = $c->param('map');
94             $c->render(template => 'edit', uwp => Traveller::Mapper::example(), size => 'subsector', rules => 'mgp', id => '');
95             } => 'main';
96              
97             get '/edit/:id' => [id => qr/\d+/] => sub {
98             my $c = shift;
99             my $id = $c->param('id');
100             $c->redirect_to($c->url_for('edit', size => 'subsector', rules => 'mgp', id => $id));
101             };
102              
103             get '/edit/:size/:id' => [size => ['subsector', 'sector']] => [id => qr/\d+/] => sub {
104             my $c = shift;
105             my $size = $c->param('size');
106             my $id = $c->param('id');
107             $c->redirect_to($c->url_for('edit', size => $size, rules => 'mgp', id => $id));
108             };
109              
110             get '/edit/:size/:rules/:id' => [size => ['subsector', 'sector']] => [id => qr/\d+/] => sub {
111             my $c = shift;
112             my $size = $c->param('size');
113             my $rules = $c->param('rules');
114             my $id = $c->param('id');
115             my $density = $c->param('density');
116             srand($id);
117             if ($size eq 'sector') {
118             my $uwp = subsector()->init(32, 40, $rules, $density)->str;
119             $c->render(template => 'edit-sector', id => $id, rules => $rules, uwp => $uwp);
120             } else {
121             my $uwp = subsector()->init(8, 10, $rules, $density)->str;
122             $c->render(template => 'edit', id => $id, rules => $rules, uwp => $uwp);
123             }
124             flush();
125             } => 'edit';
126              
127             # → see any '/map' below!
128             # get '/map' => sub {
129             # my $c = shift;
130             # $c->render(template => 'map_all', uwp => Traveller::Mapper::example(), size => 'subsector', rules => 'mgp');
131             # };
132              
133             get '/map/:id' => [id => qr/\d+/] => sub {
134             my $c = shift;
135             my $id = $c->param('id');
136             $c->redirect_to($c->url_for('map_all', size => 'subsector', rules => 'mgp', id => $id));
137             };
138              
139             get '/map/:size/:id' => [size => ['subsector', 'sector']] => [id => qr/\d+/] => sub {
140             my $c = shift;
141             my $size = $c->param('size');
142             my $id = $c->param('id');
143             $c->redirect_to($c->url_for('map_all', size => $size, rules => 'mgp', id => $id));
144             };
145              
146             get '/map/:size/:rules/:id' => [size => ['subsector', 'sector']] => [id => qr/\d+/] => sub {
147             my $c = shift;
148             my $size = $c->param('size');
149             my $rules = $c->param('rules');
150             my $id = $c->param('id');
151             my $wiki = $c->param('wiki');
152             my $density = $c->param('density') || 50;
153             srand($id);
154             my $map = mapper($rules);
155             my $uwp;
156             if ($size eq 'sector') {
157             $uwp = subsector()->init(32, 40, $rules, $density/100)->str;
158             } else {
159             $uwp = subsector()->init(8, 10, $rules, $density/100)->str;
160             }
161             my $url = $c->url_for('uwp', size => $size, rules => $rules, id => $id);
162             $url = $url->query(density => $density) if $density and $density != 50;
163             $map->initialize($uwp, $wiki, $url);
164             $map->communications();
165             $map->trade();
166             flush();
167             $c->render(text => $map->svg, format => 'svg');
168             } => 'map_all';
169              
170             any '/map' => sub {
171             my $c = shift;
172             my $wiki = $c->param('wiki');
173             my $trade = $c->param('trade');
174             my $uwp = $c->param('map') || Traveller::Mapper::example();
175             my $size = $c->param('size') || 'subsector';
176             my $rules = $c->param('rules') || 'mgp';
177             my $source;
178             if (!$uwp) {
179             my $id = int(rand(INT_MAX));
180             srand($id);
181             $uwp = subsector()->init(8, 10, $rules)->str;
182             $source = $c->url_for('uwp', id => $id);
183             }
184             my $map = mapper($rules);
185             $map->initialize($uwp, $wiki, $source);
186             $map->communications();
187             $map->trade();
188             flush();
189             if ($trade) {
190             $c->render(text => $map->text, format => 'txt');
191             } else {
192             $c->render(text => $map->svg, format => 'svg');
193             }
194             } => 'map';
195              
196             get '/help' => sub {
197             my $c = shift;
198             my $classic = $c->param('classic');
199             my $mpts = $c->param('mpts');
200             $c->render(classic => $classic, mpts => $mpts);
201             };
202              
203             sub subsector {
204 1     1   12 return Traveller::Subsector->new;
205             }
206              
207             sub mapper {
208 0     0     my $rules = shift;
209 0 0         if ($rules eq 'mpts') {
    0          
210 0           return Traveller::Mapper::Classic::MPTS->new;
211             } elsif ($rules eq 'ct') {
212 0           return Traveller::Mapper::Classic->new;
213             } else {
214 0           return Traveller::Mapper->new;
215             }
216             }
217              
218             app->start;
219              
220             __DATA__