-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetchParseSortAndLoadTournaments.js
66 lines (52 loc) · 1.83 KB
/
fetchParseSortAndLoadTournaments.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
function fetchParseSortAndLoadTournamentsForUser(url, tournaments, handleFinish){
var result = fetch( url ).then(
function( response ) {
if ( !response.ok ) {
console.log(response);
response.text().then(body => console.log(body));
throw new Exception( response.status )
}
return response.text()
}).then( function( text ) {
var json = JSON.parse('['+text.split("}\n{").join("},{")+']');//TODO:how do i parse this properly
for (var x in json){
if (json[x]["status"] != 30 && json[x]["variant"]["key"] == "crazyhouse") {
tournaments.push(new Tournament(json[x]));
}
}
}).then ( function(){
tournaments.sort(function(a, b){return a.json['startsAt']-b.json['startsAt']});
}).catch( function( error ) {
console.info( "Received error ", error);
}).then ( handleFinish );
return result;
}
///////////////////////////////////////
function fetchParseSortAndLoadTournamentsOfLichess(tournaments, handleFinish){
var result = fetch( "https://lichess.org/api/tournament" ).then( function( response ) {
if ( !response.ok ) {
console.log(response);
response.text().then(body => console.log(body));
throw new Exception( response.status )
}
return response.json()
}).then( function( json ) {
for (var x in json["started"]){
var t = json["started"][x];
if (t["variant"]["key"]=="crazyhouse"){
tournaments.push(new Tournament(t));
}
}
for (var x in json["created"]){
var t = json["created"][x];
if (t["variant"]["key"]=="crazyhouse"){
tournaments.push(new Tournament(t));
}
}
}).then ( function(){
tournaments.sort(function(a, b){return a.json['startsAt']-b.json['startsAt']});
}).catch( function( error ) {
console.info( "Received error ", error);
}).then (handleFinish );
return result;
}