summaryrefslogtreecommitdiff
path: root/js/codeq/login.js
blob: d449835be3b61c944fa4d7ec1caf6caf22772cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(function(){
    var groups = {
        'family_relations': {
            'name': 'Family relations',
            'problems': [
                {'id': 'ancestor_2', 'name': 'ancestor/2'},
                {'id': 'aunt_2', 'name': 'aunt/2'},
                {'id': 'brother_2', 'name': 'brother/2'},
                {'id': 'connected_3', 'name': 'connected/3'},
                {'id': 'cousin_2', 'name': 'cousin/2'}
            ]
        },
        'lists': {
            'name': 'Lists',
            'problems': [
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''}
            ]
        },
        'sorting': {
            'name': 'Sorting',
            'problems': [
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''}
            ]
        },
        'other': {
            'name': 'Other',
            'problems': [
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''}
            ]
        },
        'sets': {
            'name': 'Sets',
            'problems': [
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''}
            ]
        },
        'trees': {
            'name': 'Trees',
            'problems': [
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''}
            ]
        },
        'license_plates': {
            'name': 'License plates',
        },
        'clp_fd': {
            'name': 'CLP(FD',
            'problems': [
                {'id': 'gcd_3', 'name': 'gcd/3'},
                {'id': 'magic_1', 'name': 'magic/1'},
                {'id': 'puzzle_abc_3', 'name': 'puzzle_abc/3'},
                {'id': 'puzzle_beth_1', 'name': 'puzzle_beth/1'},
                {'id': 'puzzle_momson_2', 'name': 'puzzle_momson/2'},
                {'id': 'puzzle_ratio_2', 'name': 'puzzle_ratio/2'},
                {'id': 'tobase_3', 'name': 'tobase/3'}
            ]
        },
        'clp_r': {
            'name': 'CLP(R)',
            'problems': [
                {'id': 'bounding_box_3', 'name': 'bounding_box/3'},
                {'id': 'center_3', 'name': 'center/3'},
                {'id': 'linear_opts_3', 'name': 'linear_opts/3'},
                {'id': 'max_sum_2', 'name': 'max_sum/2'},
                {'id': 'megabytes_2', 'name': 'megabytes/2'},
                {'id': 'turkey_3', 'name': 'turkey/3'}
            ]
        },
        'dcg': {
            'name': 'DCG',
            'problems': [
                {'id': 'ab_2', 'name': 'ab/2'},
                {'id': 'digit_2', 'name': 'digit/2'},
                {'id': 'expr_2', 'name': 'expr/2'},
                {'id': 'expr_3', 'name': 'expr/3'},
                {'id': 'flower_2', 'name': 'flower/2'}
            ]
        },
        'denotational_semantics': {
            'name': 'Denotational semantics',
            'problems': [
                {'id': 'algol_3', 'name': 'algol/3'},
                {'id': 'algol_for_3', 'name': 'algol_for/3'},
                {'id': 'algol_if_3', 'name': 'algol_if/3'},
                {'id': 'prog_8puzzle_2', 'name': 'prog_8puzzle/2'},
                {'id': 'prog_8puzzle_3', 'name': 'prog_8puzzle/3'}
            ]
        },
        'old_exams': {
            'name': 'Old exams',
            'problems': [
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''},
                {'id': '', 'name': ''}
            ]
        }
    };

    $("#submit").on('click', function () {
        var group = $('#problem_group').val(),
            problem = $('#problems').val();
        if (!group) alert('Choose a problem group');
        else if (!problem) alert('Choose a problem');
        else {
            $.ajax({
                'type': 'POST',
                'url': '/codeq/login',
                'accepts': 'application/json',
                'contentType': 'application/json; charset=UTF-8',
                'data': JSON.stringify({
                    'username': $('#username').val(),
                    'password': $('#password').val()
                }),
                'processData': false,
                'dataType': 'json',
                'error': function loginErrorHandler(jqXHR, textStatus, errorThrown) {
                    alert('Request for login failed: ' + (errorThrown || textStatus));
                },
                'success': function loginSuccessHandler(data, textStatus, jqXHR) {
                    if (data && (data.code === 0)) {
                        window.location = 'prolog.html#sid=' + data.sid + '/grp=' + group + '/prb=' + problem;
                    }
                    else {
                        alert('Login failed: code=' + data.code + ', reason=' + data.message);
                    }
                }
            });
        }
    });

    $(document).ready(function () {
        var jqGroup = $('#problem_group'),
            jqProblems = $('#problems'),
            id, g, first_group, html = [];

        for (id in groups) {
            g = groups[id];
            html.push('<option value="', id, '">', g.name, '</option>\n');
        }
        jqGroup.html(html.join(''));
        first_group = html[1];
        html = null;

        jqGroup.on('click', function () {
            var g = groups[jqGroup.val()],
                html = [],
                problems, i, p;
            if (g) {
                problems = g.problems;
                for (i = 0; i < problems.length; i++) {
                    p = problems[i];
                    if (!p.id) continue;
                    html.push('<option value="', p.id, '">', p.name, '</option>\n')
                }
            }
            jqProblems.html(html.join(''));
        });

        jqGroup.val(first_group);
    });
})();