forked from powerlanguage/guess-my-word
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
158 lines (133 loc) · 4.44 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const urlParams = new URLSearchParams(window.location.search);
const useProdBackend = urlParams.get('useProd') === ''
|| window.location.hostname.includes('hryanjones.com'); // includes to allow for beta on home.hryanjones.com
const BOARD_SERVER = useProdBackend
// ? 'https://home.hryanjones.com'
? 'https://ec2.hryanjones.com'
: 'http://192.168.0.144:8080';
// const BOARD_SERVER = 'https://hryanjones.builtwithdark.com';
// const BAD_NAMES_SERVER = 'https://hryanjones.builtwithdark.com/gmw/bad-names';
const UNKNOWN_LEADERBOARD_ERROR = 'Sorry, the completion board is having trouble right now. Please try again in a little bit. (contact @hryanjones if it persists)';
function makeLeaderboardRequest(timezonelessDate, wordlist, onSuccess, onFailure, postData, extraURL = '') {
const url = `${BOARD_SERVER}/leaderboard/${timezonelessDate}/wordlist/${wordlist}${extraURL}`;
return makeRequest(url, onSuccess, onFailure, postData);
}
function makeRequest(url, onSuccess, onFailure, postData) {
let responseStatus;
let body;
let method = 'GET';
let headers;
if (postData) {
method = 'POST';
headers = { 'Content-Type': 'application/json' };
body = JSON.stringify(postData);
}
return fetch(url, {
method,
mode: 'cors',
cache: 'no-store', // *default, no-cache, reload, force-cache, only-if-cached
headers,
body,
})
.then((response) => {
responseStatus = response.status;
return response.json(); // need to send JSON parsing through promise
})
.catch(onFailure)
.then((json) => {
if (responseStatus !== 200 && responseStatus !== 201) {
onFailure(json, responseStatus);
return;
}
onSuccess(json);
});
}
// Utilities
function getTimezonelessLocalDate(date) {
return `${date.getFullYear()}-${getMonth(date)}-${getMonthDay(date)}`;
}
function getMonth(date) {
return leftPad((date.getMonth() + 1).toString(), 2);
}
function getMonthDay(date) {
return leftPad(date.getDate().toString(), 2);
}
function leftPad(string, desiredLength, character = '0') {
if (string.length === desiredLength) {
return string;
}
return leftPad(character + string, desiredLength);
}
function getFormattedTime(milliseconds) {
if (!Number.isInteger(milliseconds)) {
return '';
}
let seconds = Math.round((milliseconds) / 1000);
const hours = Math.floor(seconds / 3600);
seconds %= 3600;
const minutes = Math.floor(seconds / 60);
seconds %= 60;
const formattedTime = [];
if (hours) {
formattedTime.push(`${hours}h`);
}
if (minutes) {
formattedTime.push(`${minutes}m`);
}
if (seconds) {
formattedTime.push(`${seconds}s`);
}
return formattedTime.join(' ') || '0s';
}
function isToday(date) {
if (typeof date === 'string') {
date = new Date(date);
}
return datesMatch(now(), date);
}
function now() {
return new Date();
}
function datesMatch(date1, date2) { // ignores time
return date1.getFullYear() === date2.getFullYear()
&& date1.getMonth() === date2.getMonth()
&& date1.getDate() === date2.getDate();
}
// # Local Storage Persistence
const IS_LOCAL_STORAGE_AVAILABLE = testLocalStorage();
function testLocalStorage() {
// stolen from https://stackoverflow.com/questions/16427636/check-if-localstorage-is-available
const test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch (e) {
return false;
}
}
const SAVED_GAMES_KEYS_BY_DIFFICULTY = {
normal: 'savedGame_normal',
hard: 'savedGame_hard',
};
function getSavedGameByDifficulty(difficulty) {
if (!IS_LOCAL_STORAGE_AVAILABLE) return undefined;
const savedGameKey = SAVED_GAMES_KEYS_BY_DIFFICULTY[difficulty];
const savedGameJSON = difficulty && localStorage.getItem(savedGameKey);
try {
return savedGameJSON && JSON.parse(savedGameJSON);
} catch (e) {
localStorage.removeItem(savedGameKey);
}
return undefined;
}
const USERNAMES_USED_KEY = 'usernamesUsed';
function getStoredUserNames() {
if (!IS_LOCAL_STORAGE_AVAILABLE) return [];
const usernamesJSON = localStorage.getItem(USERNAMES_USED_KEY);
try {
return usernamesJSON && JSON.parse(usernamesJSON) || [];
} catch (error) {
return [];
}
}