just making sure to have a backup online
This commit is contained in:
@@ -1,19 +1,9 @@
|
||||
import React from 'react';
|
||||
const URLleaderboard = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/leaderboard";
|
||||
const URLPlayer = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/player/";
|
||||
const URLPlayerMaps = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/player/maps/";
|
||||
const URLMaps = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/map/";
|
||||
function makeOptions(method, body) {
|
||||
var opts = {
|
||||
method: method,
|
||||
headers: {
|
||||
"Content-type": "application/json"
|
||||
}
|
||||
};
|
||||
if (body) {
|
||||
opts.body = JSON.stringify(body);
|
||||
}
|
||||
return opts;
|
||||
}
|
||||
const URLAllMaps = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/allmaps";
|
||||
|
||||
function handleHttpErrors(res) {
|
||||
if (!res.ok) {
|
||||
@@ -22,22 +12,123 @@ function handleHttpErrors(res) {
|
||||
return res.json();
|
||||
}
|
||||
|
||||
class DataFacade {
|
||||
getLeaderBoard = () => {
|
||||
return fetch(URLleaderboard).then(handleHttpErrors);
|
||||
class DataFacade extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
getLeaderBoardFromCache = async (incrementer) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("leaderboardTimer"));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
if (!CachedTime || timeExpired > 30 * 60000) {
|
||||
localStorage.setItem("leaderboardTimer", JSON.stringify(Date.now()));
|
||||
localStorage.removeItem("leaderboard");
|
||||
incrementer = 0;
|
||||
}
|
||||
var leaderBoardData = localStorage.getItem("leaderboard");
|
||||
if (incrementer === 0 && leaderBoardData) {
|
||||
const cachedLeaderBoard = JSON.parse(leaderBoardData);
|
||||
return cachedLeaderBoard;
|
||||
} else {
|
||||
const players = await fetch(URLleaderboard + "/" + incrementer).then(handleHttpErrors);
|
||||
if (leaderBoardData !== null) {
|
||||
const cachedLeaderBoard = JSON.parse(leaderBoardData);
|
||||
players.map(e => (
|
||||
cachedLeaderBoard.push(e)
|
||||
))
|
||||
localStorage.setItem("leaderboard", JSON.stringify(cachedLeaderBoard));
|
||||
return cachedLeaderBoard;
|
||||
} else {
|
||||
localStorage.setItem("leaderboard", JSON.stringify(players));
|
||||
return players;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
getPlayer = steamID => {
|
||||
return fetch(URLPlayer + steamID).then(handleHttpErrors);
|
||||
getAllMapsFromCache = async () => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("allmapsTimer"));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
if (!CachedTime || timeExpired > 30 * 60000) {
|
||||
localStorage.setItem("allmapsTimer", JSON.stringify(Date.now()));
|
||||
localStorage.removeItem("allmaps");
|
||||
}
|
||||
var allMapsData = localStorage.getItem("allmaps");
|
||||
if (allMapsData) {
|
||||
const cachedAllMaps = JSON.parse(allMapsData);
|
||||
return cachedAllMaps;
|
||||
} else {
|
||||
const allMaps = await fetch(URLAllMaps).then(handleHttpErrors);
|
||||
localStorage.setItem("allmaps", JSON.stringify(allMaps));
|
||||
return allMaps;
|
||||
}
|
||||
};
|
||||
|
||||
getPlayerFromCache = async (steamID) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("initialCachedplayers" + steamID));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
if (!CachedTime || timeExpired > 30 * 60000) {
|
||||
localStorage.setItem("initialCachedplayers" + steamID, JSON.stringify(Date.now()));
|
||||
localStorage.removeItem("player/" + steamID)
|
||||
}
|
||||
var storageString = "player/" + steamID;
|
||||
const playerData = localStorage.getItem(storageString);
|
||||
if (playerData) {
|
||||
const player = JSON.parse(playerData);
|
||||
return player;
|
||||
} else {
|
||||
const player = await fetch(URLPlayer + steamID).then(handleHttpErrors);
|
||||
localStorage.setItem(storageString, JSON.stringify(player));
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
getPlayerTimes = steamID => {
|
||||
return fetch(URLPlayerMaps + steamID).then(handleHttpErrors);
|
||||
getPlayerTimesFromCache = async (steamID) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("initialCachedplayersTimes" + steamID));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
if (!CachedTime || timeExpired > 30 * 60000) {
|
||||
localStorage.setItem("initialCachedplayersTimes" + steamID, JSON.stringify(Date.now()));
|
||||
localStorage.removeItem("playertimes/" + steamID)
|
||||
}
|
||||
var storageString = "playertimes/" + steamID;
|
||||
var playerTimesData = localStorage.getItem(storageString);
|
||||
if (playerTimesData) {
|
||||
const cachedPlayerTimes = JSON.parse(playerTimesData)
|
||||
return cachedPlayerTimes;
|
||||
} else {
|
||||
const playertimes = await fetch(URLPlayerMaps + steamID).then(handleHttpErrors);
|
||||
localStorage.setItem(storageString, JSON.stringify(playertimes));
|
||||
return playertimes;
|
||||
}
|
||||
}
|
||||
|
||||
getMapTimes = (mapname, stage) => {
|
||||
return fetch(URLMaps + mapname + "/" + stage).then(handleHttpErrors);
|
||||
getMapTimesFromCache = async (mapname, stage) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("maptimescached" + mapname + stage));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
if (!CachedTime || timeExpired > 30 * 60000) {
|
||||
localStorage.setItem("maptimescached" + mapname + stage, JSON.stringify(Date.now()));
|
||||
localStorage.removeItem("map/" + mapname + stage);
|
||||
}
|
||||
var mapTimesData = localStorage.getItem("map/" + mapname + stage);
|
||||
if (mapTimesData) {
|
||||
const cachedMapTimes = JSON.parse(mapTimesData);
|
||||
return cachedMapTimes;
|
||||
} else {
|
||||
const mapTimes = await fetch(URLMaps + mapname + "/" + stage).then(handleHttpErrors);
|
||||
localStorage.setItem("map/" + mapname + stage, JSON.stringify(mapTimes));
|
||||
return mapTimes;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default new DataFacade();
|
||||
Reference in New Issue
Block a user