forked from laverdet/screeps-steamless-client
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from shanemadden/formatting
fix: Re-apply reverted formatting improvement commits
- Loading branch information
Showing
4 changed files
with
99 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,58 @@ | ||
/** | ||
* This script is used to display the status of a server by changing the color of an element. | ||
*/ | ||
((document) => { | ||
const setLoading = (elem: HTMLElement) => { | ||
elem.style.backgroundColor = 'transparent'; | ||
elem.style.border = '2px solid gold'; | ||
elem.setAttribute('title', 'Loading...'); | ||
}; | ||
|
||
const setStatus = (elem: HTMLElement, color: string, title: string) => { | ||
elem.style.border = 'none'; | ||
elem.style.backgroundColor = color; | ||
elem.setAttribute('title', title); | ||
}; | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
const statusIcons = document.querySelectorAll('[data-api]') as NodeListOf<HTMLElement>; | ||
|
||
statusIcons.forEach((elem) => { | ||
setStatus(elem, 'silver', 'Click to get server status'); | ||
|
||
elem.addEventListener('click', async function handleClick() { | ||
elem.removeEventListener('click', handleClick); | ||
elem.style.cursor = 'default'; | ||
|
||
setLoading(elem); | ||
|
||
try { | ||
const host = elem.getAttribute('data-api'); | ||
if (!host) { | ||
setStatus(elem, 'red', 'Invalid host'); | ||
return; | ||
} | ||
|
||
const response = (await Promise.race([ | ||
fetch(host), | ||
new Promise((_, reject) => setTimeout(() => reject('Request timed out.'), 10000)), | ||
])) as Response; | ||
|
||
if (response.ok && response.status === 200) { | ||
const data = await response.json(); | ||
const users = Number(data.users || 0).toLocaleString(); | ||
const title = data.ok ? `Online (${users} users)` : 'Online'; | ||
setStatus(elem, 'green', title); | ||
} else { | ||
setStatus(elem, 'red', 'Unavailable'); | ||
} | ||
} catch (error) { | ||
setStatus(elem, 'red', 'Offline'); | ||
} | ||
}); | ||
}); | ||
|
||
const setLoading = (elem: HTMLElement) => { | ||
elem.style.backgroundColor = 'transparent'; | ||
elem.style.border = '2px solid gold'; | ||
elem.setAttribute('title', 'Loading...'); | ||
}; | ||
|
||
const setStatus = (elem: HTMLElement, color: string, title: string) => { | ||
elem.style.border = 'none'; | ||
elem.style.backgroundColor = color; | ||
elem.setAttribute('title', title); | ||
}; | ||
|
||
document.addEventListener('DOMContentLoaded', async () => { | ||
const statusIcons = document.querySelectorAll('[data-api]') as NodeListOf<HTMLElement>; | ||
|
||
statusIcons.forEach((elem) => { | ||
setStatus(elem, 'silver', 'Click to get server status'); | ||
|
||
handleClick(elem); | ||
|
||
elem.addEventListener('click', () => handleClick(elem)); | ||
}); | ||
})(document); | ||
}); | ||
|
||
async function handleClick(elem: HTMLElement) { | ||
elem.removeEventListener('click', () => handleClick(elem)); | ||
elem.style.cursor = 'default'; | ||
|
||
setLoading(elem); | ||
|
||
try { | ||
const host = elem.getAttribute('data-api'); | ||
if (!host) { | ||
setStatus(elem, 'red', 'Invalid host'); | ||
return; | ||
} | ||
|
||
const response = (await Promise.race([ | ||
fetch(host), | ||
new Promise((_, reject) => setTimeout(() => reject('Request timed out.'), 10000)), | ||
])) as Response; | ||
|
||
if (response.ok && response.status === 200) { | ||
const data = await response.json(); | ||
const users = Number(data.users || 0).toLocaleString(); | ||
const title = data.ok ? `Online (${users} users)` : 'Online'; | ||
setStatus(elem, 'green', title); | ||
} else { | ||
setStatus(elem, 'red', 'Unavailable'); | ||
} | ||
} catch (error) { | ||
setStatus(elem, 'red', 'Offline'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters