]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Data.pm
Improve standings
[gruntmaster-page.git] / lib / Gruntmaster / Data.pm
... / ...
CommitLineData
1package Gruntmaster::Data;
2use v5.14;
3use warnings;
4use parent qw/Exporter/;
5
6use JSON qw/encode_json decode_json/;
7
8use Redis;
9
10our $contest;
11my $redis = Redis->new;
12my $pubsub = Redis->new;
13
14sub dynsub{
15 no strict 'refs';
16 *{$_[0]} = $_[1];
17}
18
19BEGIN {
20 for my $cmd (qw/multi exec smembers get hget hdel hset sadd srem incr hmset hsetnx publish del/) {
21 dynsub uc $cmd, sub { $redis->$cmd(@_) };
22 }
23
24 for my $cmd (qw/subscribe wait_for_messages/) {
25 dynsub uc $cmd, sub { $pubsub->$cmd(@_) };
26 }
27}
28
29sub cp { defined $contest ? "contest.$contest." : '' }
30
31sub multi () { MULTI }
32sub rexec () { EXEC }
33
34sub problems () { SMEMBERS cp . 'problem' }
35sub contests () { SMEMBERS cp . 'contest' }
36sub users () { SMEMBERS cp . 'user' }
37sub jobcard () { GET cp . 'job' }
38
39sub job_results (_) { decode_json HGET cp . "job.$_[0]", 'results' }
40sub set_job_results ($+) { HSET cp . "job.$_[0]", 'results', encode_json $_[1] }
41sub job_inmeta (_) { decode_json HGET cp . "job.$_[0]", 'inmeta' }
42sub set_job_inmeta ($+) { HSET cp . "job.$_[0]", 'inmeta', encode_json $_[1] }
43sub problem_meta (_) { decode_json HGET cp . "problem.$_[0]", 'meta' }
44sub set_problem_meta ($+) { HSET cp . "problem.$_[0]", 'meta', encode_json $_[1] }
45sub job_daemon (_) { HGET cp . "job.$_[0]", 'daemon' }
46sub set_job_daemon ($$) { HSETNX cp . "job.$_[0]", 'daemon', $_[1] };
47
48sub defhash{
49 my ($name, @keys) = @_;
50 for my $key (@keys) {
51 dynsub "${name}_$key", sub (_) { HGET cp . "$name.$_[0]", $key };
52 dynsub "set_${name}_$key", sub ($$) { HSET cp . "$name.$_[0]", $key, $_[1] };
53 }
54
55 dynsub "edit_$name", sub {
56 my ($key, %values) = @_;
57 HMSET cp . "$name.$key", %values;
58 };
59
60 dynsub "insert_$name", sub {
61 my ($key, %values) = @_;
62 SADD cp . $name, $key or return;
63 HMSET cp . "$name.$key", %values;
64 };
65 dynsub "remove_$name", sub (_) {
66 my $key = shift;
67 SREM cp . $name, $key;
68 DEL cp . "$name.$key";
69 };
70
71 dynsub "push_$name", sub {
72 my $nr = INCR cp . $name;
73 HMSET cp . "$name.$nr", @_;
74 $nr
75 };
76}
77
78defhash problem => qw/name level statement owner author/;
79defhash contest => qw/start end name owner/;
80defhash job => qw/date errors extension filesize private problem result result_text user/;
81defhash user => qw/name email town university level/;
82
83sub clean_job (_){
84 HDEL cp . "job.$_[0]", qw/result result_text results daemon/
85}
86
87sub mark_open {
88 my ($problem, $user) = @_;
89 HSETNX cp . 'open', "$problem.$user", time;
90}
91
92sub get_open {
93 my ($problem, $user) = @_;
94 HGET cp . 'open', "$problem.$user";
95}
96
97our @EXPORT = do {
98 no strict 'refs';
99 grep { $_ =~ /^[a-zA-Z]/ and exists &$_ } keys %{__PACKAGE__ . '::'};
100};
101
1021
This page took 0.022591 seconds and 4 git commands to generate.