Commit | Line | Data |
---|---|---|
014ee8a6 MG |
1 | #!/usr/bin/perl -w |
2 | use v5.14; | |
3 | ||
4 | use Gruntmaster::Data; | |
014ee8a6 MG |
5 | |
6 | use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ]; | |
7 | use File::Slurp qw/read_file/; | |
756368d0 | 8 | use JSON qw/encode_json/; |
014ee8a6 | 9 | use Term::ANSIColor qw/RED RESET/; |
0d2f8987 | 10 | use Getopt::Long qw/:config require_order/; |
014ee8a6 MG |
11 | |
12 | ################################################## | |
13 | ||
30e287c3 MG |
14 | my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'; |
15 | my $db = Gruntmaster::Data->connect($dsn); | |
014ee8a6 MG |
16 | |
17 | sub cmd_help{ | |
18 | exec perldoc => $0 | |
19 | } | |
20 | ||
014ee8a6 MG |
21 | sub cmd_add{ |
22 | my $id = shift; | |
23 | my $name = prompt 'Problem name'; | |
646feed4 | 24 | my $private = prompt('Private?', '-yn') eq 'y'; |
756368d0 | 25 | my $contest = prompt 'Contest'; |
014ee8a6 MG |
26 | my $author = prompt 'Problem author (full name)'; |
27 | my $owner = prompt 'Problem owner (username)'; | |
28 | my $level = prompt 'Problem level', -menu => "beginner\neasy\nmedium\nhard"; | |
29 | my $statement = read_file prompt 'File with problem statement', -complete => 'filenames'; | |
cd25d613 MG |
30 | my $generator = prompt 'Generator', -menu => "File\nRun\nUndef"; |
31 | my $runner = prompt 'Runner', -menu => "File\nVerifier\nInteractive"; | |
32 | my $judge = prompt 'Judge', -menu => "Absolute\nPoints"; | |
33 | my $testcnt = prompt 'Test count', '-i'; | |
014ee8a6 | 34 | |
cd25d613 MG |
35 | my $timeout = prompt 'Time limit (seconds)', '-n'; |
36 | my $olimit = prompt 'Output limit (bytes)', '-i'; | |
014ee8a6 MG |
37 | say 'Memory limits are broken, so I won\'t ask you for one'; |
38 | ||
756368d0 | 39 | my (@tests, $gensource, $genformat, $versource, $verformat); |
014ee8a6 | 40 | |
756368d0 MG |
41 | if ($generator eq 'Run') { |
42 | $gensource = read_file prompt, '[Generator::Run] Generator file name', -complete => 'filenames'; | |
43 | $genformat = prompt '[Generator::Run] Generator format', -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/]; | |
014ee8a6 MG |
44 | } |
45 | ||
756368d0 MG |
46 | if ($runner eq 'File') { |
47 | my $default = $judge eq 'Points' ? 10 : 'Ok'; | |
48 | $tests[$_ - 1] = prompt "[Runner::File] Score for test ${_} [$default]", -default => $default for 1 .. $testcnt; | |
49 | } | |
014ee8a6 | 50 | |
756368d0 MG |
51 | if ($runner eq 'Verifier' || $runner eq 'Interactive') { |
52 | say RED, 'WARNING: Runner::Interactive is experimental', RESET if $runner eq 'Interactive'; | |
53 | $versource = prompt "[Runner::$runner] Verifier file name", -complete => 'filenames'; | |
54 | $verformat = prompt "[Runner::$runner] Verifier format", -menu => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/]; | |
014ee8a6 MG |
55 | } |
56 | ||
4af36605 | 57 | my %options = ( |
756368d0 | 58 | id => $id, |
646feed4 MG |
59 | name => $name, |
60 | level => $level, | |
61 | statement => $statement, | |
62 | author => $author, | |
63 | owner => $owner, | |
64 | generator => $generator, | |
65 | runner => $runner, | |
66 | judge => $judge, | |
67 | testcnt => $testcnt, | |
4af36605 MG |
68 | ); |
69 | $options{private} = $private if $private; | |
70 | $options{timeout} = $timeout if $timeout; | |
71 | $options{olimit} = $olimit if $olimit; | |
72 | $options{tests} = encode_json \@tests if @tests; | |
73 | $options{gensource} = $gensource if $gensource; | |
74 | $options{genformat} = $genformat if $genformat; | |
75 | $options{versource} = $versource if $versource; | |
76 | $options{verformat} = $verformat if $verformat; | |
77 | $db->problems->create (\%options); | |
756368d0 MG |
78 | |
79 | $db->contest_problems->create({problem => $id, contest => $contest}) if $contest; | |
014ee8a6 MG |
80 | } |
81 | ||
82 | sub cmd_set{ | |
83 | my $file; | |
84 | GetOptions ( 'file!' => \$file ); | |
4af36605 | 85 | my ($id, %values) = @_; |
014ee8a6 | 86 | %values = map { $_ => scalar read_file $values{$_} } keys %values if $file; |
4af36605 | 87 | $db->problem($id)->update(\%values) |
014ee8a6 MG |
88 | } |
89 | ||
5214523a | 90 | sub cmd_get{ |
4af36605 | 91 | my ($id, $col) = @_; |
5214523a MG |
92 | say $db->problem($id)->get_column($col) |
93 | } | |
94 | ||
014ee8a6 MG |
95 | sub cmd_list{ |
96 | local $, = "\n"; | |
4af36605 | 97 | say map {$_->id} $db->problems->all |
014ee8a6 MG |
98 | } |
99 | ||
100 | sub cmd_rm{ | |
4af36605 | 101 | $db->problem(shift)->delete |
014ee8a6 MG |
102 | } |
103 | ||
104 | sub cmd_show{ | |
4af36605 MG |
105 | my %columns = $db->problem(shift)->get_columns; |
106 | print <<END | |
107 | Name: $columns{name} | |
108 | Author: $columns{author} | |
109 | Statement written by: $columns{writer} | |
110 | Owner: $columns{owner} | |
111 | Level: $columns{level} | |
112 | Output limit: $columns{olimit} | |
113 | Time limit: $columns{timeout} | |
114 | Test count: $columns{testcnt} | |
115 | Generator: $columns{generator} | |
116 | Runner: $columns{runner} | |
117 | Judge: $columns{judge} | |
118 | Private: $columns{private} | |
119 | END | |
014ee8a6 MG |
120 | } |
121 | ||
122 | ################################################## | |
123 | ||
014ee8a6 MG |
124 | my $cmd = 'cmd_' . shift; |
125 | cmd_help unless exists $main::{$cmd}; | |
126 | no strict 'refs'; | |
756368d0 | 127 | $cmd->(@ARGV); |
014ee8a6 MG |
128 | |
129 | 1; | |
130 | __END__ | |
131 | ||
132 | =encoding utf-8 | |
133 | ||
134 | =head1 NAME | |
135 | ||
136 | gruntmaster-problem - shell interface to Gruntmaster 6000 problems | |
137 | ||
138 | =head1 SYNOPSIS | |
139 | ||
5214523a MG |
140 | gruntmaster-problem add problem_id |
141 | gruntmaster-problem list | |
142 | gruntmaster-problem rm problem_id | |
143 | gruntmaster-problem show problem_id | |
144 | gruntmaster-problem set [--file] problem_id key value | |
145 | gruntmaster-problem get problem_id key | |
014ee8a6 MG |
146 | |
147 | =head1 DESCRIPTION | |
148 | ||
e90402be MG |
149 | gruntmaster-problem is a tool for managing problems. |
150 | ||
e90402be MG |
151 | =over |
152 | ||
153 | =item B<list> | |
154 | ||
155 | Prints the list of problems in the selected contest. | |
156 | ||
157 | =item B<show> I<id> | |
158 | ||
159 | Prints detailed information about problem I<id>. | |
160 | ||
161 | =item B<add> I<id> | |
162 | ||
163 | Adds a new problem with id I<id>. | |
164 | ||
165 | =item B<rm> I<id> | |
166 | ||
167 | Removes the problem with id I<id>. | |
168 | ||
169 | =item B<set> I<id> I<key> I<value> | |
170 | ||
171 | Sets the I<key> configuration option of problem I<id> to I<value>. | |
172 | ||
5214523a MG |
173 | =item B<get> I<id> I<key> |
174 | ||
175 | Get the value of the I<key> configuration option of problem I<id> | |
176 | ||
e90402be MG |
177 | =item B<set> --file I<id> I<key> I<file> |
178 | ||
179 | Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>. | |
180 | ||
181 | =back | |
182 | ||
183 | =head1 AUTHOR | |
184 | ||
185 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
186 | ||
187 | =head1 COPYRIGHT AND LICENSE | |
188 | ||
189 | Copyright (C) 2014 by Marius Gavrilescu | |
190 | ||
4af36605 MG |
191 | This library is free software; you can redistribute it and/or modify |
192 | it under the same terms as Perl itself, either Perl version 5.18.1 or, | |
193 | at your option, any later version of Perl 5 you may have available. | |
014ee8a6 MG |
194 | |
195 | ||
196 | =cut |