var client = { api: "https://api.flanchan.moe/rtbw", wsapi: "wss://api.flanchan.moe/rtbw", timer: undefined, info: undefined, template: undefined, allTime: undefined, allTimeAverage: undefined, sessionAllTime: undefined, sessionAllTimeAverage: undefined, startupLast5: undefined, onWSclose: function(){}, superBuffer: undefined, superGrapher: function(s){}, preloadCycles: 10, start: function(grapher, cb=null, err=null) { client.grapher = grapher; $.ajaxSetup({ error: function(x, e) { if(err!=null) err(e); } }); $.getJSON(client.api+"/info", function(res) { client.info = res; $.getJSON(client.api+"/template", function(template) { client.template = template; client.sessionAllTime = $.extend({}, template); client.sessionAllTimeAverage = $.extend({}, template); client.sessionAllTimeAverage._n = 0; $.getJSON(client.api+"/all", function(alltime) { client.allTime = alltime; $.getJSON(client.api+"/all/average", function(aaverage) { client.allTimeAverage = aaverage; $.getJSON(client.api+"/buffer/super", function(sbuf) { client.superBuffer = sbuf; $.getJSON(client.api+"/buffer/"+client.preloadCycles, function(last5) { client.startupLast5 = last5; try { var ws = undefined; try { ws = client.ws = new WebSocket(client.wsapi+"/cycle"); }catch(e){ if(err!=null) err(e); return; } ws.onopen = function() { if(cb!==null) cb(); }; ws.onclose = function() { client.onWSclose(); }; ws.onmessage = function(data) { var sdt = JSON.parse(data.data); if(sdt.type=="cycle") { var dt = sdt.data; client.allTime = cycleAdd(client.allTime, [dt]); client.sessionAllTime = cycleAdd(client.sessionAllTime, [dt]); client.allTimeAverage = cycleAcc(client.allTimeAverage, [dt], client.allTimeAverage._n); client.allTimeAverage._n +=1; client.sessionAllTimeAverage = cycleAcc(client.sessionAllTimeAverage, [dt], client.sessionAllTimeAverage._n); client.sessionAllTimeAverage._n +=1; client.grapher(dt); } else if(sdt.type=="supercycle") { client.superGrapher(sdt.data); } }; } catch(e) { if(err!=null) err(e); } }); }); }); }); }); }); }, stop: function() { if(client.timer!==undefined) clearInterval(client.timer); } };