From 52c37fbb02fffcee88105a426bd49ba09c8c2f26 Mon Sep 17 00:00:00 2001 From: Patrick Engel Date: Wed, 22 Jul 2020 07:46:59 +0000 Subject: [PATCH] Upload New File --- cd.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 cd.js diff --git a/cd.js b/cd.js new file mode 100644 index 0000000..929df16 --- /dev/null +++ b/cd.js @@ -0,0 +1,61 @@ +const fflog = "https://www.fflogs.com:443/v1/report/fights/"; +const api = "?api_key="; + +var indexViewModel; + +// Do stuff when HTML parsing is ready +$(() => { + // Document is ready + function IndexViewModel() { + var self = this; + self.apiKey = ko.observable(localStorage.getItem("apikey")); + self.logID = ko.observable(""); + self.fights = ko.observableArray(); + self.friendlies = ko.observableArray(); + self.displayFightPicker = ko.observable(false); + self.selectedFight = ko.observable(); + + self.getLog = async function() { + var log = await fetchLog(self.logID()); + + self.fights(log["fights"]); + self.friendlies(log["friendlies"]); + self.displayFightPicker(true); + }; + + self.formatTime = function(fight) { + let secs = (fight.end_time - fight.start_time)/1000; + let mins = Math.floor(secs / 60); + let rest = Math.floor(secs % 60); + return mins+"m"+rest+"s"; + }; + + self.apiKeyChanged = function() { + console.log("Setting apiKey to ",self.apiKey()); + localStorage.setItem("apikey",self.apiKey()); + }; + + self.fightChanged = function() { + console.log("Fight has changed to ",self.selectedFight()); + }; + }; + indexViewModel = new IndexViewModel(); + ko.applyBindings(indexViewModel); +}); + +// API request um JSON zu erhalten +async function fetchLog(fightId) { + let url = fflog + fightId + api + indexViewModel.apiKey(); + + console.log("Fetching log "+fightId); + var res = await fetch(url); + if (!res.ok) { + console.log(res); + throw Error(res.text); + } + var log = await res.json(); + + //get specific fight data + console.log(log); + return log; +} \ No newline at end of file