Skip to content

Commit

Permalink
refactor: move magic numbers into config
Browse files Browse the repository at this point in the history
  • Loading branch information
aajfranklin committed Mar 28, 2020
1 parent 97feee3 commit 47a59c9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.idea
config.js
.DS_Store
background/endpointAnalysis.js
sandbox/
Expand Down
12 changes: 6 additions & 6 deletions scripts/background/messageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function MessageHandler() {
method: 'GET',
headers: {'x-click-and-roll': 'true'},
cache: false,
timeout: 10000
timeout: config.networkTimeout
})
};

Expand Down Expand Up @@ -93,7 +93,7 @@ function MessageHandler() {
this.utils.saveToLocalStorage('cache-records', []);
return [];
}
return cacheRecords.length >= 100 ? this.cleanCache(cacheRecords) : cacheRecords;
return cacheRecords.length >= config.maxCachedPlayers ? this.cleanCache(cacheRecords) : cacheRecords;
});
};

Expand All @@ -106,7 +106,7 @@ function MessageHandler() {

this.statsInCacheAndCurrent = (cacheRecords, id) => {
const player = cacheRecords.filter(player => player.id === id)[0] || null;
return player !== null && (!player.active || Date.now() - player.timestamp < (3 * 60 * 60 * 1000));
return player !== null && (!player.active || Date.now() - player.timestamp < (config.playerUpdateInterval));
};

this.fetchNonCachedStats = (id) => {
Expand Down Expand Up @@ -138,7 +138,7 @@ function MessageHandler() {
return this.utils.getFromLocalStorage('timestamp')
.then(timestamp => {
return new Promise(resolve => {
const timeout = Math.max(0, 1000 - (Date.now() - timestamp));
const timeout = Math.max(0, config.requestRateLimit - (Date.now() - timestamp));
setTimeout(resolve, timeout);
});
});
Expand Down Expand Up @@ -287,14 +287,14 @@ function MessageHandler() {

this.formatHeight = (height) => {
if (height) {
const metricHeight = Math.round(height[0] * 30.48 + height.substring(2) * 2.54);
const metricHeight = Math.round(height[0] * config.cmPerFeet + height.substring(2) * config.cmPerInch);
return `${height} (${Math.floor(metricHeight / 100)}.${(metricHeight % 100).toString().padStart(2, '0')} m)`;
}
return 'n/a';
};

this.formatWeight = (weight) => {
return weight ? `${weight} lb (${Math.round(weight * 0.45359237)} kg)` : 'n/a';
return weight ? `${weight} lb (${Math.round(weight * config.kgPerLb)} kg)` : 'n/a';
};

this.getPlayerImageUrl = (fullName) => {
Expand Down
9 changes: 4 additions & 5 deletions scripts/content/clickAndRoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function ClickAndRoll() {
return this.utils.getFromLocalStorage('players');
})
.then(players => {
const playersUpdatedWithin24Hours = Date.now() - lastUpdated < (24 * 60 * 60 * 1000);
const playersUpdatedWithin24Hours = Date.now() - lastUpdated < (config.playersUpdateInterval);
if (players && playersUpdatedWithin24Hours) return Promise.resolve(players);
return this.utils.sendRuntimeMessage({message: 'fetchPlayers'})
})
Expand Down Expand Up @@ -114,7 +114,7 @@ function ClickAndRoll() {
};

this.handleMouseEnter = (mouseEnterEvent) => {
this.hoverTimer = setTimeout(() => this.handleHover(mouseEnterEvent), 500);
this.hoverTimer = setTimeout(() => this.handleHover(mouseEnterEvent), config.hoverTimeout);
};

this.handleMouseLeave = () => {
Expand Down Expand Up @@ -228,7 +228,7 @@ function ClickAndRoll() {
// 2 pixel left offset to accommodate box shadow of frame's inner elements
const overlayLeft = this.activeName.isInLeftHalf
? rect.left + scrollX - 2
: rect.left + scrollX - 2 - this.getHalfViewWidth() + rect.width + Math.max(this.getHalfViewWidth() - 800, 0);
: rect.left + scrollX - 2 - this.getHalfViewWidth() + rect.width + Math.max(this.getHalfViewWidth() - config.maxFrameContainerWidth, 0);

const overlayTop = this.activeName.isInTopHalf
? rect.top + scrollY + rect.height
Expand Down Expand Up @@ -301,9 +301,8 @@ function ClickAndRoll() {

this.checkContentHeight = () => {
const frameContent = this.getFrameDocument().getElementById('content');
const playerHeaderHeight = 37;

if (frameContent.scrollHeight + playerHeaderHeight < (this.getHalfViewHeight()) - 2) {
if (frameContent.scrollHeight + config.playerHeaderHeight < (this.getHalfViewHeight()) - 2) {
frameContent.classList.add('short-career');
}
};
Expand Down
14 changes: 14 additions & 0 deletions scripts/utils/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const config = {
cmPerFeet: 30.48,
cmPerInch: 2.54,
defaultOffSettings: ['reverse'],
hoverTimeout: 500,
kgPerLb: 0.45359237,
maxCachedPlayers: 100,
maxFrameContainerWidth: 825,
networkTimeout: 10 * 1000,
playerHeaderHeight: 37,
playerUpdateInterval: 3 * 60 * 60 * 1000,
playersUpdateInterval: 24 * 60 * 60 * 1000,
requestRateLimit: 1000
};
2 changes: 1 addition & 1 deletion view/page.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#click-and-roll-frame-container {
height: calc(50vh + 2px);
max-width: 800px;
max-width: 825px;
position: absolute;
width: calc(50vw + 2px);
z-index: 2147483647;
Expand Down

0 comments on commit 47a59c9

Please sign in to comment.