@@ -10,6 +10,148 @@ exports.get_scoreboard = function(req, res) {
10
10
res . sendfile ( SCOREBOARD_PATH , { root : __dirname } ) ;
11
11
} ;
12
12
13
+ exports . get_graph = function ( req , res ) {
14
+
15
+ } ;
16
+
13
17
var generate_scoreboard = function ( ) {
18
+ getTeamScores ( function ( teams ) {
19
+ var content = "" ;
20
+
21
+ content += "<table class=\"table table-striped table-hover\" style=\"table-layout: fixed; width: 100%;\">\r\n" ;
22
+ content += "\t<thead><tr>\r\n\t\t<th style=\"width:10%;\">Place</th>\r\n\t\t<th style=\"width:60%;\">Team</th>\r\n\t\t<th style=\"width:20%;\">School</th>\r\n\t\t<th style=\"width:10%;\">Score</th></tr></thead>\r\n" ;
23
+ for ( var i = 0 ; i < teams . length ; i ++ ) {
24
+ content += "\t<tr>\r\n" ;
25
+ content += "\t\t<td>" + ( i + 1 ) + "</td>\r\n" ;
26
+ content += "\t\t<td style=\"word-break:break-all;\">" + teams [ i ] . teamname + "</td>\r\n" ;
27
+ content += "\t\t<td style=\"word-break:break-all;\">" + teams [ i ] . school + "</td>\r\n" ;
28
+ content += "\t\t<td>" + teams [ i ] . points + "</td>\r\n" ;
29
+ content += "\t</tr>\r\n" ;
30
+ }
31
+ content += "</table>\r\n" ;
32
+
33
+ fs . chmodSync ( __dirname + SCOREBOARD_PATH , 0755 ) ;
34
+ fs . writeFile ( __dirname + SCOREBOARD_PATH , content , function ( err ) {
35
+ if ( err ) {
36
+ console . log ( "[api/scoreboard.js] error generating scoreboard" ) ;
37
+ } else {
38
+ console . log ( "[api/scoreboard.js] generated scoreboard" ) ;
39
+ }
40
+ } ) ;
41
+ } ) ;
42
+ } ;
43
+
44
+ var generate_graph = function ( ) {
45
+
46
+ } ;
47
+
48
+ var getTeamScores = function ( callback ) {
49
+ common . db . collection ( "accounts" ) . find ( {
50
+ group : 1
51
+ } ) . toArray ( function ( err , teams ) {
52
+ if ( err ) {
53
+ console . log ( "[api/scoreboard.js] couldn't load accounts" ) ;
54
+ } else {
55
+ common . db . collection ( "submissions" ) . find ( {
56
+ correct : true ,
57
+ } ) . toArray ( function ( err2 , submissions ) {
58
+ if ( err2 ) {
59
+ console . log ( "[api/scoreboard.js] couldn't load submissions" ) ;
60
+ } else {
61
+ common . db . collection ( "problems" ) . find ( {
62
+ value : {
63
+ $gt : 0
64
+ }
65
+ } ) . toArray ( function ( err3 , problems ) {
66
+ if ( err3 ) {
67
+ console . log ( "[api/scoreboard.js] couldn't load problems" ) ;
68
+ } else {
69
+ var teamArray = [ ] ;
70
+ for ( var i = 0 ; i < teams . length ; i ++ ) {
71
+ teams [ i ] . lastUpdated = 0 ;
72
+ var points = 0 ;
73
+ for ( var j = 0 ; j < submissions . length ; j ++ ) {
74
+ if ( submissions [ j ] . _id . valueOf ( ) . indexOf ( teams [ i ] . _id . valueOf ( ) ) > - 1 ) {
75
+ var time = moment ( submissions [ j ] . timestamp ) . diff ( common . startDate ) ;
76
+ if ( time > teams [ i ] . lastUpdated ) {
77
+ teams [ i ] . lastUpdated = time ;
78
+ }
79
+ var prob_points ;
80
+ for ( var k = 0 ; k < problems . length ; k ++ ) {
81
+ if ( problems [ k ] . pid == submissions [ j ] . pid ) {
82
+ prob_points = problems [ k ] . value ;
83
+ break ;
84
+ }
85
+ }
86
+ points += prob_points ;
87
+ }
88
+ }
89
+ teams [ i ] . points = points ;
90
+ }
91
+
92
+ teams . sort ( function ( a , b ) {
93
+ if ( a . points > b . points ) {
94
+ return - 1 ;
95
+ }
96
+ if ( a . points < b . points ) {
97
+ return 1 ;
98
+ }
99
+ return a . lastUpdated - b . lastUpdated ;
100
+ } ) ;
101
+
102
+ for ( var i = 0 ; i < teams . length ; i ++ ) {
103
+ teamArray . push ( {
104
+ tid : teams [ i ] . _id . valueOf ( ) ,
105
+ place : ( i + 1 ) ,
106
+ teamname : teams [ i ] . teamname ,
107
+ school : teams [ i ] . school ,
108
+ points : teams [ i ] . points
109
+ } ) ;
110
+ }
111
+
112
+ callback ( teamArray ) ;
113
+ }
114
+ } ) ;
115
+ }
116
+ } ) ;
117
+ }
118
+ } ) ;
119
+ } ;
120
+
121
+ /*
122
+ for generating graph... will be used later
14
123
15
- } ;
124
+ var nTeamArray = [];
125
+ async.each(teamArray, function(team, callback2) {
126
+ common.db.collection("accounts").find({
127
+ _id: team._id
128
+ }).toArray(function(err, accounts) {
129
+ if (accounts.length == 1) {
130
+ var account = accounts[0];
131
+ common.db.collection("submissions").find({
132
+ tid: team._id.valueOf(),
133
+ correct: true
134
+ }).sort({
135
+ timestamp: 1
136
+ }).toArray(function(err, data) {
137
+ for(var i=data.length-1; i>=0; i--) {
138
+ if (moment(data[i].timestamp).isAfter(common.endDate)) {
139
+ data.spliace(i, 1);
140
+ }
141
+ }
142
+ team.submissions = data;
143
+ team.p = 0;
144
+ nTeamArray.push(team);
145
+ callback2();
146
+ });
147
+ }
148
+ });
149
+ }, function(err) {
150
+ var indices = [];
151
+ for(var i=0; i<teams.length; i++) {
152
+ for(var j=0; j<nTeamArray[i].submissions.length; j++) {
153
+ var index = moment(nTeamArray[i].submissions[j].timestamp).diff(common.startDate);
154
+ }
155
+ }
156
+ });
157
+ */
0 commit comments