hnininini

This commit is contained in:
Guillem George 2026-05-16 16:25:05 +02:00
parent 8cab4ac558
commit 50d46d0f0c
3 changed files with 59 additions and 33 deletions

View file

@ -14,8 +14,12 @@ async function fetchCanvas(room) {
method: "GET",
});
if (!response.ok) {
console.error("Could not retrieve room canvas: " + response.statusText);
if (!response || !response.ok) {
console.error(
"Could not retrieve room canvas: " + response
? response.statusText
: "null",
);
console.debug(await response.text());
return null;
}
@ -29,38 +33,47 @@ async function fetchCanvas(room) {
}
// Splits a string into fixed length substrings
String.prototype.chunk = function(size) {
return [].concat.apply([],
this.split('').map(function(x,i){ return i%size ? [] : this.slice(i,i+size) }, this)
)
}
String.prototype.chunk = function (size) {
return [].concat.apply(
[],
this.split("").map(function (x, i) {
return i % size ? [] : this.slice(i, i + size);
}, this),
);
};
// get the canvas of a room and deserialize it
async function getCanvas(room) {
const raw_pixels = await fetchCanvas(room);
if (!raw_pixels || !raw_pixels.pixels) {
console.error("Aborting canvas deserialization")
console.error("Aborting canvas deserialization");
return null;
}
// Convert to a an array of strings representing binary (beurk)
let raw_binary_str = ""
let raw_binary_str = "";
for (let i = 0; i < raw_pixels.pixels.length; i++) {
raw_binary_str = raw_binary_str.concat(raw_pixels.pixels.charCodeAt(i).toString(2).padStart(8, "0"))
raw_binary_str = raw_binary_str.concat(
raw_pixels.pixels.charCodeAt(i).toString(2).padStart(8, "0"),
);
}
// console.debug(raw_binary_str)
// Chunk it to an array of fixed-length string (5)
let pixel_array = raw_binary_str.chunk(5)
if (pixel_array[pixel_array.length-1].length < 5)
pixel_array.pop()
let pixel_array = raw_binary_str.chunk(5);
if (pixel_array[pixel_array.length - 1].length < 5) {
pixel_array.pop();
}
// console.debug(pixel_array)
// Convert into numbers
pixel_array = pixel_array.map((pixel) => parseInt( pixel, 2 ))
pixel_array = pixel_array.map((pixel) => parseInt(pixel, 2));
// console.debug(pixel_array)
return pixel_array
return pixel_array;
}
// subscribe to the stream of a room