-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.js
3609 lines (3477 loc) · 142 KB
/
api.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var request = require( 'request' );
var cheerio = require( 'cheerio' );
var fs = require( 'fs' );
var mysql = require( 'mysql' );
var express = require( 'express' );
var app = express();
app.enable( "jsonp callback" );
var http = require( 'http' );
var url = require( 'url' );
var server = http.createServer( app );
var zlib = require( 'zlib' );
var Q = require( 'q' );
var log4js = require( 'log4js' );
var race = require('./racecache.js');
var bodyParser = require( 'body-parser' );
var program = require( 'commander' );
var compress = require( 'compression' );
var os = require( 'os' );
var logger = require ( 'morgan' );
program
.version( '0.0.3' )
.option( '-c, --config <file>', 'Use a different config file. Default ./cfg.json' )
.option( '-u, --allow-update', 'Allow api/players/X/update route' )
.option( '-l, --loglevel <LEVEL>', 'Default is DEBUG. levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL' )
.option( '-n, --nologin', 'Don\'t log in to quakelive.com. For use with */update & */get routes' )
.parse( process.argv );
var allow_update = program['allow-update'] || false;
_logger = log4js.getLogger( 'api' );
_logger.setLevel( log4js.levels[program.level] || log4js.levels.DEBUG );
// read cfg.json
var data = fs.readFileSync( program.config || __dirname + '/cfg.json' );
var cfg;
try {
cfg = JSON.parse( data );
_logger.info( 'Parsed config file' );
}
catch( err ) {
_logger.error( 'failed to parse cfg: ' + err );
process.exit();
}
var Countries = require( './public/countries.json' );
//multipleStatements: true,
cfg.mysql_db.multipleStatements = true;
cfg.mysql_db.waitForConnections = false;
cfg.mysql_db.connectionLimit = 15;
var dbpool = mysql.createPool( cfg.mysql_db );
// core
var ldcore = require( './ldcore.js' );
ldcore.init( dbpool, { useCache: false, loglevel: program.loglevel } );
if( program.nologin ) {
ldcore.loginToQuakeliveWebsite();
}
// gametypes
var GT = {
ca: 'Clan Arena',
duel: 'Duel',
ctf: 'Capture the Flag',
race: 'Race',
tdm: 'Team Deathmatch',
ffa: 'Free For All',
ad: 'Attack & Defend',
dom: 'Domination',
ft: 'Freeze Tag',
rr: 'Red Rover',
harvester: 'Harvester',
harv: 'Harvester',
fctf: 'fctf',
}
var allowedCols = [ 'PLAY_TIME', 'IMPRESSIVE', 'EXCELLENT', 'ACC', 'KILLS', 'DEATHS', 'SHOTS', 'G_K', 'SCORE', 'QUIT', 'RL_K', 'LG_K', 'RG_K', 'RG_A', 'LG_A', 'DAMAGE_DEALT', 'DAMAGE_TAKEN', 'RATIO', 'NET_DAMAGE', 'DPS', 'RANK' ];
var allowedColFuncs = [ 'sum', 'avg' ];
// counter
var requests_counter = 0;
var requests_counter_api = 0;
var requests_counter_pub = 0;
var requests_counter_total = 0;
// cache
var qlscache = require( './qlscache.js' );
qlscache.init( cfg, program.loglevel );
var maxAge_public, maxAge_api, http_cache_time;
var env = process.env.NODE_ENV || 'development';
if( 'development' == env ) {
// dev env should not use
maxAge_public = 0;
maxAge_api = 0;
maxAge_api_long = 0;
http_cache_time = 0;
}
if( 'production' == env ) {
maxAge_public = 24*60*60*1000;
maxAge_api = 60*1000;
maxAge_api_long = 60*60*1000;
http_cache_time = 60*60;
}
// gzip/compress
//app.use( compress() );
// http console logger
app.use( log4js.connectLogger( _logger, { level: log4js.levels.INFO, format: ':response-timems :method :url ' } ) );
// http log to file
var logFile = fs.createWriteStream( cfg.api.httplogfile, { flags: 'w' } );
app.use( logger ('combined', {stream: logFile }));
app.use( bodyParser.urlencoded( { extended: true } ) );
// count requests made
app.use( function( req, res, next ) {
++requests_counter;
++requests_counter_pub;
++requests_counter_total;
next();
} );
// serve static html files
app.use( express.static( __dirname + '/public', { maxAge: maxAge_public } ) );
// serve saved games from /get/game/<PUBLIC_ID>.json.gz
//app.use( '/get/game', express.static( __dirname + '/games' ) );
app.use( function( req, res, next ) {
//--requests_counter;
--requests_counter_pub;
++requests_counter_api;
next();
} );
var _perpage = 20;
var lastgames = [];
// api
app.get( '/api', function ( req, res ) {
res.jsonp( { data: { routes: app._router.stack } } );
res.end();
} );
app.get( '/api/search/players/:search_str', function ( req, res ) {
var sql = 'select /*api/search/players/*/ p.NAME as PLAYER, p.ID as PLAYER_ID, p.COUNTRY, c.NAME as CLAN, c.ID as CLAN_ID from Player p left join Clan c on c.ID=p.CLAN_ID WHERE p.NAME like ? ORDER BY NULL LIMIT 200';
dbpool.getConnection( function( err, conn ) {
if( err ) throw err;
conn.query( sql, [ req.params.search_str + "%" ], function( err, rows ) {
conn.release();
if( err ) throw err;
res.jsonp( { data: rows } );
res.end();
} );
} );
} );
app.get( '/api/players', function (req, res) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var sql = [];
sql.push( 'select /*api/players*/' );
sql.push( 'p.COUNTRY,' );
sql.push( 'p.NAME as PLAYER,' );
sql.push( 'c.NAME as CLAN,' );
sql.push( 'c.ID as CLAN_ID' );
sql.push( 'from Player p' );
sql.push( 'left join Clan c' );
sql.push( 'on c.ID=p.CLAN_ID' );
sql.push( 'order by p.NAME asc' );
sql.push( 'limit ' + _offset + ', ' + _perPage + '' );
sql1 = 'select count(ID) as totalRecordCount from Player';
sql = sql.join( ' ' );
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql + ';' + sql1, function( err, resulty ) {
if( err ) { _logger.error( err ); }
conn.release();
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty[0], queryRecordCount: resulty[1][0].totalRecordCount, totalRecordCount: resulty[1][0].totalRecordCount, ms: ( new Date().getTime() - _start ) } );
res.end();
} );
} );
} );
app.get( '/api/players/:player', function( req, res ) {
var nick = mysql_real_escape_string(req.params.player);
var sql = [];
sql.push( 'select /*api/players/X*/' );
sql.push( 'p.ID as PLAYER_ID,' );
sql.push( 'p.NAME as PLAYER,' );
sql.push( 'p.COUNTRY,' );
sql.push( 'p.CLAN_ID,' );
sql.push( 'c.NAME as CLAN' );
sql.push( 'from Player p' );
sql.push( 'left join Clan c' );
sql.push( 'on c.ID=p.CLAN_ID' );
sql.push( 'where p.NAME=?' );
sql = sql.join( ' ' );
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [nick], function( err, rows ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: rows[0] } );
res.end();
conn.release();
});
});
});
app.get( '/api/players/:player/lastgame', function( req, res ) {
var nick = mysql_real_escape_string( req.params.player );
sql = [];
sql.push( 'select /*api/players/X/lastgame*/' );
sql.push( 'g.PUBLIC_ID,' );
sql.push( 'g.GAME_TIMESTAMP' );
sql.push( 'from Game g' );
sql.push( 'left join GamePlayer gp' );
sql.push( 'on gp.GAME_ID=g.ID' );
sql.push( 'where PLAYER_ID=( select ID from Player where NAME=? )' );
sql.push( 'order by g.GAME_TIMESTAMP' );
sql = sql.join( ' ' );
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [nick], function( err, rows ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
out = rows[0];
out.PLAYER = nick;
res.jsonp( { data: out } );
res.end();
conn.release();
});
});
} );
app.get( '/api/players/:player/games', function( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var sql = [];
sql.push( 'select PUBLIC_ID,' );
sql.push( 'GAME_TIMESTAMP,' );
sql.push( 'm.NAME as MAP,' );
sql.push( 'GAME_TYPE,' );
sql.push( 'o.NAME as OWNER,' );
sql.push( 'o.COUNTRY as OWNER_COUNTRY,' );
sql.push( '( case when ( GAME_TYPE in ("ca","ctf","tdm","ad","harv","fctf","rr","ft","dom") and g.WINNING_TEAM = gp.TEAM and gp.RANK > 0 ) or ( ( RANK = 1 and GAME_TYPE = "ffa" ) or ( RANK = 1 and GAME_TYPE = "race" ) or ( RANK = 1 and GAME_TYPE = "duel" ) ) then 1 else 0 end ) as WIN,' );
sql.push( 'RANK,' );
sql.push( 'RULESET,' );
sql.push( 'RANKED,' );
sql.push( 'PREMIUM,' );
sql.push( 'p.NAME as PLAYER,' );
sql.push( 'p.COUNTRY as COUNTRY,' );
sql.push( 'p.ID as PLAYER_ID' );
sql.push( 'from GamePlayer gp' );
sql.push( 'left join Player p on p.ID=gp.PLAYER_ID' );
sql.push( 'left join Game g on g.ID=gp.GAME_ID' );
sql.push( 'left join Map m on m.ID=g.MAP_ID' );
sql.push( 'left join Player o on o.ID=g.OWNER_ID ' );
sql.push( 'where gp.PLAYER_ID=( select ID from Player where NAME="' + req.params.player + '" ) ' );
sql.push( 'order by null' );
sql.push( 'limit ' + _offset + ', ' + _perPage + '' );
//
/*
sql.push( 'select' );
sql.push( 'g.ID,' );
sql.push( 'g.PUBLIC_ID,' );
sql.push( 'GAME_TYPE,' );
sql.push( 'RULESET,' );
sql.push( 'RANKED,' );
sql.push( 'PREMIUM,' );
sql.push( 'TOTAL_KILLS,' );
sql.push( 'GAME_LENGTH,' );
sql.push( 'NUM_PLAYERS,' );
sql.push( 'm.NAME as MAP,' );
sql.push( 'g.GAME_TIMESTAMP,' );
sql.push( 'o.NAME as OWNER,' );
sql.push( 'o.COUNTRY as OWNER_COUNTRY' );
sql.push( 'from Game g' );
sql.push( 'left join Player o' );
sql.push( 'on o.ID=g.OWNER_ID' );
sql.push( 'left join Map m on m.ID=g.MAP_ID' );
sql.push( 'where g.ID in ( select gp.GAME_ID from GamePlayer gp where gp.PLAYER_ID=( select p.ID from Player p where p.NAME="' + req.params.player + '" ) )' );
sql.push( 'order by GAME_TIMESTAMP' );
sql.push( 'limit ' + _offset + ', ' + _perPage + '' );
*/
sql2 = 'select count(PLAYER_ID) as totalRecordCount from GamePlayer where PLAYER_ID=( select ID from Player where NAME="' + req.params.player + '" )';
//sql2 = 'select count(ID) as totalRecordCount from Game';
sql = sql.join( ' ' );
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql + ';' + sql2, function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty[0], queryRecordCount: resulty[1][0].totalRecordCount, totalRecordCount: resulty[1][0].totalRecordCount, ms: ( new Date().getTime() - _start ) } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/hostedgames', function( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var owner = mysql_real_escape_string( req.params.player );
var sql = 'SELECT /*api/owners/X/games*/ g.*, m.NAME as MAP, o.NAME as OWNER, o.COUNTRY as OWNER_COUNTRY, fs.NAME FIRST_SCORER, ls.NAME as LAST_SCORER, dd.NAME as DAMAGE_DELIVERED_NICK, dt.NAME as DAMAGE_TAKEN_NICK, '
+ 'ld.NAME as LEAST_DEATHS_NICK, md.NAME as MOST_DEATHS_NICK, ma.NAME as MOST_ACCURATE_NICK '
+ 'FROM Game g inner join Map m on m.ID=g.MAP_ID '
+ 'left outer join Player o on o.ID=g.OWNER_ID '
+ 'left outer join Player fs on fs.ID=g.FIRST_SCORER_ID '
+ 'left outer join Player ls on ls.ID=g.LAST_SCORER_ID '
+ 'left outer join Player dd on dd.ID=g.DMG_DELIVERED_ID '
+ 'left outer join Player dt on dt.ID=g.DMG_TAKEN_ID '
+ 'left outer join Player ld on ld.ID=g.LEAST_DEATHS_ID '
+ 'left outer join Player md on md.ID=g.MOST_DEATHS_ID '
+ 'left outer join Player ma on ma.ID=g.MOST_ACCURATE_ID '
+ 'where o.NAME="' + owner + '" '
+ 'order by g.GAME_TIMESTAMP desc '
+ 'limit ' + _offset + ', ' + _perPage + ''
;
sql2 = 'select count(OWNER_ID) as totalRecordCount from Game where OWNER_ID=( select ID from Player where NAME="' + req.params.player + '" )';
//sql2 = 'select count(ID) as totalRecordCount from Game';
//sql = sql.join( ' ' );
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql + ';' + sql2, function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty[0], queryRecordCount: resulty[1][0].totalRecordCount, totalRecordCount: resulty[1][0].totalRecordCount, ms: ( new Date().getTime() - _start ) } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/hostedgames/maps', function( req, res ) {
var sql = 'select /*api/maps*/ m.NAME as MAP, g.MAP_ID, count(g.ID) as MATCHES_PLAYED from Game g left join Map m on m.ID=g.MAP_ID where OWNER_ID=(select ID from Player where NAME=?) group by MAP_ID';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/hostedgames/overview', function ( req, res ) {
var sql = 'select /*api/players/:player/hostedgames/overview*/';
sql += 'GAME_TYPE, ';
sql += 'count(1) as MATCHES_PLAYED, ';
sql += 'sum(GAME_LENGTH) as GAME_LENGTH, ';
sql += 'sum(TOTAL_KILLS) as TOTAL_KILLS ';
sql += 'from Game g ';
sql += 'where g.OWNER_ID=( select ID from Player where NAME=? ) ';
sql += 'group by GAME_TYPE ';
sql += 'order by null ';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/hostedgames/games/graphs/perweek', function( req, res ) {
var sql = 'select year(FROM_UNIXTIME(GAME_TIMESTAMP)) as year, week(FROM_UNIXTIME(GAME_TIMESTAMP),1) as week, count(GAME_TIMESTAMP) as c from Game where OWNER_ID=( select ID from Player where NAME=? ) group by year, week';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/weapons', function( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var owner = mysql_real_escape_string( req.params.player );
var sql = 'SELECT /*api/players/:p/weapons*/'
+ 'sum(gp.G_K) as G_K,'
+ 'sum(gp.BFG_S) as BFG_S,'
+ 'sum(gp.BFG_H) as BFG_H,'
+ 'sum(gp.BFG_K) as BFG_K,'
+ 'sum(gp.CG_S) as CG_S,'
+ 'sum(gp.CG_H) as CG_H,'
+ 'sum(gp.CG_K) as CG_K,'
+ 'sum(gp.GL_S) as GL_S,'
+ 'sum(gp.GL_H) as GL_H,'
+ 'sum(gp.GL_K) as GL_K,'
+ 'sum(gp.LG_S) as LG_S,'
+ 'sum(gp.LG_H) as LG_H,'
+ 'sum(gp.LG_K) as LG_K,'
+ 'sum(gp.MG_S) as MG_S,'
+ 'sum(gp.MG_H) as MG_H,'
+ 'sum(gp.MG_K) as MG_K,'
+ 'sum(gp.NG_S) as NG_S,'
+ 'sum(gp.NG_H) as NG_H,'
+ 'sum(gp.NG_K) as NG_K,'
+ 'sum(gp.PG_S) as PG_S,'
+ 'sum(gp.PG_H) as PG_H,'
+ 'sum(gp.PG_K) as PG_K,'
+ 'sum(gp.PM_S) as PM_S,'
+ 'sum(gp.PM_H) as PM_H,'
+ 'sum(gp.PM_K) as PM_K,'
+ 'sum(gp.RG_S) as RG_S,'
+ 'sum(gp.RG_H) as RG_H,'
+ 'sum(gp.RG_K) as RG_K,'
+ 'sum(gp.RL_S) as RL_S,'
+ 'sum(gp.RL_H) as RL_H,'
+ 'sum(gp.RL_K) as RL_K,'
+ 'sum(gp.SG_S) as SG_S,'
+ 'sum(gp.SG_H) as SG_H,'
+ 'sum(gp.SG_K) as SG_K,'
+ 'sum(gp.HMG_S) as HMG_S,'
+ 'sum(gp.HMG_H) as HMG_H,'
+ 'sum(gp.HMG_K) as HMG_K, '
+ 'sum(gp.IMPRESSIVE) as IMPRESSIVE, '
+ 'sum(gp.EXCELLENT) as EXCELLENT, '
+ 'count(1) as MATCHES_PLAYED '
+ 'FROM GamePlayer gp '
+ 'left join Player p on p.ID=gp.PLAYER_ID '
+ 'where p.NAME=? '
;
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty[0] } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/weapons/graphs/perweek', function( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var owner = mysql_real_escape_string( req.params.player );
var sql = 'SELECT /*api/players/:p/weapons*/'
+ 'sum(gp.G_K) as G_K, '
+ 'sum(gp.BFG_S) as BFG_S, '
+ 'sum(gp.BFG_H) as BFG_H, '
+ 'sum(gp.BFG_K) as BFG_K, '
+ 'sum(gp.CG_S) as CG_S, '
+ 'sum(gp.CG_H) as CG_H, '
+ 'sum(gp.CG_K) as CG_K, '
+ 'sum(gp.GL_S) as GL_S, '
+ 'sum(gp.GL_H) as GL_H, '
+ 'sum(gp.GL_K) as GL_K, '
+ 'sum(gp.LG_S) as LG_S, '
+ 'sum(gp.LG_H) as LG_H, '
+ 'sum(gp.LG_K) as LG_K, '
+ 'sum(gp.MG_S) as MG_S, '
+ 'sum(gp.MG_H) as MG_H, '
+ 'sum(gp.MG_K) as MG_K, '
+ 'sum(gp.NG_S) as NG_S, '
+ 'sum(gp.NG_H) as NG_H, '
+ 'sum(gp.NG_K) as NG_K, '
+ 'sum(gp.PG_S) as PG_S, '
+ 'sum(gp.PG_H) as PG_H, '
+ 'sum(gp.PG_K) as PG_K, '
+ 'sum(gp.PM_S) as PM_S, '
+ 'sum(gp.PM_H) as PM_H, '
+ 'sum(gp.PM_K) as PM_K, '
+ 'sum(gp.RG_S) as RG_S, '
+ 'sum(gp.RG_H) as RG_H, '
+ 'sum(gp.RG_K) as RG_K, '
+ 'sum(gp.RL_S) as RL_S, '
+ 'sum(gp.RL_H) as RL_H, '
+ 'sum(gp.RL_K) as RL_K, '
+ 'sum(gp.SG_S) as SG_S, '
+ 'sum(gp.SG_H) as SG_H, '
+ 'sum(gp.SG_K) as SG_K, '
+ 'sum(gp.HMG_S) as HMG_S, '
+ 'sum(gp.HMG_H) as HMG_H, '
+ 'sum(gp.HMG_K) as HMG_K, '
+ 'sum(gp.IMPRESSIVE) as IMPRESSIVE, '
+ 'sum(gp.EXCELLENT) as EXCELLENT, '
+ 'year(FROM_UNIXTIME(g.GAME_TIMESTAMP)) as year, '
+ 'week(FROM_UNIXTIME(g.GAME_TIMESTAMP),1) as week, '
+ 'count(1) as MATCHES_PLAYED '
+ 'FROM GamePlayer gp '
+ 'left join Player p on p.ID=gp.PLAYER_ID '
+ 'left join Game g on g.ID=gp.GAME_ID '
+ 'where p.NAME=? '
+ 'group by year, week ';
;
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/weapons/graphs/permonth', function( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var owner = mysql_real_escape_string( req.params.player );
var sql = 'SELECT /*api/players/:p/weapons*/'
+ 'sum(gp.G_K) as G_K, '
+ 'sum(gp.BFG_S) as BFG_S, '
+ 'sum(gp.BFG_H) as BFG_H, '
+ 'sum(gp.BFG_K) as BFG_K, '
+ 'sum(gp.CG_S) as CG_S, '
+ 'sum(gp.CG_H) as CG_H, '
+ 'sum(gp.CG_K) as CG_K, '
+ 'sum(gp.GL_S) as GL_S, '
+ 'sum(gp.GL_H) as GL_H, '
+ 'sum(gp.GL_K) as GL_K, '
+ 'sum(gp.LG_S) as LG_S, '
+ 'sum(gp.LG_H) as LG_H, '
+ 'sum(gp.LG_K) as LG_K, '
+ 'sum(gp.MG_S) as MG_S, '
+ 'sum(gp.MG_H) as MG_H, '
+ 'sum(gp.MG_K) as MG_K, '
+ 'sum(gp.NG_S) as NG_S, '
+ 'sum(gp.NG_H) as NG_H, '
+ 'sum(gp.NG_K) as NG_K, '
+ 'sum(gp.PG_S) as PG_S, '
+ 'sum(gp.PG_H) as PG_H, '
+ 'sum(gp.PG_K) as PG_K, '
+ 'sum(gp.PM_S) as PM_S, '
+ 'sum(gp.PM_H) as PM_H, '
+ 'sum(gp.PM_K) as PM_K, '
+ 'sum(gp.RG_S) as RG_S, '
+ 'sum(gp.RG_H) as RG_H, '
+ 'sum(gp.RG_K) as RG_K, '
+ 'sum(gp.RL_S) as RL_S, '
+ 'sum(gp.RL_H) as RL_H, '
+ 'sum(gp.RL_K) as RL_K, '
+ 'sum(gp.SG_S) as SG_S, '
+ 'sum(gp.SG_H) as SG_H, '
+ 'sum(gp.SG_K) as SG_K, '
+ 'sum(gp.HMG_S) as HMG_S, '
+ 'sum(gp.HMG_H) as HMG_H, '
+ 'sum(gp.HMG_K) as HMG_K, '
+ 'sum(gp.IMPRESSIVE) as IMPRESSIVE, '
+ 'sum(gp.EXCELLENT) as EXCELLENT, '
+ 'year(FROM_UNIXTIME(g.GAME_TIMESTAMP)) as year, '
+ 'month(FROM_UNIXTIME(g.GAME_TIMESTAMP)) as month, '
+ 'count(1) as MATCHES_PLAYED '
+ 'FROM GamePlayer gp '
+ 'left join Player p on p.ID=gp.PLAYER_ID '
+ 'left join Game g on g.ID=gp.GAME_ID '
+ 'where p.NAME=? '
+ 'group by year, month ';
;
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/weapons/graphs/peryear', function( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var owner = mysql_real_escape_string( req.params.player );
var sql = 'SELECT /*api/players/:p/weapons*/'
+ 'sum(gp.G_K) as G_K, '
+ 'sum(gp.BFG_S) as BFG_S, '
+ 'sum(gp.BFG_H) as BFG_H, '
+ 'sum(gp.BFG_K) as BFG_K, '
+ 'sum(gp.CG_S) as CG_S, '
+ 'sum(gp.CG_H) as CG_H, '
+ 'sum(gp.CG_K) as CG_K, '
+ 'sum(gp.GL_S) as GL_S, '
+ 'sum(gp.GL_H) as GL_H, '
+ 'sum(gp.GL_K) as GL_K, '
+ 'sum(gp.LG_S) as LG_S, '
+ 'sum(gp.LG_H) as LG_H, '
+ 'sum(gp.LG_K) as LG_K, '
+ 'sum(gp.MG_S) as MG_S, '
+ 'sum(gp.MG_H) as MG_H, '
+ 'sum(gp.MG_K) as MG_K, '
+ 'sum(gp.NG_S) as NG_S, '
+ 'sum(gp.NG_H) as NG_H, '
+ 'sum(gp.NG_K) as NG_K, '
+ 'sum(gp.PG_S) as PG_S, '
+ 'sum(gp.PG_H) as PG_H, '
+ 'sum(gp.PG_K) as PG_K, '
+ 'sum(gp.PM_S) as PM_S, '
+ 'sum(gp.PM_H) as PM_H, '
+ 'sum(gp.PM_K) as PM_K, '
+ 'sum(gp.RG_S) as RG_S, '
+ 'sum(gp.RG_H) as RG_H, '
+ 'sum(gp.RG_K) as RG_K, '
+ 'sum(gp.RL_S) as RL_S, '
+ 'sum(gp.RL_H) as RL_H, '
+ 'sum(gp.RL_K) as RL_K, '
+ 'sum(gp.SG_S) as SG_S, '
+ 'sum(gp.SG_H) as SG_H, '
+ 'sum(gp.SG_K) as SG_K, '
+ 'sum(gp.HMG_S) as HMG_S, '
+ 'sum(gp.HMG_H) as HMG_H, '
+ 'sum(gp.HMG_K) as HMG_K, '
+ 'sum(gp.IMPRESSIVE) as IMPRESSIVE, '
+ 'sum(gp.EXCELLENT) as EXCELLENT, '
+ 'year(FROM_UNIXTIME(g.GAME_TIMESTAMP)) as year, '
+ 'count(1) as MATCHES_PLAYED '
+ 'FROM GamePlayer gp '
+ 'left join Player p on p.ID=gp.PLAYER_ID '
+ 'left join Game g on g.ID=gp.GAME_ID '
+ 'where p.NAME=? '
+ 'group by year ';
;
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/overview', function ( req, res ) {
var sql = 'select /*api/players/:player/overview*/';
sql += 'GAME_TYPE, ';
sql += 'count(1) as MATCHES_PLAYED, ';
sql += 'sum(GAME_LENGTH) as GAME_LENGTH, ';
sql += 'sum(TOTAL_KILLS) as TOTAL_KILLS ';
sql += 'from Game g ';
sql += 'left join GamePlayer gp on gp.GAME_ID=g.ID ';
sql += 'left join Player p on p.ID=gp.PLAYER_ID ';
sql += 'where p.NAME=? ';
sql += 'group by GAME_TYPE ';
sql += 'order by null ';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/games/graphs/perweek', function ( req, res ) {
var sql = 'select /*api/players/:player/games/graphs/perweek*/';
sql += 'year(FROM_UNIXTIME(GAME_TIMESTAMP)) as year, ';
sql += 'week(FROM_UNIXTIME(GAME_TIMESTAMP),1) as week, ';
sql += 'count(GAME_TIMESTAMP) as c ';
sql += 'from Game g ';
sql += 'left join GamePlayer gp on gp.GAME_ID=g.ID ';
sql += 'left join Player p on p.ID=gp.PLAYER_ID ';
sql += 'where p.NAME=? ';
sql += 'group by year, week';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
conn.release();
} );
} );
} );
/*
app.get( '/api/players/:player/rank', function( req, res ) {
//var ranking = new glicko2.Glicko2( settings );
// UNIX_TIMESTAMP( DATE_SUB( NOW(), INTERVAL 30 day ) )
var sql = 'select ID, NAME from Player where NAME=? ';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, rows ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: { games: rows } } );
res.end();
conn.release();
} );
} );
} );
*/
app.get( '/api/players/:player/clans', function ( req, res ) {
var sql = 'select /*api/players/X/clans*/ c.ID as CLAN_ID, c.NAME as CLAN, count(*) as MATCHES_PLAYED from Player p inner join GamePlayer gp on gp.PLAYER_ID=p.ID inner join Clan c on c.ID=gp.CLAN_ID '
+ ' where p.NAME=? group by p.NAME, c.NAME';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [ req.params.player ], function( err, rows ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: rows } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/update', function ( req, res ) {
var queryObject = url.parse( req.url, true ).query;
if (!allow_update) {
res.jsonp({ data: {}, error: [{ not_allowed: "" }] });
res.end();
return;
}
var d = new Date();
var _date = d.getFullYear() + '-' + ( d.getMonth() + 1 ) + '-' + d.getUTCDate();
if( typeof queryObject.date != 'undefined' ) { _date = queryObject.date; }
var nick = mysql_real_escape_string(req.params.player);
var _url = 'http://www.quakelive.com/profile/matches_by_week/' + nick + '/' + _date;
request( _url, function( err, resp, body ) {
if( err ) { throw err; }
$ = cheerio.load( body );
var conn;
var updatedGames = [];
var scanned = 0;
Q.ninvoke(dbpool, "getConnection")
.then(function(c) { conn = c; })
.then(function() {
return ldcore.init( conn, { useCache: false } ).then(function() {
var tasks = [];
$('.areaMapC').each(function () {
++scanned;
var publicId = $(this).attr('id').split('_')[1];
tasks.push(
ldcore.query( 'SELECT /*api/players/X/update*/ PUBLIC_ID FROM Game WHERE PUBLIC_ID=?', [publicId] )
.then(function(result) {
if (result.length == 0) {
updatedGames.push( publicId );
return get_game( ldcore, publicId );
}
else {
return undefined;
}
})
);
});
return Q.allSettled(tasks);
})
.then(function() {
res.jsonp({ data: { player: nick, updated: updatedGames.length, scanned: scanned, updated_games: updatedGames } });
res.end();
})
.fail(function (err) {
res.jsonp({ data: { }, error: err });
res.end();
})
.finally(function () {
conn.release();
});
} );
} );
} );
app.get( '/api/players/:player/maps', function( req, res ) {
var sql = 'select /*api/players/:player/maps*/ ';
sql += 'm.NAME as MAP, ';
sql += 'g.MAP_ID, ';
sql += 'count(g.ID) as MATCHES_PLAYED ';
sql += 'from Game g ';
sql += 'left join Map m on m.ID=g.MAP_ID ';
sql += 'left join GamePlayer gp on gp.GAME_ID=g.ID ';
sql += 'left join Player p on p.ID=gp.PLAYER_ID ';
sql += 'where p.NAME=? ';
sql += 'group by MAP_ID ';
//_dt = qlscache.doCache( req, sql, [req.params.player], { time: 24*60*60*1000 } );
//res.jsonp( _dt );
//res.end();
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, rows ) {
if( err ) { _logger.error( err ); }
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: rows } );
res.end();
conn.release();
} );
} );
} );
app.get( '/api/players/:player/race', function( req, res ) {
var queryObject = url.parse(req.url, true).query;
var _playerNick = req.params.player;
var _ruleset = queryObject.ruleset == "vql" ? 2 : 0;
var _weapons = queryObject.weapons == "off" ? 1 : 0;
var _mapName = queryObject.map;
sql = "select m.NAME MAP,p.NAME PLAYER_NICK,p.NAME PLAYER,r.SCORE,from_unixtime(r.GAME_TIMESTAMP) GAME_TIMESTAMP,r.RANK,g.PUBLIC_ID, " +
" leader.NAME LEADER_NICK,best.SCORE LEADER_SCORE" +
" from Race r inner join Player p on p.ID=r.PLAYER_ID inner join Map m on m.ID=r.MAP_ID left outer join Game g on g.ID=r.GAME_ID " +
" left outer join Race best on best.MAP_ID=r.MAP_ID and best.MODE=r.mode and best.RANK=1 left outer join Player leader on leader.ID=best.PLAYER_ID" +
" where p.NAME=? and r.MODE=?";
if(queryObject.map)
sql += " and m.NAME=?";
sql += " order by m.NAME";
dbpool.getConnection( function( err, conn ) {
conn.query( sql, [_playerNick, _ruleset + _weapons, _mapName], function( err2, rows ) {
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: { ruleset: _ruleset ? "vql" : "pql", weapons: _weapons ? "off" : "on", scores: rows } } );
conn.release();
res.end();
});
});
});
app.get( '/api/players/:player/top50/:gametype/:colFunc/:col', function( req, res ) {
var queryObject = url.parse( req.url, true ).query;
//alt = mysql_real_escape_string( req.params.alt );
//col = queryObject.col || 'PLAY_TIME';
//colFunc = queryObject.colFunc || 'sum';
gt = mysql_real_escape_string( req.params.gametype ) || 'all';
col = mysql_real_escape_string( req.params.col ) || 'PLAY_TIME';
colFunc = mysql_real_escape_string( req.params.colFunc ) || 'sum';
//interval = queryObject.interval || 30;
allowedGt = [ 'all' ];
sort = queryObject.sort || 'desc';
for( var i in GT ) {
allowedGt.push( i );
}
allowedIntervals = [ 30, 7, 1 ];
if( allowedGt.indexOf( gt ) == -1 || allowedCols.indexOf( col ) == -1 || allowedColFuncs.indexOf( colFunc ) == -1 ) {
res.jsonp( { data: [], msg: 'invalid gametype, col, colFunc' } );
res.end();
}
gametypeWhere = '';
if( gt != 'all' ) {
gametypeWhere = ' and g.GAME_TYPE="' + gt + '" ';
}
mingamesWhere = '';
if( colFunc == 'avg' ) {
mingamesWhere = ' having MATCHES_PLAYED > 5 ';
}
col_name = col;
// col rewrites
if( col == 'ACC' ) { col = 'HITS/SHOTS*100'; }
else if( col == 'RG_A' ) { col = 'RG_H/RG_S*100'; }
else if( col == 'LG_A' ) { col = 'LG_H/LG_S*100'; }
else if( col == 'RATIO' ) { col = 'KILLS/DEATHS'; }
else if( col == 'NET_DAMAGE' ) { col = 'DAMAGE_DEALT-DAMAGE_TAKEN'; }
else if( col == 'DPS' ) { col = 'DAMAGE_DEALT/PLAY_TIME'; }
else if( col == 'RANK' ) { col = 'case when RANK = -1 then 16 else RANK end '; }
var sql = 'select x.*, ' +
'p.NAME as PLAYER, ' +
'p.COUNTRY as COUNTRY ' +
'from ' +
'( select ' +
'gp.PLAYER_ID, ' +
'count(1) as MATCHES_PLAYED, ' +
'round(' + colFunc + '( ' + col + ' ),2) as ' + col_name +
//'round(avg(gp.HITS/gp.SHOTS)*100,2) as ACC ' +
' from Game g ' +
'join GamePlayer gp on gp.GAME_ID=g.ID ' +
'where PLAYER_ID=(SELECT ID FROM Player WHERE NAME=?) AND g.GAME_TIMESTAMP > UNIX_TIMESTAMP( DATE_SUB( NOW(), INTERVAL 30 day ) ) ' +
gametypeWhere +
additionalWhere +
'group by gp.PLAYER_ID ' +
mingamesWhere +
'order by ' + col_name + ' ' + sort + ' limit 50 ) ' +
'x left join Player p on p.ID=x.PLAYER_ID ' +
'';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
conn.release();
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
} );
} );
} );
app.get( '/api/players/:player/hostedgames/top50/:gametype/:colFunc/:col', function( req, res ) {
var queryObject = url.parse( req.url, true ).query;
//alt = mysql_real_escape_string( req.params.alt );
//col = queryObject.col || 'PLAY_TIME';
//colFunc = queryObject.colFunc || 'sum';
gt = mysql_real_escape_string( req.params.gametype ) || 'all';
col = mysql_real_escape_string( req.params.col ) || 'PLAY_TIME';
colFunc = mysql_real_escape_string( req.params.colFunc ) || 'sum';
//interval = queryObject.interval || 30;
sort = queryObject.sort || 'desc';
allowedGt = [ 'all' ];
for( var i in GT ) {
allowedGt.push( i );
}
allowedIntervals = [ 30, 7, 1 ];
if( allowedGt.indexOf( gt ) == -1 || allowedCols.indexOf( col ) == -1 || allowedColFuncs.indexOf( colFunc ) == -1 ) {
res.jsonp( { data: [], msg: 'invalid gametype, col, colFunc' } );
res.end();
}
gametypeWhere = '';
if( gt != 'all' ) {
gametypeWhere = ' and g.GAME_TYPE="' + gt + '" ';
}
mingamesWhere = '';
if( colFunc == 'avg' ) {
mingamesWhere = ' having MATCHES_PLAYED > 5 ';
}
col_name = col;
// col rewrites
if( col == 'ACC' ) { col = 'HITS/SHOTS*100'; }
else if( col == 'RG_A' ) { col = 'RG_H/RG_S*100'; }
else if( col == 'LG_A' ) { col = 'LG_H/LG_S*100'; }
else if( col == 'RATIO' ) { col = 'KILLS/DEATHS'; }
else if( col == 'NET_DAMAGE' ) { col = 'DAMAGE_DEALT-DAMAGE_TAKEN'; }
else if( col == 'DPS' ) { col = 'DAMAGE_DEALT/PLAY_TIME'; }
else if( col == 'RANK' ) { col = 'case when RANK = -1 then 16 else RANK end '; }
var sql = 'select x.*, ' +
'p.NAME as PLAYER, ' +
'p.COUNTRY as COUNTRY ' +
'from ' +
'( select ' +
'gp.PLAYER_ID, ' +
'count(1) as MATCHES_PLAYED, ' +
'round(' + colFunc + '( ' + col + ' ),2) as ' + col_name +
//'round(avg(gp.HITS/gp.SHOTS)*100,2) as ACC ' +
' from Game g ' +
'join GamePlayer gp on gp.GAME_ID=g.ID ' +
'where OWNER_ID=(SELECT ID FROM Player WHERE NAME=?) AND g.GAME_TIMESTAMP > UNIX_TIMESTAMP( DATE_SUB( NOW(), INTERVAL 30 day ) ) ' +
gametypeWhere +
'group by gp.PLAYER_ID ' +
mingamesWhere +
'order by ' + col_name + ' ' + sort + ' limit 50 ) ' +
'x left join Player p on p.ID=x.PLAYER_ID ' +
'';
dbpool.getConnection( function( err, conn ) {
if( err ) { _logger.error( err ); }
conn.query( sql, [req.params.player], function( err, resulty ) {
if( err ) { _logger.error( err ); }
conn.release();
res.set( 'Cache-Control', 'public, max-age=' + http_cache_time );
res.jsonp( { data: resulty } );
res.end();
} );
} );
} );
app.get( '/api/games', function ( req, res ) {
_start = new Date().getTime();
var queryObject = url.parse( req.url, true ).query;
_page = queryObject.page || 1;
_perPage = queryObject.perPage || 10;
if( _perPage > 100 ) { _perPage = 100; }
_offset = queryObject.offset || 0;
var sql = [];
sql.push( 'select /*api/games*/' );
sql.push( 'g.PUBLIC_ID,' );
sql.push( 'g.GAME_TYPE,' );
sql.push( 'g.GAME_TIMESTAMP,' );
sql.push( 'g.TOTAL_KILLS,' );
sql.push( 'g.GAME_LENGTH,' );