saving some updates
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import React from 'react';
|
||||
import { trackPromise } from 'react-promise-tracker';
|
||||
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/";
|
||||
const URLAllMaps = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/allmaps";
|
||||
const URLMapsSize = "http://localhost:8080/racetimer_endpoints-1.0/api/timers/mapsizecache";
|
||||
|
||||
function handleHttpErrors(res) {
|
||||
if (!res.ok) {
|
||||
@@ -20,8 +22,6 @@ class DataFacade extends React.Component {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
getLeaderBoardFromCache = async (incrementer) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("leaderboardTimer"));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
@@ -36,22 +36,27 @@ class DataFacade extends React.Component {
|
||||
const cachedLeaderBoard = JSON.parse(leaderBoardData);
|
||||
return cachedLeaderBoard;
|
||||
} else {
|
||||
const players = await fetch(URLleaderboard + "/" + incrementer).then(handleHttpErrors);
|
||||
const players = await trackPromise(fetch(URLleaderboard + "/" + incrementer).then(handleHttpErrors));
|
||||
if (leaderBoardData !== null) {
|
||||
const cachedLeaderBoard = JSON.parse(leaderBoardData);
|
||||
if (players === null) {
|
||||
return cachedLeaderBoard;
|
||||
}
|
||||
players.map(e => (
|
||||
cachedLeaderBoard.push(e)
|
||||
))
|
||||
localStorage.setItem("leaderboard", JSON.stringify(cachedLeaderBoard));
|
||||
return cachedLeaderBoard;
|
||||
} else {
|
||||
|
||||
} else if (incrementer === 0) {
|
||||
localStorage.setItem("leaderboard", JSON.stringify(players));
|
||||
return players;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
getAllMapsFromCache = async () => {
|
||||
|
||||
getMapCountFromCache = async () => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("allmapsTimer"));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
@@ -64,11 +69,14 @@ class DataFacade extends React.Component {
|
||||
const cachedAllMaps = JSON.parse(allMapsData);
|
||||
return cachedAllMaps;
|
||||
} else {
|
||||
const allMaps = await fetch(URLAllMaps).then(handleHttpErrors);
|
||||
localStorage.setItem("allmaps", JSON.stringify(allMaps));
|
||||
return allMaps;
|
||||
const maps = await trackPromise(fetch(URLAllMaps).then(handleHttpErrors));
|
||||
localStorage.setItem("allmaps", JSON.stringify(maps));
|
||||
return maps;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
getPlayerFromCache = async (steamID) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("initialCachedplayers" + steamID));
|
||||
@@ -84,13 +92,13 @@ class DataFacade extends React.Component {
|
||||
const player = JSON.parse(playerData);
|
||||
return player;
|
||||
} else {
|
||||
const player = await fetch(URLPlayer + steamID).then(handleHttpErrors);
|
||||
const player = await trackPromise(fetch(URLPlayer + steamID).then(handleHttpErrors));
|
||||
localStorage.setItem(storageString, JSON.stringify(player));
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
getPlayerTimesFromCache = async (steamID) => {
|
||||
getPlayerTimesFromCache = async (steamID, incrementer) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("initialCachedplayersTimes" + steamID));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
@@ -100,17 +108,45 @@ class DataFacade extends React.Component {
|
||||
}
|
||||
var storageString = "playertimes/" + steamID;
|
||||
var playerTimesData = localStorage.getItem(storageString);
|
||||
if (playerTimesData) {
|
||||
if (incrementer === 0 && 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;
|
||||
const playertimes = await trackPromise(fetch(URLPlayerMaps + steamID + "/" + incrementer).then(handleHttpErrors));
|
||||
if (playerTimesData !== null) {
|
||||
const cachedPlayerTimes = JSON.parse(playerTimesData);
|
||||
playertimes.map(e => (
|
||||
cachedPlayerTimes.push(e)
|
||||
))
|
||||
localStorage.setItem(storageString, JSON.stringify(cachedPlayerTimes));
|
||||
return cachedPlayerTimes;
|
||||
} else if (incrementer === 0) {
|
||||
localStorage.setItem(storageString, JSON.stringify(playertimes));
|
||||
return playertimes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getMapTimesFromCache = async (mapname, stage) => {
|
||||
getMapTimesFromCacheSize = async (mapname, stage) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("maptimessizecached" + mapname + stage));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
if (!CachedTime || timeExpired > 30 * 60000) {
|
||||
localStorage.setItem("maptimessizecached" + mapname + stage, JSON.stringify(Date.now()));
|
||||
localStorage.removeItem("mapsizecache" + mapname + stage);
|
||||
}
|
||||
var maptimesCacheSizeData = localStorage.getItem("mapsizecache" + mapname + stage);
|
||||
if (maptimesCacheSizeData) {
|
||||
const cachedsize = JSON.parse(maptimesCacheSizeData);
|
||||
return cachedsize;
|
||||
} else {
|
||||
const mapCacheSize = await fetch(URLMapsSize + "/" + mapname + "/" + stage).then(handleHttpErrors);
|
||||
localStorage.setItem("mapsizecache" + mapname + stage, JSON.stringify(mapCacheSize));
|
||||
return mapCacheSize;
|
||||
}
|
||||
}
|
||||
|
||||
getMapTimesFromCache = async (mapname, stage, incrementer) => {
|
||||
const CachedTime = JSON.parse(localStorage.getItem("maptimescached" + mapname + stage));
|
||||
const timeExpired = CachedTime ? Date.now() - CachedTime : 0;
|
||||
//30 mins
|
||||
@@ -119,16 +155,23 @@ class DataFacade extends React.Component {
|
||||
localStorage.removeItem("map/" + mapname + stage);
|
||||
}
|
||||
var mapTimesData = localStorage.getItem("map/" + mapname + stage);
|
||||
if (mapTimesData) {
|
||||
if (incrementer === 0 && 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;
|
||||
const mapTimes = await trackPromise(fetch(URLMaps + mapname + "/" + stage + "/" + incrementer).then(handleHttpErrors));
|
||||
if (mapTimesData !== null) {
|
||||
const cachedMapTimes = JSON.parse(mapTimesData);
|
||||
mapTimes.map(e => (
|
||||
cachedMapTimes.push(e)
|
||||
))
|
||||
localStorage.setItem("map/" + mapname + stage, JSON.stringify(cachedMapTimes));
|
||||
return cachedMapTimes;
|
||||
} else if (incrementer === 0) {
|
||||
localStorage.setItem("map/" + mapname + stage, JSON.stringify(mapTimes));
|
||||
return mapTimes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
export default new DataFacade();
|
||||
Reference in New Issue
Block a user