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