aboutsummaryrefslogtreecommitdiff
path: root/webAO/client/setEmote.ts
blob: 2b5a614fd4f44f68c58db5204282ef5dfb0f3f51 (plain)
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
import Client from "../client";
import transparentPng from "../constants/transparentPng";
import fileExists from "../utils/fileExists";

/**
 * Sets all the img tags to the right sources
 * @param {*} chatmsg
 */

const setEmote = async (
    AO_HOST: string,
    client: Client,
    charactername: string,
    emotename: string,
    prefix: string,
    pair: boolean,
    side: string
) => {
    const pairID = pair ? "pair" : "char";
    const characterFolder = `${AO_HOST}characters/`;
    const acceptedPositions = ["def", "pro", "wit"];
    const position = acceptedPositions.includes(side) ? `${side}_` : "";
    const emoteSelector = document.getElementById(
        `client_${position}${pairID}_img`
    ) as HTMLImageElement;
    const extensionsMap = [".gif", ".png", ".apng", ".webp", ".webp.static"];

    for (const extension of extensionsMap) {
    // Hides all sprites before creating a new sprite

        if (client.viewport.getLastCharacter() !== client.viewport.getChatmsg().name) {
            emoteSelector.src = transparentPng;
        }
        let url;
        if (extension === ".png") {
            url = `${characterFolder}${encodeURI(charactername)}/${encodeURI(
                emotename
            )}${extension}`;
        } else if (extension === ".webp.static") {
            url = `${characterFolder}${encodeURI(charactername)}/${encodeURI(
                emotename
            )}.webp`;    
        } else {
            url = `${characterFolder}${encodeURI(charactername)}/${encodeURI(
                prefix
            )}${encodeURI(emotename)}${extension}`;
        }
        const exists = await fileExists(url);
        if (exists) {
            emoteSelector.src = url;
            break;
        }
    }
};
export default setEmote;