summaryrefslogtreecommitdiff
path: root/rushs/eplace/src/rooms/canvas/index.js
blob: 440e0d3b1c4bfe7de7e96fd5278a508e3b84800b (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
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
// FIXME: This file should handle the room canvas API
// Link buttons to their respective functions
// Functions may include:

import { getStudent } from "../../students";
import { authedAPIRequest } from "../../utils/auth";
import { getPlacementData } from "./utils";

// - getCanvas (get the canvas of a room and deserialize it)
export async function getCanvas(slug) {
    const config = {
        method: "get",
    };

    return authedAPIRequest(`/rooms/${slug}/canvas`, config)
        .then(async (res) => {
            if (!res) {
                return null;
            }

            const response = await res.json();

            const pixels = response.pixels
                .split("")
                .map((c) => c.charCodeAt(0).toString(2).padStart(8, "0"))
                .join("")
                .match(/.{5}/g)
                .map((b) => parseInt(b, 2));

            return pixels;
        })
        .catch((error) => {
            console.log(error);
            return null;
        });
}
// - subscribeToRoom (subscribe to the stream of a room)
// - getPixelInfo (get the pixel info of a room)
export async function getPixelInfo() {
    const info = getPlacementData();

    const response = await authedAPIRequest(
        `/rooms/epi-place/canvas/pixels?posX=${info.posX}&posY=${info.posY}`,
        { method: "get" },
    );

    if (!response) {
        return null;
    } else {
        return response.json();
    }
}

// - placePixel (place a pixel in a room)
export async function placePixel() {
    const info = getPlacementData();

    const res = await authedAPIRequest(`/rooms/epi-place/canvas/pixels`, {
        method: "POST",
        headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
        },
        body: JSON.stringify({
            posX: info.posX,
            posY: info.posY,
            color: info.color,
        }),
    });

    if (!res) {
        return;
    }

    const response = await res.json();

    const placeDate = new Date(response.timestamp);
    const studentInfo = await getStudent(response.placedByUid);

    if (!studentInfo) {
        return;
    }

    document.getElementById("tooltip-time").innerHTML =
        placeDate.toLocaleTimeString();
    document.getElementById("tooltip-date").innerHTML =
        placeDate.toLocaleDateString();
    document
        .getElementById("tooltip-info-avatar")
        .setAttribute("src", studentInfo.avatarURL ?? "/default-avatar.png");
    document.getElementById("tooltip-info-quote").innerHTML = studentInfo.quote;
    document.getElementById("tooltip-info-login").innerHTML = studentInfo.login;
}
document
    .getElementById("color-place-button")
    .addEventListener("click", placePixel);