b942897ac41d12388c93b4427474dd0c34a94db0
[gruntmaster-data.git] / gruntmaster-problem
1 #!/usr/bin/perl -w
2 use v5.14;
3
4 use Gruntmaster::Data;
5
6 use IO::Prompter [ -style => 'bold', '-stdio', '-verbatim' ];
7 use File::Slurp qw/read_file/;
8 use JSON qw/encode_json/;
9 use Term::ANSIColor qw/RED RESET/;
10 use Getopt::Long qw/:config require_order/;
11
12 ##################################################
13
14 my $dsn = $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:';
15 my $db = Gruntmaster::Data->connect($dsn);
16
17 sub cmd_help{
18 exec perldoc => $0
19 }
20
21 sub cmd_add{
22 my $id = shift;
23 my $name = prompt 'Problem name';
24 my $private = prompt('Private?', '-yn') eq 'y';
25 my $contest = prompt 'Contest';
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';
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';
34
35 my $timeout = prompt 'Time limit (seconds)', '-n';
36 my $olimit = prompt 'Output limit (bytes)', '-i';
37 say 'Memory limits are broken, so I won\'t ask you for one';
38
39 my (@tests, $gensource, $genformat, $versource, $verformat);
40
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/];
44 }
45
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 }
50
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/];
55 }
56
57 my %options = (
58 id => $id,
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,
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);
78
79 $db->contest_problems->create({problem => $id, contest => $contest}) if $contest;
80 }
81
82 sub cmd_set{
83 my $file;
84 GetOptions ( 'file!' => \$file );
85 my ($id, %values) = @_;
86 %values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
87 $db->problem($id)->update(\%values)
88 }
89
90 sub cmd_get{
91 my ($id, $col) = @_;
92 say $db->problem($id)->get_column($col)
93 }
94
95 sub cmd_list{
96 local $, = "\n";
97 say map {$_->id} $db->problems->all
98 }
99
100 sub cmd_rm{
101 $db->problem(shift)->delete
102 }
103
104 sub cmd_show{
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
120 }
121
122 ##################################################
123
124 my $cmd = 'cmd_' . shift;
125 cmd_help unless exists $main::{$cmd};
126 no strict 'refs';
127 $cmd->(@ARGV);
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
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
146
147 =head1 DESCRIPTION
148
149 gruntmaster-problem is a tool for managing problems.
150
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
173 =item B<get> I<id> I<key>
174
175 Get the value of the I<key> configuration option of problem I<id>
176
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
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.
194
195
196 =cut
This page took 0.033027 seconds and 3 git commands to generate.