Fix gruntmaster-contest
[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 $db = Gruntmaster::Data->connect('dbi:Pg:');
15
16 sub cmd_help{
17 exec perldoc => $0
18 }
19
20 sub cmd_add{
21 my $id = shift;
22 my $name = prompt 'Problem name';
23 my $private = prompt('Private?', '-yn') eq 'y';
24 my $contest = prompt 'Contest';
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';
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';
33
34 my $timeout = prompt 'Time limit (seconds)', '-n';
35 my $olimit = prompt 'Output limit (bytes)', '-i';
36 say 'Memory limits are broken, so I won\'t ask you for one';
37
38 my (@tests, $gensource, $genformat, $versource, $verformat);
39
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/];
43 }
44
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 }
49
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/];
54 }
55
56 $db->problems->create ({
57 id => $id,
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,
67 (private => $private)x!! $private,
68 (timeout => $timeout)x!! $timeout,
69 (olimit => $olimit)x!! $olimit,
70 (tests => encode_json \@tests)x!! @tests,
71 (gensource => $gensource)x!! $gensource,
72 (genformat => $genformat)x!! $genformat,
73 (versource => $versource)x!! $versource,
74 (verformat => $verformat)x!! $verformat,
75 });
76
77 $db->contest_problems->create({problem => $id, contest => $contest}) if $contest;
78 #PUBLISH genpage => $contest ? "ct/$contest/pb/index.html" : 'pb/index.html';
79 #PUBLISH genpage => $contest ? "ct/$contest/pb/$id.html" : "pb/$id.html";
80 }
81
82 sub cmd_set{
83 my $file;
84 GetOptions ( 'file!' => \$file );
85 my ($id, %values) = @ARGV;
86 %values = map { $_ => scalar read_file $values{$_} } keys %values if $file;
87 $db->problem($id)->update(\%values);
88 #PUBLISH genpage => 'pb/index.html';
89 #PUBLISH genpage => "pb/$id.html";
90 }
91
92 sub cmd_list{
93 local $, = "\n";
94 say map {$_->id} $db->problems->all;
95 }
96
97 sub cmd_rm{
98 $db->problem(shift)->delete;
99 #PUBLISH genpage => $contest ? "ct/$contest/pb/index.html" : 'pb/index.html';
100 }
101
102 sub cmd_show{
103 local $_ = shift or goto &cmd_list;
104 }
105
106 ##################################################
107
108 my $cmd = 'cmd_' . shift;
109 cmd_help unless exists $main::{$cmd};
110 no strict 'refs';
111 $cmd->(@ARGV);
112
113 1;
114 __END__
115
116 =encoding utf-8
117
118 =head1 NAME
119
120 gruntmaster-problem - shell interface to Gruntmaster 6000 problems
121
122 =head1 SYNOPSIS
123
124 gruntmaster-problem [--contest=mycontest] add problem_id
125 gruntmaster-problem [--contest=mycontest] list
126 gruntmaster-problem [--contest=mycontest] rm problem_id
127 gruntmaster-problem [--contest=mycontest] show problem_id
128 gruntmaster-problem [--contest=mycontest] set [--file] problem_id key value
129
130 =head1 DESCRIPTION
131
132 gruntmaster-problem is a tool for managing problems.
133
134 Select the contest with the optional argument I<--contest>.
135
136 =over
137
138 =item B<list>
139
140 Prints the list of problems in the selected contest.
141
142 =item B<show> I<id>
143
144 Prints detailed information about problem I<id>.
145
146 =item B<add> I<id>
147
148 Adds a new problem with id I<id>.
149
150 =item B<rm> I<id>
151
152 Removes the problem with id I<id>.
153
154 =item B<set> I<id> I<key> I<value>
155
156 Sets the I<key> configuration option of problem I<id> to I<value>.
157
158 =item B<set> --file I<id> I<key> I<file>
159
160 Sets the I<key> configuration option of problem I<id> to the contents of the file I<file>.
161
162 =back
163
164 =head1 AUTHOR
165
166 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
167
168 =head1 COPYRIGHT AND LICENSE
169
170 Copyright (C) 2014 by Marius Gavrilescu
171
172 This library is free software: you can redistribute it and/or modify
173 it under the terms of the GNU Affero General Public License as published by
174 the Free Software Foundation, either version 3 of the License, or
175 (at your option) any later version.
176
177
178 =cut
This page took 0.031672 seconds and 4 git commands to generate.