forked from lexogrine/csgo-react-hud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cf3c97
commit 4e05b1b
Showing
4 changed files
with
29 additions
and
44 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
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,41 +1,24 @@ | ||
import api from './api'; | ||
interface AvatarObject { | ||
steamid: string, | ||
done: boolean, | ||
loading: boolean, | ||
custom: string, | ||
steam: string | ||
interface AvatarLoader { | ||
loader: Promise<string>, | ||
url: string, | ||
} | ||
|
||
export const avatars: AvatarObject[] = []; | ||
export const avatars: { [key: string]: AvatarLoader } = {}; | ||
|
||
export const getAvatarURL = async (steamid: string) =>{ | ||
const avatar = avatars.filter(avatar => avatar.steamid === steamid)[0]; | ||
if(avatar && (avatar.loading || avatar.done)){ | ||
return; | ||
export const loadAvatarURL = (steamid: string) => { | ||
if(!steamid) return; | ||
if(avatars[steamid]) return avatars[steamid].url; | ||
avatars[steamid] = { | ||
url: '', | ||
loader: new Promise((resolve) => { | ||
api.players.getAvatarURLs(steamid).then(result => { | ||
avatars[steamid].url = result.custom || result.custom; | ||
resolve(result.custom || result.custom); | ||
}).catch(() => { | ||
delete avatars[steamid]; | ||
resolve(''); | ||
}); | ||
}) | ||
} | ||
avatars.push({steamid, done: false, loading: true, custom: '', steam: ''}) | ||
|
||
try { | ||
const response = await api.players.getAvatarURLs(steamid); | ||
if(response.steam.length || response.custom.length){ | ||
for(let i = 0; i < avatars.length; i++) { | ||
if(avatars[i].steamid === steamid){ | ||
avatars[i].done = true; | ||
avatars[i].loading = false; | ||
avatars[i].steam = response.steam; | ||
avatars[i].custom = response.custom; | ||
} | ||
} | ||
} | ||
} catch { | ||
for(let i = 0; i < avatars.length; i++) { | ||
if(avatars[i].steamid === steamid){ | ||
avatars[i].done = true; | ||
avatars[i].loading = false; | ||
} | ||
} | ||
} | ||
|
||
return; | ||
} | ||
} |